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 <kexidb/utils.h>
00024 #include "pqxxdriver.h"
00025 #include "pqxxconnection.h"
00026 #include <string>
00027 
00028 #include <kdebug.h>
00029 
00030 using namespace KexiDB;
00031 
00032 KEXIDB_DRIVER_INFO( pqxxSqlDriver, pqxxsql )
00033 
00034 //==================================================================================
00035 //
00036 pqxxSqlDriver::pqxxSqlDriver( QObject *parent, const char *name, const QStringList &args )
00037     : Driver( parent, name, args )
00038 {
00039     d->isFileDriver = false;
00040     d->features = SingleTransactions | CursorForward | CursorBackward;
00042 
00043     beh->UNSIGNED_TYPE_KEYWORD = "";
00044     beh->ROW_ID_FIELD_NAME = "oid";
00045     beh->SPECIAL_AUTO_INCREMENT_DEF = false;
00046     beh->AUTO_INCREMENT_TYPE = "SERIAL";
00047     beh->AUTO_INCREMENT_FIELD_OPTION = "";
00048     beh->AUTO_INCREMENT_PK_FIELD_OPTION = "PRIMARY KEY";
00049     beh->ALWAYS_AVAILABLE_DATABASE_NAME = "template1";
00050     beh->QUOTATION_MARKS_FOR_IDENTIFIER = '"';
00051     beh->SQL_KEYWORDS = keywords;
00052     initSQLKeywords(233);
00053 
00054     //predefined properties
00055     d->properties["client_library_version"] = "";//TODO
00056     d->properties["default_server_encoding"] = ""; //TODO
00057 
00058     d->typeNames[Field::Byte]="SMALLINT";
00059     d->typeNames[Field::ShortInteger]="SMALLINT";
00060     d->typeNames[Field::Integer]="INTEGER";
00061     d->typeNames[Field::BigInteger]="BIGINT";
00062     d->typeNames[Field::Boolean]="BOOLEAN";
00063     d->typeNames[Field::Date]="DATE";
00064     d->typeNames[Field::DateTime]="TIMESTAMP";
00065     d->typeNames[Field::Time]="TIME";
00066     d->typeNames[Field::Float]="REAL";
00067     d->typeNames[Field::Double]="DOUBLE PRECISION";
00068     d->typeNames[Field::Text]="CHARACTER VARYING";
00069     d->typeNames[Field::LongText]="TEXT";
00070     d->typeNames[Field::BLOB]="BYTEA";
00071 }
00072 
00073 //==================================================================================
00074 //Override the default implementation to allow for NUMERIC type natively
00075 QString pqxxSqlDriver::sqlTypeName(int id_t, int p) const
00076 { 
00077     if (id_t==Field::Null)
00078         return "NULL";
00079     if (id_t==Field::Float || id_t==Field::Double)
00080     {
00081         if (p>0)
00082         {
00083             return "NUMERIC";
00084         }
00085         else
00086         {
00087             return d->typeNames[id_t];
00088         }
00089     }
00090     else
00091     {
00092         return d->typeNames[id_t];
00093     }
00094 }
00095 
00096 //==================================================================================
00097 //
00098 pqxxSqlDriver::~pqxxSqlDriver()
00099 {
00100 //  delete d;
00101 }
00102 
00103 //==================================================================================
00104 //
00105 KexiDB::Connection*
00106 pqxxSqlDriver::drv_createConnection( ConnectionData &conn_data )
00107 {
00108     return new pqxxSqlConnection( this, conn_data );
00109 }
00110 
00111 //==================================================================================
00112 //
00113 bool pqxxSqlDriver::isSystemObjectName( const QString& n ) const
00114 {
00115     return Driver::isSystemObjectName(n);
00116 }
00117 
00118 //==================================================================================
00119 //
00120 bool pqxxSqlDriver::drv_isSystemFieldName( const QString& ) const
00121 {
00122     return false;
00123 }
00124 
00125 //==================================================================================
00126 //
00127 bool pqxxSqlDriver::isSystemDatabaseName( const QString& n ) const
00128 {
00129     return n.lower()=="template1" || n.lower()=="template0";
00130 }
00131 
00132 //==================================================================================
00133 //
00134 QString pqxxSqlDriver::escapeString( const QString& str) const
00135 {
00136     return QString(pqxx::Quote(str.ascii()).c_str());
00137 }
00138 
00139 //==================================================================================
00140 //
00141 QCString pqxxSqlDriver::escapeString( const QCString& str) const
00142 {
00143     return QCString(pqxx::Quote(QString(str).ascii()).c_str());
00144 }
00145 
00146 //==================================================================================
00147 //
00148 QString pqxxSqlDriver::drv_escapeIdentifier( const QString& str) const {
00149     return QString(str).replace( '"', "\"\"" );
00150 }
00151 
00152 //==================================================================================
00153 //
00154 QCString pqxxSqlDriver::drv_escapeIdentifier( const QCString& str) const {
00155     return QCString(str).replace( '"', "\"\"" );
00156 }
00157 
00158 //==================================================================================
00159 //
00160 QString pqxxSqlDriver::escapeBLOB(const QByteArray& array) const
00161 {
00162     return KexiDB::escapeBLOB(array, KexiDB::BLOBEscapeOctal);
00163 }
00164 
00165 QString pqxxSqlDriver::valueToSQL( uint ftype, const QVariant& v ) const
00166 {
00167     if (ftype==Field::Boolean) {
00168         // use SQL compliant TRUE or FALSE as described here
00169         // http://www.postgresql.org/docs/8.0/interactive/datatype-boolean.html
00170         // 1 or 0 does not work
00171         return v.toInt()==0 ? QString::fromLatin1("FALSE") : QString::fromLatin1("TRUE");
00172     }
00173     return Driver::valueToSQL(ftype, v);
00174 }
00175 
00176 
00177 #include "pqxxdriver.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys