kexi

kexidb/utils.h
1/* This file is part of the KDE project
2 Copyright (C) 2004-2007 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20
21#ifndef KEXIDB_UTILS_H
22#define KEXIDB_UTILS_H
23
24#include <tqvaluelist.h>
25#include <tqvariant.h>
26
27#include <kexidb/connection.h>
28#include <kexidb/driver.h>
29
30class TQDomNode;
31class TQDomElement;
32class TQDomDocument;
33
34namespace KexiDB
35{
37 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, TableSchema *table,
38 const TQString &keyname, const TQString &keyval)
39 {
40 return table!=0 && conn.executeSQL("DELETE FROM " + table->name() + " WHERE "
41 + keyname + "=" + conn.driver()->valueToSQL( Field::Text, TQVariant(keyval) ));
42 }
43
44 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, const TQString &tableName,
45 const TQString &keyname, const TQString &keyval)
46 {
47 return conn.executeSQL("DELETE FROM " + tableName + " WHERE "
48 + keyname + "=" + conn.driver()->valueToSQL( Field::Text, TQVariant(keyval) ));
49 }
50
51 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, TableSchema *table,
52 const TQString &keyname, int keyval)
53 {
54 return table!=0 && conn.executeSQL("DELETE FROM " + table->name() + " WHERE "
55 + keyname + "=" + conn.driver()->valueToSQL( Field::Integer, TQVariant(keyval) ));
56 }
57
58 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, const TQString &tableName,
59 const TQString &keyname, int keyval)
60 {
61 return conn.executeSQL("DELETE FROM " + tableName + " WHERE "
62 + keyname + "=" + conn.driver()->valueToSQL( Field::Integer, TQVariant(keyval) ));
63 }
64
66 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, const TQString &tableName,
67 const TQString &keyname1, Field::Type keytype1, const TQVariant& keyval1,
68 const TQString &keyname2, Field::Type keytype2, const TQVariant& keyval2)
69 {
70 return conn.executeSQL("DELETE FROM " + tableName + " WHERE "
71 + keyname1 + "=" + conn.driver()->valueToSQL( keytype1, keyval1 )
72 + " AND " + keyname2 + "=" + conn.driver()->valueToSQL( keytype2, keyval2 ));
73 }
74
75 inline KEXI_DB_EXPORT bool replaceRow(Connection &conn, TableSchema *table,
76 const TQString &keyname, const TQString &keyval, const TQString &valname, TQVariant val, int ftype)
77 {
78 if (!table || !KexiDB::deleteRow(conn, table, keyname, keyval))
79 return false;
80 return conn.executeSQL("INSERT INTO " + table->name()
81 + " (" + keyname + "," + valname + ") VALUES ("
82 + conn.driver()->valueToSQL( Field::Text, TQVariant(keyval) ) + ","
83 + conn.driver()->valueToSQL( ftype, val) + ")");
84 }
85
86 typedef TQValueList<uint> TypeGroupList;
87
89 KEXI_DB_EXPORT const TypeGroupList typesForGroup(Field::TypeGroup typeGroup);
90
92 KEXI_DB_EXPORT TQStringList typeNamesForGroup(Field::TypeGroup typeGroup);
93
95 KEXI_DB_EXPORT TQStringList typeStringsForGroup(Field::TypeGroup typeGroup);
96
101 KEXI_DB_EXPORT Field::Type defaultTypeForGroup(Field::TypeGroup typeGroup);
102
108 KEXI_DB_EXPORT TQString simplifiedTypeName(const Field& field);
109
112 inline bool isEmptyValue(Field *f, const TQVariant &v) {
113 if (f->hasEmptyProperty() && v.toString().isEmpty() && !v.toString().isNull())
114 return true;
115 return v.isNull();
116 }
117
124 KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, TQString& msg, TQString &details);
125
128 KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, TQString& msg);
129
131 KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, ResultInfo *result);
132
137 inline KEXI_DB_EXPORT TQString sqlWhere(Driver *drv, Field::Type t,
138 const TQString fieldName, const TQVariant value)
139 {
140 if (value.isNull())
141 return fieldName + " is NULL";
142 return fieldName + "=" + drv->valueToSQL( t, value );
143 }
144
147 KEXI_DB_EXPORT int idForObjectName( Connection &conn, const TQString& objName, int objType );
148
150 class KEXI_DB_EXPORT TableOrQuerySchema {
151 public:
157 TableOrQuerySchema(Connection *conn, const TQCString& name);
158
164 TableOrQuerySchema(Connection *conn, const TQCString& name, bool table);
165
170 TableOrQuerySchema(FieldList &tableOrQuery);
171
176 TableOrQuerySchema(Connection *conn, int id);
177
181
185
187 QuerySchema* query() const { return m_query; }
188
190 TableSchema* table() const { return m_table; }
191
193 TQCString name() const;
194
196 TQString captionOrName() const;
197
199 uint fieldCount() const;
200
202 const QueryColumnInfo::Vector columns(bool unique = false);
203
206 Field* field(const TQString& name);
207
210 QueryColumnInfo* columnInfo(const TQString& name);
211
213 Connection* connection() const;
214
216 TQString debugString();
217
219 void debug();
220
221 protected:
222 TQCString m_name;
225 QuerySchema* m_query;
226 };
227
229
234 int rowCount(Connection &conn, const TQString& sql);
235
237
243 KEXI_DB_EXPORT int rowCount(const TableSchema& tableSchema);
244
246
247 KEXI_DB_EXPORT int rowCount(QuerySchema& querySchema);
248
250
251 KEXI_DB_EXPORT int rowCount(TableOrQuerySchema& tableOrQuery);
252
256 KEXI_DB_EXPORT int fieldCount(TableOrQuerySchema& tableOrQuery);
257
262 KEXI_DB_EXPORT void connectionTestDialog(TQWidget* parent, const ConnectionData& data,
263 MessageHandler& msgHandler);
264
266 KEXI_DB_EXPORT TQMap<TQString,TQString> toMap( const ConnectionData& data );
267
269 KEXI_DB_EXPORT void fromMap( const TQMap<TQString,TQString>& map, ConnectionData& data );
270
276
292 KEXI_DB_EXPORT bool splitToTableAndFieldParts(const TQString& string,
293 TQString& tableName, TQString& fieldName,
295
297 KEXI_DB_EXPORT bool supportsVisibleDecimalPlacesProperty(Field::Type type);
298
310 KEXI_DB_EXPORT TQString formatNumberForVisibleDecimalPlaces(double value, int decimalPlaces);
311
313 KEXI_DB_EXPORT bool isBuiltinTableFieldProperty( const TQCString& propertyName );
314
316 KEXI_DB_EXPORT bool isExtendedTableFieldProperty( const TQCString& propertyName );
317
321 KEXI_DB_EXPORT Field::Type intToFieldType( int type );
322
328 KEXI_DB_EXPORT bool setFieldProperties( Field& field, const TQMap<TQCString, TQVariant>& values );
329
337 KEXI_DB_EXPORT bool setFieldProperty(Field& field, const TQCString& propertyName,
338 const TQVariant& value);
339
344 KEXI_DB_EXPORT TQVariant loadPropertyValueFromDom( const TQDomNode& node );
345
347 KEXI_DB_EXPORT int loadIntPropertyValueFromDom( const TQDomNode& node, bool* ok );
348
350 KEXI_DB_EXPORT TQString loadStringPropertyValueFromDom( const TQDomNode& node, bool* ok );
351
359 KEXI_DB_EXPORT TQDomElement saveNumberElementToDom(TQDomDocument& doc, TQDomElement& parentEl,
360 const TQString& elementName, int value);
361
366 KEXI_DB_EXPORT TQDomElement saveBooleanElementToDom(TQDomDocument& doc, TQDomElement& parentEl,
367 const TQString& elementName, bool value);
368
376 KEXI_DB_EXPORT TQVariant emptyValueForType( Field::Type type );
377
386 KEXI_DB_EXPORT TQVariant notEmptyValueForType( Field::Type type );
387
397
399
404 KEXI_DB_EXPORT TQString escapeBLOB(const TQByteArray& array, BLOBEscapingType type);
405
410 KEXI_DB_EXPORT TQByteArray pgsqlByteaToByteArray(const char* data, int length);
411
418 KEXI_DB_EXPORT TQString variantToString( const TQVariant& v );
419
423 KEXI_DB_EXPORT TQVariant stringToVariant( const TQString& s, TQVariant::Type type, bool &ok );
424
428 KEXI_DB_EXPORT bool isDefaultValueAllowed( Field* field );
429
436 KEXI_DB_EXPORT void getLimitsForType(Field::Type type, int &minValue, int &maxValue);
437
439 KEXI_DB_EXPORT void debugRowData(const RowData& rowData);
440
444
447 inline TQVariant cstringToVariant(const char* data, KexiDB::Field* f, int length = -1)
448 {
449 if (!data)
450 return TQVariant();
451 // from mo st to least frequently used types:
452
453 if (!f || f->isTextType())
454 return TQString::fromUtf8(data, length);
455 if (f->isIntegerType()) {
457 return TQVariant( TQString::fromLatin1(data, length).toLongLong() );
458 return TQVariant( TQString::fromLatin1(data, length).toInt() );
459 }
460 if (f->isFPNumericType())
461 return TQString::fromLatin1(data, length).toDouble();
462 if (f->type()==KexiDB::Field::BLOB) {
463 TQByteArray ba;
464 ba.duplicate(data, length);
465 return ba;
466 }
467 // the default
469 TQVariant result(TQString::fromUtf8(data, length));
470 if (!result.cast( KexiDB::Field::variantType(f->type()) ))
471 return TQVariant();
472 return result;
473 }
474}
475
476#endif
Database specific connection data, e.g. host, port.
Provides database connection, allowing queries and data modification.
Definition connection.h:62
bool executeSQL(const TQString &statement)
Driver * driver() const
Definition connection.h:78
Generic database abstraction.
Definition driver.h:67
virtual TQString valueToSQL(uint ftype, const TQVariant &v) const
Definition driver.cpp:250
Meta-data for a field.
Definition field.h:73
Type type() const
Definition field.cpp:134
TQVariant::Type variantType() const
Converts field's type to TQVariant equivalent as accurate as possible.
Definition field.h:291
bool hasEmptyProperty() const
Definition field.h:388
bool isFPNumericType() const
Definition field.h:266
bool isIntegerType() const
Definition field.h:259
bool isTextType() const
Definition field.h:280
Helper class that assigns additional information for the column in a query.
Definition queryschema.h:49
KexiDB::QuerySchema provides information about database query.
TableSchema * m_table
and we still want name() and acptionOrName() work.
TQCString m_name
the name is kept here because m_table and m_table can be 0
QuerySchema * query() const
TableSchema * table() const
High-level database connectivity library with database backend drivers.
KEXI_DB_EXPORT TQDomElement saveBooleanElementToDom(TQDomDocument &doc, TQDomElement &parentEl, const TQString &elementName, bool value)
KEXI_DB_EXPORT bool isDefaultValueAllowed(Field *field)
KEXI_DB_EXPORT TQVariant stringToVariant(const TQString &s, TQVariant::Type type, bool &ok)
KEXI_DB_EXPORT void connectionTestDialog(TQWidget *parent, const ConnectionData &data, MessageHandler &msgHandler)
KEXI_DB_EXPORT bool supportsVisibleDecimalPlacesProperty(Field::Type type)
KEXI_DB_EXPORT TQVariant emptyValueForType(Field::Type type)
KEXI_DB_EXPORT TQString simplifiedTypeName(const Field &field)
int rowCount(Connection &conn, const TQString &sql)
KEXI_DB_EXPORT Field::Type maximumForIntegerTypes(Field::Type t1, Field::Type t2)
KEXI_DB_EXPORT TQStringList typeStringsForGroup(Field::TypeGroup typeGroup)
KEXI_DB_EXPORT TQVariant loadPropertyValueFromDom(const TQDomNode &node)
KEXI_DB_EXPORT TQVariant notEmptyValueForType(Field::Type type)
KEXI_DB_EXPORT TQString escapeBLOB(const TQByteArray &array, BLOBEscapingType type)
KEXI_DB_EXPORT bool setFieldProperty(Field &field, const TQCString &propertyName, const TQVariant &value)
bool isEmptyValue(Field *f, const TQVariant &v)
KEXI_DB_EXPORT bool isBuiltinTableFieldProperty(const TQCString &propertyName)
KEXI_DB_EXPORT Field::Type intToFieldType(int type)
KEXI_DB_EXPORT void fromMap(const TQMap< TQString, TQString > &map, ConnectionData &data)
TQVariant cstringToVariant(const char *data, KexiDB::Field *f, int length=-1)
KEXI_DB_EXPORT bool isExtendedTableFieldProperty(const TQCString &propertyName)
TQValueVector< TQVariant > RowData
structure for storing single record with type information
Definition connection.h:47
KEXI_DB_EXPORT TQStringList typeNamesForGroup(Field::TypeGroup typeGroup)
KEXI_DB_EXPORT void debug(const QuerySchemaParameterList &list)
Shows debug information for list.
KEXI_DB_EXPORT const TypeGroupList typesForGroup(Field::TypeGroup typeGroup)
KEXI_DB_EXPORT bool setFieldProperties(Field &field, const TQMap< TQCString, TQVariant > &values)
KEXI_DB_EXPORT TQString loadStringPropertyValueFromDom(const TQDomNode &node, bool *ok)
KEXI_DB_EXPORT TQString sqlWhere(Driver *drv, Field::Type t, const TQString fieldName, const TQVariant value)
KEXI_DB_EXPORT bool deleteRow(Connection &conn, TableSchema *table, const TQString &keyname, const TQString &keyval)
for convenience
KEXI_DB_EXPORT TQMap< TQString, TQString > toMap(const ConnectionData &data)
KEXI_DB_EXPORT int loadIntPropertyValueFromDom(const TQDomNode &node, bool *ok)
KEXI_DB_EXPORT int fieldCount(TableOrQuerySchema &tableOrQuery)
KEXI_DB_EXPORT TQString formatNumberForVisibleDecimalPlaces(double value, int decimalPlaces)
KEXI_DB_EXPORT TQString variantToString(const TQVariant &v)
KEXI_DB_EXPORT TQByteArray pgsqlByteaToByteArray(const char *data, int length)
BLOBEscapingType
Escaping types used in escapeBLOB().
@ BLOBEscapeXHex
escaping like X'1FAD', used by sqlite (hex numbers)
@ BLOBEscapeOctal
escaping like 'zk\000$x', used by pgsql
@ BLOBEscape0xHex
escaping like 0x1FAD, used by mysql (hex numbers)
@ BLOBEscapeHex
escaping like 1FAD without quotes or prefixes
KEXI_DB_EXPORT bool splitToTableAndFieldParts(const TQString &string, TQString &tableName, TQString &fieldName, SplitToTableAndFieldPartsOptions option=FailIfNoTableOrFieldName)
KEXI_DB_EXPORT void getLimitsForType(Field::Type type, int &minValue, int &maxValue)
KEXI_DB_EXPORT int idForObjectName(Connection &conn, const TQString &objName, int objType)
KEXI_DB_EXPORT void debugRowData(const RowData &rowData)
KEXI_DB_EXPORT TQDomElement saveNumberElementToDom(TQDomDocument &doc, TQDomElement &parentEl, const TQString &elementName, int value)
SplitToTableAndFieldPartsOptions
Used in splitToTableAndFieldParts().
@ SetFieldNameIfNoTableName
see splitToTableAndFieldParts()
@ FailIfNoTableOrFieldName
default value for splitToTableAndFieldParts()
KEXI_DB_EXPORT void getHTMLErrorMesage(Object *obj, TQString &msg, TQString &details)
KEXI_DB_EXPORT Field::Type defaultTypeForGroup(Field::TypeGroup typeGroup)