kexi

mysqldriver.cpp

00001 /* This file is part of the KDE project
00002 Copyright (C) 2002   Lucijan Busch <lucijan@gmx.at>
00003 Daniel Molkentin <molkentin@kde.org>
00004 Copyright (C) 2003   Joseph Wenninger<jowenn@kde.org>
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Library General Public
00008 License as published by the Free Software Foundation; either
00009 version 2 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Library General Public License for more details.
00015 
00016 You should have received a copy of the GNU Library General Public License
00017 along with this program; see the file COPYING.  If not, write to
00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #ifdef Q_WS_WIN
00023 # include <mysql/config-win.h>
00024 #endif
00025 #include <mysql_version.h>
00026 #include <mysql.h>
00027 #define BOOL bool
00028 
00029 #include <qvariant.h>
00030 #include <qfile.h>
00031 #include <qdict.h>
00032 
00033 #include <kgenericfactory.h>
00034 #include <kdebug.h>
00035 
00036 #include "mysqldriver.h"
00037 #include "mysqlconnection.h"
00038 #include <kexidb/field.h>
00039 #include <kexidb/driver_p.h>
00040 
00041 using namespace KexiDB;
00042 
00043 KEXIDB_DRIVER_INFO( MySqlDriver, mysql )
00044 
00045 /* TODO: Implement buffered/unbuffered, rather than buffer everything.
00046    Each MYSQL connection can only handle at most one unbuffered cursor,
00047    so MySqlConnection should keep count?
00048  */
00049 
00050 
00056 MySqlDriver::MySqlDriver(QObject *parent, const char *name, const QStringList &args) : Driver(parent, name,args)
00057 {
00058 //  KexiDBDrvDbg << "MySqlDriver::MySqlDriver()" << endl;
00059 
00060     d->isFileDriver=false;
00061     d->features=IgnoreTransactions | CursorForward;
00062 
00063     beh->ROW_ID_FIELD_NAME="LAST_INSERT_ID()";
00064     beh->ROW_ID_FIELD_RETURNS_LAST_AUTOINCREMENTED_VALUE=true;
00065     beh->_1ST_ROW_READ_AHEAD_REQUIRED_TO_KNOW_IF_THE_RESULT_IS_EMPTY=false;
00066     beh->USING_DATABASE_REQUIRED_TO_CONNECT=false;
00067     beh->QUOTATION_MARKS_FOR_IDENTIFIER='`';
00068     beh->SQL_KEYWORDS = keywords;
00069     initSQLKeywords(331);
00070     
00071     //predefined properties
00072 #if MYSQL_VERSION_ID < 40000
00073     d->properties["client_library_version"] = MYSQL_SERVER_VERSION; //nothing better
00074     d->properties["default_server_encoding"] = MYSQL_CHARSET; //nothing better
00075 #elif MYSQL_VERSION_ID < 50000
00076 //OK?   d->properties["client_library_version"] = mysql_get_client_version();
00077 #endif
00078 
00079     d->typeNames[Field::Byte]="TINYINT";
00080     d->typeNames[Field::ShortInteger]="SMALLINT";
00081     d->typeNames[Field::Integer]="INT";
00082     d->typeNames[Field::BigInteger]="BIGINT";
00083     // Can use BOOLEAN here, but BOOL has been in MySQL longer
00084     d->typeNames[Field::Boolean]="BOOL";
00085     d->typeNames[Field::Date]="DATE";
00086     d->typeNames[Field::DateTime]="DATETIME";
00087     d->typeNames[Field::Time]="TIME";
00088     d->typeNames[Field::Float]="FLOAT";
00089     d->typeNames[Field::Double]="DOUBLE";
00090     d->typeNames[Field::Text]="VARCHAR";
00091     d->typeNames[Field::LongText]="LONGTEXT";
00092     d->typeNames[Field::BLOB]="BLOB";
00093 }
00094 
00095 MySqlDriver::~MySqlDriver()
00096 {
00097 }
00098 
00099 KexiDB::Connection*
00100 MySqlDriver::drv_createConnection( ConnectionData &conn_data )
00101 {
00102     return new MySqlConnection( this, conn_data );
00103 }
00104 
00105 bool MySqlDriver::isSystemDatabaseName(const QString &n) const
00106 {
00107     return n.lower()=="mysql" || Driver::isSystemObjectName(n);
00108 }
00109 
00110 bool MySqlDriver::drv_isSystemFieldName(const QString&) const {
00111     return false;
00112 }
00113 
00114 QString MySqlDriver::escapeString(const QString& str) const
00115 {
00116     //escape as in http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html
00118 
00119     const int old_length = str.length();
00120     int i;
00121     for ( i = 0; i < old_length; i++ ) { //anything to escape?
00122         const unsigned int ch = str[i].unicode();
00123         if (ch == '\\' || ch == '\'' || ch == '"' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\b' || ch == '\0')
00124             break;
00125     }
00126     if (i >= old_length) { //no characters to escape
00127         return QString::fromLatin1("'") + str + QString::fromLatin1("'");
00128     }
00129 
00130     QChar *new_string = new QChar[ old_length * 3 + 1 ]; // a worst case approximation
00132     int new_length = 0;
00133     new_string[new_length++] = '\''; //prepend '
00134     for ( i = 0; i < old_length; i++, new_length++ ) {
00135         const unsigned int ch = str[i].unicode();
00136         if (ch == '\\') {
00137             new_string[new_length++] = '\\';
00138             new_string[new_length] = '\\';
00139         }
00140         else if (ch <= '\'') {//check for speedup
00141             if (ch == '\'') {
00142                 new_string[new_length++] = '\\';
00143                 new_string[new_length] = '\'';
00144             }
00145             else if (ch == '"') {
00146                 new_string[new_length++] = '\\';
00147                 new_string[new_length] = '"';
00148             }
00149             else if (ch == '\n') {
00150                 new_string[new_length++] = '\\';
00151                 new_string[new_length] = 'n';
00152             }
00153             else if (ch == '\r') {
00154                 new_string[new_length++] = '\\';
00155                 new_string[new_length] = 'r';
00156             }
00157             else if (ch == '\t') {
00158                 new_string[new_length++] = '\\';
00159                 new_string[new_length] = 't';
00160             }
00161             else if (ch == '\b') {
00162                 new_string[new_length++] = '\\';
00163                 new_string[new_length] = 'b';
00164             }
00165             else if (ch == '\0') {
00166                 new_string[new_length++] = '\\';
00167                 new_string[new_length] = '0';
00168             }
00169             else
00170                 new_string[new_length] = str[i];
00171         }
00172         else
00173             new_string[new_length] = str[i];
00174     }
00175 
00176     new_string[new_length++] = '\''; //append '
00177     QString result(new_string, new_length);
00178     delete [] new_string;
00179     return result;
00180 }
00181 
00182 QString MySqlDriver::escapeBLOB(const QByteArray& array) const
00183 {
00184     return escapeBLOBInternal(array, BLOB_ESCAPING_TYPE_USE_0x);
00185 }
00186 
00187 QCString MySqlDriver::escapeString(const QCString& str) const
00188 {
00191 
00192     return QCString("'")+QCString(str)
00193         .replace( '\\', "\\\\" )
00194         .replace( '\'', "\\''" )
00195         .replace( '"', "\\\"" )
00196         + QCString("'");
00197 }
00198 
00202 QString MySqlDriver::drv_escapeIdentifier( const QString& str) const {
00203     return QString(str).replace('`', "'");
00204 }
00205 
00206 QCString MySqlDriver::drv_escapeIdentifier( const QCString& str) const {
00207     return QCString(str).replace('`', "'");
00208 }
00209 
00210 #include "mysqldriver.moc"
00211 
KDE Home | KDE Accessibility Home | Description of Access Keys