/*************************************************************************** * 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 #include #include using namespace Kross::KexiDB; /*************************************************************************** *KexiDBSchema */ template KexiDBSchema::KexiDBSchema(const TQString& name, ::KexiDB::SchemaData* schema, ::KexiDB::FieldList* fieldlist) : Kross::Api::Class(name) , m_schema(schema) , m_fieldlist(fieldlist) { this->template addFunction0("name", this, &KexiDBSchema::name); this->template addFunction1("setName", this, &KexiDBSchema::setName); this->template addFunction0("caption", this, &KexiDBSchema::caption); this->template addFunction1("setCaption", this, &KexiDBSchema::setCaption); this->template addFunction0("description", this, &KexiDBSchema::description); this->template addFunction1("setDescription", this, &KexiDBSchema::setDescription); this->template addFunction0("fieldlist", this, &KexiDBSchema::fieldlist); } template KexiDBSchema::~KexiDBSchema() { } template const TQString KexiDBSchema::name() const { return m_schema->name(); } template void KexiDBSchema::setName(const TQString& name) { m_schema->setName(name); } template const TQString KexiDBSchema::caption() const { return m_schema->caption(); } template void KexiDBSchema::setCaption(const TQString& caption) { m_schema->setCaption(caption); } template const TQString KexiDBSchema::description() const { return m_schema->description(); } template void KexiDBSchema::setDescription(const TQString& description) { m_schema->setDescription(description); } template KexiDBFieldList* KexiDBSchema::fieldlist() const { return new KexiDBFieldList(m_fieldlist); } /*************************************************************************** * KexiDBTableSchema */ KexiDBTableSchema::KexiDBTableSchema(::KexiDB::TableSchema* tableschema) : KexiDBSchema("KexiDBTableSchema", tableschema, tableschema) { this->addFunction0("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", queryschema, queryschema) { this->addFunction0("statement", this, &KexiDBQuerySchema::statement); this->addFunction1("setStatement", this, &KexiDBQuerySchema::setStatement); this->addFunction1("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\".").arg(key).arg(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\"").arg(key).arg(v.typeName()).arg(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; }