1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/***************************************************************************
* kexidbschema.cpp
* This file is part of the KDE project
* copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
* You should have received a copy of the GNU Library General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
***************************************************************************/
#include "kexidbschema.h"
#include "kexidbfieldlist.h"
#include <tqregexp.h>
#include <kdebug.h>
#include <api/variant.h>
using namespace Kross::KexiDB;
/***************************************************************************
*KexiDBSchema
*/
template<class T>
KexiDBSchema<T>::KexiDBSchema(const TQString& name, ::KexiDB::SchemaData* schema, ::KexiDB::FieldList* fieldlist)
: Kross::Api::Class<T>(name)
, m_schema(schema)
, m_fieldlist(fieldlist)
{
this->template addFunction0<Kross::Api::Variant>("name", this, &KexiDBSchema<T>::name);
this->template addFunction1<void, Kross::Api::Variant>("setName", this, &KexiDBSchema<T>::setName);
this->template addFunction0<Kross::Api::Variant>("caption", this, &KexiDBSchema<T>::caption);
this->template addFunction1<void, Kross::Api::Variant>("setCaption", this, &KexiDBSchema<T>::setCaption);
this->template addFunction0<Kross::Api::Variant>("description", this, &KexiDBSchema<T>::description);
this->template addFunction1<void, Kross::Api::Variant>("setDescription", this, &KexiDBSchema<T>::setDescription);
this->template addFunction0<KexiDBFieldList>("fieldlist", this, &KexiDBSchema<T>::fieldlist);
}
template<class T>
KexiDBSchema<T>::~KexiDBSchema<T>() {
}
template<class T>
const TQString KexiDBSchema<T>::name() const {
return m_schema->name();
}
template<class T>
void KexiDBSchema<T>::setName(const TQString& name) {
m_schema->setName(name);
}
template<class T>
const TQString KexiDBSchema<T>::caption() const {
return m_schema->caption();
}
template<class T>
void KexiDBSchema<T>::setCaption(const TQString& caption) {
m_schema->setCaption(caption);
}
template<class T>
const TQString KexiDBSchema<T>::description() const {
return m_schema->description();
}
template<class T>
void KexiDBSchema<T>::setDescription(const TQString& description) {
m_schema->setDescription(description);
}
template<class T>
KexiDBFieldList* KexiDBSchema<T>::fieldlist() const {
return new KexiDBFieldList(m_fieldlist);
}
/***************************************************************************
* KexiDBTableSchema
*/
KexiDBTableSchema::KexiDBTableSchema(::KexiDB::TableSchema* tableschema)
: KexiDBSchema<KexiDBTableSchema>("KexiDBTableSchema", tableschema, tableschema)
{
this->addFunction0<KexiDBQuerySchema>("query", this, &KexiDBTableSchema::query);
}
KexiDBTableSchema::~KexiDBTableSchema() {
}
const TQString KexiDBTableSchema::getClassName() const {
return "Kross::KexiDB::KexiDBTableSchema";
}
::KexiDB::TableSchema* KexiDBTableSchema::tableschema() {
return static_cast< ::KexiDB::TableSchema* >(m_schema);
}
KexiDBQuerySchema* KexiDBTableSchema::query() {
return new KexiDBQuerySchema( tableschema()->query() );
}
/***************************************************************************
* KexiDBQuerySchema
*/
KexiDBQuerySchema::KexiDBQuerySchema(::KexiDB::QuerySchema* queryschema)
: KexiDBSchema<KexiDBQuerySchema>("KexiDBQuerySchema", queryschema, queryschema)
{
this->addFunction0<Kross::Api::Variant>("statement", this, &KexiDBQuerySchema::statement);
this->addFunction1<void, Kross::Api::Variant>("setStatement", this, &KexiDBQuerySchema::setStatement);
this->addFunction1<Kross::Api::Variant, Kross::Api::Variant>("setWhereExpression", this, &KexiDBQuerySchema::setWhereExpression);
}
KexiDBQuerySchema::~KexiDBQuerySchema() {
}
const TQString KexiDBQuerySchema::getClassName() const {
return "Kross::KexiDB::KexiDBQuerySchema";
}
::KexiDB::QuerySchema* KexiDBQuerySchema::queryschema() {
return static_cast< ::KexiDB::QuerySchema* >(m_schema);
}
const TQString KexiDBQuerySchema::statement() const {
return static_cast< ::KexiDB::QuerySchema* >(m_schema)->statement();
}
void KexiDBQuerySchema::setStatement(const TQString& statement) {
static_cast< ::KexiDB::QuerySchema* >(m_schema)->setStatement(statement);
}
bool KexiDBQuerySchema::setWhereExpression(const TQString& whereexpression) {
::KexiDB::BaseExpr* oldexpr = static_cast< ::KexiDB::QuerySchema* >(m_schema)->whereExpression();
///@todo use ::KexiDB::Parser for such kind of parser-functionality.
TQString s = whereexpression;
try {
TQRegExp re("[\"',]{1,1}");
while(true) {
s.remove(TQRegExp("^[\\s,]+"));
int pos = s.find('=');
if(pos < 0) break;
TQString key = s.left(pos).stripWhiteSpace();
s = s.mid(pos + 1).stripWhiteSpace();
TQString value;
int sp = s.find(re);
if(sp >= 0) {
if(re.cap(0) == ",") {
value = s.left(sp).stripWhiteSpace();
s = s.mid(sp+1).stripWhiteSpace();
}
else {
int ep = s.find(re.cap(0),sp+1);
value = s.mid(sp+1,ep-1);
s = s.mid(ep + 1);
}
}
else {
value = s;
s = TQString();
}
::KexiDB::Field* field = static_cast< ::KexiDB::QuerySchema* >(m_schema)->field(key);
if(! field)
throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("Invalid WHERE-expression: Field \"%1\" does not exists in tableschema \"%2\".").tqarg(key).tqarg(m_schema->name())) );
TQVariant v(value);
if(! v.cast(field->variantType()))
throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("Invalid WHERE-expression: The for Field \"%1\" defined value is of type \"%2\" rather then the expected type \"%3\"").tqarg(key).tqarg(v.typeName()).tqarg(field->variantType())) );
static_cast< ::KexiDB::QuerySchema* >(m_schema)->addToWhereExpression(field,v);
}
}
catch(Kross::Api::Exception::Ptr e) {
Kross::krosswarning("Exception in Kross::KexiDB::KexiDBQuerySchema::setWhereExpression: ");
static_cast< ::KexiDB::QuerySchema* >(m_schema)->setWhereExpression(oldexpr); // fallback
return false;
}
return true;
}
|