kexi

kexidbschema.cpp

00001 /***************************************************************************
00002  * kexidbschema.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 
00021 #include "kexidbschema.h"
00022 #include "kexidbfieldlist.h"
00023 
00024 #include <qregexp.h>
00025 #include <kdebug.h>
00026 
00027 #include <api/variant.h>
00028 
00029 using namespace Kross::KexiDB;
00030 
00031 /***************************************************************************
00032  *KexiDBSchema
00033  */
00034 
00035 template<class T>
00036 KexiDBSchema<T>::KexiDBSchema(const QString& name, ::KexiDB::SchemaData* schema, ::KexiDB::FieldList* fieldlist)
00037     : Kross::Api::Class<T>(name)
00038     , m_schema(schema)
00039     , m_fieldlist(fieldlist)
00040 {
00041     this->template addFunction0<Kross::Api::Variant>("name", this, &KexiDBSchema<T>::name);
00042     this->template addFunction1<void, Kross::Api::Variant>("setName", this, &KexiDBSchema<T>::setName);
00043 
00044     this->template addFunction0<Kross::Api::Variant>("caption", this, &KexiDBSchema<T>::caption);
00045     this->template addFunction1<void, Kross::Api::Variant>("setCaption", this, &KexiDBSchema<T>::setCaption);
00046 
00047     this->template addFunction0<Kross::Api::Variant>("description", this, &KexiDBSchema<T>::description);
00048     this->template addFunction1<void, Kross::Api::Variant>("setDescription", this, &KexiDBSchema<T>::setDescription);
00049 
00050     this->template addFunction0<KexiDBFieldList>("fieldlist", this, &KexiDBSchema<T>::fieldlist);
00051 }
00052 
00053 template<class T>
00054 KexiDBSchema<T>::~KexiDBSchema<T>() {
00055 }
00056 
00057 template<class T>
00058 const QString KexiDBSchema<T>::name() const {
00059     return m_schema->name();
00060 }
00061 
00062 template<class T>
00063 void KexiDBSchema<T>::setName(const QString& name) {
00064     m_schema->setName(name);
00065 }
00066 
00067 template<class T>
00068 const QString KexiDBSchema<T>::caption() const {
00069     return m_schema->caption();
00070 }
00071 
00072 template<class T>
00073 void KexiDBSchema<T>::setCaption(const QString& caption) {
00074     m_schema->setCaption(caption);
00075 }
00076 
00077 template<class T>
00078 const QString KexiDBSchema<T>::description() const {
00079     return m_schema->description();
00080 }
00081 
00082 template<class T>
00083 void KexiDBSchema<T>::setDescription(const QString& description) {
00084     m_schema->setDescription(description);
00085 }
00086 
00087 template<class T>
00088 KexiDBFieldList* KexiDBSchema<T>::fieldlist() const {
00089     return new KexiDBFieldList(m_fieldlist);
00090 }
00091 
00092 /***************************************************************************
00093  * KexiDBTableSchema
00094  */
00095 
00096 KexiDBTableSchema::KexiDBTableSchema(::KexiDB::TableSchema* tableschema)
00097     : KexiDBSchema<KexiDBTableSchema>("KexiDBTableSchema", tableschema, tableschema)
00098 {
00099     this->addFunction0<KexiDBQuerySchema>("query", this, &KexiDBTableSchema::query);
00100 }
00101 
00102 KexiDBTableSchema::~KexiDBTableSchema() {
00103 }
00104 
00105 const QString KexiDBTableSchema::getClassName() const {
00106     return "Kross::KexiDB::KexiDBTableSchema";
00107 }
00108 
00109 ::KexiDB::TableSchema* KexiDBTableSchema::tableschema() {
00110     return static_cast< ::KexiDB::TableSchema* >(m_schema);
00111 }
00112 
00113 KexiDBQuerySchema* KexiDBTableSchema::query() {
00114     return new KexiDBQuerySchema( tableschema()->query() );
00115 }
00116 
00117 /***************************************************************************
00118  * KexiDBQuerySchema
00119  */
00120 
00121 KexiDBQuerySchema::KexiDBQuerySchema(::KexiDB::QuerySchema* queryschema)
00122     : KexiDBSchema<KexiDBQuerySchema>("KexiDBQuerySchema", queryschema, queryschema)
00123 {
00124     this->addFunction0<Kross::Api::Variant>("statement", this, &KexiDBQuerySchema::statement);
00125     this->addFunction1<void, Kross::Api::Variant>("setStatement", this, &KexiDBQuerySchema::setStatement);
00126     this->addFunction1<Kross::Api::Variant, Kross::Api::Variant>("setWhereExpression", this, &KexiDBQuerySchema::setWhereExpression);
00127 }
00128 
00129 KexiDBQuerySchema::~KexiDBQuerySchema() {
00130 }
00131 
00132 const QString KexiDBQuerySchema::getClassName() const {
00133     return "Kross::KexiDB::KexiDBQuerySchema";
00134 }
00135 
00136 ::KexiDB::QuerySchema* KexiDBQuerySchema::queryschema() {
00137     return static_cast< ::KexiDB::QuerySchema* >(m_schema);
00138 }
00139 
00140 const QString KexiDBQuerySchema::statement() const {
00141     return static_cast< ::KexiDB::QuerySchema* >(m_schema)->statement();
00142 }
00143 
00144 void KexiDBQuerySchema::setStatement(const QString& statement) {
00145     static_cast< ::KexiDB::QuerySchema* >(m_schema)->setStatement(statement);
00146 }
00147 
00148 bool KexiDBQuerySchema::setWhereExpression(const QString& whereexpression) {
00149     ::KexiDB::BaseExpr* oldexpr = static_cast< ::KexiDB::QuerySchema* >(m_schema)->whereExpression();
00150 
00152     QString s = whereexpression;
00153     try {
00154         QRegExp re("[\"',]{1,1}");
00155         while(true) {
00156             s.remove(QRegExp("^[\\s,]+"));
00157             int pos = s.find('=');
00158             if(pos < 0) break;
00159             QString key = s.left(pos).stripWhiteSpace();
00160             s = s.mid(pos + 1).stripWhiteSpace();
00161 
00162             QString value;
00163             int sp = s.find(re);
00164             if(sp >= 0) {
00165                 if(re.cap(0) == ",") {
00166                     value = s.left(sp).stripWhiteSpace();
00167                     s = s.mid(sp+1).stripWhiteSpace();
00168                 }
00169                 else {
00170                     int ep = s.find(re.cap(0),sp+1);
00171                     value = s.mid(sp+1,ep-1);
00172                     s = s.mid(ep + 1);
00173                 }
00174             }
00175             else {
00176                 value = s;
00177                 s = QString::null;
00178             }
00179 
00180             ::KexiDB::Field* field = static_cast< ::KexiDB::QuerySchema* >(m_schema)->field(key);
00181             if(! field)
00182                 throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("Invalid WHERE-expression: Field \"%1\" does not exists in tableschema \"%2\".").arg(key).arg(m_schema->name())) );
00183 
00184             QVariant v(value);
00185             if(! v.cast(field->variantType()))
00186                 throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("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())) );
00187 
00188             static_cast< ::KexiDB::QuerySchema* >(m_schema)->addToWhereExpression(field,v);
00189         }
00190     }
00191     catch(Kross::Api::Exception::Ptr e) {
00192         Kross::krosswarning("Exception in Kross::KexiDB::KexiDBQuerySchema::setWhereExpression: ");
00193         static_cast< ::KexiDB::QuerySchema* >(m_schema)->setWhereExpression(oldexpr); // fallback
00194         return false;
00195     }
00196     return true;
00197 }
KDE Home | KDE Accessibility Home | Description of Access Keys