PageRenderTime 33ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/drivers/sybase/SybaseConnection.cpp

https://gitlab.com/LongAiR/KDb
C++ | 218 lines | 133 code | 43 blank | 42 comment | 10 complexity | 9e51208698af989b3ac07b4858f04544 MD5 | raw file
  1. /* This file is part of the KDE project
  2. Copyright (C) 2007 Sharan Rao <sharanrao@gmail.com>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this program; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. * Boston, MA 02110-1301, USA.
  15. */
  16. #include <QRegularExpression>
  17. #include <kgenericfactory.h>
  18. #include "SybaseDriver.h"
  19. #include "SybaseConnection.h"
  20. #include "SybaseConnection_p.h"
  21. #include "SybasePreparedStatement.h"
  22. #include "KDbError.h"
  23. SybaseConnection::SybaseConnection(KDbDriver *driver, const KDbConnectionData& connData)
  24. : KDbConnection(driver, connData)
  25. , d(new SybaseConnectionInternal(this))
  26. {
  27. }
  28. SybaseConnection::~SybaseConnection()
  29. {
  30. destroy();
  31. }
  32. bool SybaseConnection::drv_connect(KDbServerVersionInfo* version)
  33. {
  34. const bool ok = d->db_connect(*data());
  35. if (!ok)
  36. return false;
  37. // we can retrieve the server name and the server version using global variables
  38. // @@servername
  39. // @@version
  40. QString serverVersionString;
  41. if (!querySingleString(KDbEscapedString("SELECT @@servername") , &version.string)) {
  42. sybaseWarning() << "Couldn't fetch server name";
  43. }
  44. if (!querySingleString(KDbEscapedString("SELECT @@version"), &serverVersionString)) {
  45. sybaseWarning() << "Couldn't fetch server version";
  46. }
  47. QRegularExpression versionRe("^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$");
  48. QRegularExpressionMatch match = versionRe.match(serverVersionString);
  49. if (match.hasMatch()) {
  50. version.major = match.captured(1).toInt();
  51. version.minor = match.captured(2).toInt();
  52. version.release = match.captured(3).toInt();
  53. }
  54. return true;
  55. }
  56. bool SybaseConnection::drv_disconnect()
  57. {
  58. return d->db_disconnect();
  59. }
  60. KDbCursor* SybaseConnection::prepareQuery(const KDbEscapedString& sql, int cursor_options)
  61. {
  62. return new SybaseCursor(this, sql, cursor_options);
  63. }
  64. KDbCursor* SybaseConnection::prepareQuery(KDbQuerySchema* query, int cursor_options)
  65. {
  66. return new SybaseCursor(this, query, cursor_options);
  67. }
  68. bool SybaseConnection::drv_getDatabasesList(QStringList* list)
  69. {
  70. // select * from master..sysdatabases ?
  71. // todo: verify.
  72. return queryStringList(KDbEscapedString("SELECT name FROM master..sysdatabases"), list) ;
  73. }
  74. bool SybaseConnection::drv_createDatabase(const QString &dbName)
  75. {
  76. //sybaseDebug() << dbName;
  77. // mysql_create_db deprecated, use SQL here.
  78. if (drv_executeSql(KDbEscapedString("CREATE DATABASE ") + dbName)) {
  79. // set allow_nulls_by_default option to true
  80. KDbEscapedString allowNullsQuery = KDbEscapedString("sp_dboption %1, allow_nulls_by_default, true").arg(dbName);
  81. if (drv_executeSql(allowNullsQuery.data()))
  82. return true;
  83. }
  84. d->storeResult();
  85. return false;
  86. }
  87. bool SybaseConnection::drv_useDatabase(const QString &dbName, bool *cancelled, KDbMessageHandler* msgHandler)
  88. {
  89. Q_UNUSED(cancelled);
  90. Q_UNUSED(msgHandler);
  91. //! @todo is here escaping needed?
  92. return d->useDatabase(dbName) ;
  93. }
  94. bool SybaseConnection::drv_closeDatabase()
  95. {
  96. // here we disconnect the connection
  97. return true;
  98. }
  99. bool SybaseConnection::drv_dropDatabase(const QString &dbName)
  100. {
  101. return drv_executeSql(KDbEscapedString("DROP DATABASE ") + escapeString(dbName));
  102. }
  103. bool SybaseConnection::drv_executeSql(const KDbEscapedString& sql)
  104. {
  105. return d->executeSql(sql);
  106. }
  107. quint64 SybaseConnection::drv_lastInsertRecordId()
  108. {
  109. int rowId = 0;
  110. querySingleNumber(KDbEscapedString("Select @@IDENTITY"), &rowId);
  111. return (qint64)rowId;
  112. }
  113. int SybaseConnection::serverResult()
  114. {
  115. return d->res;
  116. }
  117. QString SybaseConnection::serverResultName() const
  118. {
  119. return QString();
  120. }
  121. /*void SybaseConnection::drv_clearServerResult()
  122. {
  123. if (!d)
  124. return;
  125. d->res = 0;
  126. }*/
  127. bool SybaseConnection::drv_containsTable(const QString &tableName)
  128. {
  129. return resultExists(KDbEscapedString("SELECT name FROM sysobjects WHERE type='U' AND name=%1")
  130. .arg(escapeString(tableName)));
  131. }
  132. bool SybaseConnection::drv_getTablesList(QStringList* list)
  133. {
  134. return queryStringList(KDbEscapedString("SELECT name FROM sysobjects WHERE type='U'"), list);
  135. }
  136. KDbPreparedStatement SybaseConnection::prepareStatement(KDbPreparedStatement::StatementType type,
  137. KDbFieldList* fields)
  138. {
  139. return SybasePreparedStatement(type, *d, fields);
  140. }
  141. bool KDbSybaseConnection::drv_beforeInsert(const QString& table, KDbFieldList* fields)
  142. {
  143. if (fields.autoIncrementFields()->isEmpty())
  144. return true;
  145. // explicit insertion into IDENTITY fields !!
  146. return drv_executeSql(KDbEscapedString("SET IDENTITY_INSERT %1 ON").arg(escapeIdentifier(table)));
  147. }
  148. bool KDbSybaseConnection::drv_afterInsert(const QString& table, KDbFieldList* fields)
  149. {
  150. // should we instead just set a flag when an identity_insert has taken place and only check for that
  151. // flag here ?
  152. if (fields.autoIncrementFields()->isEmpty())
  153. return true;
  154. // explicit insertion into IDENTITY fields has taken place. Turn off IDENTITY_INSERT
  155. return drv_executeSql(KDbEscapedString("SET IDENTITY_INSERT %1 OFF").arg(escapeIdentifier(table)));
  156. }
  157. bool KDbSybaseConnection::drv_beforeUpdate(const QString& table, KDbFieldList* fields)
  158. {
  159. if (fields->autoIncrementFields()->isEmpty())
  160. return true;
  161. // explicit update of IDENTITY fields has taken place.
  162. return drv_executeSql(KDbEscapedString("SET IDENTITY_UPDATE %1 ON").arg(escapeIdentifier(table)));
  163. }
  164. bool KDbSybaseConnection::drv_afterUpdate(const QString& table, KDbFieldList& fields)
  165. {
  166. // should we instead just set a flag when an identity_update has taken place and only check for that
  167. // flag here ?
  168. if (fields.autoIncrementFields()->isEmpty())
  169. return true;
  170. // explicit insertion into IDENTITY fields has taken place. Turn off IDENTITY_INSERT
  171. return drv_executeSql(KDbEscapedString("SET IDENTITY_UPDATE %1 OFF").arg(escapeIdentifier(table)));
  172. }