kexi

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