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 
00026 #include <api/variant.h>
00027 
00028 using namespace Kross::KexiDB;
00029 
00030 /***************************************************************************
00031  *KexiDBSchema
00032  */
00033 
00034 template<class T>
00035 KexiDBSchema<T>::KexiDBSchema(const QString& name, ::KexiDB::SchemaData* schema, ::KexiDB::FieldList* fieldlist)
00036     : Kross::Api::Class<T>(name)
00037     , m_schema(schema)
00038     , m_fieldlist(fieldlist)
00039 {
00040     addFunction("name", &KexiDBSchema<T>::name);
00041     addFunction("setName", &KexiDBSchema<T>::setName,
00042         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00043 
00044     addFunction("caption", &KexiDBSchema<T>::caption);
00045     addFunction("setCaption", &KexiDBSchema<T>::setCaption,
00046         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00047 
00048     addFunction("description", &KexiDBSchema<T>::description);
00049     addFunction("setDescription", &KexiDBSchema<T>::setDescription,
00050         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00051 
00052     addFunction("fieldlist", &KexiDBSchema<T>::fieldlist);
00053 }
00054 
00055 template<class T>
00056 KexiDBSchema<T>::~KexiDBSchema<T>()
00057 {
00058 }
00059 
00060 template<class T>
00061 Kross::Api::Object::Ptr KexiDBSchema<T>::name(Kross::Api::List::Ptr)
00062 {
00063     return new Kross::Api::Variant(m_schema->name());
00064 }
00065 
00066 template<class T>
00067 Kross::Api::Object::Ptr KexiDBSchema<T>::setName(Kross::Api::List::Ptr args)
00068 {
00069     m_schema->setName(Kross::Api::Variant::toString(args->item(0)));
00070     return name(args);
00071 }
00072 
00073 template<class T>
00074 Kross::Api::Object::Ptr KexiDBSchema<T>::caption(Kross::Api::List::Ptr)
00075 {
00076     return new Kross::Api::Variant(m_schema->caption());
00077 }
00078 
00079 template<class T>
00080 Kross::Api::Object::Ptr KexiDBSchema<T>::setCaption(Kross::Api::List::Ptr args)
00081 {
00082     m_schema->setCaption(Kross::Api::Variant::toString(args->item(0)));
00083     return caption(args);
00084 }
00085 
00086 template<class T>
00087 Kross::Api::Object::Ptr KexiDBSchema<T>::description(Kross::Api::List::Ptr)
00088 {
00089     return new Kross::Api::Variant(m_schema->description());
00090 }
00091 
00092 template<class T>
00093 Kross::Api::Object::Ptr KexiDBSchema<T>::setDescription(Kross::Api::List::Ptr args)
00094 {
00095     m_schema->setDescription(Kross::Api::Variant::toString(args->item(0)));
00096     return description(args);
00097 }
00098 
00099 template<class T>
00100 Kross::Api::Object::Ptr KexiDBSchema<T>::fieldlist(Kross::Api::List::Ptr)
00101 {
00102     //TODO cache; pass optional KexiDBFieldList* to our class and return here.
00103     return new KexiDBFieldList(m_fieldlist);
00104 }
00105 
00106 /***************************************************************************
00107  * KexiDBTableSchema
00108  */
00109 
00110 KexiDBTableSchema::KexiDBTableSchema(::KexiDB::TableSchema* tableschema)
00111     : KexiDBSchema<KexiDBTableSchema>("KexiDBTableSchema", tableschema, tableschema)
00112 {
00113     addFunction("query", &KexiDBTableSchema::query);
00114 }
00115 
00116 KexiDBTableSchema::~KexiDBTableSchema()
00117 {
00118 }
00119 
00120 const QString KexiDBTableSchema::getClassName() const
00121 {
00122     return "Kross::KexiDB::KexiDBTableSchema";
00123 }
00124 
00125 ::KexiDB::TableSchema* KexiDBTableSchema::tableschema()
00126 {
00127     return static_cast< ::KexiDB::TableSchema* >(m_schema);
00128 }
00129 
00130 Kross::Api::Object::Ptr KexiDBTableSchema::query(Kross::Api::List::Ptr)
00131 {
00132     return new KexiDBQuerySchema( tableschema()->query() );
00133 }
00134 
00135 /***************************************************************************
00136  * KexiDBQuerySchema
00137  */
00138 
00139 KexiDBQuerySchema::KexiDBQuerySchema(::KexiDB::QuerySchema* queryschema)
00140     : KexiDBSchema<KexiDBQuerySchema>("KexiDBQuerySchema", queryschema, queryschema)
00141 {
00142     addFunction("statement", &KexiDBQuerySchema::statement);
00143     addFunction("setStatement", &KexiDBQuerySchema::setStatement);
00144     addFunction("setWhereExpression", &KexiDBQuerySchema::setWhereExpression);
00145 }
00146 
00147 KexiDBQuerySchema::~KexiDBQuerySchema()
00148 {
00149 }
00150 
00151 const QString KexiDBQuerySchema::getClassName() const
00152 {
00153     return "Kross::KexiDB::KexiDBQuerySchema";
00154 }
00155 
00156 ::KexiDB::QuerySchema* KexiDBQuerySchema::queryschema()
00157 {
00158     return static_cast< ::KexiDB::QuerySchema* >(m_schema);
00159 }
00160 
00161 Kross::Api::Object::Ptr KexiDBQuerySchema::statement(Kross::Api::List::Ptr)
00162 {
00163     return new Kross::Api::Variant(
00164            static_cast< ::KexiDB::QuerySchema* >(m_schema)->statement() );
00165 }
00166 
00167 Kross::Api::Object::Ptr KexiDBQuerySchema::setStatement(Kross::Api::List::Ptr args)
00168 {
00169     static_cast< ::KexiDB::QuerySchema* >(m_schema)->setStatement(
00170         Kross::Api::Variant::toString(args->item(0))
00171     );
00172     return statement(args);
00173 }
00174 
00175 Kross::Api::Object::Ptr KexiDBQuerySchema::setWhereExpression(Kross::Api::List::Ptr args)
00176 {
00177     ::KexiDB::BaseExpr* oldexpr = static_cast< ::KexiDB::QuerySchema* >(m_schema)->whereExpression();
00178 
00180     QString s = Kross::Api::Variant::toString(args->item(0));
00181     try {
00182         QRegExp re("[\"',]{1,1}");
00183         while(true) {
00184             s.remove(QRegExp("^[\\s,]+"));
00185             int pos = s.find('=');
00186             if(pos < 0) break;
00187             QString key = s.left(pos).stripWhiteSpace();
00188             s = s.mid(pos + 1).stripWhiteSpace();
00189 
00190             QString value;
00191             int sp = s.find(re);
00192             if(sp >= 0) {
00193                 if(re.cap(0) == ",") {
00194                     value = s.left(sp).stripWhiteSpace();
00195                     s = s.mid(sp+1).stripWhiteSpace();
00196                 }
00197                 else {
00198                     int ep = s.find(re.cap(0),sp+1);
00199                     value = s.mid(sp+1,ep-1);
00200                     s = s.mid(ep + 1);
00201                 }
00202             }
00203             else {
00204                 value = s;
00205                 s = QString::null;
00206             }
00207 
00208             ::KexiDB::Field* field = static_cast< ::KexiDB::QuerySchema* >(m_schema)->field(key);
00209             if(! field)
00210                 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())) );
00211 
00212             QVariant v(value);
00213             if(! v.cast(field->variantType()))
00214                 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())) );
00215 
00216             static_cast< ::KexiDB::QuerySchema* >(m_schema)->addToWhereExpression(field,v);
00217         }
00218     }
00219     catch(Kross::Api::Exception::Ptr e) {
00220         kdWarning() << "Exception in Kross::KexiDB::KexiDBQuerySchema::setWhereExpression: " << e->toString() << endl;
00221         static_cast< ::KexiDB::QuerySchema* >(m_schema)->setWhereExpression(oldexpr); // fallback
00222         return new Kross::Api::Variant(QVariant(false,0));
00223     }
00224     return new Kross::Api::Variant(QVariant(true,0));
00225 }
KDE Home | KDE Accessibility Home | Description of Access Keys