kexi

utils.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004-2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library 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 library 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 library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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 
00105     inline bool isEmptyValue(Field *f, const QVariant &v) {
00106         if (f->hasEmptyProperty() && v.toString().isEmpty() && !v.toString().isNull())
00107             return true;
00108         return v.isNull();
00109     }
00110 
00117     KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, QString& msg, QString &details);
00118 
00121     KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, QString& msg);
00122 
00124     KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, ResultInfo *result);
00125 
00130     inline KEXI_DB_EXPORT QString sqlWhere(Driver *drv, Field::Type t, 
00131         const QString fieldName, const QVariant value)
00132     {
00133         if (value.isNull())
00134             return fieldName + " is NULL";
00135         return fieldName + "=" + drv->valueToSQL( t, value );
00136     }
00137 
00140     KEXI_DB_EXPORT int idForObjectName( Connection &conn, const QString& objName, int objType );
00141 
00143     class KEXI_DB_EXPORT TableOrQuerySchema {
00144         public:
00150             TableOrQuerySchema(Connection *conn, const QCString& name);
00151 
00157             TableOrQuerySchema(Connection *conn, const QCString& name, bool table);
00158 
00163             TableOrQuerySchema(FieldList &tableOrQuery);
00164             
00169             TableOrQuerySchema(Connection *conn, int id);
00170 
00173             TableOrQuerySchema(TableSchema* table);
00174 
00177             TableOrQuerySchema(QuerySchema* query);
00178 
00180             QuerySchema* query() const { return m_query; }
00181 
00183             TableSchema* table() const { return m_table; }
00184 
00186             QCString name() const;
00187 
00189             QString captionOrName() const;
00190 
00192             const QueryColumnInfo::Vector columns(bool unique = false);
00193 
00196             Field* field(const QString& name);
00197 
00200             QueryColumnInfo* columnInfo(const QString& name);
00201 
00203             Connection* connection() const;
00204 
00206             QString debugString();
00207 
00209             void debug();
00210 
00211         protected:
00212             QCString m_name; 
00213 
00214             TableSchema* m_table;
00215             QuerySchema* m_query;
00216     };
00217 
00219 
00225     KEXI_DB_EXPORT int rowCount(const TableSchema& tableSchema);
00226 
00228     KEXI_DB_EXPORT int rowCount(QuerySchema& querySchema);
00229 
00231     KEXI_DB_EXPORT int rowCount(TableOrQuerySchema& tableOrQuery);
00232 
00236     KEXI_DB_EXPORT int fieldCount(TableOrQuerySchema& tableOrQuery);
00237 
00242     KEXI_DB_EXPORT void connectionTestDialog(QWidget* parent, const ConnectionData& data, 
00243         MessageHandler& msgHandler);
00244 
00246     KEXI_DB_EXPORT QMap<QString,QString> toMap( const ConnectionData& data );
00247 
00249     KEXI_DB_EXPORT void fromMap( const QMap<QString,QString>& map, ConnectionData& data );
00250 
00252     enum SplitToTableAndFieldPartsOptions {
00253         FailIfNoTableOrFieldName = 0, 
00254         SetFieldNameIfNoTableName = 1 
00255     };
00256 
00272     KEXI_DB_EXPORT bool splitToTableAndFieldParts(const QString& string, 
00273         QString& tableName, QString& fieldName, 
00274         SplitToTableAndFieldPartsOptions option = FailIfNoTableOrFieldName);
00275 
00277     KEXI_DB_EXPORT bool supportsVisibleDecimalPlacesProperty(Field::Type type);
00278 
00290     KEXI_DB_EXPORT QString formatNumberForVisibleDecimalPlaces(double value, int decimalPlaces);
00291 
00293     KEXI_DB_EXPORT bool isBuiltinTableFieldProperty( const QCString& propertyName );
00294 
00296     KEXI_DB_EXPORT bool isExtendedTableFieldProperty( const QCString& propertyName );
00297 
00301     KEXI_DB_EXPORT Field::Type intToFieldType( int type );
00302 
00308     KEXI_DB_EXPORT bool setFieldProperties( Field& field, const QMap<QCString, QVariant>& values );
00309 
00317     KEXI_DB_EXPORT bool setFieldProperty(Field& field, const QCString& propertyName, 
00318         const QVariant& value);
00319 
00324     KEXI_DB_EXPORT QVariant loadPropertyValueFromDom( const QDomNode& node );
00325 
00327     KEXI_DB_EXPORT int loadIntPropertyValueFromDom( const QDomNode& node, bool* ok );
00328 
00330     KEXI_DB_EXPORT QString loadStringPropertyValueFromDom( const QDomNode& node, bool* ok );
00331 
00339     QDomElement saveNumberElementToDom(QDomDocument& doc, QDomElement& parentEl, 
00340         const QString& elementName, int value);
00341 
00346     QDomElement saveBooleanElementToDom(QDomDocument& doc, QDomElement& parentEl, 
00347         const QString& elementName, bool value);
00348 
00356     KEXI_DB_EXPORT QVariant emptyValueForType( Field::Type type );
00357 
00366     KEXI_DB_EXPORT QVariant notEmptyValueForType( Field::Type type );
00367 
00369     enum BLOBEscapingType {
00370         BLOBEscapeXHex = 1,        
00371         BLOBEscape0xHex,           
00372         BLOBEscapeHex,              
00373         BLOBEscapeOctal           
00374 
00375 
00376     };
00377 
00379 
00384     KEXI_DB_EXPORT QString escapeBLOB(const QByteArray& array, BLOBEscapingType type);
00385 
00392     KEXI_DB_EXPORT QString variantToString( const QVariant& v );
00393 
00397     KEXI_DB_EXPORT QVariant stringToVariant( const QString& s, QVariant::Type type, bool &ok );
00398 
00402     KEXI_DB_EXPORT bool isDefaultValueAllowed( Field* field );
00403 
00409 
00410     KEXI_DB_EXPORT void getLimitsForType(Field::Type type, int &minValue, int &maxValue);
00411 
00413     KEXI_DB_EXPORT void debugRowData(const RowData& rowData);
00414 
00417     KEXI_DB_EXPORT Field::Type maximumForIntegerTypes(Field::Type t1, Field::Type t2);
00418 }
00419 
00420 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys