00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef KEXIDB_UTILS_H
00022 #define KEXIDB_UTILS_H
00023
00024 #include <qvaluelist.h>
00025 #include <qvariant.h>
00026
00027 #include <kexidb/connection.h>
00028 #include <kexidb/driver.h>
00029
00030 class QDomNode;
00031 class QDomElement;
00032 class QDomDocument;
00033
00034 namespace KexiDB
00035 {
00037 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, TableSchema *table,
00038 const QString &keyname, const QString &keyval)
00039 {
00040 return table!=0 && conn.executeSQL("DELETE FROM " + table->name() + " WHERE "
00041 + keyname + "=" + conn.driver()->valueToSQL( Field::Text, QVariant(keyval) ));
00042 }
00043
00044 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, const QString &tableName,
00045 const QString &keyname, const QString &keyval)
00046 {
00047 return conn.executeSQL("DELETE FROM " + tableName + " WHERE "
00048 + keyname + "=" + conn.driver()->valueToSQL( Field::Text, QVariant(keyval) ));
00049 }
00050
00051 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, TableSchema *table,
00052 const QString &keyname, int keyval)
00053 {
00054 return table!=0 && conn.executeSQL("DELETE FROM " + table->name() + " WHERE "
00055 + keyname + "=" + conn.driver()->valueToSQL( Field::Integer, QVariant(keyval) ));
00056 }
00057
00058 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, const QString &tableName,
00059 const QString &keyname, int keyval)
00060 {
00061 return conn.executeSQL("DELETE FROM " + tableName + " WHERE "
00062 + keyname + "=" + conn.driver()->valueToSQL( Field::Integer, QVariant(keyval) ));
00063 }
00064
00066 inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, const QString &tableName,
00067 const QString &keyname1, Field::Type keytype1, const QVariant& keyval1,
00068 const QString &keyname2, Field::Type keytype2, const QVariant& keyval2)
00069 {
00070 return conn.executeSQL("DELETE FROM " + tableName + " WHERE "
00071 + keyname1 + "=" + conn.driver()->valueToSQL( keytype1, keyval1 )
00072 + " AND " + keyname2 + "=" + conn.driver()->valueToSQL( keytype2, keyval2 ));
00073 }
00074
00075 inline KEXI_DB_EXPORT bool replaceRow(Connection &conn, TableSchema *table,
00076 const QString &keyname, const QString &keyval, const QString &valname, QVariant val, int ftype)
00077 {
00078 if (!table || !KexiDB::deleteRow(conn, table, keyname, keyval))
00079 return false;
00080 return conn.executeSQL("INSERT INTO " + table->name()
00081 + " (" + keyname + "," + valname + ") VALUES ("
00082 + conn.driver()->valueToSQL( Field::Text, QVariant(keyval) ) + ","
00083 + conn.driver()->valueToSQL( ftype, val) + ")");
00084 }
00085
00086 typedef QValueList<uint> TypeGroupList;
00087
00089 KEXI_DB_EXPORT const TypeGroupList typesForGroup(Field::TypeGroup typeGroup);
00090
00092 KEXI_DB_EXPORT QStringList typeNamesForGroup(Field::TypeGroup typeGroup);
00093
00095 KEXI_DB_EXPORT QStringList typeStringsForGroup(Field::TypeGroup typeGroup);
00096
00101 KEXI_DB_EXPORT Field::Type defaultTypeForGroup(Field::TypeGroup typeGroup);
00102
00107
00108 KEXI_DB_EXPORT QString simplifiedTypeName(const Field& field);
00109
00112 inline bool isEmptyValue(Field *f, const QVariant &v) {
00113 if (f->hasEmptyProperty() && v.toString().isEmpty() && !v.toString().isNull())
00114 return true;
00115 return v.isNull();
00116 }
00117
00124 KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, QString& msg, QString &details);
00125
00128 KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, QString& msg);
00129
00131 KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, ResultInfo *result);
00132
00137 inline KEXI_DB_EXPORT QString sqlWhere(Driver *drv, Field::Type t,
00138 const QString fieldName, const QVariant value)
00139 {
00140 if (value.isNull())
00141 return fieldName + " is NULL";
00142 return fieldName + "=" + drv->valueToSQL( t, value );
00143 }
00144
00147 KEXI_DB_EXPORT int idForObjectName( Connection &conn, const QString& objName, int objType );
00148
00150 class KEXI_DB_EXPORT TableOrQuerySchema {
00151 public:
00157 TableOrQuerySchema(Connection *conn, const QCString& name);
00158
00164 TableOrQuerySchema(Connection *conn, const QCString& name, bool table);
00165
00170 TableOrQuerySchema(FieldList &tableOrQuery);
00171
00176 TableOrQuerySchema(Connection *conn, int id);
00177
00180 TableOrQuerySchema(TableSchema* table);
00181
00184 TableOrQuerySchema(QuerySchema* query);
00185
00187 QuerySchema* query() const { return m_query; }
00188
00190 TableSchema* table() const { return m_table; }
00191
00193 QCString name() const;
00194
00196 QString captionOrName() const;
00197
00199 uint fieldCount() const;
00200
00202 const QueryColumnInfo::Vector columns(bool unique = false);
00203
00206 Field* field(const QString& name);
00207
00210 QueryColumnInfo* columnInfo(const QString& name);
00211
00213 Connection* connection() const;
00214
00216 QString debugString();
00217
00219 void debug();
00220
00221 protected:
00222 QCString m_name;
00223
00224 TableSchema* m_table;
00225 QuerySchema* m_query;
00226 };
00227
00229
00234 int rowCount(Connection &conn, const QString& sql);
00235
00237
00243 KEXI_DB_EXPORT int rowCount(const TableSchema& tableSchema);
00244
00246
00247 KEXI_DB_EXPORT int rowCount(QuerySchema& querySchema);
00248
00250
00251 KEXI_DB_EXPORT int rowCount(TableOrQuerySchema& tableOrQuery);
00252
00256 KEXI_DB_EXPORT int fieldCount(TableOrQuerySchema& tableOrQuery);
00257
00262 KEXI_DB_EXPORT void connectionTestDialog(QWidget* parent, const ConnectionData& data,
00263 MessageHandler& msgHandler);
00264
00266 KEXI_DB_EXPORT QMap<QString,QString> toMap( const ConnectionData& data );
00267
00269 KEXI_DB_EXPORT void fromMap( const QMap<QString,QString>& map, ConnectionData& data );
00270
00272 enum SplitToTableAndFieldPartsOptions {
00273 FailIfNoTableOrFieldName = 0,
00274 SetFieldNameIfNoTableName = 1
00275 };
00276
00292 KEXI_DB_EXPORT bool splitToTableAndFieldParts(const QString& string,
00293 QString& tableName, QString& fieldName,
00294 SplitToTableAndFieldPartsOptions option = FailIfNoTableOrFieldName);
00295
00297 KEXI_DB_EXPORT bool supportsVisibleDecimalPlacesProperty(Field::Type type);
00298
00310 KEXI_DB_EXPORT QString formatNumberForVisibleDecimalPlaces(double value, int decimalPlaces);
00311
00313 KEXI_DB_EXPORT bool isBuiltinTableFieldProperty( const QCString& propertyName );
00314
00316 KEXI_DB_EXPORT bool isExtendedTableFieldProperty( const QCString& propertyName );
00317
00321 KEXI_DB_EXPORT Field::Type intToFieldType( int type );
00322
00328 KEXI_DB_EXPORT bool setFieldProperties( Field& field, const QMap<QCString, QVariant>& values );
00329
00337 KEXI_DB_EXPORT bool setFieldProperty(Field& field, const QCString& propertyName,
00338 const QVariant& value);
00339
00344 KEXI_DB_EXPORT QVariant loadPropertyValueFromDom( const QDomNode& node );
00345
00347 KEXI_DB_EXPORT int loadIntPropertyValueFromDom( const QDomNode& node, bool* ok );
00348
00350 KEXI_DB_EXPORT QString loadStringPropertyValueFromDom( const QDomNode& node, bool* ok );
00351
00359 KEXI_DB_EXPORT QDomElement saveNumberElementToDom(QDomDocument& doc, QDomElement& parentEl,
00360 const QString& elementName, int value);
00361
00366 KEXI_DB_EXPORT QDomElement saveBooleanElementToDom(QDomDocument& doc, QDomElement& parentEl,
00367 const QString& elementName, bool value);
00368
00376 KEXI_DB_EXPORT QVariant emptyValueForType( Field::Type type );
00377
00386 KEXI_DB_EXPORT QVariant notEmptyValueForType( Field::Type type );
00387
00389 enum BLOBEscapingType {
00390 BLOBEscapeXHex = 1,
00391 BLOBEscape0xHex,
00392 BLOBEscapeHex,
00393 BLOBEscapeOctal
00394
00395
00396 };
00397
00399
00404 KEXI_DB_EXPORT QString escapeBLOB(const QByteArray& array, BLOBEscapingType type);
00405
00410 KEXI_DB_EXPORT QByteArray pgsqlByteaToByteArray(const char* data, int length);
00411
00418 KEXI_DB_EXPORT QString variantToString( const QVariant& v );
00419
00423 KEXI_DB_EXPORT QVariant stringToVariant( const QString& s, QVariant::Type type, bool &ok );
00424
00428 KEXI_DB_EXPORT bool isDefaultValueAllowed( Field* field );
00429
00435
00436 KEXI_DB_EXPORT void getLimitsForType(Field::Type type, int &minValue, int &maxValue);
00437
00439 KEXI_DB_EXPORT void debugRowData(const RowData& rowData);
00440
00443 KEXI_DB_EXPORT Field::Type maximumForIntegerTypes(Field::Type t1, Field::Type t2);
00444
00447 inline QVariant cstringToVariant(const char* data, KexiDB::Field* f, int length = -1)
00448 {
00449 if (!data)
00450 return QVariant();
00451
00452
00453 if (!f || f->isTextType())
00454 return QString::fromUtf8(data, length);
00455 if (f->isIntegerType()) {
00456 if (f->type()==KexiDB::Field::BigInteger)
00457 return QVariant( QString::fromLatin1(data, length).toLongLong() );
00458 return QVariant( QString::fromLatin1(data, length).toInt() );
00459 }
00460 if (f->isFPNumericType())
00461 return QString::fromLatin1(data, length).toDouble();
00462 if (f->type()==KexiDB::Field::BLOB) {
00463 QByteArray ba;
00464 ba.duplicate(data, length);
00465 return ba;
00466 }
00467
00469 QVariant result(QString::fromUtf8(data, length));
00470 if (!result.cast( KexiDB::Field::variantType(f->type()) ))
00471 return QVariant();
00472 return result;
00473 }
00474 }
00475
00476 #endif