diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 01:29:50 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 01:29:50 +0000 |
commit | 8362bf63dea22bbf6736609b0f49c152f975eb63 (patch) | |
tree | 0eea3928e39e50fae91d4e68b21b1e6cbae25604 /kexi/plugins/scripting/kexidb | |
download | koffice-8362bf63dea22bbf6736609b0f49c152f975eb63.tar.gz koffice-8362bf63dea22bbf6736609b0f49c152f975eb63.zip |
Added old abandoned KDE3 version of koffice
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/koffice@1077364 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kexi/plugins/scripting/kexidb')
24 files changed, 2739 insertions, 0 deletions
diff --git a/kexi/plugins/scripting/kexidb/Makefile.am b/kexi/plugins/scripting/kexidb/Makefile.am new file mode 100644 index 000000000..12f825010 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/Makefile.am @@ -0,0 +1,30 @@ +include $(top_srcdir)/kexi/Makefile.global + +INCLUDES = -I$(top_srcdir)/kexi $(KROSS_INCLUDES) $(all_includes) + +kde_module_LTLIBRARIES = krosskexidb.la + +krosskexidb_la_SOURCES = \ + kexidbfield.cpp \ + kexidbfieldlist.cpp \ + kexidbschema.cpp \ + kexidbparser.cpp \ + kexidbcursor.cpp \ + kexidbtransaction.cpp \ + kexidbconnectiondata.cpp \ + kexidbconnection.cpp \ + kexidbdriver.cpp \ + kexidbdrivermanager.cpp \ + kexidbmodule.cpp + +krosskexidb_la_LDFLAGS = $(all_libraries) $(KDE_PLUGIN) $(VER_INFO) -module +krosskexidb_la_LIBADD = \ + $(LIB_QT) \ + $(LIB_KDECORE) \ + $(LIB_KROSS_API) \ + $(LIB_KROSS_MAIN) \ + $(top_builddir)/kexi/kexidb/libkexidb.la \ + $(top_builddir)/kexi/kexidb/parser/libkexidbparser.la + +METASOURCES = AUTO +SUBDIRS = . diff --git a/kexi/plugins/scripting/kexidb/kexidbconnection.cpp b/kexi/plugins/scripting/kexidb/kexidbconnection.cpp new file mode 100644 index 000000000..d3b7cc76f --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbconnection.cpp @@ -0,0 +1,221 @@ +/*************************************************************************** + * kexidbconnection.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 "kexidbconnection.h" +#include "kexidbconnectiondata.h" +#include "kexidbdrivermanager.h" +#include "kexidbdriver.h" +#include "kexidbcursor.h" +#include "kexidbfieldlist.h" +#include "kexidbschema.h" +#include "kexidbtransaction.h" +#include "kexidbparser.h" + +#include <api/exception.h> + +#include <kdebug.h> + +#include <kexidb/transaction.h> + +using namespace Kross::KexiDB; + +KexiDBConnection::KexiDBConnection(::KexiDB::Connection* connection, KexiDBDriver* driver, KexiDBConnectionData* connectiondata) + : Kross::Api::Class<KexiDBConnection>("KexiDBConnection") + , m_connection(connection) + , m_connectiondata(connectiondata ? connectiondata : new KexiDBConnectionData(connection->data())) + , m_driver(driver ? driver : new KexiDBDriver(connection->driver())) +{ + this->addFunction0< Kross::Api::Variant >("hadError", this, &KexiDBConnection::hadError); + this->addFunction0< Kross::Api::Variant >("lastError", this, &KexiDBConnection::lastError); + + this->addFunction0< KexiDBConnectionData >("data", this, &KexiDBConnection::data); + this->addFunction0< KexiDBDriver >("driver", this, &KexiDBConnection::driver); + + this->addFunction0< Kross::Api::Variant >("connect", this, &KexiDBConnection::connect); + this->addFunction0< Kross::Api::Variant >("isConnected", this, &KexiDBConnection::isConnected); + this->addFunction0< Kross::Api::Variant >("disconnect", this, &KexiDBConnection::disconnect); + + this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("databaseExists", this, &KexiDBConnection::databaseExists); + this->addFunction0< Kross::Api::Variant >("currentDatabase", this, &KexiDBConnection::currentDatabase); + this->addFunction0< Kross::Api::Variant >("databaseNames", this, &KexiDBConnection::databaseNames); + this->addFunction0< Kross::Api::Variant >("isDatabaseUsed", this, &KexiDBConnection::isDatabaseUsed); + this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("useDatabase", this, &KexiDBConnection::useDatabase); + this->addFunction0< Kross::Api::Variant >("closeDatabase", this, &KexiDBConnection::closeDatabase); + + this->addFunction0< Kross::Api::Variant >("tableNames", this, &KexiDBConnection::tableNames); + this->addFunction0< Kross::Api::Variant >("queryNames", this, &KexiDBConnection::queryNames); + + this->addFunction1< KexiDBCursor, Kross::Api::Variant >("executeQueryString", this, &KexiDBConnection::executeQueryString); + this->addFunction1< KexiDBCursor, KexiDBQuerySchema >("executeQuerySchema", this, &KexiDBConnection::executeQuerySchema); + + addFunction("insertRecord", &KexiDBConnection::insertRecord); + + this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("createDatabase", this, &KexiDBConnection::createDatabase); + this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("dropDatabase", this, &KexiDBConnection::dropDatabase); + + this->addFunction1< Kross::Api::Variant, KexiDBTableSchema >("createTable", this, &KexiDBConnection::createTable); + this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("dropTable", this, &KexiDBConnection::dropTable); + this->addFunction2< Kross::Api::Variant, KexiDBTableSchema, KexiDBTableSchema >("alterTable", this, &KexiDBConnection::alterTable); + this->addFunction2< Kross::Api::Variant, KexiDBTableSchema, Kross::Api::Variant >("alterTableName", this, &KexiDBConnection::alterTableName); + + this->addFunction1< KexiDBTableSchema, Kross::Api::Variant >("tableSchema", this, &KexiDBConnection::tableSchema); + this->addFunction1< Kross::Api::Variant, KexiDBTableSchema >("isEmptyTable", this, &KexiDBConnection::isEmptyTable); + this->addFunction1< KexiDBQuerySchema, Kross::Api::Variant >("querySchema", this, &KexiDBConnection::querySchema); + + this->addFunction0< Kross::Api::Variant >("autoCommit", this, &KexiDBConnection::autoCommit); + this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("setAutoCommit", this, &KexiDBConnection::setAutoCommit); + + this->addFunction0< KexiDBTransaction >("beginTransaction", this, &KexiDBConnection::beginTransaction); + this->addFunction1< Kross::Api::Variant, KexiDBTransaction >("commitTransaction", this, &KexiDBConnection::commitTransaction); + this->addFunction1< Kross::Api::Variant, KexiDBTransaction >("rollbackTransaction", this, &KexiDBConnection::rollbackTransaction); + this->addFunction0< KexiDBTransaction >("defaultTransaction", this, &KexiDBConnection::defaultTransaction); + this->addFunction1< void, KexiDBTransaction >("setDefaultTransaction", this, &KexiDBConnection::setDefaultTransaction); + this->addFunction0<Kross::Api::List>("transactions", this, &KexiDBConnection::transactions); + + this->addFunction0< KexiDBParser >("parser", this, &KexiDBConnection::parser); +} + +KexiDBConnection::~KexiDBConnection() { +} + +const QString KexiDBConnection::getClassName() const { + return "Kross::KexiDB::KexiDBConnection"; +} + +::KexiDB::Connection* KexiDBConnection::connection() const { + if(! m_connection) + throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("KexiDB::Connection is NULL.")) ); + //if(m_connection->error()) + // throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("KexiDB::Connection error: %1").arg(m_connection->errorMsg())) ); + return m_connection; +} + +bool KexiDBConnection::hadError() const { return connection()->error(); } +const QString KexiDBConnection::lastError() const { return connection()->errorMsg(); } + +KexiDBConnectionData* KexiDBConnection::data() { return m_connectiondata.data(); } +KexiDBDriver* KexiDBConnection::driver() { return m_driver.data(); } + +bool KexiDBConnection::connect() { return connection()->connect(); } +bool KexiDBConnection::isConnected() { return connection()->isConnected(); } +bool KexiDBConnection::disconnect() { return connection()->disconnect(); } + +bool KexiDBConnection::isReadOnly() const { return connection()->isReadOnly(); } + +bool KexiDBConnection::databaseExists(const QString& dbname) { return connection()->databaseExists(dbname); } +const QString KexiDBConnection::currentDatabase() const { return connection()->currentDatabase(); } +const QStringList KexiDBConnection::databaseNames() const { return connection()->databaseNames(); } +bool KexiDBConnection::isDatabaseUsed() const { return connection()->isDatabaseUsed(); } +bool KexiDBConnection::useDatabase(const QString& dbname) { return connection()->databaseExists(dbname) && m_connection->useDatabase(dbname); } +bool KexiDBConnection::closeDatabase() { return connection()->closeDatabase(); } + +const QStringList KexiDBConnection::allTableNames() const { return connection()->tableNames(true); } +const QStringList KexiDBConnection::tableNames() const { return connection()->tableNames(false); } + +const QStringList KexiDBConnection::queryNames() const { + bool ok = true; + QStringList queries = connection()->objectNames(::KexiDB::QueryObjectType, &ok); + if(! ok) throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("Failed to determinate querynames.")) ); + return queries; +} + +KexiDBCursor* KexiDBConnection::executeQueryString(const QString& sqlquery) { + // The ::KexiDB::Connection::executeQuery() method does not check if we pass a valid SELECT-statement + // or e.g. a DROP TABLE operation. So, let's check for such dangerous operations right now. + ::KexiDB::Parser parser( connection() ); + if(! parser.parse(sqlquery)) + throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("Failed to parse query: %1 %2").arg(parser.error().type()).arg(parser.error().error())) ); + if( parser.query() == 0 || parser.operation() != ::KexiDB::Parser::OP_Select ) + throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("Invalid query operation \"%1\"").arg(parser.operationString()) ) ); + ::KexiDB::Cursor* cursor = connection()->executeQuery(sqlquery); + return cursor ? new KexiDBCursor(cursor) : 0; +} + +KexiDBCursor* KexiDBConnection::executeQuerySchema(KexiDBQuerySchema* queryschema) { + ::KexiDB::Cursor* cursor = connection()->executeQuery( *queryschema->queryschema() ); + return cursor ? new KexiDBCursor(cursor) : 0; +} + +/*TODO +bool KexiDBConnection::insertRecordIntoFieldlist(KexiDBFieldList* fieldlist, QValueList<QVariant> values) { + return connection()->insertRecord(*fieldlist->fieldlist(), values); +} + +bool KexiDBConnection::insertRecordIntoTable(KexiDBTableSchema* tableschema, QValueList<QVariant> values) { + return connection()->insertRecord(*tableschema->tableschema(), values); +} +*/ +Kross::Api::Object::Ptr KexiDBConnection::insertRecord(Kross::Api::List::Ptr args) { + QValueList<QVariant> values = Kross::Api::Variant::toList(args->item(1)); + Kross::Api::Object::Ptr obj = args->item(0); + if(obj->getClassName() == "Kross::KexiDB::KexiDBFieldList") + return new Kross::Api::Variant( + QVariant(connection()->insertRecord( + *Kross::Api::Object::fromObject<KexiDBFieldList>(obj)->fieldlist(), + values + ), 0)); + return new Kross::Api::Variant( + QVariant(connection()->insertRecord( + *Kross::Api::Object::fromObject<KexiDBTableSchema>(obj)->tableschema(), + values + ), 0)); +} + +bool KexiDBConnection::createDatabase(const QString& dbname) { return connection()->createDatabase(dbname); } +bool KexiDBConnection::dropDatabase(const QString& dbname) { return connection()->dropDatabase(dbname); } + +bool KexiDBConnection::createTable(KexiDBTableSchema* tableschema) { return connection()->createTable(tableschema->tableschema(), false); } +bool KexiDBConnection::dropTable(const QString& tablename) { return true == connection()->dropTable(tablename); } +bool KexiDBConnection::alterTable(KexiDBTableSchema* fromschema, KexiDBTableSchema* toschema) { return true == connection()->alterTable(*fromschema->tableschema(), *toschema->tableschema()); } +bool KexiDBConnection::alterTableName(KexiDBTableSchema* tableschema, const QString& newtablename) { return connection()->alterTableName(*tableschema->tableschema(), newtablename); } + +KexiDBTableSchema* KexiDBConnection::tableSchema(const QString& tablename) const { + ::KexiDB::TableSchema* tableschema = connection()->tableSchema(tablename); + return tableschema ? new KexiDBTableSchema(tableschema) : 0; +} + +bool KexiDBConnection::isEmptyTable(KexiDBTableSchema* tableschema) const { + bool success; + bool notempty = connection()->isEmpty(*tableschema->tableschema(), success); + return (! (success && notempty)); +} + +KexiDBQuerySchema* KexiDBConnection::querySchema(const QString& queryname) const { + ::KexiDB::QuerySchema* queryschema = connection()->querySchema(queryname); + return queryschema ? new KexiDBQuerySchema(queryschema) : 0; +} + +bool KexiDBConnection::autoCommit() const { return connection()->autoCommit(); } +bool KexiDBConnection::setAutoCommit(bool enabled) { return connection()->setAutoCommit(enabled); } + +KexiDBTransaction* KexiDBConnection::beginTransaction() { + ::KexiDB::Transaction t = connection()->beginTransaction(); + return new KexiDBTransaction(t); +} + +bool KexiDBConnection::commitTransaction(KexiDBTransaction* transaction) { return connection()->commitTransaction( transaction->transaction() ); } +bool KexiDBConnection::rollbackTransaction(KexiDBTransaction* transaction) { return connection()->rollbackTransaction( transaction->transaction() ); } +KexiDBTransaction* KexiDBConnection::defaultTransaction() { return new KexiDBTransaction( connection()->defaultTransaction() ); } +void KexiDBConnection::setDefaultTransaction(KexiDBTransaction* transaction) { connection()->setDefaultTransaction( transaction->transaction() ); } + +Kross::Api::List* KexiDBConnection::transactions() { + return new Kross::Api::ListT<KexiDBTransaction>( connection()->transactions() ); +} + +KexiDBParser* KexiDBConnection::parser() { return new KexiDBParser(this, new ::KexiDB::Parser(connection())); } diff --git a/kexi/plugins/scripting/kexidb/kexidbconnection.h b/kexi/plugins/scripting/kexidb/kexidbconnection.h new file mode 100644 index 000000000..7e1a7d3a6 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbconnection.h @@ -0,0 +1,194 @@ +/*************************************************************************** + * kexidbconnection.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBCONNECTION_H +#define KROSS_KEXIDB_KEXIDBCONNECTION_H + +#include <qstring.h> +#include <ksharedptr.h> + +#include <api/object.h> +#include <api/variant.h> +#include <api/list.h> +#include <api/class.h> + +//#include <kexidb/driver.h> +#include <kexidb/connection.h> + +namespace Kross { namespace KexiDB { + + // Forward declarations. + class KexiDBDriver; + class KexiDBConnectionData; + class KexiDBCursor; + class KexiDBTableSchema; + class KexiDBQuerySchema; + class KexiDBTransaction; + class KexiDBParser; + + /** + * A connection to a database. + * + * Example (in Python) ; + * @code + * # Import the kexidb module. + * import krosskexidb + * # Get the drivermanager. + * drivermanager = krosskexidb.DriverManager() + * # We need a connectiondata object. + * connectiondata = drivermanager.createConnectionData() + * # Fill the new connectiondata object with what we need to connect. + * connectiondata.setFileName("/home/user/kexisqlite3file.kexi") + * # Create the database-driver to access the SQLite3 backend. + * driver = drivermanager.driver("SQLite3") + * # Create the connection now. + * connection = driver.createConnection(connectiondata) + * # Establish the connection. + * if not connection.connect(): raise("Failed to connect with db") + * # Open database for usage. The filebased driver uses the filename as databasename. + * if not connection.useDatabase("/home/user/kexisqlite3file.kexi"): raise("Failed to use db") + * @endcode + */ + class KexiDBConnection : public Kross::Api::Class<KexiDBConnection> + { + public: + KexiDBConnection(::KexiDB::Connection* connection, KexiDBDriver* driver = 0, KexiDBConnectionData* connectiondata = 0); + virtual ~KexiDBConnection(); + virtual const QString getClassName() const; + + private: + + /** Return true if there was an error during last operation on the database. */ + bool hadError() const; + /** Return the last errormessage. */ + const QString lastError() const; + + /** Return the KexiDBConnectionData object used to create this connection. */ + KexiDBConnectionData* data(); + /** Return the KexiDBDriver object this connection belongs too. */ + KexiDBDriver* driver(); + + /** Try to connect and return true if we are successfully connected now. */ + bool connect(); + /** Return true if we are connected. */ + bool isConnected(); + /** Disconnect and return true if we are successfully disconnected now. */ + bool disconnect(); + + /** Returns true if the connection is read-only. */ + bool isReadOnly() const; + + /** Return true if the as argument passed databasename exists. */ + bool databaseExists(const QString& dbname); + /** Return the name of currently used database for this connection or empty + string if there is no used database. */ + const QString currentDatabase() const; + /** Return list of database names for opened connection. */ + const QStringList databaseNames() const; + /** Return true if connection is properly established. */ + bool isDatabaseUsed() const; + /** Opens an existing database specified by the as argument passed databasename + and returns true if the database is used now. */ + bool useDatabase(const QString& dbname); + /** Closes currently used database for this connection. */ + bool closeDatabase(); + + /** Return names of all table schemas stored in currently used database include the + internal KexiDB system table names (kexi__*) */ + const QStringList allTableNames() const; + /** Return names of all table schemas without the internal KexiDB system table names (kexi__*) */ + const QStringList tableNames() const; + /** Return names of all query schemas stored in currently used database. */ + const QStringList queryNames() const; + + /** Executes query described by the as argument passed sqlstatement-string. Returns the + opened cursor created for results of this query. */ + KexiDBCursor* executeQueryString(const QString& sqlquery); + /** Executes query described by the as argument passed KexiDBQuerySchema object. Returns + the opened cursor created for results of this query. */ + KexiDBCursor* executeQuerySchema(KexiDBQuerySchema* queryschema); + +//TODO replace following method with a proxymethod. + /** Inserts the as argument passed KexiDBField object. */ + Kross::Api::Object::Ptr insertRecord(Kross::Api::List::Ptr); + + /** Creates new database with the as argument passed databasename. */ + bool createDatabase(const QString& dbname); + /** Drops the as argument passed databasename. */ + bool dropDatabase(const QString& dbname); + + /** Creates table defined by the as argument passed KexiTableSchema object. */ + bool createTable(KexiDBTableSchema* tableschema); + /** Drops table defined by the as argument passed KexiDBTableSchema object. */ + bool dropTable(const QString& tablename); + /** Alters the as first argument passed KexiDBTableSchema object using the as + second argument passed KexiDBTableSchema. */ + bool alterTable(KexiDBTableSchema* fromschema, KexiDBTableSchema* toschema); + /** Alters the tablename of the as first argument passed KexiDBTableSchema into + the as second argument passed new tablename. */ + bool alterTableName(KexiDBTableSchema* tableschema, const QString& newtablename); + + /** Returns the KexiDBTableSchema object of the table matching to the as argument + passed tablename. */ + KexiDBTableSchema* tableSchema(const QString& tablename) const; + /** Returns true if there is at least one valid record in the as argument passed tablename. */ + bool isEmptyTable(KexiDBTableSchema* tableschema) const; + /** Returns the KexiDBQuerySchema object of the query matching to the as argument passed queryname. */ + KexiDBQuerySchema* querySchema(const QString& queryname) const; + + /** Return true if the \"auto commit\" option is on. */ + bool autoCommit() const; + /** Set the auto commit option. This does not affect currently started transactions and can + be changed even when connection is not established. */ + bool setAutoCommit(bool enabled); + + /** Creates new transaction handle and starts a new transaction. */ + KexiDBTransaction* beginTransaction(); + /** Commits the as rgument passed KexiDBTransaction object. */ + bool commitTransaction(KexiDBTransaction* transaction); + /** Rollback the as rgument passed KexiDBTransaction object. */ + bool rollbackTransaction(KexiDBTransaction* transaction); + /** Return the KEXIDBTransaction object for default transaction for this connection. */ + KexiDBTransaction* defaultTransaction(); + /** Sets default transaction that will be used as context for operations on data in opened + database for this connection. */ + void setDefaultTransaction(KexiDBTransaction* transaction); + + /** Return list of currently active KexiDBTransaction objects. */ + Kross::Api::List* transactions(); + + /** Return a KexiDBParser object. */ + KexiDBParser* parser(); + + private: + ::KexiDB::Connection* connection() const; + ::KexiDB::Connection* m_connection; + + KSharedPtr<KexiDBConnectionData> m_connectiondata; + KSharedPtr<KexiDBDriver> m_driver; + + /// Initialize the class instance. + void initialize(); + + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbconnectiondata.cpp b/kexi/plugins/scripting/kexidb/kexidbconnectiondata.cpp new file mode 100644 index 000000000..61b81d3e2 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbconnectiondata.cpp @@ -0,0 +1,112 @@ +/*************************************************************************** + * kexidbconnectiondata.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 "kexidbconnectiondata.h" + +#include <qvariant.h> + +using namespace Kross::KexiDB; + +KexiDBConnectionData::KexiDBConnectionData(::KexiDB::ConnectionData* data) + : Kross::Api::Class<KexiDBConnectionData>("KexiDBConnectionData") + , m_data(data) +{ + this->addFunction0< Kross::Api::Variant >("caption", this, &KexiDBConnectionData::caption); + this->addFunction1< void, Kross::Api::Variant >("setCaption", this, &KexiDBConnectionData::setCaption); + + this->addFunction0< Kross::Api::Variant >("description", this, &KexiDBConnectionData::description); + this->addFunction1< void, Kross::Api::Variant >("setDescription", this, &KexiDBConnectionData::setDescription); + + this->addFunction0< Kross::Api::Variant >("driverName", this, &KexiDBConnectionData::driverName); + this->addFunction1< void, Kross::Api::Variant >("setDriverName", this, &KexiDBConnectionData::setDriverName); + + this->addFunction0< Kross::Api::Variant >("localSocketFileUsed", this, &KexiDBConnectionData::localSocketFileUsed); + this->addFunction1< void, Kross::Api::Variant >("setLocalSocketFileUsed", this, &KexiDBConnectionData::setLocalSocketFileUsed); + + this->addFunction0< Kross::Api::Variant >("localSocketFileName", this, &KexiDBConnectionData::localSocketFileName); + this->addFunction1< void, Kross::Api::Variant >("setLocalSocketFileName", this, &KexiDBConnectionData::setLocalSocketFileName); + + this->addFunction0< Kross::Api::Variant >("databaseName", this, &KexiDBConnectionData::databaseName); + this->addFunction1< void, Kross::Api::Variant >("setDatabaseName", this, &KexiDBConnectionData::setDatabaseName); + + this->addFunction0< Kross::Api::Variant >("hostName", this, &KexiDBConnectionData::hostName); + this->addFunction1< void, Kross::Api::Variant >("setHostName", this, &KexiDBConnectionData::setHostName); + + this->addFunction0< Kross::Api::Variant >("port", this, &KexiDBConnectionData::port); + this->addFunction1< void, Kross::Api::Variant >("setPort", this, &KexiDBConnectionData::setPort); + + this->addFunction0< Kross::Api::Variant >("password", this, &KexiDBConnectionData::password); + this->addFunction1< void, Kross::Api::Variant >("setPassword", this, &KexiDBConnectionData::setPassword); + + this->addFunction0< Kross::Api::Variant >("userName", this, &KexiDBConnectionData::userName); + this->addFunction1< void, Kross::Api::Variant >("setUserName", this, &KexiDBConnectionData::setUserName); + + this->addFunction0< Kross::Api::Variant >("fileName", this, &KexiDBConnectionData::fileName); + this->addFunction1< void, Kross::Api::Variant >("setFileName", this, &KexiDBConnectionData::setFileName); + + this->addFunction0< Kross::Api::Variant >("dbPath", this, &KexiDBConnectionData::dbPath); + this->addFunction0< Kross::Api::Variant >("dbFileName", this, &KexiDBConnectionData::dbFileName); + this->addFunction0< Kross::Api::Variant >("serverInfoString", this, &KexiDBConnectionData::serverInfoString); +} + +KexiDBConnectionData::~KexiDBConnectionData() +{ + //delete m_data; +} + +const QString KexiDBConnectionData::getClassName() const +{ + return "Kross::KexiDB::KexiDBConnectionData"; +} + +const QString KexiDBConnectionData::caption() const { return m_data->caption; } +void KexiDBConnectionData::setCaption(const QString& name) { m_data->caption = name; } + +const QString KexiDBConnectionData::description() const { return m_data->description; } +void KexiDBConnectionData::setDescription(const QString& desc) { m_data->description = desc; } + +const QString KexiDBConnectionData::driverName() const { return m_data->driverName; } +void KexiDBConnectionData::setDriverName(const QString& driver) { m_data->driverName = driver; } + +bool KexiDBConnectionData::localSocketFileUsed() const { return m_data->useLocalSocketFile; } +void KexiDBConnectionData::setLocalSocketFileUsed(bool used) { m_data->useLocalSocketFile = used; } +const QString KexiDBConnectionData::localSocketFileName() const { return m_data->localSocketFileName; } +void KexiDBConnectionData::setLocalSocketFileName(const QString& socketfilename) { m_data->localSocketFileName = socketfilename; } + +const QString KexiDBConnectionData::databaseName() const { return m_dbname; } +void KexiDBConnectionData::setDatabaseName(const QString& dbname) { m_dbname = dbname; } + +const QString KexiDBConnectionData::hostName() const { return m_data->hostName; } +void KexiDBConnectionData::setHostName(const QString& hostname) { m_data->hostName = hostname; } + +int KexiDBConnectionData::port() const { return m_data->port; } +void KexiDBConnectionData::setPort(int p) { m_data->port = p; } + +const QString KexiDBConnectionData::password() const { return m_data->password; } +void KexiDBConnectionData::setPassword(const QString& passwd) { m_data->password = passwd; } + +const QString KexiDBConnectionData::userName() const { return m_data->userName; } +void KexiDBConnectionData::setUserName(const QString& username) { m_data->userName = username; } + +const QString KexiDBConnectionData::fileName() const { return m_data->fileName(); } +void KexiDBConnectionData::setFileName(const QString& filename) { m_data->setFileName(filename); } + +const QString KexiDBConnectionData::dbPath() const { return m_data->dbPath(); } +const QString KexiDBConnectionData::dbFileName() const { return m_data->dbFileName(); } +const QString KexiDBConnectionData::serverInfoString() const { return m_data->serverInfoString(true); } diff --git a/kexi/plugins/scripting/kexidb/kexidbconnectiondata.h b/kexi/plugins/scripting/kexidb/kexidbconnectiondata.h new file mode 100644 index 000000000..aaddffbd8 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbconnectiondata.h @@ -0,0 +1,126 @@ +/*************************************************************************** + * kexidbconnectiondata.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBCONNECTIONDATA_H +#define KROSS_KEXIDB_KEXIDBCONNECTIONDATA_H + +#include <qstring.h> + +#include <api/object.h> +#include <api/variant.h> +#include <api/list.h> +#include <api/class.h> + +#include <kexidb/connection.h> +#include <kexidb/connectiondata.h> + +namespace Kross { namespace KexiDB { + + /** + * A KexiDBConnectionData is used to store the details needed for + * a connection with a database. + */ + class KexiDBConnectionData : public Kross::Api::Class<KexiDBConnectionData> + { + friend class KexiDBDriverManager; + public: + KexiDBConnectionData(::KexiDB::ConnectionData* data); + virtual ~KexiDBConnectionData(); + operator ::KexiDB::ConnectionData& () { return *m_data; } + operator ::KexiDB::ConnectionData* () { return m_data; } + virtual const QString getClassName() const; + ::KexiDB::ConnectionData* data() { return m_data; } + + private: + + /** Return the connection name. */ + const QString caption() const; + /** Set the connection name. */ + void setCaption(const QString& name); + + /** Return the description. */ + const QString description() const; + /** Set the description. */ + void setDescription(const QString& desc); + + /** Return drivername. */ + const QString driverName() const; + /** Set the drivername. */ + void setDriverName(const QString& driver); + + /** Return true if a local socket file is used else false. */ + bool localSocketFileUsed() const; + /** Set if the local socket file should be used. */ + void setLocalSocketFileUsed(bool used); + /** Return the local socket filename. */ + const QString localSocketFileName() const; + /** Set the local socket filename. */ + void setLocalSocketFileName(const QString& socketfilename); + + // For serverbased drivers + + /** Return the database name. */ + const QString databaseName() const; + /** Set the database name. */ + void setDatabaseName(const QString& dbname); + + /** Return the hostname. */ + const QString hostName() const; + /** Set the hostname. */ + void setHostName(const QString& hostname); + + /** Return the port number. */ + int port() const; + /** Set the port number. */ + void setPort(int p); + + /** Return the password. */ + const QString password() const; + /** Set the password. */ + void setPassword(const QString& passwd); + + /** Return the username. */ + const QString userName() const; + /** Set the username. */ + void setUserName(const QString& username); + + // For filebased drivers + + /** Return the filename. */ + const QString fileName() const; + /** Set the filename. */ + void setFileName(const QString& filename); + + /** Return the database path. */ + const QString dbPath() const; + /** Return the database filename. */ + const QString dbFileName() const; + + /** Return a user-friendly string representation. */ + const QString serverInfoString() const; + + private: + ::KexiDB::ConnectionData* m_data; + QString m_dbname; + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbcursor.cpp b/kexi/plugins/scripting/kexidb/kexidbcursor.cpp new file mode 100644 index 000000000..3bc1763d3 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbcursor.cpp @@ -0,0 +1,139 @@ +/*************************************************************************** + * kexidbcursor.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 "kexidbcursor.h" +#include "kexidbconnection.h" + +#include <kexidb/tableschema.h> +#include <kexidb/queryschema.h> + +#include <kdebug.h> + +using namespace Kross::KexiDB; + +KexiDBCursor::KexiDBCursor(::KexiDB::Cursor* cursor) + : Kross::Api::Class<KexiDBCursor>("KexiDBCursor") + , m_cursor(cursor) +{ + this->addFunction0<Kross::Api::Variant>("open", this, &KexiDBCursor::open ); + this->addFunction0<Kross::Api::Variant>("isOpened", this, &KexiDBCursor::isOpened ); + this->addFunction0<Kross::Api::Variant>("reopen", this, &KexiDBCursor::reopen ); + this->addFunction0<Kross::Api::Variant>("close", this, &KexiDBCursor::close ); + this->addFunction0<Kross::Api::Variant>("moveFirst", this, &KexiDBCursor::moveFirst ); + this->addFunction0<Kross::Api::Variant>("moveLast", this, &KexiDBCursor::moveLast ); + this->addFunction0<Kross::Api::Variant>("movePrev", this, &KexiDBCursor::movePrev ); + this->addFunction0<Kross::Api::Variant>("moveNext", this, &KexiDBCursor::moveNext ); + this->addFunction0<Kross::Api::Variant>("bof", this, &KexiDBCursor::bof ); + this->addFunction0<Kross::Api::Variant>("eof", this, &KexiDBCursor::eof ); + this->addFunction0<Kross::Api::Variant>("at", this, &KexiDBCursor::at ); + this->addFunction0<Kross::Api::Variant>("fieldCount", this, &KexiDBCursor::fieldCount ); + this->addFunction1<Kross::Api::Variant, Kross::Api::Variant>("value", this, &KexiDBCursor::value ); + this->addFunction2<Kross::Api::Variant, Kross::Api::Variant, Kross::Api::Variant>("setValue", this, &KexiDBCursor::setValue ); + this->addFunction0<Kross::Api::Variant>("save", this, &KexiDBCursor::save ); +} + +KexiDBCursor::~KexiDBCursor() +{ + ///@todo check ownership + //delete m_cursor; + + clearBuffers(); +} + +void KexiDBCursor::clearBuffers() +{ + QMap<Q_LLONG, Record*>::ConstIterator + it( m_modifiedrecords.constBegin() ), end( m_modifiedrecords.constEnd() ); + for( ; it != end; ++it) + delete it.data(); + m_modifiedrecords.clear(); +} + +const QString KexiDBCursor::getClassName() const +{ + return "Kross::KexiDB::KexiDBCursor"; +} + +bool KexiDBCursor::open() { return m_cursor->open(); } +bool KexiDBCursor::isOpened() { return m_cursor->isOpened(); } +bool KexiDBCursor::reopen() { return m_cursor->reopen(); } +bool KexiDBCursor::close() { return m_cursor->close(); } + +bool KexiDBCursor::moveFirst() { return m_cursor->moveFirst(); } +bool KexiDBCursor::moveLast() { return m_cursor->moveLast(); } +bool KexiDBCursor::movePrev() { return m_cursor->movePrev(); } +bool KexiDBCursor::moveNext() { return m_cursor->moveNext(); } + +bool KexiDBCursor::bof() { return m_cursor->bof(); } +bool KexiDBCursor::eof() { return m_cursor->eof(); } + +Q_LLONG KexiDBCursor::at() { return m_cursor->at(); } +uint KexiDBCursor::fieldCount() { return m_cursor->fieldCount(); } + +QVariant KexiDBCursor::value(uint index) +{ + return m_cursor->value(index); +} + +bool KexiDBCursor::setValue(uint index, QVariant value) +{ + ::KexiDB::QuerySchema* query = m_cursor->query(); + if(! query) { + kdDebug() << "Invalid query in KexiDBCursor::setValue index=" << index << " value=" << value << endl; + return false; + } + + ::KexiDB::QueryColumnInfo* column = query->fieldsExpanded().at(index); + if(! column) { + kdDebug() << "Invalid column in KexiDBCursor::setValue index=" << index << " value=" << value << endl; + return false; + } + + const Q_LLONG position = m_cursor->at(); + if(! m_modifiedrecords.contains(position)) + m_modifiedrecords.replace(position, new Record(m_cursor)); + m_modifiedrecords[position]->buffer->insert(*column, value); + return true; +} + +bool KexiDBCursor::save() +{ + if(m_modifiedrecords.count() < 1) + return true; + + //It is needed to close the cursor before we are able to update the rows + //since else the database could be locked (e.g. at the case of SQLite a + //KexiDB: Object ERROR: 6: SQLITE_LOCKED would prevent updating). + //Maybe it works fine with other drivers like MySQL or Postqre? + m_cursor->close(); + + bool ok = true; + QMap<Q_LLONG, Record*>::ConstIterator + it( m_modifiedrecords.constBegin() ), end( m_modifiedrecords.constEnd() ); + for( ; it != end; ++it) { + bool b = m_cursor->updateRow(it.data()->rowdata, * it.data()->buffer, m_cursor->isBuffered()); + if(ok) { + ok = b; + //break; + } + } + //m_cursor->close(); + clearBuffers(); + return ok; +} diff --git a/kexi/plugins/scripting/kexidb/kexidbcursor.h b/kexi/plugins/scripting/kexidb/kexidbcursor.h new file mode 100644 index 000000000..6e92a38ec --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbcursor.h @@ -0,0 +1,159 @@ +/*************************************************************************** + * kexidbcursor.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBCURSOR_H +#define KROSS_KEXIDB_KEXIDBCURSOR_H + +#include <qstring.h> + +#include <api/object.h> +#include <api/variant.h> +#include <api/list.h> +#include <api/class.h> + +#include <kexidb/cursor.h> +#include <kexidb/roweditbuffer.h> + +namespace Kross { namespace KexiDB { + + // Forward declaration. + class KexiDBConnection; + + /** + * The cursor provides a control structure for the successive traversal + * of records in a result set as returned e.g. by a query. + * + * Example (in Python) that shows how to iterate over the result of a query; + * @code + * # Once we have a KexiDBConnection object we are able to execute a query string and get a cursor as result. + * cursor = connection.executeQueryString("SELECT * from emp") + * # Let's check if the query was successfully. + * if not cursor: raise("Query failed") + * # Walk through all items in the table. + * while(not cursor.eof()): + * # Iterate over the fields the record has. + * for i in range( cursor.fieldCount() ): + * # Print some information. + * print "%s %s %s" % (cursor.at(), i, cursor.value(i)) + * # and move on to the next record. + * cursor.moveNext() + * @endcode + * + * Example (in Python) that shows how to use a cursor to strip + * all whitespaces at the beginning and the end from the values + * in a table; + * @code + * import krosskexidb + * drivermanager = krosskexidb.DriverManager() + * connectiondata = drivermanager.createConnectionDataByFile("/home/me/kexiprojectfile.kexi") + * driver = drivermanager.driver( connectiondata.driverName() ) + * connection = driver.createConnection(connectiondata) + * if not connection.connect(): raise "Failed to connect" + * if not connection.useDatabase( connectiondata.databaseName() ): + * if not connection.useDatabase( connectiondata.fileName() ): + * raise "Failed to use database" + * + * table = connection.tableSchema("emp") + * query = table.query() + * cursor = connection.executeQuerySchema(query) + * if not cursor: raise("Query failed") + * while(not cursor.eof()): + * for i in range( cursor.fieldCount() ): + * v = str( cursor.value(i) ) + * if v.startswith(' ') or v.endswith(' '): + * cursor.setValue(i, v.strip()) + * cursor.moveNext() + * if not cursor.save(): raise "Failed to save changes" + * @endcode + */ + class KexiDBCursor : public Kross::Api::Class<KexiDBCursor> + { + public: + KexiDBCursor(::KexiDB::Cursor* cursor); + virtual ~KexiDBCursor(); + virtual const QString getClassName() const; + + private: + + /** Opens the cursor. */ + bool open(); + /** Returns true if the cursor is opened else false. */ + bool isOpened(); + /** Closes and then opens again the same cursor. */ + bool reopen(); + /** Closes previously opened cursor. */ + bool close(); + + /** Moves current position to the first record and retrieves it. */ + bool moveFirst(); + /** Moves current position to the last record and retrieves it. */ + bool moveLast(); + /** Moves current position to the previous record and retrieves it. */ + bool movePrev(); + /** Moves current position to the next record and retrieves it. */ + bool moveNext(); + + /** Returns true if current position is before first record. */ + bool bof(); + /** Returns true if current position is after last record. */ + bool eof(); + + /** Returns current internal position of the cursor's query. Records + are numbered from 0; the value -1 means that the cursor does not + point to a valid record. */ + Q_LLONG at(); + /** Returns the number of fields available for this cursor. */ + uint fieldCount(); + /** Returns the value stored in the passed column number (counting from 0). */ + QVariant value(uint index); + /** Set the value for the field defined with index. The new value is buffered + and does not got written as long as save() is not called. */ + bool setValue(uint index, QVariant value); + + /** Save any changes done with setValue(). You should call this only once at + the end of all value/setValue iterations cause the cursor is closed once + the changes got saved successfully. */ + bool save(); + + private: + ::KexiDB::Cursor* m_cursor; + + class Record { + public: + ::KexiDB::RowData rowdata; + ::KexiDB::RowEditBuffer* buffer; + Record(::KexiDB::Cursor* cursor) + : buffer( new ::KexiDB::RowEditBuffer(true) ) + { + cursor->storeCurrentRow(rowdata); + } + ~Record() + { + delete buffer; + } + }; + QMap<Q_LLONG, Record*> m_modifiedrecords; + + void clearBuffers(); + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbdriver.cpp b/kexi/plugins/scripting/kexidb/kexidbdriver.cpp new file mode 100644 index 000000000..f019b237f --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbdriver.cpp @@ -0,0 +1,70 @@ +/*************************************************************************** + * kexidbdriver.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 "kexidbdriver.h" +#include "kexidbdrivermanager.h" + +#include <qvaluelist.h> +#include <qptrlist.h> +#include <kdebug.h> + +#include <kexidb/connection.h> + +using namespace Kross::KexiDB; + +KexiDBDriver::KexiDBDriver(::KexiDB::Driver* driver) + : Kross::Api::Class<KexiDBDriver>("KexiDBDriver") + , m_driver(driver) +{ + this->addFunction0<Kross::Api::Variant>("isValid", this, &KexiDBDriver::isValid ); + this->addFunction0<Kross::Api::Variant>("versionMajor", this, &KexiDBDriver::versionMajor ); + this->addFunction0<Kross::Api::Variant>("versionMinor", this, &KexiDBDriver::versionMinor ); + this->addFunction1<Kross::Api::Variant, Kross::Api::Variant>("escapeString", this, &KexiDBDriver::escapeString); + this->addFunction0<Kross::Api::Variant>("isFileDriver", this, &KexiDBDriver::isFileDriver ); + this->addFunction0<Kross::Api::Variant>("fileDBDriverMimeType", this, &KexiDBDriver::fileDBDriverMimeType ); + this->addFunction1<Kross::Api::Variant, Kross::Api::Variant>("isSystemObjectName", this, &KexiDBDriver::isSystemObjectName ); + this->addFunction1<Kross::Api::Variant, Kross::Api::Variant>("isSystemDatabaseName", this, &KexiDBDriver::isSystemDatabaseName ); + this->addFunction1<Kross::Api::Variant, Kross::Api::Variant>("isSystemFieldName", this, &KexiDBDriver::isSystemFieldName ); + this->addFunction2<Kross::Api::Variant, Kross::Api::Variant, Kross::Api::Variant> ("valueToSQL", this, &KexiDBDriver::valueToSQL ); + + this->addFunction1<KexiDBConnection, KexiDBConnectionData>("createConnection", this, &KexiDBDriver::createConnection); + this->addFunction0< Kross::Api::ListT< KexiDBConnection > >("connectionsList", this, &KexiDBDriver::connectionsList); +} + +KexiDBDriver::~KexiDBDriver() +{ +} + +const QString KexiDBDriver::getClassName() const +{ + return "Kross::KexiDB::KexiDBDriver"; +} + +bool KexiDBDriver::isValid() { return m_driver->isValid(); } +int KexiDBDriver::versionMajor() { return m_driver->version().major; } +int KexiDBDriver::versionMinor() { return m_driver->version().minor; } +QString KexiDBDriver::escapeString(const QString& s) { return m_driver->escapeString(s); } +bool KexiDBDriver::isFileDriver() { return m_driver->isFileDriver(); } +QString KexiDBDriver::fileDBDriverMimeType() { return m_driver->fileDBDriverMimeType(); } +bool KexiDBDriver::isSystemObjectName(const QString& name) { return m_driver->isSystemObjectName(name); } +bool KexiDBDriver::isSystemDatabaseName(const QString& name) { return m_driver->isSystemDatabaseName(name); } +bool KexiDBDriver::isSystemFieldName(const QString& name) { return m_driver->isSystemFieldName(name); } +QString KexiDBDriver::valueToSQL(const QString& fieldtype, const QVariant& value) { return m_driver->valueToSQL(fieldtype, value); } +KexiDBConnection* KexiDBDriver::createConnection(KexiDBConnectionData* data) { return new KexiDBConnection( m_driver->createConnection(*data) ); } +QPtrList< ::KexiDB::Connection > KexiDBDriver::connectionsList() { return m_driver->connectionsList(); } diff --git a/kexi/plugins/scripting/kexidb/kexidbdriver.h b/kexi/plugins/scripting/kexidb/kexidbdriver.h new file mode 100644 index 000000000..edf7283c7 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbdriver.h @@ -0,0 +1,114 @@ +/*************************************************************************** + * kexidbdriver.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBDRIVER_H +#define KROSS_KEXIDB_KEXIDBDRIVER_H + +#include <qstring.h> +#include <qvaluelist.h> +//#include <qguardedptr.h> + +#include <api/object.h> +#include <api/variant.h> +#include <api/list.h> +#include <api/class.h> + +#include <kexidb/driver.h> + +#include "kexidbconnection.h" +#include "kexidbconnectiondata.h" + +namespace Kross { namespace KexiDB { + + /** + * Drivers are the implementations Kexi uses to access the + * driver-backends. + * + * Example (in Python) ; + * @code + * # Import the kexidb module. + * import krosskexidb + * # Get the drivermanager. + * drivermanager = krosskexidb.DriverManager() + * # Create the driver now. + * driver = drivermanager.driver("SQLite3") + * # Check if the driver is valid. + * if not driver.isValid(): raise "Invalid driver" + * # Create a connectiondata object. + * connectiondata = drivermanager.createConnectionData() + * # Fill the new connectiondata object with what we need to connect. + * connectiondata.setFileName("/home/user/kexisqlite3file.kexi") + * # Print the list of connections before. + * print driver.connectionsList() + * # Create the connection now. + * connection = driver.createConnection(connectiondata) + * # Print the list of connections again. This includes our just created connection now. + * print driver.connectionsList() + * @endcode + */ + class KexiDBDriver : public Kross::Api::Class<KexiDBDriver> + { + public: + KexiDBDriver(::KexiDB::Driver* driver); + virtual ~KexiDBDriver(); + virtual const QString getClassName() const; + + private: + + /** Return true if this driver is valid else false. */ + bool isValid(); + /** The drivers major versionnumber. */ + int versionMajor(); + /** The drivers minor versionnumber. */ + int versionMinor(); + /** Driver-specific SQL string escaping. For example the " or ' char may + need to be escaped for values used within SQL-statements. */ + QString escapeString(const QString& s); + /** Returns true if this driver is file-based. */ + bool isFileDriver(); + /** Return a name of MIME type of files handled by this driver if it is a + file-based database's driver otherwise returns null string. */ + QString fileDBDriverMimeType(); + /** Returns true if the passed string is a system object's name, eg. name + of build-in system table that cannot be used or created by a user. */ + bool isSystemObjectName(const QString& name); + /** Returns true if the passed string is a system database's name, eg. name + of build-in, system database that cannot be used or created by a user. */ + bool isSystemDatabaseName(const QString& name); + /** Returns true if the passed string is a system field's name, build-in + system field that cannot be used or created by a user. */ + bool isSystemFieldName(const QString& name); + /** The as second argument passed string got escaped to be usable within + a SQL-statement and those escaped string got returned by the method. + The first argument defines the fieldtype to what we should escape the + second argument to. */ + QString valueToSQL(const QString& fieldtype, const QVariant& value); + /** Create a new KexiDBConnection object and return it. */ + KexiDBConnection* createConnection(KexiDBConnectionData* data); + /** Return a list of KexiDBConnection objects. */ + QPtrList< ::KexiDB::Connection > connectionsList(); + + private: + ::KexiDB::Driver* m_driver; + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbdrivermanager.cpp b/kexi/plugins/scripting/kexidb/kexidbdrivermanager.cpp new file mode 100644 index 000000000..66a0df268 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbdrivermanager.cpp @@ -0,0 +1,178 @@ +/*************************************************************************** + * kexidbdrivermanager.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 "kexidbdrivermanager.h" +#include "kexidbdriver.h" +#include "kexidbconnectiondata.h" +#include "kexidbfield.h" +#include "kexidbschema.h" + +#include <api/exception.h> + +#include <qguardedptr.h> +#include <kdebug.h> +#include <kmimetype.h> + +#include <kexidb/driver.h> +#include <kexidb/connectiondata.h> +#include <kexidb/field.h> +#include <kexidb/tableschema.h> +#include <kexidb/queryschema.h> + +using namespace Kross::KexiDB; + +KexiDBDriverManager::KexiDBDriverManager() + : Kross::Api::Class<KexiDBDriverManager>("DriverManager") +{ + //krossdebug( QString("Kross::KexiDB::KexiDBDriverManager::KexiDBDriverManager()") ); + + this->addFunction0< Kross::Api::Variant >("driverNames", this, &KexiDBDriverManager::driverNames); + + this->addFunction1< KexiDBDriver, Kross::Api::Variant >("driver", this, &KexiDBDriverManager::driver); + this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("lookupByMime", this, &KexiDBDriverManager::lookupByMime); + this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("mimeForFile", this, &KexiDBDriverManager::mimeForFile); + + this->addFunction0< KexiDBConnectionData >("createConnectionData", this, &KexiDBDriverManager::createConnectionData); + this->addFunction1< KexiDBConnectionData, Kross::Api::Variant >("createConnectionDataByFile", this, &KexiDBDriverManager::createConnectionDataByFile); + this->addFunction0< KexiDBField >("field", this, &KexiDBDriverManager::field); + this->addFunction1< KexiDBTableSchema, Kross::Api::Variant >("tableSchema", this, &KexiDBDriverManager::tableSchema); + this->addFunction0< KexiDBQuerySchema>("querySchema", this, &KexiDBDriverManager::querySchema); +} + +KexiDBDriverManager::~KexiDBDriverManager() { + //krossdebug( QString("Kross::KexiDB::KexiDBDriverManager::~KexiDBDriverManager()") ); +} + +const QString KexiDBDriverManager::getClassName() const { + return "Kross::KexiDB::KexiDBDriverManager"; +} + +KexiDB::DriverManager& KexiDBDriverManager::driverManager() +{ + if(m_drivermanager.error()) + throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("KexiDB::DriverManager error: %1").arg(m_drivermanager.errorMsg())) ); + return m_drivermanager; +} + +const QStringList KexiDBDriverManager::driverNames() { + return driverManager().driverNames(); +} + +KexiDBDriver* KexiDBDriverManager::driver(const QString& drivername) { + QGuardedPtr< ::KexiDB::Driver > driver = driverManager().driver(drivername); // caching is done by the DriverManager + if(! driver) return 0; + if(driver->error()) throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("KexiDB::Driver error for drivername '%1': %2").arg(drivername).arg(driver->errorMsg())) ); + return new KexiDBDriver(driver); +} + +const QString KexiDBDriverManager::lookupByMime(const QString& mimetype) { + return driverManager().lookupByMime(mimetype); +} + +const QString KexiDBDriverManager::mimeForFile(const QString& filename) { + QString mimename = KMimeType::findByFileContent( filename )->name(); + if(mimename.isEmpty() || mimename=="application/octet-stream" || mimename=="text/plain") + mimename = KMimeType::findByURL(filename)->name(); + return mimename; +} + +KexiDBConnectionData* KexiDBDriverManager::createConnectionData() { + return new KexiDBConnectionData( new ::KexiDB::ConnectionData() ); +} + +KexiDBConnectionData* KexiDBDriverManager::createConnectionDataByFile(const QString& filename) { + //! @todo reuse the original code! + + QString mimename = KMimeType::findByFileContent(filename)->name(); + if(mimename.isEmpty() || mimename=="application/octet-stream" || mimename=="text/plain") + mimename = KMimeType::findByURL(filename)->name(); + + if(mimename == "application/x-kexiproject-shortcut" || mimename == "application/x-kexi-connectiondata") { + KConfig config(filename, true, false); + QString groupkey; + QStringList groups(config.groupList()); + QStringList::ConstIterator it, end( groups.constEnd() ); + for( it = groups.constBegin(); it != end; ++it) { + if((*it).lower()!="file information") { + groupkey = *it; + break; + } + } + if(groupkey.isNull()) { + kdDebug() << "No groupkey in KexiDBDriverManager::createConnectionDataByFile filename=" << filename << endl; + return 0; + } + + config.setGroup(groupkey); + //QString type( config.readEntry("type", "database").lower() ); + //bool isDatabaseShortcut = (type == "database"); + + ::KexiDB::ConnectionData* data = new ::KexiDB::ConnectionData(); + int version = config.readNumEntry("version", 2); //KexiDBShortcutFile_version + data->setFileName(QString::null); + data->caption = config.readEntry("caption"); + data->description = config.readEntry("comment"); + QString dbname = config.readEntry("name"); + data->driverName = config.readEntry("engine"); + data->hostName = config.readEntry("server"); + data->port = config.readNumEntry("port", 0); + data->useLocalSocketFile = config.readBoolEntry("useLocalSocketFile", false); + data->localSocketFileName = config.readEntry("localSocketFile"); + + if(version >= 2 && config.hasKey("encryptedPassword")) { + data->password = config.readEntry("encryptedPassword"); + uint len = data->password.length(); + for (uint i=0; i<len; i++) + data->password[i] = QChar( data->password[i].unicode() - 47 - i ); + } + if(data->password.isEmpty()) + data->password = config.readEntry("password"); + + data->savePassword = ! data->password.isEmpty(); + data->userName = config.readEntry("user"); + + KexiDBConnectionData* c = new KexiDBConnectionData(data); + c->setDatabaseName(dbname); + return c; + } + + QString const drivername = driverManager().lookupByMime(mimename); + if(! drivername) { + kdDebug() << "No driver in KexiDBDriverManager::createConnectionDataByFile filename=" << filename << " mimename=" << mimename << endl; + return 0; + } + + ::KexiDB::ConnectionData* data = new ::KexiDB::ConnectionData(); + data->setFileName(filename); + data->driverName = drivername; + return new KexiDBConnectionData(data); +} + +KexiDBField* KexiDBDriverManager::field() { + return new KexiDBField( new ::KexiDB::Field() ); +} + +KexiDBTableSchema* KexiDBDriverManager::tableSchema(const QString& tablename) { + return new KexiDBTableSchema( new ::KexiDB::TableSchema(tablename) ); +} + +KexiDBQuerySchema* KexiDBDriverManager::querySchema() { + return new KexiDBQuerySchema( new ::KexiDB::QuerySchema() ); +} + diff --git a/kexi/plugins/scripting/kexidb/kexidbdrivermanager.h b/kexi/plugins/scripting/kexidb/kexidbdrivermanager.h new file mode 100644 index 000000000..b6e311083 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbdrivermanager.h @@ -0,0 +1,105 @@ +/*************************************************************************** + * kexidbdrivermanager.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBDRIVERMANAGER_H +#define KROSS_KEXIDB_KEXIDBDRIVERMANAGER_H + +#include <qstring.h> + +#include <api/object.h> +#include <api/variant.h> +#include <api/list.h> +#include <api/class.h> + +#include <kexidb/drivermanager.h> + +namespace Kross { namespace KexiDB { + + // Forward declarations. + class KexiDBDriver; + class KexiDBConnectionData; + class KexiDBField; + class KexiDBTableSchema; + class KexiDBQuerySchema; + + /** + * The drivermanager is the base class to access KexiDBDriver objects and provides + * common functionality to deal with the KexiDB module. + * + * Example (in Python) ; + * @code + * # Import the kexidb module. + * import krosskexidb + * # Get the drivermanager. + * drivermanager = krosskexidb.DriverManager() + * # Let's determinate the mimetype (e.g. "application/x-sqlite3"). + * mimetype = drivermanager.mimeForFile("/home/user/mykexidbfile.kexi") + * # Now we use that mimetype to get the name of the driver to handle that file (e.g. "SQLite3") + * drivername = drivermanager.lookupByMime(mimetype) + * # We are able to create the driver now. + * driver = drivermanager.driver(drivername) + * @endcode + */ + class KexiDBDriverManager : public Kross::Api::Class<KexiDBDriverManager> + { + public: + KexiDBDriverManager(); + virtual ~KexiDBDriverManager(); + virtual const QString getClassName() const; + + private: + + /** Returns a list with avaible drivernames. */ + const QStringList driverNames(); + + /** Return the to the defined drivername matching KexiDBDriver object. */ + KexiDBDriver* driver(const QString& drivername); + + /** Return the to the defined mimetype-string matching drivername. */ + const QString lookupByMime(const QString& mimetype); + + /** Return the matching mimetype for the defined file. */ + const QString mimeForFile(const QString& filename); + + /** Return a new KexiDBConnectionData object. */ + KexiDBConnectionData* createConnectionData(); + + /** Create and return a KexiDBConnectionData object. Fill the content of the + KexiDBConnectionData object with the defined file as. The file could be e.g. + a *.kexi file or a *.kexis file. */ + KexiDBConnectionData* createConnectionDataByFile(const QString& filename); + + /** Return a new KexiDBField object. */ + KexiDBField* field(); + + /** Return a new KexiDBTableSchema object. */ + KexiDBTableSchema* tableSchema(const QString& tablename); + + /** Return a new KexiDBQuerySchema object. */ + KexiDBQuerySchema* querySchema(); + + private: + inline ::KexiDB::DriverManager& driverManager(); + ::KexiDB::DriverManager m_drivermanager; + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbfield.cpp b/kexi/plugins/scripting/kexidb/kexidbfield.cpp new file mode 100644 index 000000000..949b5e1a6 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbfield.cpp @@ -0,0 +1,147 @@ +/*************************************************************************** + * kexidbfield.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 "kexidbfield.h" + +#include <api/variant.h> + +using namespace Kross::KexiDB; + +KexiDBField::KexiDBField(::KexiDB::Field* field) + : Kross::Api::Class<KexiDBField>("KexiDBField") + , m_field(field) +{ + this->addFunction0< Kross::Api::Variant >("type", this, &KexiDBField::type); + this->addFunction1< void, Kross::Api::Variant >("setType", this, &KexiDBField::setType); + + this->addFunction0< Kross::Api::Variant >("subType", this, &KexiDBField::subType); + this->addFunction1< void, Kross::Api::Variant >("setSubType", this, &KexiDBField::setSubType); + + this->addFunction0< Kross::Api::Variant >("variantType", this, &KexiDBField::variantType); + this->addFunction0< Kross::Api::Variant >("typeGroup", this, &KexiDBField::typeGroup); + + this->addFunction0< Kross::Api::Variant >("isAutoInc", this, &KexiDBField::isAutoInc); + this->addFunction1< void, Kross::Api::Variant >("setAutoInc", this, &KexiDBField::setAutoInc); + + this->addFunction0< Kross::Api::Variant >("isUniqueKey", this, &KexiDBField::isUniqueKey); + this->addFunction1< void, Kross::Api::Variant >("setUniqueKey", this, &KexiDBField::setUniqueKey); + + this->addFunction0< Kross::Api::Variant >("isPrimaryKey", this, &KexiDBField::isPrimaryKey); + this->addFunction1< void, Kross::Api::Variant >("setPrimaryKey", this, &KexiDBField::setPrimaryKey); + + this->addFunction0< Kross::Api::Variant >("isForeignKey", this, &KexiDBField::isForeignKey); + this->addFunction1< void, Kross::Api::Variant >("setForeignKey", this, &KexiDBField::setForeignKey); + + this->addFunction0< Kross::Api::Variant >("isNotNull", this, &KexiDBField::isNotNull); + this->addFunction1< void, Kross::Api::Variant >("setNotNull", this, &KexiDBField::setNotNull); + + this->addFunction0< Kross::Api::Variant >("isNotEmpty", this, &KexiDBField::isNotEmpty); + this->addFunction1< void, Kross::Api::Variant >("setNotEmpty", this, &KexiDBField::setNotEmpty); + + this->addFunction0< Kross::Api::Variant >("isIndexed", this, &KexiDBField::isIndexed); + this->addFunction1< void, Kross::Api::Variant >("setIndexed", this, &KexiDBField::setIndexed); + + this->addFunction0< Kross::Api::Variant >("isUnsigned", this, &KexiDBField::isUnsigned); + this->addFunction1< void, Kross::Api::Variant >("setUnsigned", this, &KexiDBField::setUnsigned); + + this->addFunction0< Kross::Api::Variant >("name", this, &KexiDBField::name); + this->addFunction1< void, Kross::Api::Variant >("setName", this, &KexiDBField::setName); + + this->addFunction0< Kross::Api::Variant >("caption", this, &KexiDBField::caption); + this->addFunction1< void, Kross::Api::Variant >("setCaption", this, &KexiDBField::setCaption); + + this->addFunction0< Kross::Api::Variant >("description", this, &KexiDBField::description); + this->addFunction1< void, Kross::Api::Variant >("setDescription", this, &KexiDBField::setDescription); + + this->addFunction0< Kross::Api::Variant >("length", this, &KexiDBField::length); + this->addFunction1< void, Kross::Api::Variant >("setLength", this, &KexiDBField::setLength); + + this->addFunction0< Kross::Api::Variant >("precision", this, &KexiDBField::precision); + this->addFunction1< void, Kross::Api::Variant >("setPrecision", this, &KexiDBField::setPrecision); + + this->addFunction0< Kross::Api::Variant >("width", this, &KexiDBField::width); + this->addFunction1< void, Kross::Api::Variant >("setWidth", this, &KexiDBField::setWidth); + + this->addFunction0< Kross::Api::Variant >("defaultValue", this, &KexiDBField::defaultValue); + this->addFunction1< void, Kross::Api::Variant >("setDefaultValue", this, &KexiDBField::setDefaultValue); +} + +KexiDBField::~KexiDBField() +{ +} + +const QString KexiDBField::getClassName() const +{ + return "Kross::KexiDB::KexiDBField"; +} + +const QString KexiDBField::type() { return m_field->typeString(); } +void KexiDBField::setType(const QString type) { m_field->setType( ::KexiDB::Field::typeForString(type) ); } + +const QString KexiDBField::subType() { return m_field->subType(); } +void KexiDBField::setSubType(const QString& subtype) { m_field->setSubType(subtype); } + +const QString KexiDBField::variantType() { return QVariant::typeToName( m_field->variantType() ); } +const QString KexiDBField::typeGroup() { return m_field->typeGroupString(); } + +bool KexiDBField::isAutoInc() { return m_field->isAutoIncrement(); } +void KexiDBField::setAutoInc(bool autoinc) { m_field->setAutoIncrement(autoinc); } + +bool KexiDBField::isUniqueKey() { return m_field->isUniqueKey(); } +void KexiDBField::setUniqueKey(bool unique) { m_field->setUniqueKey(unique); } + +bool KexiDBField::isPrimaryKey() { return m_field->isPrimaryKey(); } +void KexiDBField::setPrimaryKey(bool primary) { m_field->setPrimaryKey(primary); } + +bool KexiDBField::isForeignKey() { return m_field->isForeignKey(); } +void KexiDBField::setForeignKey(bool foreign) { m_field->setForeignKey(foreign); } + +bool KexiDBField::isNotNull() { return m_field->isNotNull(); } +void KexiDBField::setNotNull(bool notnull) { m_field->setNotNull(notnull); } + +bool KexiDBField::isNotEmpty() { return m_field->isNotEmpty(); } +void KexiDBField::setNotEmpty(bool notempty) { m_field->setNotEmpty(notempty); } + +bool KexiDBField::isIndexed() { return m_field->isIndexed(); } +void KexiDBField::setIndexed(bool indexed) { m_field->setIndexed(indexed); } + +bool KexiDBField::isUnsigned() { return m_field->isUnsigned(); } +void KexiDBField::setUnsigned(bool isunsigned) { m_field->setUnsigned(isunsigned); } + +const QString KexiDBField::name() { return m_field->name(); } +void KexiDBField::setName(const QString& name) { m_field->setName(name); } + +const QString KexiDBField::caption() { return m_field->caption(); } +void KexiDBField::setCaption(const QString& caption) { m_field->setCaption(caption); } + +const QString KexiDBField::description() { return m_field->description(); } +void KexiDBField::setDescription(const QString& desc) { m_field->setDescription(desc); } + +uint KexiDBField::length() { return m_field->length(); } +void KexiDBField::setLength(uint length) { m_field->setLength(length); } + +uint KexiDBField::precision() { return m_field->precision(); } +void KexiDBField::setPrecision(uint precision) { m_field->setPrecision(precision); } + +uint KexiDBField::width() { return m_field->width(); } +void KexiDBField::setWidth(uint width) { m_field->setWidth(width); } + +QVariant KexiDBField::defaultValue() { return m_field->defaultValue(); } +void KexiDBField::setDefaultValue(const QVariant& defaultvalue) { m_field->setDefaultValue(defaultvalue); } diff --git a/kexi/plugins/scripting/kexidb/kexidbfield.h b/kexi/plugins/scripting/kexidb/kexidbfield.h new file mode 100644 index 000000000..a4c2ef238 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbfield.h @@ -0,0 +1,148 @@ +/*************************************************************************** + * kexidbfield.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBFIELD_H +#define KROSS_KEXIDB_KEXIDBFIELD_H + +#include <qstring.h> + +#include <api/object.h> +#include <api/list.h> +#include <api/class.h> + +#include <kexidb/drivermanager.h> +#include <kexidb/field.h> + +namespace Kross { namespace KexiDB { + + /** + * A field in a record. + */ + class KexiDBField : public Kross::Api::Class<KexiDBField> + { + public: + KexiDBField(::KexiDB::Field* field); + virtual ~KexiDBField(); + virtual const QString getClassName() const; + ::KexiDB::Field* field() { return m_field; } + + private: + + /** Returns the type string for this field, e.g. "Integer" for Integer type. */ + const QString type(); + /** Sets the type string for this field, e.g. "Integer" for Integer type. */ + void setType(const QString type); + + /** Returns the optional subtype for this field. Subtype is a string providing + additional hint for field's type. E.g. for BLOB type, it can be a MIME type or + certain QVariant type name, for example: "QPixmap", "QColor" or "QFont". */ + const QString subType(); + /** Sets the optional subtype for this field. */ + void setSubType(const QString& subtype); + + /** Returns the QVariant::typeName which is equivalent to the type this field has. */ + const QString variantType(); + /** Returns type group string for this field, e.g. "IntegerGroup" for IntegerGroup type. */ + const QString typeGroup(); + + /** Returns true if the field is autoincrement (e.g. integer/numeric). */ + bool isAutoInc(); + /** Sets auto increment flag. */ + void setAutoInc(bool autoinc); + + /** Returns true if the field is member of single-field unique key. */ + bool isUniqueKey(); + /** Specifies whether the field has single-field unique constraint or not. */ + void setUniqueKey(bool unique); + + /** Returns true if the field is member of single-field primary key. */ + bool isPrimaryKey(); + /** Specifies whether the field is single-field primary key or not. */ + void setPrimaryKey(bool primary); + + /** Returns true if the field is member of single-field foreign key. */ + bool isForeignKey(); + /** Sets whether the field has to be declared with single-field foreign key. */ + void setForeignKey(bool foreign); + + /** Returns true if the field is not allowed to be null. */ + bool isNotNull(); + /** Specifies whether the field has single-field unique constraint or not. */ + void setNotNull(bool notnull); + + /** Returns true if the field is not allowed to be empty. */ + bool isNotEmpty(); + /** Specifies whether the field has single-field unique constraint or not. */ + void setNotEmpty(bool notempty); + + /** Returns true if the field is indexed using single-field database index. */ + bool isIndexed(); + /** Specifies whether the field is indexed or not. */ + void setIndexed(bool indexed); + + /** Returns true if the field is an unsigned integer. */ + bool isUnsigned(); + /** Specifies whether the field is an unsigned integer or not. */ + void setUnsigned(bool isunsigned); + + /** Returns the name of this field. */ + const QString name(); + /** Sets the name of this field. */ + void setName(const QString& name); + + /** Returns the caption of this field. */ + const QString caption(); + /** Sets the caption of this field. */ + void setCaption(const QString& caption); + + /** Returns the descriptive text for this field. */ + const QString description(); + /** Set the description for this field. */ + void setDescription(const QString& desc); + + /** Returns the length of text if the field type is text. */ + uint length(); + /** Sets the length for this field. Only works for Text Type (not including LongText). */ + void setLength(uint length); + + /** Returns precision for numeric and other fields that have both length and + precision (floating point types). */ + uint precision(); + /** Sets the precision for numeric and other fields. */ + void setPrecision(uint precision); + + /** Returns the width of this field (usually in pixels or points). + 0 (the default) means there is no hint for the width. */ + uint width(); + /** Sets the width of this field. */ + void setWidth(uint width); + + /** Returns the default value this field has. */ + QVariant defaultValue(); + /** Sets the default value this field has. */ + void setDefaultValue(const QVariant& defaultvalue); + + private: + ::KexiDB::Field* m_field; + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbfieldlist.cpp b/kexi/plugins/scripting/kexidb/kexidbfieldlist.cpp new file mode 100644 index 000000000..f36bf0b05 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbfieldlist.cpp @@ -0,0 +1,100 @@ +/*************************************************************************** + * kexidbfieldlist.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 "kexidbfieldlist.h" +#include "kexidbfield.h" + +#include <api/variant.h> +#include <api/exception.h> + +#include <kdebug.h> + +using namespace Kross::KexiDB; + +KexiDBFieldList::KexiDBFieldList(::KexiDB::FieldList* fieldlist) + : Kross::Api::Class<KexiDBFieldList>("KexiDBFieldList") + , m_fieldlist(fieldlist) +{ + this->addFunction0< Kross::Api::Variant >("fieldCount", this, &KexiDBFieldList::fieldCount); + this->addFunction1< KexiDBField, Kross::Api::Variant >("field", this, &KexiDBFieldList::field); + this->addFunction1< KexiDBField, Kross::Api::Variant >("fieldByName", this, &KexiDBFieldList::fieldByName); + + this->addFunction0< Kross::Api::List >("fields", this, &KexiDBFieldList::fields); + + this->addFunction1< Kross::Api::Variant, KexiDBField >("hasField", this, &KexiDBFieldList::hasField); + this->addFunction0< Kross::Api::Variant >("names", this, &KexiDBFieldList::names); + + this->addFunction1< void, KexiDBField >("addField", this, &KexiDBFieldList::addField); + this->addFunction2< void, Kross::Api::Variant, KexiDBField >("insertField", this, &KexiDBFieldList::insertField); + this->addFunction1< void, KexiDBField >("removeField", this, &KexiDBFieldList::removeField); + this->addFunction0< void >("clear", this, &KexiDBFieldList::clear); + this->addFunction1< void, KexiDBFieldList >("setFields", this, &KexiDBFieldList::setFields); + + this->addFunction1< KexiDBFieldList, Kross::Api::Variant >("subList", this, &KexiDBFieldList::subList); +} + +KexiDBFieldList::~KexiDBFieldList() +{ +} + +const QString KexiDBFieldList::getClassName() const +{ + return "Kross::KexiDB::KexiDBFieldList"; +} + +uint KexiDBFieldList::fieldCount() { + return m_fieldlist->fieldCount(); +} + +KexiDBField* KexiDBFieldList::field(uint index) { + ::KexiDB::Field* field = m_fieldlist->field(index); + return field ? new KexiDBField(field) : 0; +} + +KexiDBField* KexiDBFieldList::fieldByName(const QString& name) { + ::KexiDB::Field* field = m_fieldlist->field(name); + return field ? new KexiDBField(field) : 0; +} + +Kross::Api::List* KexiDBFieldList::fields() { + return new Kross::Api::ListT<KexiDBField>( *m_fieldlist->fields() ); +} + +bool KexiDBFieldList::hasField(KexiDBField* field) { return m_fieldlist->hasField( field->field() ); } +const QStringList KexiDBFieldList::names() const { return m_fieldlist->names(); } +void KexiDBFieldList::addField(KexiDBField* field) { m_fieldlist->addField( field->field() ); } +void KexiDBFieldList::insertField(uint index, KexiDBField* field) { m_fieldlist->insertField(index, field->field()); } +void KexiDBFieldList::removeField(KexiDBField* field) { m_fieldlist->removeField( field->field() ); } +void KexiDBFieldList::clear() { m_fieldlist->clear(); } + +void KexiDBFieldList::setFields(KexiDBFieldList* fieldlist) { + m_fieldlist->clear(); + ::KexiDB::FieldList* fl = fieldlist->fieldlist(); + for(::KexiDB::Field::ListIterator it = *fl->fields(); it.current(); ++it) + m_fieldlist->addField( it.current() ); +} + +KexiDBFieldList* KexiDBFieldList::subList(QValueList<QVariant> list) { + QValueList<QVariant>::ConstIterator it( list.constBegin() ), end( list.constEnd() ); + QStringList sl; + for(; it != end; ++it) sl.append( (*it).toString() ); + ::KexiDB::FieldList* fl = m_fieldlist->subList(sl); + return fl ? new Kross::KexiDB::KexiDBFieldList(fl) : 0; +} + diff --git a/kexi/plugins/scripting/kexidb/kexidbfieldlist.h b/kexi/plugins/scripting/kexidb/kexidbfieldlist.h new file mode 100644 index 000000000..ee990eb32 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbfieldlist.h @@ -0,0 +1,104 @@ +/*************************************************************************** + * kexidbfieldlist.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBFIELDLIST_H +#define KROSS_KEXIDB_KEXIDBFIELDLIST_H + +#include <qstring.h> + +#include <api/object.h> +#include <api/list.h> +#include <api/class.h> + +#include <kexidb/drivermanager.h> +#include <kexidb/fieldlist.h> + +namespace Kross { namespace KexiDB { + + // Forward declarations. + class KexiDBField; + class KexiDBFieldList; + + /** + * A list of fields. The KexiDBFieldList can be used to handle KexiDBField objects + * in a backend-independent way. + * + * Example (in Python) ; + * @code + * # Get the tableschema for the "dept" table. + * table = connection.tableSchema("dept") + * # Create a KexiDBFieldList based on the table and filled with the selected fields. + * subfields = ["deptno","name","loc"] + * fieldlist = table.fieldlist().subList(subfields) + * # Create the "SELECT * from dept;" queryschema. + * query = table.query() + * # We change the queryschema to "SELECT deptno,name,loc FROM dept;" now. + * query.fieldlist().setFields(fieldlist) + * # and change the query to "SELECT deptno,name,loc FROM dept WHERE deptno=5;" + * query.setWhereExpression("deptno=5") + * # Execute the query and get a KexiDBCursor object as result which could be used to iterate through the result. + * cursor = connection.executeQuerySchema(query) + * @endcode + */ + class KexiDBFieldList : public Kross::Api::Class<KexiDBFieldList> + { + public: + KexiDBFieldList(::KexiDB::FieldList* fieldlist); + virtual ~KexiDBFieldList(); + virtual const QString getClassName() const; + ::KexiDB::FieldList* fieldlist() { return m_fieldlist; } + + private: + + /** Returns the number of fields. */ + uint fieldCount(); + /** Return the field specified by the index-number passed as an argument. */ + KexiDBField* field(uint index); + /** Return the field specified by the as an argument passed fieldname. */ + KexiDBField* fieldByName(const QString& name); + + /** Returns a list of all fields. */ + Kross::Api::List* fields(); + /** Returns true if the KexiDBField object passed as an argument is in the field list. */ + bool hasField(KexiDBField* field); + /** Return a list of field names. */ + const QStringList names() const; + + /** Adds the KexiDBField object passed as an argument to the field list. */ + void addField(KexiDBField* field); + /** Inserts the KexiDBField object passed as the second argument + into the field list at the position defined by the first argument. */ + void insertField(uint index, KexiDBField* field); + /** Removes the KexiDBField object passed as an argument from the field list. */ + void removeField(KexiDBField* field); + /** Removes all KexiDBField objects from the fieldlist. */ + void clear(); + /** Set the fieldlist to the as argument passed list of fields. */ + void setFields(KexiDBFieldList* fieldlist); + /** Creates and returns list that contain fields selected by name. */ + KexiDBFieldList* subList(QValueList<QVariant> list); + + private: + ::KexiDB::FieldList* m_fieldlist; + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbmodule.cpp b/kexi/plugins/scripting/kexidb/kexidbmodule.cpp new file mode 100644 index 000000000..36f7b71f0 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbmodule.cpp @@ -0,0 +1,74 @@ +/*************************************************************************** + * kexidbmodule.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 "kexidbmodule.h" +#include "kexidbdrivermanager.h" +#include "kexidbconnection.h" + +//#include <api/object.h> +//#include <api/variant.h> +#include <main/manager.h> + +#include <kdebug.h> + +// The as version() published versionnumber of this kross-module. +#define KROSS_KEXIDB_VERSION 1 + +extern "C" +{ + /** + * Exported an loadable function as entry point to use + * the \a KexiDBModule. + */ + Kross::Api::Object* KDE_EXPORT init_module(Kross::Api::Manager* manager) + { + return new Kross::KexiDB::KexiDBModule(manager); + } +} + +using namespace Kross::KexiDB; + +KexiDBModule::KexiDBModule(Kross::Api::Manager* /*manager*/) + : Kross::Api::Module("KexiDB") + //, m_manager(manager) +{ + //kdDebug() << "Kross::KexiDB::KexiDBModule Ctor" << endl; + addChild( "version", new Kross::Api::Variant(KROSS_KEXIDB_VERSION) ); + addChild( new KexiDBDriverManager() ); +} + +KexiDBModule::~KexiDBModule() +{ + //kdDebug() << "Kross::KexiDB::KexiDBModule Dtor" << endl; +} + +const QString KexiDBModule::getClassName() const +{ + return "Kross::KexiDB::KexiDBModule"; +} + +Kross::Api::Object::Ptr KexiDBModule::get(const QString& name, void* p) +{ + if(name == "KexiDBConnection") { + ::KexiDB::Connection* connection = (::KexiDB::Connection*)p; + if(connection) + return new KexiDBConnection(connection); + } + return 0; +} diff --git a/kexi/plugins/scripting/kexidb/kexidbmodule.h b/kexi/plugins/scripting/kexidb/kexidbmodule.h new file mode 100644 index 000000000..b91b60479 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbmodule.h @@ -0,0 +1,69 @@ +/*************************************************************************** + * kexidbmodule.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBMODULE_H +#define KROSS_KEXIDB_KEXIDBMODULE_H + +#include <qstring.h> +#include <qvariant.h> + +#include <api/module.h> + +namespace Kross { namespace Api { + class Manager; +}} + +namespace Kross { + +/** + * KrossKexiDB provides access to the KexiDB database functionality. + */ +namespace KexiDB { + + /** + * \internal + * The KexiDBModule is the implementation of a kross-module. + */ + class KexiDBModule : public Kross::Api::Module + { + public: + KexiDBModule(Kross::Api::Manager* manager); + virtual ~KexiDBModule(); + virtual const QString getClassName() const; + + /** + * \internal + * Variable module-method use to call transparent some functionality + * the module provides. + * + * \param name A name passed to the method. This name is used internaly + * to determinate what the caller likes to do. Each implemented + * module have to implement what should be done. + * \param p A variable pointer passed to the method. It depends on + * the module and the name what this pointer is. + * \return a \a Kross::Api::Object or NULL. + */ + virtual Kross::Api::Object::Ptr get(const QString& name, void* p = 0); + + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbparser.cpp b/kexi/plugins/scripting/kexidb/kexidbparser.cpp new file mode 100644 index 000000000..b022570d3 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbparser.cpp @@ -0,0 +1,77 @@ +/*************************************************************************** + * kexidbparser.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 "kexidbparser.h" +#include "kexidbschema.h" +#include "kexidbconnection.h" + +#include <api/variant.h> + +using namespace Kross::KexiDB; + +KexiDBParser::KexiDBParser(KexiDBConnection* connection, ::KexiDB::Parser* parser) + : Kross::Api::Class<KexiDBParser>("KexiDBParser") + , m_connection(connection) + , m_parser(parser) +{ + this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("parse", this, &KexiDBParser::parse); + this->addFunction0< void >("clear", this, &KexiDBParser::clear); + + this->addFunction0< Kross::Api::Variant >("operation", this, &KexiDBParser::operation); + + this->addFunction0< KexiDBTableSchema >("table", this, &KexiDBParser::table); + this->addFunction0< KexiDBQuerySchema >("query", this, &KexiDBParser::query); + this->addFunction0< KexiDBConnection >("connection", this, &KexiDBParser::connection); + this->addFunction0< Kross::Api::Variant >("statement", this, &KexiDBParser::statement); + + this->addFunction0< Kross::Api::Variant >("errorType", this, &KexiDBParser::errorType); + this->addFunction0< Kross::Api::Variant >("errorMsg", this, &KexiDBParser::errorMsg); + this->addFunction0< Kross::Api::Variant >("errorAt", this, &KexiDBParser::errorAt); +} + +KexiDBParser::~KexiDBParser() +{ +} + +const QString KexiDBParser::getClassName() const +{ + return "Kross::KexiDB::KexiDBParser"; +} + +bool KexiDBParser::parse(const QString& sql) { return m_parser->parse(sql); } +void KexiDBParser::clear() { m_parser->clear(); } +const QString KexiDBParser::operation() { return m_parser->operationString(); } + +KexiDBTableSchema* KexiDBParser::table() { + ::KexiDB::TableSchema* t = m_parser->table(); + return t ? new KexiDBTableSchema(t) : 0; +} + +KexiDBQuerySchema* KexiDBParser::query() { + ::KexiDB::QuerySchema* q = m_parser->query(); + return q ? new KexiDBQuerySchema(q) : 0; +} + +KexiDBConnection* KexiDBParser::connection() { return m_connection; } +const QString KexiDBParser::statement() { return m_parser->statement(); } + +const QString KexiDBParser::errorType() { return m_parser->error().type(); } +const QString KexiDBParser::errorMsg() { return m_parser->error().error(); } +int KexiDBParser::errorAt() { return m_parser->error().at(); } diff --git a/kexi/plugins/scripting/kexidb/kexidbparser.h b/kexi/plugins/scripting/kexidb/kexidbparser.h new file mode 100644 index 000000000..09ac22daa --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbparser.h @@ -0,0 +1,95 @@ +/*************************************************************************** + * kexidbparser.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBPARSER_H +#define KROSS_KEXIDB_KEXIDBPARSER_H + +#include <qstring.h> + +#include <api/object.h> +#include <api/list.h> +#include <api/class.h> + +#include <kexidb/drivermanager.h> +#include <kexidb/parser/parser.h> + +namespace Kross { namespace KexiDB { + + // Forward declaration. + class KexiDBConnection; + class KexiDBTableSchema; + class KexiDBQuerySchema; + + /** + * The KexiDBParser could be used to parse SQL-statements. + * + * Example (in Python) ; + * @code + * # First we need a parser object. + * parser = connection.parser() + * # Parse a SQL-statement. + * parser.parse("SELECT * from table1") + * # The operation could be e.g. SELECT or INSERT. + * if parser.operation() == 'Error': + * raise parser.errorMsg() + * # Print some feedback. + * print "Successfully parsed the SQL-statement %s" % parser.statement() + * @endcode + */ + class KexiDBParser : public Kross::Api::Class<KexiDBParser> + { + public: + KexiDBParser(KexiDBConnection* connection, ::KexiDB::Parser* parser); + virtual ~KexiDBParser(); + virtual const QString getClassName() const; + + private: + + /** Clears previous results and runs the parser on the SQL statement passed as an argument. */ + bool parse(const QString& sql); + /** Clears parsing results. */ + void clear(); + /** Returns the resulting operation. */ + const QString operation(); + + /** Returns the KexiDBTableSchema object on a CREATE TABLE operation. */ + KexiDBTableSchema* table(); + /** Returns the KexiDBQuerySchema object on a SELECT operation. */ + KexiDBQuerySchema* query(); + /** Returns the KexiDBConnection object pointing to the used database connection. */ + KexiDBConnection* connection(); + /** Returns the SQL query statement. */ + const QString statement(); + + /** Returns the type string of the last error. */ + const QString errorType(); + /** Returns the message of the last error. */ + const QString errorMsg(); + /** Returns the position where the last error occurred. */ + int errorAt(); + + private: + KexiDBConnection* m_connection; + ::KexiDB::Parser* m_parser; + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbschema.cpp b/kexi/plugins/scripting/kexidb/kexidbschema.cpp new file mode 100644 index 000000000..e07917f3c --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbschema.cpp @@ -0,0 +1,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 <qregexp.h> +#include <kdebug.h> + +#include <api/variant.h> + +using namespace Kross::KexiDB; + +/*************************************************************************** + *KexiDBSchema + */ + +template<class T> +KexiDBSchema<T>::KexiDBSchema(const QString& 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 QString KexiDBSchema<T>::name() const { + return m_schema->name(); +} + +template<class T> +void KexiDBSchema<T>::setName(const QString& name) { + m_schema->setName(name); +} + +template<class T> +const QString KexiDBSchema<T>::caption() const { + return m_schema->caption(); +} + +template<class T> +void KexiDBSchema<T>::setCaption(const QString& caption) { + m_schema->setCaption(caption); +} + +template<class T> +const QString KexiDBSchema<T>::description() const { + return m_schema->description(); +} + +template<class T> +void KexiDBSchema<T>::setDescription(const QString& 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 QString 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 QString KexiDBQuerySchema::getClassName() const { + return "Kross::KexiDB::KexiDBQuerySchema"; +} + +::KexiDB::QuerySchema* KexiDBQuerySchema::queryschema() { + return static_cast< ::KexiDB::QuerySchema* >(m_schema); +} + +const QString KexiDBQuerySchema::statement() const { + return static_cast< ::KexiDB::QuerySchema* >(m_schema)->statement(); +} + +void KexiDBQuerySchema::setStatement(const QString& statement) { + static_cast< ::KexiDB::QuerySchema* >(m_schema)->setStatement(statement); +} + +bool KexiDBQuerySchema::setWhereExpression(const QString& whereexpression) { + ::KexiDB::BaseExpr* oldexpr = static_cast< ::KexiDB::QuerySchema* >(m_schema)->whereExpression(); + + ///@todo use ::KexiDB::Parser for such kind of parser-functionality. + QString s = whereexpression; + try { + QRegExp re("[\"',]{1,1}"); + while(true) { + s.remove(QRegExp("^[\\s,]+")); + int pos = s.find('='); + if(pos < 0) break; + QString key = s.left(pos).stripWhiteSpace(); + s = s.mid(pos + 1).stripWhiteSpace(); + + QString 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 = QString::null; + } + + ::KexiDB::Field* field = static_cast< ::KexiDB::QuerySchema* >(m_schema)->field(key); + if(! field) + 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())) ); + + QVariant v(value); + if(! v.cast(field->variantType())) + 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())) ); + + 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; +} diff --git a/kexi/plugins/scripting/kexidb/kexidbschema.h b/kexi/plugins/scripting/kexidb/kexidbschema.h new file mode 100644 index 000000000..61b6bc885 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbschema.h @@ -0,0 +1,134 @@ +/*************************************************************************** + * kexidbschema.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBSCHEMA_H +#define KROSS_KEXIDB_KEXIDBSCHEMA_H + +#include <qstring.h> + +#include <api/object.h> +#include <api/class.h> + +#include <kexidb/drivermanager.h> +#include <kexidb/schemadata.h> +#include <kexidb/tableschema.h> +#include <kexidb/queryschema.h> + +namespace Kross { namespace KexiDB { + + // Forward-declarations. + class KexiDBFieldList; + class KexiDBQuerySchema; + + /** + * The KexiDBSchema object provides common functionality for schemas + * like KexiDBTableSchema or KexiDBQuerySchema. + * + * Example (in Python) ; + * @code + * # Get the tableschema from a KexiDBConnection object. + * tableschema = connection.tableSchema("dept") + * # Print some information. + * print "table=%s description=%s" % (tableschema.name(), tableschema.description()) + * # Get the "SELECT * FROM dept;" queryschema for the table. + * queryschema = tableschema.query() + * # Walk through the fields/columns the queryschema has and print the fieldnames. + * for field in queryschema.fieldlist().fields(): + * print "fieldname=%s" % field.name() + * # Execute the query. The returned KexiDBCursor object could be used then to iterate through the result. + * cursor = connection.executeQuerySchema(queryschema) + * @endcode + */ + template<class T> + class KexiDBSchema : public Kross::Api::Class<T> + { + public: + KexiDBSchema(const QString& name, ::KexiDB::SchemaData* schema, ::KexiDB::FieldList* fieldlist); + virtual ~KexiDBSchema(); + + private: + + /** Returns the name of the schema. */ + const QString name() const; + /** Set the name of the schema. */ + void setName(const QString& name); + + /** Returns the caption of the schema. */ + const QString caption() const; + /** Set the caption of the schema. */ + void setCaption(const QString& caption); + + /** Returns a description of the schema. */ + const QString description() const; + /** Set a description of the schema. */ + void setDescription(const QString& description); + + /** Returns the KexiDBFieldList object this schema has. */ + KexiDBFieldList* fieldlist() const; + + protected: + ::KexiDB::SchemaData* m_schema; + ::KexiDB::FieldList* m_fieldlist; + }; + + /** + * The KexiDBTableSchema object implements a KexiDBSchema for tables. + */ + class KexiDBTableSchema : public KexiDBSchema<KexiDBTableSchema> + { + public: + KexiDBTableSchema(::KexiDB::TableSchema* tableschema); + virtual ~KexiDBTableSchema(); + virtual const QString getClassName() const; + ::KexiDB::TableSchema* tableschema(); + + private: + + /** Return the KexiDBQuerySchema object that represents a + "SELECT * FROM this_KexiDBTableSchema_object" SQL-statement. */ + KexiDBQuerySchema* query(); + + }; + + /** + * The KexiDBTableSchema object implements a KexiDBSchema for queries. + */ + class KexiDBQuerySchema : public KexiDBSchema<KexiDBQuerySchema> + { + public: + KexiDBQuerySchema(::KexiDB::QuerySchema* queryschema); + virtual ~KexiDBQuerySchema(); + virtual const QString getClassName() const; + ::KexiDB::QuerySchema* queryschema(); + + private: + + /** Returns the SQL-statement of this query schema. */ + const QString statement() const; + /** Set the SQL-statement of this query schema. */ + void setStatement(const QString& statement); + /** Set the where-expression. */ + bool setWhereExpression(const QString& whereexpression); + + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/kexidbtransaction.cpp b/kexi/plugins/scripting/kexidb/kexidbtransaction.cpp new file mode 100644 index 000000000..d4cdff242 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbtransaction.cpp @@ -0,0 +1,52 @@ +/*************************************************************************** + * kexidbtransaction.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 "kexidbtransaction.h" +#include "kexidbconnection.h" +#include <api/variant.h> + +//#include <kdebug.h> + +using namespace Kross::KexiDB; + +KexiDBTransaction::KexiDBTransaction(::KexiDB::Transaction& transaction) + : Kross::Api::Class<KexiDBTransaction>("KexiDBTransaction") + , m_transaction(transaction) +{ + this->addFunction0< Kross::Api::Variant >("isActive", this, &KexiDBTransaction::isActive); + this->addFunction0< Kross::Api::Variant >("isNull", this, &KexiDBTransaction::isNull); +} + +KexiDBTransaction::~KexiDBTransaction() +{ +} + +const QString KexiDBTransaction::getClassName() const +{ + return "Kross::KexiDB::KexiDBTransaction"; +} + +::KexiDB::Transaction& KexiDBTransaction::transaction() +{ + return m_transaction; +} + +bool KexiDBTransaction::isActive() const { return m_transaction.active(); } +bool KexiDBTransaction::isNull() const { return m_transaction.isNull(); } diff --git a/kexi/plugins/scripting/kexidb/kexidbtransaction.h b/kexi/plugins/scripting/kexidb/kexidbtransaction.h new file mode 100644 index 000000000..6a6b5785b --- /dev/null +++ b/kexi/plugins/scripting/kexidb/kexidbtransaction.h @@ -0,0 +1,62 @@ +/*************************************************************************** + * kexidbtransaction.h + * 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. + ***************************************************************************/ + +#ifndef KROSS_KEXIDB_KEXIDBTRANSACTION_H +#define KROSS_KEXIDB_KEXIDBTRANSACTION_H + +#include <qstring.h> + +#include <api/class.h> + +#include <kexidb/drivermanager.h> +#include <kexidb/transaction.h> + +namespace Kross { namespace KexiDB { + + // Forward declaration. + class KexiDBConnection; + + /** + * Transactions are used to ensure that integrity of a database is + * maintained. + */ + class KexiDBTransaction : public Kross::Api::Class<KexiDBTransaction> + { + public: + KexiDBTransaction(::KexiDB::Transaction& transaction); + virtual ~KexiDBTransaction(); + virtual const QString getClassName() const; + ::KexiDB::Transaction& transaction(); + + private: + + /** Return true if the transaction is active (ie. started). */ + bool isActive() const; + + /** Return true if the transaction is uninitialized (null). */ + bool isNull() const; + + private: + ::KexiDB::Transaction& m_transaction; + }; + +}} + +#endif + diff --git a/kexi/plugins/scripting/kexidb/readme.dox b/kexi/plugins/scripting/kexidb/readme.dox new file mode 100644 index 000000000..c4e33a5b4 --- /dev/null +++ b/kexi/plugins/scripting/kexidb/readme.dox @@ -0,0 +1,32 @@ +/** @mainpage KrossKexiDB + * + * The Kross KexiDB module provides a scripting bridge to the + * KexiDB library. KexiDB is the database abstraction layer used + * within Kexi to deal with all supported database-backends like + * SQLite, MySQL and Postqre. + * + * The @a KexiDBDriverManager is the manager module which provides + * the entry point to access the KexiDB functionality from + * scripting languages like Python and Ruby. + * + * @see http://www.kexi-project.org/scripting/ + * @see http://kross.dipe.org + * @see http://www.kexi-project.org/wiki/wikiview/index.php?Scripting + * + * @section Legal + * + * @li copyright (C) 2004-2006 by Sebastian Sauer (mail AT dipe DOT 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. + */ |