KDb

SybaseConnection.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2007 Sharan Rao <sharanrao@gmail.com>
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public
6License as published by the Free Software Foundation; either
7version 2 of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public License
15along with this program; see the file COPYING. If not, write to
16the 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
36SybaseConnection::~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
77KDbCursor* SybaseConnection::prepareQuery(const KDbEscapedString& sql, int cursor_options)
78{
79 return new SybaseCursor(this, sql, cursor_options);
80}
81
82KDbCursor* 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
108bool 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
134quint64 SybaseConnection::drv_lastInsertRecordId()
135{
136 int rowId = 0;
137 querySingleNumber(KDbEscapedString("Select @@IDENTITY"), &rowId);
138 return (qint64)rowId;
139}
140
141int 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
169KDbPreparedStatement SybaseConnection::prepareStatement(KDbPreparedStatement::StatementType type,
170 KDbFieldList* fields)
171{
172 return SybasePreparedStatement(type, *d, fields);
173}
174
175bool 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
186bool 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
199bool 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
208bool 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}
Database specific connection data, e.g. host, port.
Provides database connection, allowing queries and data modification.
virtual KDbEscapedString escapeString(const QString &str) const
bool queryStringList(const KDbEscapedString &sql, QStringList *list, int column=0)
tristate querySingleString(const KDbEscapedString &sql, QString *value, int column=0, QueryRecordOptions options=QueryRecordOption::Default)
KDbConnectionData data() const
virtual bool drv_connect()=0
tristate querySingleNumber(const KDbEscapedString &sql, int *number, int column=0, QueryRecordOptions options=QueryRecordOption::Default)
tristate resultExists(const KDbEscapedString &sql, QueryRecordOptions options=QueryRecordOption::Default)
Provides database cursor functionality.
Definition KDbCursor.h:69
Database driver's abstraction.
Definition KDbDriver.h:50
Specialized string for escaping.
KDbField::List * autoIncrementFields() const
Prepared database command for optimizing sequences of multiple database actions.
KDbQuerySchema provides information about database query.
SybaseConnection(KDbDriver *driver, const KDbConnectionData &connData)
virtual bool drv_executeSql(const KDbEscapedString &sql)
Executes query for a raw SQL statement sql without returning resulting records.
virtual QString serverResultName() const
Implemented for KDbResultable.
virtual bool drv_getTablesList(QStringList *list)
virtual bool drv_createDatabase(const QString &dbName=QString())
virtual bool drv_useDatabase(const QString &dbName=QString(), bool *cancelled=0, KDbMessageHandler *msgHandler=0)
virtual bool drv_closeDatabase()
virtual bool drv_dropDatabase(const QString &dbName=QString())
virtual bool drv_disconnect()
virtual bool drv_containsTable(const QString &tableName)
virtual bool drv_getDatabasesList(QStringList *list)
KCOREADDONS_EXPORT unsigned int version()
KDB_EXPORT QString escapeIdentifier(const QString &string)
Definition KDb.cpp:1334
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.