kexi

mysqldriver.cpp
1/* This file is part of the KDE project
2Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
3Daniel Molkentin <molkentin@kde.org>
4Copyright (C) 2003 Joseph Wenninger<jowenn@kde.org>
5
6This program is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public License
17along with this program; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#ifdef TQ_WS_WIN
23# include <mysql/config-win.h>
24#endif
25#include <mysql_version.h>
26#include <mysql.h>
27#define BOOL bool
28
29#include <tqvariant.h>
30#include <tqfile.h>
31#include <tqdict.h>
32
33#include <kgenericfactory.h>
34#include <kdebug.h>
35
36#include "mysqldriver.h"
37#include "mysqlconnection.h"
38#include <kexidb/field.h>
39#include <kexidb/driver_p.h>
40#include <kexidb/utils.h>
41
42using namespace KexiDB;
43
44KEXIDB_DRIVER_INFO( MySqlDriver, mysql )
45
46/* TODO: Implement buffered/unbuffered, rather than buffer everything.
47 Each MYSQL connection can only handle at most one unbuffered cursor,
48 so MySqlConnection should keep count?
49 */
50
51
57MySqlDriver::MySqlDriver(TQObject *parent, const char *name, const TQStringList &args) : Driver(parent, name,args)
58{
59// KexiDBDrvDbg << "MySqlDriver::MySqlDriver()" << endl;
60
61 d->isFileDriver=false;
63
64 beh->ROW_ID_FIELD_NAME="LAST_INSERT_ID()";
65 beh->ROW_ID_FIELD_RETURNS_LAST_AUTOINCREMENTED_VALUE=true;
66 beh->_1ST_ROW_READ_AHEAD_REQUIRED_TO_KNOW_IF_THE_RESULT_IS_EMPTY=false;
67 beh->USING_DATABASE_REQUIRED_TO_CONNECT=false;
68 beh->QUOTATION_MARKS_FOR_IDENTIFIER='`';
69 beh->SQL_KEYWORDS = keywords;
70 initSQLKeywords(331);
71
72 //predefined properties
73#if MYSQL_VERSION_ID < 40000
74 d->properties["client_library_version"] = MYSQL_SERVER_VERSION; //nothing better
75 d->properties["default_server_encoding"] = MYSQL_CHARSET; //nothing better
76#elif MYSQL_VERSION_ID < 50000
77//OK? d->properties["client_library_version"] = mysql_get_client_version();
78#endif
79
80 d->typeNames[Field::Byte]="TINYINT";
81 d->typeNames[Field::ShortInteger]="SMALLINT";
82 d->typeNames[Field::Integer]="INT";
83 d->typeNames[Field::BigInteger]="BIGINT";
84 // Can use BOOLEAN here, but BOOL has been in MySQL longer
85 d->typeNames[Field::Boolean]="BOOL";
86 d->typeNames[Field::Date]="DATE";
87 d->typeNames[Field::DateTime]="DATETIME";
88 d->typeNames[Field::Time]="TIME";
89 d->typeNames[Field::Float]="FLOAT";
90 d->typeNames[Field::Double]="DOUBLE";
91 d->typeNames[Field::Text]="VARCHAR";
92 d->typeNames[Field::LongText]="LONGTEXT";
93 d->typeNames[Field::BLOB]="BLOB";
94}
95
96MySqlDriver::~MySqlDriver()
97{
98}
99
102{
103 return new MySqlConnection( this, conn_data );
104}
105
106bool MySqlDriver::isSystemDatabaseName(const TQString &n) const
107{
108 return n.lower()=="mysql" || Driver::isSystemObjectName(n);
109}
110
111bool MySqlDriver::drv_isSystemFieldName(const TQString&) const {
112 return false;
113}
114
115TQString MySqlDriver::escapeString(const TQString& str) const
116{
117 //escape as in http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html
119
120 const int old_length = str.length();
121 int i;
122 for ( i = 0; i < old_length; i++ ) { //anything to escape?
123 const unsigned int ch = str[i].unicode();
124 if (ch == '\\' || ch == '\'' || ch == '"' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\b' || ch == '\0')
125 break;
126 }
127 if (i >= old_length) { //no characters to escape
128 return TQString::fromLatin1("'") + str + TQString::fromLatin1("'");
129 }
130
131 TQChar *new_string = new TQChar[ old_length * 3 + 1 ]; // a worst case approximation
133 int new_length = 0;
134 new_string[new_length++] = '\''; //prepend '
135 for ( i = 0; i < old_length; i++, new_length++ ) {
136 const unsigned int ch = str[i].unicode();
137 if (ch == '\\') {
138 new_string[new_length++] = '\\';
139 new_string[new_length] = '\\';
140 }
141 else if (ch <= '\'') {//check for speedup
142 if (ch == '\'') {
143 new_string[new_length++] = '\\';
144 new_string[new_length] = '\'';
145 }
146 else if (ch == '"') {
147 new_string[new_length++] = '\\';
148 new_string[new_length] = '"';
149 }
150 else if (ch == '\n') {
151 new_string[new_length++] = '\\';
152 new_string[new_length] = 'n';
153 }
154 else if (ch == '\r') {
155 new_string[new_length++] = '\\';
156 new_string[new_length] = 'r';
157 }
158 else if (ch == '\t') {
159 new_string[new_length++] = '\\';
160 new_string[new_length] = 't';
161 }
162 else if (ch == '\b') {
163 new_string[new_length++] = '\\';
164 new_string[new_length] = 'b';
165 }
166 else if (ch == '\0') {
167 new_string[new_length++] = '\\';
168 new_string[new_length] = '0';
169 }
170 else
171 new_string[new_length] = str[i];
172 }
173 else
174 new_string[new_length] = str[i];
175 }
176
177 new_string[new_length++] = '\''; //append '
178 TQString result(new_string, new_length);
179 delete [] new_string;
180 return result;
181}
182
183TQString MySqlDriver::escapeBLOB(const TQByteArray& array) const
184{
186}
187
188TQCString MySqlDriver::escapeString(const TQCString& str) const
189{
192
193 return TQCString("'")+TQCString(str)
194 .replace( '\\', "\\\\" )
195 .replace( '\'', "\\''" )
196 .replace( '"', "\\\"" )
197 + TQCString("'");
198}
199
203TQString MySqlDriver::drv_escapeIdentifier( const TQString& str) const {
204 return TQString(str).replace('`', "'");
205}
206
207TQCString MySqlDriver::drv_escapeIdentifier( const TQCString& str) const {
208 return TQCString(str).replace('`', "'");
209}
210
211#include "mysqldriver.moc"
212
Database specific connection data, e.g. host, port.
Provides database connection, allowing queries and data modification.
Definition connection.h:62
@ IgnoreTransactions
Definition driver.h:111
virtual bool isSystemObjectName(const TQString &n) const
Definition driver.cpp:230
void initSQLKeywords(int hashSize=17)
Definition driver.cpp:360
Driver(TQObject *parent, const char *name, const TQStringList &args=TQStringList())
@ ShortInteger
Definition field.h:86
MySQL database driver.
Definition mysqldriver.h:31
MySqlDriver(TQObject *parent, const char *name, const TQStringList &args=TQStringList())
virtual TQString escapeBLOB(const TQByteArray &array) const
Escape BLOB value array.
virtual Connection * drv_createConnection(ConnectionData &conn_data)
virtual bool isSystemDatabaseName(const TQString &n) const
virtual TQString drv_escapeIdentifier(const TQString &str) const
virtual TQString escapeString(const TQString &str) const
Escape a string for use as a value.
virtual bool drv_isSystemFieldName(const TQString &n) const
High-level database connectivity library with database backend drivers.
KEXI_DB_EXPORT TQString escapeBLOB(const TQByteArray &array, BLOBEscapingType type)
@ BLOBEscape0xHex
escaping like 0x1FAD, used by mysql (hex numbers)