kexi

pqxxdriver.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Adam Pigg <adam@piggz.co.uk>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <kexidb/connection.h>
00021 #include <kexidb/drivermanager.h>
00022 #include <kexidb/driver_p.h>
00023 #include "pqxxdriver.h"
00024 #include "pqxxconnection.h"
00025 #include <string>
00026 
00027 #include <kdebug.h>
00028 
00029 using namespace KexiDB;
00030 
00031 KEXIDB_DRIVER_INFO( pqxxSqlDriver, pqxxsql )
00032 
00033 //==================================================================================
00034 //
00035 pqxxSqlDriver::pqxxSqlDriver( QObject *parent, const char *name, const QStringList &args )
00036     : Driver( parent, name, args )
00037 {
00038     d->isFileDriver = false;
00039     d->features = SingleTransactions | CursorForward | CursorBackward;
00041 
00042     beh->UNSIGNED_TYPE_KEYWORD = "";
00043     beh->ROW_ID_FIELD_NAME = "oid";
00044     beh->SPECIAL_AUTO_INCREMENT_DEF = false;
00045     beh->AUTO_INCREMENT_TYPE = "SERIAL";
00046     beh->AUTO_INCREMENT_FIELD_OPTION = "";
00047     beh->AUTO_INCREMENT_PK_FIELD_OPTION = "PRIMARY KEY";
00048     beh->ALWAYS_AVAILABLE_DATABASE_NAME = "template1";
00049     beh->QUOTATION_MARKS_FOR_IDENTIFIER = '"';
00050     beh->SQL_KEYWORDS = keywords;
00051     initSQLKeywords(233);
00052 
00053     //predefined properties
00054     d->properties["client_library_version"] = "";//TODO
00055     d->properties["default_server_encoding"] = ""; //TODO
00056 
00057     d->typeNames[Field::Byte]="SMALLINT";
00058     d->typeNames[Field::ShortInteger]="SMALLINT";
00059     d->typeNames[Field::Integer]="INTEGER";
00060     d->typeNames[Field::BigInteger]="BIGINT";
00061     d->typeNames[Field::Boolean]="BOOLEAN";
00062     d->typeNames[Field::Date]="DATE";
00063     d->typeNames[Field::DateTime]="DATETIME";
00064     d->typeNames[Field::Time]="TIME";
00065     d->typeNames[Field::Float]="REAL";
00066     d->typeNames[Field::Double]="DOUBLE PRECISION";
00067     d->typeNames[Field::Text]="CHARACTER VARYING";
00068     d->typeNames[Field::LongText]="TEXT";
00069     d->typeNames[Field::BLOB]="BYTEA";
00070 }
00071 
00072 //==================================================================================
00073 //Override the default implementation to allow for NUMERIC type natively
00074 QString pqxxSqlDriver::sqlTypeName(int id_t, int p) const
00075 { 
00076     if (id_t==Field::Null)
00077         return "NULL";
00078     if (id_t==Field::Float || id_t==Field::Double)
00079     {
00080         if (p>0)
00081         {
00082             return "NUMERIC";
00083         }
00084         else
00085         {
00086             return d->typeNames[id_t];
00087         }
00088     }
00089     else
00090     {
00091         return d->typeNames[id_t];
00092     }
00093 }
00094 
00095 //==================================================================================
00096 //
00097 pqxxSqlDriver::~pqxxSqlDriver()
00098 {
00099 //  delete d;
00100 }
00101 
00102 //==================================================================================
00103 //
00104 KexiDB::Connection*
00105 pqxxSqlDriver::drv_createConnection( ConnectionData &conn_data )
00106 {
00107     return new pqxxSqlConnection( this, conn_data );
00108 }
00109 
00110 //==================================================================================
00111 //
00112 bool pqxxSqlDriver::isSystemObjectName( const QString& n ) const
00113 {
00114     return Driver::isSystemObjectName(n);
00115 }
00116 
00117 //==================================================================================
00118 //
00119 bool pqxxSqlDriver::drv_isSystemFieldName( const QString& ) const
00120 {
00121     return false;
00122 }
00123 
00124 //==================================================================================
00125 //
00126 bool pqxxSqlDriver::isSystemDatabaseName( const QString& n ) const
00127 {
00128     return n.lower()=="template1" || n.lower()=="template0";
00129 }
00130 
00131 //==================================================================================
00132 //
00133 QString pqxxSqlDriver::escapeString( const QString& str) const
00134 {
00135     return QString(pqxx::Quote(str.ascii()).c_str());
00136 }
00137 
00138 //==================================================================================
00139 //
00140 QCString pqxxSqlDriver::escapeString( const QCString& str) const
00141 {
00142     return QCString(pqxx::Quote(QString(str).ascii()).c_str());
00143 }
00144 
00145 //==================================================================================
00146 //
00147 QString pqxxSqlDriver::drv_escapeIdentifier( const QString& str) const {
00148     return QString(str).replace( '"', "\"\"" );
00149 }
00150 
00151 //==================================================================================
00152 //
00153 QCString pqxxSqlDriver::drv_escapeIdentifier( const QCString& str) const {
00154     return QCString(str).replace( '"', "\"\"" );
00155 }
00156 
00157 //==================================================================================
00158 //
00159 QString pqxxSqlDriver::escapeBLOB(const QByteArray& array) const
00160 {
00161     return escapeBLOBInternal(array, BLOB_ESCAPING_TYPE_USE_OCTAL);
00162 }
00163 
00164 QString pqxxSqlDriver::valueToSQL( uint ftype, const QVariant& v ) const
00165 {
00166     if (ftype==Field::Boolean) {
00167         // use SQL compliant TRUE or FALSE as described here
00168         // http://www.postgresql.org/docs/8.0/interactive/datatype-boolean.html
00169         // 1 or 0 does not work
00170         return v.toInt()==0 ? QString::fromLatin1("FALSE") : QString::fromLatin1("TRUE");
00171     }
00172     return Driver::valueToSQL(ftype, v);
00173 }
00174 
00175 
00176 #include "pqxxdriver.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys