1 /* This file is part of the KDE project
2    Copyright (C) 2008 Sharan Rao <sharanrao@gmail.com>
3 
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this program; see the file COPYING.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 *  Boston, MA 02110-1301, USA.
18 */
19 
20 
21 #include "XbaseDriver.h"
22 #include "XbaseCursor.h"
23 #include "XbaseConnection.h"
24 #include "XbaseConnection_p.h"
25 #include "KDbError.h"
26 
xBaseConnection(KDbDriver * driver,KDbDriver * internalDriver,const KDbConnectionData & connData)27 xBaseConnection::xBaseConnection(KDbDriver *driver, KDbDriver* internalDriver, const KDbConnectionData& connData)
28   : KDbConnection(driver, connData)
29   , d(new xBaseConnectionInternal(this, internalDriver))
30 {
31 }
32 
~xBaseConnection()33 xBaseConnection::~xBaseConnection() {
34   destroy();
35 }
36 
drv_connect(KDbServerVersionInfo * version)37 bool xBaseConnection::drv_connect(KDbServerVersionInfo* version)
38 {
39   Q_UNUSED(version);
40   const bool ok = d->db_connect(*data());
41   if (!ok)
42     return false;
43 
44   //! @todo xBase version here
45   //version.string = mysql_get_host_info(d->mysql);
46 
47   return true;
48 }
49 
drv_disconnect()50 bool xBaseConnection::drv_disconnect() {
51   return d->db_disconnect(*data());
52 }
53 
prepareQuery(const KDbEscapedString & sql,int cursor_options)54 KDbCursor* xBaseConnection::prepareQuery(const KDbEscapedString& sql, int cursor_options)
55 {
56   if ( !d->internalConn ) {
57     return nullptr;
58   }
59   KDbCursor* internalCursor = d->internalConn->prepareQuery(sql,cursor_options);
60   return new xBaseCursor( this, internalCursor, sql, cursor_options );
61 }
62 
prepareQuery(KDbQuerySchema * query,int cursor_options)63 KDbCursor* xBaseConnection::prepareQuery(KDbQuerySchema* query, int cursor_options) {
64   if ( !d->internalConn ) {
65     return nullptr;
66   }
67   KDbCursor* internalCursor = d->internalConn->prepareQuery(query, cursor_options);
68   return new xBaseCursor( this, internalCursor, query, cursor_options );
69 }
70 
drv_getDatabasesList(QStringList * list)71 bool xBaseConnection::drv_getDatabasesList(QStringList* list)
72 {
73   //! @todo Check whether this is the right thing to do
74   *list += QStringList( d->dbMap.keys() );
75 //        list<<d->internalConn->databaseNames();
76   return true;
77 }
78 
drv_createDatabase(const QString & dbName)79 bool xBaseConnection::drv_createDatabase( const QString &dbName) {
80   //! @todo Check whether this function has any use.
81   //xbaseDebug() << dbName;
82 // return d->internalConn->createDatabase(d->dbMap[dbName]);
83   return true;
84 }
85 
drv_useDatabase(const QString & dbName,bool * cancelled,KDbMessageHandler * msgHandler)86 bool xBaseConnection::drv_useDatabase(const QString &dbName, bool *cancelled, KDbMessageHandler* msgHandler)
87 {
88   Q_UNUSED(cancelled);
89   Q_UNUSED(msgHandler);
90 //! @todo is here escaping needed?
91   return d->useDatabase(dbName);
92 }
93 
drv_closeDatabase()94 bool xBaseConnection::drv_closeDatabase() {
95   if (!d->internalConn || !d->internalConn->closeDatabase() ) {
96     return false;
97   }
98   return true;
99 }
100 
drv_dropDatabase(const QString & dbName)101 bool xBaseConnection::drv_dropDatabase(const QString &dbName) {
102     Q_UNUSED(dbName);
103 //! @todo is here escaping needed
104   // Delete the directory ?
105   return true;
106 }
107 
drv_executeSql(const KDbEscapedString & sql)108 bool xBaseConnection::drv_executeSql( const KDbEscapedString& sql ) {
109   return d->executeSql(sql);
110 }
111 
drv_lastInsertRecordId()112 quint64 xBaseConnection::drv_lastInsertRecordId()
113 {
114   //! @todo
115   quint64 rowID = -1;
116   if (d->internalConn)
117     d->internalConn->lastInsertedAutoIncValue(QString(), QString(), &rowID );
118 
119   return rowID;
120 }
121 
serverResult()122 int xBaseConnection::serverResult()
123 {
124   return d->res;
125 }
126 
serverResultName() const127 QString xBaseConnection::serverResultName() const
128 {
129   if (!d->internalConn) {
130     return QString();
131   }
132   return d->internalConn->serverResultName();
133 }
134 
135 /*void xBaseConnection::drv_clearServerResult()
136 {
137   if (!d || !d->internalConn)
138     return;
139   d->internalConn->clearError();
140   d->res = 0;
141 }*/
142 
serverErrorMsg()143 QString xBaseConnection::serverErrorMsg()
144 {
145   return d->errmsg;
146 }
147 
drv_containsTable(const QString & tableName)148 bool xBaseConnection::drv_containsTable( const QString &tableName )
149 {
150   return resultExists(KDbEscapedString("SHOW TABLES LIKE %1")
151                       .arg(escapeString(tableName)));
152 }
153 
drv_getTablesList(QStringList * list)154 bool xBaseConnection::drv_getTablesList(QStringList* list)
155 {
156   if ( !d->internalConn ) {
157     return false;
158   }
159   *list += d->internalConn->tableNames();
160   return true;
161 }
162 
prepareStatementInternal()163 KDbPreparedStatementInterface* xBaseConnection::prepareStatementInternal()
164 {
165     if ( !d->internalConn )
166         return nullptr;
167 //! @todo   return new XBasePreparedStatement(*d);
168         return nullptr;
169 }
170