File indexing completed on 2024-04-21 15:29:52

0001 /* This file is part of the KDE project
0002    Copyright (C) 2008 Sharan Rao <sharanrao@gmail.com>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017 *  Boston, MA 02110-1301, USA.
0018 */
0019 
0020 
0021 #include "XbaseDriver.h"
0022 #include "XbaseCursor.h"
0023 #include "XbaseConnection.h"
0024 #include "XbaseConnection_p.h"
0025 #include "KDbError.h"
0026 
0027 xBaseConnection::xBaseConnection(KDbDriver *driver, KDbDriver* internalDriver, const KDbConnectionData& connData)
0028   : KDbConnection(driver, connData)
0029   , d(new xBaseConnectionInternal(this, internalDriver))
0030 {
0031 }
0032 
0033 xBaseConnection::~xBaseConnection() {
0034   destroy();
0035 }
0036 
0037 bool xBaseConnection::drv_connect(KDbServerVersionInfo* version)
0038 {
0039   Q_UNUSED(version);
0040   const bool ok = d->db_connect(*data());
0041   if (!ok)
0042     return false;
0043 
0044   //! @todo xBase version here
0045   //version.string = mysql_get_host_info(d->mysql);
0046 
0047   return true;
0048 }
0049 
0050 bool xBaseConnection::drv_disconnect() {
0051   return d->db_disconnect(*data());
0052 }
0053 
0054 KDbCursor* xBaseConnection::prepareQuery(const KDbEscapedString& sql, int cursor_options)
0055 {
0056   if ( !d->internalConn ) {
0057     return nullptr;
0058   }
0059   KDbCursor* internalCursor = d->internalConn->prepareQuery(sql,cursor_options);
0060   return new xBaseCursor( this, internalCursor, sql, cursor_options );
0061 }
0062 
0063 KDbCursor* xBaseConnection::prepareQuery(KDbQuerySchema* query, int cursor_options) {
0064   if ( !d->internalConn ) {
0065     return nullptr;
0066   }
0067   KDbCursor* internalCursor = d->internalConn->prepareQuery(query, cursor_options);
0068   return new xBaseCursor( this, internalCursor, query, cursor_options );
0069 }
0070 
0071 bool xBaseConnection::drv_getDatabasesList(QStringList* list)
0072 {
0073   //! @todo Check whether this is the right thing to do
0074   *list += QStringList( d->dbMap.keys() );
0075 //        list<<d->internalConn->databaseNames();
0076   return true;
0077 }
0078 
0079 bool xBaseConnection::drv_createDatabase( const QString &dbName) {
0080   //! @todo Check whether this function has any use.
0081   //xbaseDebug() << dbName;
0082 // return d->internalConn->createDatabase(d->dbMap[dbName]);
0083   return true;
0084 }
0085 
0086 bool xBaseConnection::drv_useDatabase(const QString &dbName, bool *cancelled, KDbMessageHandler* msgHandler)
0087 {
0088   Q_UNUSED(cancelled);
0089   Q_UNUSED(msgHandler);
0090 //! @todo is here escaping needed?
0091   return d->useDatabase(dbName);
0092 }
0093 
0094 bool xBaseConnection::drv_closeDatabase() {
0095   if (!d->internalConn || !d->internalConn->closeDatabase() ) {
0096     return false;
0097   }
0098   return true;
0099 }
0100 
0101 bool xBaseConnection::drv_dropDatabase(const QString &dbName) {
0102     Q_UNUSED(dbName);
0103 //! @todo is here escaping needed
0104   // Delete the directory ?
0105   return true;
0106 }
0107 
0108 bool xBaseConnection::drv_executeSql( const KDbEscapedString& sql ) {
0109   return d->executeSql(sql);
0110 }
0111 
0112 quint64 xBaseConnection::drv_lastInsertRecordId()
0113 {
0114   //! @todo
0115   quint64 rowID = -1;
0116   if (d->internalConn)
0117     d->internalConn->lastInsertedAutoIncValue(QString(), QString(), &rowID );
0118 
0119   return rowID;
0120 }
0121 
0122 int xBaseConnection::serverResult()
0123 {
0124   return d->res;
0125 }
0126 
0127 QString xBaseConnection::serverResultName() const
0128 {
0129   if (!d->internalConn) {
0130     return QString();
0131   }
0132   return d->internalConn->serverResultName();
0133 }
0134 
0135 /*void xBaseConnection::drv_clearServerResult()
0136 {
0137   if (!d || !d->internalConn)
0138     return;
0139   d->internalConn->clearError();
0140   d->res = 0;
0141 }*/
0142 
0143 QString xBaseConnection::serverErrorMsg()
0144 {
0145   return d->errmsg;
0146 }
0147 
0148 bool xBaseConnection::drv_containsTable( const QString &tableName )
0149 {
0150   return resultExists(KDbEscapedString("SHOW TABLES LIKE %1")
0151                       .arg(escapeString(tableName)));
0152 }
0153 
0154 bool xBaseConnection::drv_getTablesList(QStringList* list)
0155 {
0156   if ( !d->internalConn ) {
0157     return false;
0158   }
0159   *list += d->internalConn->tableNames();
0160   return true;
0161 }
0162 
0163 KDbPreparedStatementInterface* xBaseConnection::prepareStatementInternal()
0164 {
0165     if ( !d->internalConn )
0166         return nullptr;
0167 //! @todo   return new XBasePreparedStatement(*d);
0168         return nullptr;
0169 }