summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/drivers/pqxx/pqxxdriver.cpp
blob: ce66a6e8efa9dda0a75a66c1774129bacdadb8c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* This file is part of the KDE project
   Copyright (C) 2003 Adam Pigg <adam@piggz.co.uk>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <kexidb/connection.h>
#include <kexidb/drivermanager.h>
#include <kexidb/driver_p.h>
#include <kexidb/utils.h>
#include "pqxxdriver.h"
#include "pqxxconnection.h"
#include <string>

#include <kdebug.h>

using namespace KexiDB;

KEXIDB_DRIVER_INFO( pqxxSqlDriver, pqxxsql )

//==================================================================================
//
pqxxSqlDriver::pqxxSqlDriver( TQObject *tqparent, const char *name, const TQStringList &args )
	: Driver( tqparent, name, args )
{
	d->isFileDriver = false;
	d->features = SingleTransactions | CursorForward | CursorBackward;
//! @todo enable this when kexidb supports multiple: d->features = MultipleTransactions | CursorForward | CursorBackward;

	beh->UNSIGNED_TYPE_KEYWORD = "";
	beh->ROW_ID_FIELD_NAME = "oid";
	beh->SPECIAL_AUTO_INCREMENT_DEF = false;
	beh->AUTO_INCREMENT_TYPE = "SERIAL";
	beh->AUTO_INCREMENT_FIELD_OPTION = "";
	beh->AUTO_INCREMENT_PK_FIELD_OPTION = "PRIMARY KEY";
	beh->ALWAYS_AVAILABLE_DATABASE_NAME = "template1";
	beh->TQUOTATION_MARKS_FOR_IDENTIFIER = '"';
	beh->SQL_KEYWORDS = keywords;
	initSQLKeywords(233);

	//predefined properties
	d->properties["client_library_version"] = "";//TODO
	d->properties["default_server_encoding"] = ""; //TODO

	d->typeNames[Field::Byte]="SMALLINT";
	d->typeNames[Field::ShortInteger]="SMALLINT";
	d->typeNames[Field::Integer]="INTEGER";
	d->typeNames[Field::BigInteger]="BIGINT";
	d->typeNames[Field::Boolean]="BOOLEAN";
	d->typeNames[Field::Date]="DATE";
	d->typeNames[Field::DateTime]="TIMESTAMP";
	d->typeNames[Field::Time]="TIME";
	d->typeNames[Field::Float]="REAL";
	d->typeNames[Field::Double]="DOUBLE PRECISION";
	d->typeNames[Field::Text]="CHARACTER VARYING";
	d->typeNames[Field::LongText]="TEXT";
	d->typeNames[Field::BLOB]="BYTEA";
}

//==================================================================================
//Override the default implementation to allow for NUMERIC type natively
TQString pqxxSqlDriver::sqlTypeName(int id_t, int p) const
{ 
	if (id_t==Field::Null)
		return "NULL";
	if (id_t==Field::Float || id_t==Field::Double)
	{
		if (p>0)
		{
			return "NUMERIC";
		}
		else
		{
			return d->typeNames[id_t];
		}
	}
	else
	{
		return d->typeNames[id_t];
	}
}

//==================================================================================
//
pqxxSqlDriver::~pqxxSqlDriver()
{
//	delete d;
}

//==================================================================================
//
KexiDB::Connection*
pqxxSqlDriver::drv_createConnection( ConnectionData &conn_data )
{
	return new pqxxSqlConnection( this, conn_data );
}

//==================================================================================
//
bool pqxxSqlDriver::isSystemObjectName( const TQString& n ) const
{
	return Driver::isSystemObjectName(n);
}

//==================================================================================
//
bool pqxxSqlDriver::drv_isSystemFieldName( const TQString& ) const
{
	return false;
}

//==================================================================================
//
bool pqxxSqlDriver::isSystemDatabaseName( const TQString& n ) const
{
	return n.lower()=="template1" || n.lower()=="template0";
}

//==================================================================================
//
TQString pqxxSqlDriver::escapeString( const TQString& str) const
{
    return TQString::tqfromLatin1("'")
    	+ TQString::fromAscii( pqxx::sqlesc(std::string(str.utf8())).c_str() )
    	+ TQString::tqfromLatin1("'");
}

//==================================================================================
//
TQCString pqxxSqlDriver::escapeString( const TQCString& str) const
{
    return TQCString("'")
    	+ TQCString( pqxx::sqlesc(TQString(str).ascii()).c_str() )
    	+ TQCString("'");
}

//==================================================================================
//
TQString pqxxSqlDriver::drv_escapeIdentifier( const TQString& str) const {
	return TQString(str).replace( '"', "\"\"" );
}

//==================================================================================
//
TQCString pqxxSqlDriver::drv_escapeIdentifier( const TQCString& str) const {
	return TQCString(str).replace( '"', "\"\"" );
}

//==================================================================================
//
TQString pqxxSqlDriver::escapeBLOB(const TQByteArray& array) const
{
	return KexiDB::escapeBLOB(array, KexiDB::BLOBEscapeOctal);
}

TQString pqxxSqlDriver::valueToSQL( uint ftype, const TQVariant& v ) const
{
	if (ftype==Field::Boolean) {
		// use SQL compliant TRUE or FALSE as described here
		// http://www.postgresql.org/docs/8.0/interactive/datatype-boolean.html
		// 1 or 0 does not work
		return v.toInt()==0 ? TQString::tqfromLatin1("FALSE") : TQString::tqfromLatin1("TRUE");
	}
	return Driver::valueToSQL(ftype, v);
}


#include "pqxxdriver.moc"