kexi
mysqldriver.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00046
00047
00048
00049
00050
00056 MySqlDriver::MySqlDriver(QObject *parent, const char *name, const QStringList &args) : Driver(parent, name,args)
00057 {
00058
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
00072 #if MYSQL_VERSION_ID < 40000
00073 d->properties["client_library_version"] = MYSQL_SERVER_VERSION;
00074 d->properties["default_server_encoding"] = MYSQL_CHARSET;
00075 #elif MYSQL_VERSION_ID < 50000
00076
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
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
00118
00119 const int old_length = str.length();
00120 int i;
00121 for ( i = 0; i < old_length; i++ ) {
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) {
00127 return QString::fromLatin1("'") + str + QString::fromLatin1("'");
00128 }
00129
00130 QChar *new_string = new QChar[ old_length * 3 + 1 ];
00132 int new_length = 0;
00133 new_string[new_length++] = '\'';
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 <= '\'') {
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++] = '\'';
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
|