From 616e919ff6aea6a30e18edb37128c229e806beae Mon Sep 17 00:00:00 2001 From: mickeyl Date: Sun, 10 Aug 2003 15:40:31 +0000 Subject: merge zeckes libsql into libopie2 --- (limited to 'libopie2/opiedb') diff --git a/libopie2/opiedb/DESIGN b/libopie2/opiedb/DESIGN new file mode 100644 index 0000000..a6c7217 --- a/dev/null +++ b/libopie2/opiedb/DESIGN @@ -0,0 +1,19 @@ +Design of OpieSQL + +My intention for OpieSQL was to write +a simple c++ interface which would allow +easy use of common database functions on +different backends + +How does these goals reflect the current Implementation + +-through OSQLManager + OSQLBackEnd I'll be able to change +the default db driver and be able to add new +driver implementations + +-OSQLQuery, RawQuery, SelectQuery allows a flexible and +OO way of fast querying databases + +-much more ;) + + diff --git a/libopie2/opiedb/TODO b/libopie2/opiedb/TODO new file mode 100644 index 0000000..ca04ac6 --- a/dev/null +++ b/libopie2/opiedb/TODO @@ -0,0 +1,9 @@ +* something like Capabilities of a Driver + - ROWID + - How to declare INTEGER PRIMARY KEY + - Abstract from implementation of some dbs + - provides( Type::What ) + + * OSQLDriver DriverVersion - DatabaseVersion + * Better OSQLQueries + - more than OSQLRawQuery \ No newline at end of file diff --git a/libopie2/opiedb/opiedb.pro b/libopie2/opiedb/opiedb.pro index 0d0ab78..401646a 100644 --- a/libopie2/opiedb/opiedb.pro +++ b/libopie2/opiedb/opiedb.pro @@ -1,9 +1,24 @@ TEMPLATE = lib CONFIG += qt warn_on debug DESTDIR = $(OPIEDIR)/lib -HEADERS = - -SOURCES = +HEADERS = osqlbackend.h \ + osqldriver.h \ + osqlerror.h \ + osqlmanager.h \ + osqlquery.h \ + osqlresult.h \ + osqltable.h \ + osqlbackendmanager.h \ + osqlitedriver.h +SOURCES = osqlbackend.cpp \ + osqldriver.cpp \ + osqlerror.cpp \ + osqlmanager.cpp \ + osqlquery.cpp \ + osqlresult.cpp \ + osqltable.cpp \ + osqlbackendmanager.cpp \ + osqlitedriver.cpp INTERFACES = TARGET = opiedb2 VERSION = 1.8.1 diff --git a/libopie2/opiedb/osqlbackend.cpp b/libopie2/opiedb/osqlbackend.cpp new file mode 100644 index 0000000..d6c39a9 --- a/dev/null +++ b/libopie2/opiedb/osqlbackend.cpp @@ -0,0 +1,73 @@ + +#include "osqlbackend.h" + + +OSQLBackEnd::OSQLBackEnd( const QString& name, + const QString& vendor, + const QString& license, + const QCString& lib ) + : m_name( name), m_vendor( vendor), m_license( license ), m_lib( lib ) +{ + m_default = false; + m_pref = -1; +} +OSQLBackEnd::OSQLBackEnd( const OSQLBackEnd& back ) { + (*this) = back; +} +OSQLBackEnd::~OSQLBackEnd() { +} +bool OSQLBackEnd::operator==( const OSQLBackEnd& other ) { + if ( m_pref != other.m_pref ) return false; + if ( m_default != other.m_default ) return false; + if ( m_name != other.m_name ) return false; + if ( m_vendor != other.m_vendor ) return false; + if ( m_license != other.m_license ) return false; + if ( m_lib != other.m_lib ) return false; + + return true; +} +OSQLBackEnd &OSQLBackEnd::operator=(const OSQLBackEnd& back ) { + m_name = back.m_name; + m_vendor = back.m_vendor; + m_license = back.m_license; + m_lib = back.m_lib; + m_pref = back.m_pref; + m_default = back.m_default; + return *this; +} +QString OSQLBackEnd::name() const { + return m_name; +} +QString OSQLBackEnd::vendor() const { + return m_vendor; +} +QString OSQLBackEnd::license() const { + return m_license; +} +QCString OSQLBackEnd::library() const { + return m_lib; +} +bool OSQLBackEnd::isDefault()const { + return m_default; +} +int OSQLBackEnd::preference()const { + return m_pref; +} +void OSQLBackEnd::setName( const QString& name ) { + m_name = name; +} +void OSQLBackEnd::setVendor( const QString& vendor ) { + m_vendor = vendor; +} +void OSQLBackEnd::setLicense( const QString & license ) { + m_license = license; +} +void OSQLBackEnd::setLibrary( const QCString& lib ) { + m_lib = lib; +} +void OSQLBackEnd::setDefault( bool def) { + m_default = def; +} +void OSQLBackEnd::setPreference( int pref ) { + m_pref = pref; +} diff --git a/libopie2/opiedb/osqlbackend.h b/libopie2/opiedb/osqlbackend.h new file mode 100644 index 0000000..ad879a4 --- a/dev/null +++ b/libopie2/opiedb/osqlbackend.h @@ -0,0 +1,75 @@ + +#ifndef OSQL_BACKEND_H +#define OSQL_BACKEND_H + +#include +#include +#include + +/** + * OSQLBackEnd represents an available backend + * to the Opie Database Service + * It's used to easily extend OSQL services by + * 3rd party plugins. + * It's used to show + */ +class OSQLBackEnd /*: public QShared */ { +public: + typedef QValueList ValueList; + /** + * A basic c'tor + * @param name the user visible name of the service + * @param vendor the vendor of the service + * @param license the license of the service + * @param library what is the name of lib if builtin it's builtin + */ + OSQLBackEnd( const QString& name = QString::null, + const QString& vendor = QString::null, + const QString& license = QString::null, + const QCString& library = QCString() ); + OSQLBackEnd( const OSQLBackEnd& ); + OSQLBackEnd &operator=( const OSQLBackEnd& ); + bool operator==(const OSQLBackEnd& ); + + ~OSQLBackEnd(); + + /** @return the name */ + QString name()const; + + /** @return the vendor */ + QString vendor()const; + + /** @return the license */ + QString license()const; + + /** @return the name of the library */ + QCString library() const; + + bool isDefault()const; + int preference()const; + + /** @param name the name to set */ + void setName( const QString& name ); + + /** @param vendor the vendor to set */ + void setVendor( const QString& vendor ); + + /** @param license the license applied */ + void setLicense( const QString& license ); + + /** @param the lib to set */ + void setLibrary( const QCString& lib ); + + void setDefault( bool ); + void setPreference( int ); + +private: + QString m_name; + QString m_vendor; + QString m_license; + QCString m_lib; + bool m_default :1; + int m_pref; +}; + +#endif diff --git a/libopie2/opiedb/osqlbackendmanager.cpp b/libopie2/opiedb/osqlbackendmanager.cpp new file mode 100644 index 0000000..0f261b9 --- a/dev/null +++ b/libopie2/opiedb/osqlbackendmanager.cpp @@ -0,0 +1,99 @@ +#include +#include +#include + +#include "osqlbackendmanager.h" + +namespace { + class Config { + typedef QMap List; + public: + Config( const QString& fileName ); + /** + * Quite simple layout in nature + * BeginFile + * Key = Value + */ + bool load(); + QString value( const QString& key ); + private: + List m_list; + QString m_fileName; + }; + Config::Config( const QString& fileName ) + : m_fileName( fileName ) { + } + + bool Config::load() { + if (!QFile::exists( m_fileName ) ) + return false; + QFile file( m_fileName ); + if (!file.open(IO_ReadOnly ) ) + return false; + QStringList list = QStringList::split( '\n', file.readAll() ); + QStringList::Iterator it; + QString line; + for (it = list.begin(); it != list.end(); ++it ) { + line = (*it).stripWhiteSpace(); + qWarning("Anonymous::Config:" + line ); + QStringList test = QStringList::split(' ', line ); + m_list.insert( test[0], test[2] ); + } + return true; + } + QString Config::value( const QString& key ) { + return m_list[key]; + } +}; +OSQLBackEndManager::OSQLBackEndManager( const QStringList& path ) + :m_path( path ) +{ +} +OSQLBackEndManager::~OSQLBackEndManager() { +} +/** + * scan dirs + */ +OSQLBackEnd::ValueList OSQLBackEndManager::scan() { + OSQLBackEnd::ValueList list; + if (!m_path.isEmpty() ) { + QStringList::Iterator it; + for ( it = m_path.begin(); it != m_path.end(); ++it ) { + list += scanDir( (*it) ); + } + } + return list; +} +/** + * scan a specified dir for *.osql + */ +OSQLBackEnd::ValueList OSQLBackEndManager::scanDir( const QString& dirName ) { + OSQLBackEnd::ValueList list; + QDir dir( dirName ); + if (dir.exists() ) { + QStringList files = dir.entryList( "*.osql" ); + QStringList::Iterator it; + for ( it = files.begin(); it != files.end(); ++it ) { + list.append( file2backend( (*it) ) ); + } + } + return list; +} + +/** + * read a config file and convert it to a OSQLBackEnd + */ +OSQLBackEnd OSQLBackEndManager::file2backend( const QString& file ) { + OSQLBackEnd end; + qWarning("fileName: " + file ); + Config cfg( file ); + if (cfg.load() ) { + end.setName( cfg.value( "Name") ); + end.setVendor( cfg.value("Vendor") ); + end.setLicense( cfg.value("License") ); + end.setLibrary( cfg.value("Library").local8Bit() ); + end.setDefault( cfg.value("Default").toInt() ); + end.setPreference( cfg.value("Preference").toInt() ); + } + return end; +} diff --git a/libopie2/opiedb/osqlbackendmanager.h b/libopie2/opiedb/osqlbackendmanager.h new file mode 100644 index 0000000..bc357a9 --- a/dev/null +++ b/libopie2/opiedb/osqlbackendmanager.h @@ -0,0 +1,20 @@ +#ifndef OSQL_BACKEND_MANAGER_H +#define OSQL_BACKEND_MANAGER_H + +#include + +#include "osqlbackend.h" + +class OSQLBackEndManager { +public: + OSQLBackEndManager(const QStringList& path ); + ~OSQLBackEndManager(); + OSQLBackEnd::ValueList scan(); +private: + OSQLBackEnd::ValueList scanDir( const QString& dir ); + OSQLBackEnd file2backend( const QString& file ); + class OSQLBackEndManagerPrivate; + OSQLBackEndManagerPrivate* d; + QStringList m_path; +}; +#endif diff --git a/libopie2/opiedb/osqldriver.cpp b/libopie2/opiedb/osqldriver.cpp new file mode 100644 index 0000000..258c116 --- a/dev/null +++ b/libopie2/opiedb/osqldriver.cpp @@ -0,0 +1,13 @@ +#include + +#include "osqldriver.h" + +OSQLDriver::OSQLDriver( QLibrary* lib ) +: QObject(), m_lib(lib) { +} +OSQLDriver::~OSQLDriver() { + delete m_lib; +} +bool OSQLDriver::sync() { + return true; +} diff --git a/libopie2/opiedb/osqldriver.h b/libopie2/opiedb/osqldriver.h new file mode 100644 index 0000000..68d8ee6 --- a/dev/null +++ b/libopie2/opiedb/osqldriver.h @@ -0,0 +1,87 @@ +#ifndef OSQL_DRIVER_H +#define OSQL_DRIVER_H + +#include +#include + +#include "osqltable.h" + +class QLibrary; +class OSQLResult; +class OSQLQuery; +class OSQLError; + +/** + * A OSQLDriver implements the communication with + * a database. + * After you queried and loaded a driver you can + * set some informations and finally try to open + * the database + * + */ +class OSQLDriver : public QObject{ + Q_OBJECT +public: + enum Capabilities { RowID=0 }; + /** + * OSQLDriver constructor. It takes the QLibrary + * as parent. + * + */ + OSQLDriver( QLibrary* lib=0 ); + + virtual ~OSQLDriver(); + /** + * Id returns the identifier of the OSQLDriver + */ + virtual QString id()const = 0; + + /** + * set the UserName to the database + */ + virtual void setUserName( const QString& ) = 0; + + /** + * set the PassWord to the database + */ + virtual void setPassword( const QString& )= 0; + + /** + * set the Url + */ + virtual void setUrl( const QString& ) = 0; + + /** + * setOptions + */ + virtual void setOptions( const QStringList& ) = 0; + + /** + * tries to open a connection to the database + */ + virtual bool open() = 0; + virtual bool close() = 0; + + virtual OSQLError lastError() = 0; + + /** + * Query the Database with a OSQLQuery + * OSQLResult holds the result + */ + virtual OSQLResult query( OSQLQuery* ) = 0; + + /** + * Get a list of tables + */ + virtual OSQLTable::ValueList tables() const = 0l; + virtual bool sync(); + + +private: + QLibrary* m_lib; + class OSQLDriverPrivate; + OSQLDriverPrivate *d; + +}; + +#endif diff --git a/libopie2/opiedb/osqlerror.cpp b/libopie2/opiedb/osqlerror.cpp new file mode 100644 index 0000000..3890a50 --- a/dev/null +++ b/libopie2/opiedb/osqlerror.cpp @@ -0,0 +1,23 @@ +#include "osqlerror.h" + +OSQLError::OSQLError( const QString& driverText, + const QString& driverDatabaseText, + int type, int subType ) + : m_drvText( driverText ), m_drvDBText( driverDatabaseText ), + m_type( type ), m_number( subType ) +{ +} +OSQLError::~OSQLError() { +} +QString OSQLError::driverText()const { + return m_drvText; +} +QString OSQLError::databaseText()const { + return m_drvDBText; +} +int OSQLError::type()const { + return m_type; +} +int OSQLError::subNumber()const { + return m_number; +} diff --git a/libopie2/opiedb/osqlerror.h b/libopie2/opiedb/osqlerror.h new file mode 100644 index 0000000..35a4368 --- a/dev/null +++ b/libopie2/opiedb/osqlerror.h @@ -0,0 +1,59 @@ +#ifndef OSQL_ERROR_H +#define OSQL_ERROR_H + +#include +#include + +/** + * OSQLError is the base class of all errors + */ +class OSQLError { +public: + typedef QValueList ValueList; + enum Type { None = 0, // NoError + Internal, // Internal Error in OSQL + Unknown, // Unknown Error + Transaction, // Transaction Error + Statement, // Wrong Statement + Connection, // Connection Error( lost ) + Driver // Driver Specefic error + }; + enum DriverError { + DriverInternal=0, // internal DriverError + Permission, // Permission Problem + Abort, // Abort of the SQL + Busy, // Busy Error + Locked, // Locked + NoMem, // No Memory + ReadOnly, // Database is read only + Interrupt, // Interrupt + IOErr, // IO Error + Corrupt, // Database Corruption + NotFound, // Table not Found + Full, // Full + CantOpen, // Can not open Table/Database + Protocol, // internal protocol error + Schema, // schema changed + TooBig, // Data too big + Mismatch, // Type mismatch + Misuse // misuse + }; + OSQLError( const QString& driverText = QString::null, + const QString& driverDatabaseText = QString::null, + int type = None, int subNumber = -1 ); + ~OSQLError(); + + QString driverText()const; + QString databaseText()const; + int type()const; + int subNumber()const; +private: + QString m_drvText; + QString m_drvDBText; + int m_type; + int m_number; + class OSQLErrorPrivate; + OSQLErrorPrivate* d; +}; + +#endif diff --git a/libopie2/opiedb/osqlitedriver.cpp b/libopie2/opiedb/osqlitedriver.cpp new file mode 100644 index 0000000..9214ad3 --- a/dev/null +++ b/libopie2/opiedb/osqlitedriver.cpp @@ -0,0 +1,126 @@ +#include + +#include "osqlresult.h" +#include "osqlquery.h" +#include "osqlitedriver.h" + + +namespace { + struct Query { + OSQLError::ValueList errors; + OSQLResultItem::ValueList items; + OSQLiteDriver *driver; + }; +} + +OSQLiteDriver::OSQLiteDriver( QLibrary *lib ) + : OSQLDriver( lib ) +{ + m_sqlite = 0l; +} +OSQLiteDriver::~OSQLiteDriver() { + close(); +} + +QString OSQLiteDriver::id()const { + return QString::fromLatin1("SQLite"); +} + +void OSQLiteDriver::setUserName( const QString& ) {} +void OSQLiteDriver::setPassword( const QString& ) {} + +void OSQLiteDriver::setUrl( const QString& url ) { + m_url = url; +} +void OSQLiteDriver::setOptions( const QStringList& ) { +} +/* + * try to open a db specified via setUrl + * and options + */ +bool OSQLiteDriver::open() { + char *error; + qWarning("about to open"); + m_sqlite = sqlite_open(m_url.local8Bit(), + 0, + &error ); + + /* failed to open */ + if (m_sqlite == 0l ) { + // FIXME set the last error + qWarning("error:%s", error ); + free( error ); + return false; + } + return true; +} +/* close the db + * sqlite closes them without + * telling failure or success + */ +bool OSQLiteDriver::close() { + if (m_sqlite ) + sqlite_close( m_sqlite ), m_sqlite=0l; + + return true; +} +/* Query */ +OSQLResult OSQLiteDriver::query( OSQLQuery* qu) { + if ( !m_sqlite ) { + // FIXME set error code + OSQLResult result( OSQLResult::Failure ); + return result; + } + Query query; + query.driver = this; + char *err; + /* SQLITE_OK 0 if return code > 0 == failure */ + if ( sqlite_exec(m_sqlite, qu->query(),&call_back, &query, &err) > 0 ) { + qWarning("Error while executing"); + free(err ); + // FixMe Errors + } + qWarning("Item count is %d", query.items.count() ); + OSQLResult result(OSQLResult::Success, + query.items, + query.errors ); + return result; +} +OSQLTable::ValueList OSQLiteDriver::tables() const { + +} +OSQLError OSQLiteDriver::lastError() { + OSQLError error; + return error; +}; +/* handle a callback add the row to the global + * OSQLResultItem + */ +int OSQLiteDriver::handleCallBack( int, char**, char** ) { + return 0; +} +/* callback_handler add the values to the list*/ +int OSQLiteDriver::call_back( void* voi, int argc, + char** argv, char** columns) { + qWarning("Callback with %d items", argc ); + Query* qu = (Query*)voi; + + //copy them over to a OSQLResultItem + QMap tableString; + QMap tableInt; + for (int i = 0; i < argc; i++ ) { + qWarning("%s|%s", columns[i], argv[i] ); + tableInt.insert( i, QString::fromLocal8Bit(argv[i] ) ); + tableString.insert( QString::fromLocal8Bit( columns[i]), + QString::fromLocal8Bit( argv[i] ) ); + + } + OSQLResultItem item( tableString, tableInt ); + qu->items.append( item ); + + return ((Query*)voi)->driver->handleCallBack( argc, + argv, + columns ); + + +} diff --git a/libopie2/opiedb/osqlitedriver.h b/libopie2/opiedb/osqlitedriver.h new file mode 100644 index 0000000..6984539 --- a/dev/null +++ b/libopie2/opiedb/osqlitedriver.h @@ -0,0 +1,34 @@ +#ifndef OSQL_LITE_DRIVER_H +#define OSQL_LITE_DRIVER_H + +#include + +#include "osqldriver.h" +#include "osqlerror.h" +#include "osqlresult.h" + +class OSQLiteDriver : public OSQLDriver { + Q_OBJECT +public: + OSQLiteDriver( QLibrary *lib = 0l ); + ~OSQLiteDriver(); + QString id()const; + void setUserName( const QString& ); + void setPassword( const QString& ); + void setUrl( const QString& url ); + void setOptions( const QStringList& ); + bool open(); + bool close(); + OSQLError lastError(); + OSQLResult query( OSQLQuery* ); + OSQLTable::ValueList tables()const; +private: + OSQLError m_lastE; + OSQLResult m_result; + OSQLResultItem m_items; + int handleCallBack( int, char**, char** ); + static int call_back( void*, int, char**, char** ); + QString m_url; + sqlite *m_sqlite; +}; +#endif diff --git a/libopie2/opiedb/osqlmanager.cpp b/libopie2/opiedb/osqlmanager.cpp new file mode 100644 index 0000000..b0fea04 --- a/dev/null +++ b/libopie2/opiedb/osqlmanager.cpp @@ -0,0 +1,83 @@ + +#include + +#include "osqlbackend.h" +#include "osqldriver.h" +#include "osqlmanager.h" +#include "osqlbackendmanager.h" +#include "osqlitedriver.h" + +OSQLManager::OSQLManager() { +} +OSQLBackEnd::ValueList OSQLManager::queryBackEnd() { + m_list.clear(); + QString opie = QString::fromLatin1( getenv("OPIEDIR") ); + QString qpe = QString::fromLatin1( getenv("QPEDIR") ); + + if ( !m_path.contains(opie) && !opie.isEmpty() ) + m_path << opie; + if ( !m_path.contains(qpe) && !qpe.isEmpty() ) + m_path << qpe; + + OSQLBackEndManager mng( m_path ); + m_list = mng.scan(); + m_list += builtIn(); + + return m_list; +} +/* + * loading dso's is currently not enabled due problems with QLibrary + * beeing in libqpe and not libqte + */ +OSQLDriver* OSQLManager::load( const QString& name ) { + OSQLDriver* driver = 0l; + + if ( name == "SQLite" ) { + driver = new OSQLiteDriver(); + } + return driver; +} +/* + * same as above + */ +OSQLDriver* OSQLManager::load( const OSQLBackEnd& end) { + OSQLDriver *driver = 0l; + if ( end.library() == "builtin" && + end.name() == "SQLite" ) + driver = new OSQLiteDriver(); + + return driver; +} +/* + * let's find the a default with the highes preference + */ +OSQLDriver* OSQLManager::standard() { + OSQLDriver* driver =0l; + if ( m_list.isEmpty() ) queryBackEnd(); + OSQLBackEnd::ValueList::Iterator it; + OSQLBackEnd back; + for ( it = m_list.begin(); it != m_list.end(); ++it ) { + if ( (*it).isDefault() && + back.preference() < (*it).preference() ) { + back = (*it); + } + } + driver = load( back ); + return driver; +} +void OSQLManager::registerPath( const QString& path ) { + m_path << path; +} +bool OSQLManager::unregisterPath( const QString& path ) { + m_path.remove( path ); + return true; +} +OSQLBackEnd::ValueList OSQLManager::builtIn()const { + OSQLBackEnd::ValueList list; + // create the OSQLiteBackend + OSQLBackEnd back("SQLite","Opie e.V.","GPL", "builtin" ); + back.setDefault( true ); + back.setPreference( 50 ); + list.append( back ); + return list; +} diff --git a/libopie2/opiedb/osqlmanager.h b/libopie2/opiedb/osqlmanager.h new file mode 100644 index 0000000..b3d8d69 --- a/dev/null +++ b/libopie2/opiedb/osqlmanager.h @@ -0,0 +1,62 @@ + +#ifndef OSQL_MANAGER_H +#define OSQL_MANAGER_H + +#include + +#include "osqlbackend.h" + +/** + * OSQLManager is responsible for loading + * and unloading, querying different OSQL + * services + * Load a OSQLDriver and delete it yourself + * + */ +class OSQLDriver; +class OSQLManager : public QObject { + Q_OBJECT +public: + /** + * Empty c'tor + */ + OSQLManager(); + + /** + * Query the Manager for different backends + */ + OSQLBackEnd::ValueList queryBackEnd(); + + /** + * Load a backend with it's name from param name + */ + OSQLDriver* load( const QString& name ); + + /** + * Load a OSQLDevice from const reference of OSQLBackEnd + */ + OSQLDriver* load( const OSQLBackEnd& ); + + /** + * loads the Opie standard backend + */ + OSQLDriver *standard(); + + /** + * register path to the search path list + * When querying for services we scan all the + * registered path for backends + */ + void registerPath( const QString& path ); + + /** + * unregisterPath from the search path list + */ + bool unregisterPath( const QString& path ); +private: + OSQLBackEnd::ValueList builtIn()const; + OSQLBackEnd::ValueList m_list; + QStringList m_path; +}; + +#endif diff --git a/libopie2/opiedb/osqlquery.cpp b/libopie2/opiedb/osqlquery.cpp new file mode 100644 index 0000000..ecd53f2 --- a/dev/null +++ b/libopie2/opiedb/osqlquery.cpp @@ -0,0 +1,17 @@ + +#include "osqlquery.h" + +OSQLQuery::OSQLQuery() { +} +OSQLQuery::~OSQLQuery() { +} + +OSQLRawQuery::OSQLRawQuery(const QString& query) + : OSQLQuery(), m_query( query ) { + +} +OSQLRawQuery::~OSQLRawQuery() { +} +QString OSQLRawQuery::query()const { + return m_query; +} diff --git a/libopie2/opiedb/osqlquery.h b/libopie2/opiedb/osqlquery.h new file mode 100644 index 0000000..63c26b0 --- a/dev/null +++ b/libopie2/opiedb/osqlquery.h @@ -0,0 +1,122 @@ + +#ifndef OSQL_QUERY_H +#define OSQL_QUERY_H + +#include +#include +#include + + +/** I'm not happy with them +class OSQLQueryOrder { +public: + typedef QValueList ValueList; + OSQLQueryOrder(const QString& table = QString::null ); + OSQLQueryOrder( const OSQLQueryOrder& ); + ~OSQLQueryOrder(); + void setOrderFields( const QStringList& list ); + void setValue( const QString& fieldName, const QString& Value ); + void setValues( const QMap& ); + QMap orders() const; + QString orderStatement()const; + OSQLQueryOrder &operator=(const OSQLQueryOrder& ); +private: + QMap m_fields; + class OSQLQueryOrderPrivate; + OSQLQueryOrderPrivate* d; +}; +class OSQLWhere { +public: + typedef QValueList ValueList; + enum Vergleiche { Equal = 0, Like, Greater, GreaterEqual, + Smaller, SmallerEqual }; + OSQLWhere(const QString& table = QString::null ); + ~OSQLWhere(); + void setExpression(const QString& key, enum Vergleiche, const QString& ); + QString statement()const; +private: + int m_vergleich; + const QString& m_left; + const QString& m_right; + +}; +*/ +class OSQLQuery { +public: + OSQLQuery(); + virtual ~OSQLQuery(); + + virtual QString query()const = 0; +}; + +class OSQLRawQuery : public OSQLQuery { +public: + OSQLRawQuery( const QString& query ); + ~OSQLRawQuery(); + QString query() const; +private: + class OSQLRawQueryPrivate; + OSQLRawQueryPrivate* d; + QString m_query; + +}; +/* I'm not happy with them again +class OSQLSelectQuery : public OSQLQuery { +public: + OSQLSelectQuery(); + ~OSQLSelectQuery(); + void setTables( const QStringList& allTablesToQuery ); + void setValues( const QString& table, const QStringList& columns ); + void setOrder( const OSQLSelectQuery& ); + void setOrderList( const OSQLQueryOrder::ValueList& ); + void setWhereList( const OSQLWhere& ); + void setWhereList( const OSQLWhere::ValueList& ); + QString query()const; +private: + QStringList m_tables; + QMap m_values; + OSQLQueryOrder::ValueList m_order; + OSQLWhere::ValueList m_where; + class OSQLSelectQueryPrivate; + OSQLSelectQueryPrivate* d; +}; +class OSQLInsertQuery : public OSQLQuery { +public: + OSQLInsertQuery(const QString& table); + ~OSQLInsertQuery(); + void setInserFields( const QStringList& ); + void setValue( const QString& field, const QString& value ); + void setValues(const QMap& ); + QString query()const; +private: + QString m_table; + QStringList m_fields; + QMap m_values; +}; +class OSQLDeleteQuery : public OSQLQuery { +public: + OSQLDeleteQuery(const QString& table); + ~OSQLDeleteQuery(); + void setWhere( const OSQLWhere& ); + void setWheres( const OSQLWhere::ValueList& ); + QString query()const; +private: + QString m_table; + OSQLWhere::ValueList m_where; + +}; +class OSQLUpdateQuery : public OSQLQuery { +public: + OSQLUpdateQuery( const QString& table ); + ~OSQLUpdateQuery(); + void setWhere( const OSQLWhere& ); + void setWheres( const OSQLWhere::ValueList& ); + */ + /* replaces all previous set Values */ + /* + void setValue( const QString& field, const QString& value ); + void setValue( const QMap &fields ); + QString query() const; +}; +*/ +#endif diff --git a/libopie2/opiedb/osqlresult.cpp b/libopie2/opiedb/osqlresult.cpp new file mode 100644 index 0000000..490fb45 --- a/dev/null +++ b/libopie2/opiedb/osqlresult.cpp @@ -0,0 +1,127 @@ + +#include "osqlquery.h" +#include "osqlresult.h" + +OSQLResultItem::OSQLResultItem( const TableString& string, + const TableInt& Int) + : m_string( string ), m_int( Int ) +{ + +} +OSQLResultItem::~OSQLResultItem() { +} +OSQLResultItem::OSQLResultItem( const OSQLResultItem& item) { + *this = item; +} +OSQLResultItem &OSQLResultItem::operator=( const OSQLResultItem& other) { + m_string = other.m_string; + m_int = other.m_int; + return *this; +} +OSQLResultItem::TableString OSQLResultItem::tableString()const{ + return m_string; +} +OSQLResultItem::TableInt OSQLResultItem::tableInt()const { + return m_int; +} +QString OSQLResultItem::data( const QString& columnName, bool *ok ) { + TableString::Iterator it = m_string.find( columnName ); + + /* if found */ + if ( it != m_string.end() ) { + if ( ok ) *ok = true; + return it.data(); + }else{ + if ( ok ) *ok = false; + return QString::null; + } + +} +QString OSQLResultItem::data( int column, bool *ok ) { + TableInt::Iterator it = m_int.find( column ); + + /* if found */ + if ( it != m_int.end() ) { + if ( ok ) *ok = true; + return it.data(); + }else{ + if ( ok ) *ok = false; + return QString::null; + } +} +/* + * DateFormat is 'YYYY-MM-DD' + */ +QDate OSQLResultItem::dataToDate( const QString& column, bool *ok ) { + QDate date = QDate::currentDate(); + QString str = data( column, ok ); + if (!str.isEmpty() ) { + ;// convert + } + return date; +} +QDate OSQLResultItem::dataToDate( int column, bool *ok ) { + QDate date = QDate::currentDate(); + QString str = data( column, ok ); + if (!str.isEmpty() ) { + ;// convert + } + return date; + +} +QDateTime OSQLResultItem::dataToDateTime( const QString& column, bool *ok ) { + QDateTime time = QDateTime::currentDateTime(); + return time; +} +QDateTime OSQLResultItem::dataToDateTime( int column, bool *ok ) { + QDateTime time = QDateTime::currentDateTime(); + return time; +} + +OSQLResult::OSQLResult( enum State state, + const OSQLResultItem::ValueList& list, + const OSQLError::ValueList& error ) + : m_state( state ), m_list( list ), m_error( error ) +{ + +} +OSQLResult::~OSQLResult() { + +} +OSQLResult::State OSQLResult::state()const { + return m_state; +} +void OSQLResult::setState( OSQLResult::State state ) { + m_state = state; +} +OSQLError::ValueList OSQLResult::errors()const { + return m_error; +} +void OSQLResult::setErrors( const OSQLError::ValueList& err ) { + m_error = err; +} +OSQLResultItem::ValueList OSQLResult::results()const { + return m_list; +} +void OSQLResult::setResults( const OSQLResultItem::ValueList& result ) { + m_list = result; +} +OSQLResultItem OSQLResult::first() { + it = m_list.begin(); + return (*it); +} +OSQLResultItem OSQLResult::next(){ + ++it; + return (*it); +} +bool OSQLResult::atEnd(){ + if ( it == m_list.end() ) + return true; + + return false; +} +OSQLResultItem::ValueList::ConstIterator OSQLResult::iterator()const { + OSQLResultItem::ValueList::ConstIterator it; + it = m_list.begin(); + return it; +} diff --git a/libopie2/opiedb/osqlresult.h b/libopie2/opiedb/osqlresult.h new file mode 100644 index 0000000..9c9efa2 --- a/dev/null +++ b/libopie2/opiedb/osqlresult.h @@ -0,0 +1,112 @@ +#ifndef OSQL_RESULT_H +#define OSQL_RESULT_H + +#include +#include +#include + + +#include "osqlerror.h" +/** + * ResultItem represents one row of the resulting answer + */ +class OSQLResultItem { +public: + typedef QValueList ValueList; + /** + * TableString is used to establish the relations + * between the column name and the real item + */ + typedef QMap TableString; + + /** + * TableInt is used to establish a relation between a + * position of a column and the row value + */ + typedef QMap TableInt; + + /** + * Default c'tor. It has a TableString and a TableInt + */ + OSQLResultItem(const TableString& = TableString(), + const TableInt& = TableInt() ); + OSQLResultItem( const OSQLResultItem& ); + ~OSQLResultItem(); + OSQLResultItem &operator=( const OSQLResultItem& ); + /** + * returns the TableString + */ + TableString tableString()const; + + /** + * returns the TableInt + */ + TableInt tableInt() const; + + /** + * retrieves the Data from columnName + * + */ + QString data( const QString& columnName, bool *ok = 0); + + /** + * QString for column number + */ + QString data(int columnNumber, bool *ok = 0); + + /** + * Date conversion from columnName + */ + QDate dataToDate( const QString& columnName, bool *ok = 0 ); + + /** + * Date conversion from column-number + */ + QDate dataToDate( int columnNumber, bool *ok = 0 ); + + QDateTime dataToDateTime( const QString& columName, bool *ok = 0 ); + QDateTime dataToDateTime( int columnNumber, bool *ok = 0 ); +private: + TableString m_string; + TableInt m_int; +}; + +/** + * the OSQLResult + * either a SQL statement failed or succeeded + */ +class OSQLResult { +public: + /** The State of a Result */ + enum State{ Success = 0, Failure,Undefined }; + + /** + * default c'tor + * @param state The State of the Result + * @param r ResultItems + * @prarm errors the Errors a OSQLResult created + */ + OSQLResult( enum State state = Undefined, + const OSQLResultItem::ValueList& r= OSQLResultItem::ValueList(), + const OSQLError::ValueList& errors = OSQLError::ValueList() ); + ~OSQLResult(); + State state()const; + OSQLError::ValueList errors()const; + OSQLResultItem::ValueList results()const; + + void setState( enum State state ); + void setErrors( const OSQLError::ValueList& error ); + void setResults( const OSQLResultItem::ValueList& result ); + + OSQLResultItem first(); + OSQLResultItem next(); + bool atEnd(); + OSQLResultItem::ValueList::ConstIterator iterator()const; +private: + enum State m_state; + OSQLResultItem::ValueList m_list; + OSQLError::ValueList m_error; + OSQLResultItem::ValueList::Iterator it; +}; + +#endif diff --git a/libopie2/opiedb/osqltable.cpp b/libopie2/opiedb/osqltable.cpp new file mode 100644 index 0000000..cde40f4 --- a/dev/null +++ b/libopie2/opiedb/osqltable.cpp @@ -0,0 +1,46 @@ +#include "osqltable.h" + +OSQLTableItem::OSQLTableItem() {} +OSQLTableItem::OSQLTableItem( enum Type type, + const QString& field, + const QVariant& var) + : m_type( type ), m_field( field ), m_var( var ) +{ + +} +OSQLTableItem::~OSQLTableItem() {} +OSQLTableItem::OSQLTableItem( const OSQLTableItem& item) { + *this = item; +} +OSQLTableItem &OSQLTableItem::operator=(const OSQLTableItem& other) { + m_var = other.m_var; + m_field = other.m_field; + m_type = other.m_type; + return *this; +} +QString OSQLTableItem::fieldName()const{ + return m_field; +} +OSQLTableItem::Type OSQLTableItem::type()const { + return m_type; +} +QVariant OSQLTableItem::more()const { + return m_var; +} + +OSQLTable::OSQLTable( const QString& tableName ) + : m_table( tableName ) +{ + +} +OSQLTable::~OSQLTable() { +} +void OSQLTable::setColumns( const OSQLTableItem::ValueList& list) { + m_list = list; +} +OSQLTableItem::ValueList OSQLTable::columns()const { + return m_list; +} +QString OSQLTable::tableName()const { + return m_table; +} diff --git a/libopie2/opiedb/osqltable.h b/libopie2/opiedb/osqltable.h new file mode 100644 index 0000000..87f7e74 --- a/dev/null +++ b/libopie2/opiedb/osqltable.h @@ -0,0 +1,95 @@ +#ifndef OSQL_TABLE_H +#define OSQL_TABLE_H + +#include +#include +#include + +/** + * OSQLTableItem saves one column of a complete + * table + */ +class OSQLTableItem { +public: + typedef QValueList ValueList; + /** + * Type kinds ( to be extended ) + */ + enum Type { Undefined=-1, Integer=0, BigInteger =1, + Float = 2, VarChar = 4 }; + /** + * A constructor + * @param type the Type of the Column + * @param fieldName the Name of the Column + * @param var a Variant + */ + OSQLTableItem(); + OSQLTableItem( enum Type type, + const QString& fieldName, + const QVariant& var= QVariant() ); + + /** + * copy c'tor + */ + OSQLTableItem( const OSQLTableItem& ); + + /** + * d'tor + */ + ~OSQLTableItem(); + + OSQLTableItem& operator=( const OSQLTableItem& ); + + /** + * the fieldName + */ + QString fieldName() const; + + /** + * the field Type + */ + Type type() const; + QVariant more() const; +private: + class OSQLTableItemPrivate; + OSQLTableItemPrivate* d; + Type m_type; + QString m_field; + QVariant m_var; +}; + +/** + * A OSQLTable consists of OSQLTableItems + */ +class OSQLTable { +public: + typedef QValueList ValueList; + + /** + * @param tableName the Name of the Table + */ + OSQLTable(const QString& tableName); + + /** + * d'tor + */ + ~OSQLTable(); + + /** + * setColumns sets the Columns of the Table + */ + void setColumns( const OSQLTableItem::ValueList& ); + + /** + * returns all columns of the table + */ + OSQLTableItem::ValueList columns() const; + + QString tableName()const; + +private: + QString m_table; + OSQLTableItem::ValueList m_list; +}; + +#endif -- cgit v0.9.0.2