KDb

SybaseConnection.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2007 Sharan Rao <[email protected]>
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 #include <QRegularExpression>
21 
22 #include <kgenericfactory.h>
23 
24 #include "SybaseDriver.h"
25 #include "SybaseConnection.h"
26 #include "SybaseConnection_p.h"
27 #include "SybasePreparedStatement.h"
28 #include "KDbError.h"
29 
31  : KDbConnection(driver, connData)
32  , d(new SybaseConnectionInternal(this))
33 {
34 }
35 
36 SybaseConnection::~SybaseConnection()
37 {
38  destroy();
39 }
40 
42 {
43  const bool ok = d->db_connect(*data());
44  if (!ok)
45  return false;
46 
47  // we can retrieve the server name and the server version using global variables
48  // @@servername
49  // @@version
50 
51  QString serverVersionString;
52 
53  if (!querySingleString(KDbEscapedString("SELECT @@servername") , &version.string)) {
54  sybaseWarning() << "Couldn't fetch server name";
55  }
56 
57  if (!querySingleString(KDbEscapedString("SELECT @@version"), &serverVersionString)) {
58  sybaseWarning() << "Couldn't fetch server version";
59  }
60 
61  QRegularExpression versionRe("^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$");
62  QRegularExpressionMatch match = versionRe.match(serverVersionString);
63  if (match.hasMatch()) {
64  version.major = match.captured(1).toInt();
65  version.minor = match.captured(2).toInt();
66  version.release = match.captured(3).toInt();
67  }
68 
69  return true;
70 }
71 
73 {
74  return d->db_disconnect();
75 }
76 
77 KDbCursor* SybaseConnection::prepareQuery(const KDbEscapedString& sql, int cursor_options)
78 {
79  return new SybaseCursor(this, sql, cursor_options);
80 }
81 
82 KDbCursor* SybaseConnection::prepareQuery(KDbQuerySchema* query, int cursor_options)
83 {
84  return new SybaseCursor(this, query, cursor_options);
85 }
86 
88 {
89  // select * from master..sysdatabases ?
90  // todo: verify.
91  return queryStringList(KDbEscapedString("SELECT name FROM master..sysdatabases"), list) ;
92 }
93 
95 {
96  //sybaseDebug() << dbName;
97  // mysql_create_db deprecated, use SQL here.
98  if (drv_executeSql(KDbEscapedString("CREATE DATABASE ") + dbName)) {
99  // set allow_nulls_by_default option to true
100  KDbEscapedString allowNullsQuery = KDbEscapedString("sp_dboption %1, allow_nulls_by_default, true").arg(dbName);
101  if (drv_executeSql(allowNullsQuery.data()))
102  return true;
103  }
104  d->storeResult();
105  return false;
106 }
107 
108 bool SybaseConnection::drv_useDatabase(const QString &dbName, bool *cancelled, KDbMessageHandler* msgHandler)
109 {
110  Q_UNUSED(cancelled);
111  Q_UNUSED(msgHandler);
112 
113  //! @todo is here escaping needed?
114  return d->useDatabase(dbName) ;
115 }
116 
118 {
119 // here we disconnect the connection
120  return true;
121 }
122 
124 {
125 
126  return drv_executeSql(KDbEscapedString("DROP DATABASE ") + escapeString(dbName));
127 }
128 
130 {
131  return d->executeSql(sql);
132 }
133 
134 quint64 SybaseConnection::drv_lastInsertRecordId()
135 {
136  int rowId = 0;
137  querySingleNumber(KDbEscapedString("Select @@IDENTITY"), &rowId);
138  return (qint64)rowId;
139 }
140 
141 int SybaseConnection::serverResult()
142 {
143  return d->res;
144 }
145 
147 {
148  return QString();
149 }
150 
151 /*void SybaseConnection::drv_clearServerResult()
152 {
153  if (!d)
154  return;
155  d->res = 0;
156 }*/
157 
159 {
160  return resultExists(KDbEscapedString("SELECT name FROM sysobjects WHERE type='U' AND name=%1")
161  .arg(escapeString(tableName)));
162 }
163 
165 {
166  return queryStringList(KDbEscapedString("SELECT name FROM sysobjects WHERE type='U'"), list);
167 }
168 
169 KDbPreparedStatement SybaseConnection::prepareStatement(KDbPreparedStatement::StatementType type,
170  KDbFieldList* fields)
171 {
172  return SybasePreparedStatement(type, *d, fields);
173 }
174 
175 bool KDbSybaseConnection::drv_beforeInsert(const QString& table, KDbFieldList* fields)
176 {
177 
178  if (fields.autoIncrementFields()->isEmpty())
179  return true;
180 
181  // explicit insertion into IDENTITY fields !!
182  return drv_executeSql(KDbEscapedString("SET IDENTITY_INSERT %1 ON").arg(escapeIdentifier(table)));
183 
184 }
185 
186 bool KDbSybaseConnection::drv_afterInsert(const QString& table, KDbFieldList* fields)
187 {
188  // should we instead just set a flag when an identity_insert has taken place and only check for that
189  // flag here ?
190 
191  if (fields.autoIncrementFields()->isEmpty())
192  return true;
193 
194  // explicit insertion into IDENTITY fields has taken place. Turn off IDENTITY_INSERT
195  return drv_executeSql(KDbEscapedString("SET IDENTITY_INSERT %1 OFF").arg(escapeIdentifier(table)));
196 
197 }
198 
199 bool KDbSybaseConnection::drv_beforeUpdate(const QString& table, KDbFieldList* fields)
200 {
201  if (fields->autoIncrementFields()->isEmpty())
202  return true;
203 
204  // explicit update of IDENTITY fields has taken place.
205  return drv_executeSql(KDbEscapedString("SET IDENTITY_UPDATE %1 ON").arg(escapeIdentifier(table)));
206 }
207 
208 bool KDbSybaseConnection::drv_afterUpdate(const QString& table, KDbFieldList& fields)
209 {
210  // should we instead just set a flag when an identity_update has taken place and only check for that
211  // flag here ?
212 
213  if (fields.autoIncrementFields()->isEmpty())
214  return true;
215 
216  // explicit insertion into IDENTITY fields has taken place. Turn off IDENTITY_INSERT
217  return drv_executeSql(KDbEscapedString("SET IDENTITY_UPDATE %1 OFF").arg(escapeIdentifier(table)));
218 }
KDbField::List * autoIncrementFields() const
Provides database cursor functionality.
Definition: KDbCursor.h:68
KDB_EXPORT QString escapeIdentifier(const QString &string)
Definition: KDb.cpp:1334
virtual bool drv_useDatabase(const QString &dbName=QString(), bool *cancelled=0, KDbMessageHandler *msgHandler=0)
tristate querySingleNumber(const KDbEscapedString &sql, int *number, int column=0, QueryRecordOptions options=QueryRecordOption::Default)
virtual QString serverResultName() const
Implemented for KDbResultable.
tristate querySingleString(const KDbEscapedString &sql, QString *value, int column=0, QueryRecordOptions options=QueryRecordOption::Default)
Specialized string for escaping.
virtual bool drv_executeSql(const KDbEscapedString &sql)
Executes query for a raw SQL statement sql without returning resulting records.
Database driver's abstraction.
Definition: KDbDriver.h:49
virtual bool drv_getDatabasesList(QStringList *list)
bool queryStringList(const KDbEscapedString &sql, QStringList *list, int column=0)
virtual bool drv_createDatabase(const QString &dbName=QString())
virtual bool drv_closeDatabase()
KDbConnectionData data() const
bool isEmpty() const const
tristate resultExists(const KDbEscapedString &sql, QueryRecordOptions options=QueryRecordOption::Default)
virtual KDbEscapedString escapeString(const QString &str) const
virtual bool drv_connect()=0
virtual bool drv_getTablesList(QStringList *list)
virtual bool drv_disconnect()
virtual bool drv_dropDatabase(const QString &dbName=QString())
KDbQuerySchema provides information about database query.
unsigned int version()
Definition: KDb.cpp:336
virtual bool drv_containsTable(const QString &tableName)
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
Database specific connection data, e.g. host, port.
Prepared database command for optimizing sequences of multiple database actions.
SybaseConnection(KDbDriver *driver, const KDbConnectionData &connData)
Provides database connection, allowing queries and data modification.
Definition: KDbConnection.h:51
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:21:34 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.