kexi

utils.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004-2007 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             uint fieldCount() const;
00193 
00195             const QueryColumnInfo::Vector columns(bool unique = false);
00196 
00199             Field* field(const QString& name);
00200 
00203             QueryColumnInfo* columnInfo(const QString& name);
00204 
00206             Connection* connection() const;
00207 
00209             QString debugString();
00210 
00212             void debug();
00213 
00214         protected:
00215             QCString m_name; 
00216 
00217             TableSchema* m_table;
00218             QuerySchema* m_query;
00219     };
00220 
00222 
00227     int rowCount(Connection &conn, const QString& sql);
00228 
00230 
00236     KEXI_DB_EXPORT int rowCount(const TableSchema& tableSchema);
00237 
00239 
00240     KEXI_DB_EXPORT int rowCount(QuerySchema& querySchema);
00241 
00243 
00244     KEXI_DB_EXPORT int rowCount(TableOrQuerySchema& tableOrQuery);
00245 
00249     KEXI_DB_EXPORT int fieldCount(TableOrQuerySchema& tableOrQuery);
00250 
00255     KEXI_DB_EXPORT void connectionTestDialog(QWidget* parent, const ConnectionData& data, 
00256         MessageHandler& msgHandler);
00257 
00259     KEXI_DB_EXPORT QMap<QString,QString> toMap( const ConnectionData& data );
00260 
00262     KEXI_DB_EXPORT void fromMap( const QMap<QString,QString>& map, ConnectionData& data );
00263 
00265     enum SplitToTableAndFieldPartsOptions {
00266         FailIfNoTableOrFieldName = 0, 
00267         SetFieldNameIfNoTableName = 1 
00268     };
00269 
00285     KEXI_DB_EXPORT bool splitToTableAndFieldParts(const QString& string, 
00286         QString& tableName, QString& fieldName, 
00287         SplitToTableAndFieldPartsOptions option = FailIfNoTableOrFieldName);
00288 
00290     KEXI_DB_EXPORT bool supportsVisibleDecimalPlacesProperty(Field::Type type);
00291 
00303     KEXI_DB_EXPORT QString formatNumberForVisibleDecimalPlaces(double value, int decimalPlaces);
00304 
00306     KEXI_DB_EXPORT bool isBuiltinTableFieldProperty( const QCString& propertyName );
00307 
00309     KEXI_DB_EXPORT bool isExtendedTableFieldProperty( const QCString& propertyName );
00310 
00314     KEXI_DB_EXPORT Field::Type intToFieldType( int type );
00315 
00321     KEXI_DB_EXPORT bool setFieldProperties( Field& field, const QMap<QCString, QVariant>& values );
00322 
00330     KEXI_DB_EXPORT bool setFieldProperty(Field& field, const QCString& propertyName, 
00331         const QVariant& value);
00332 
00337     KEXI_DB_EXPORT QVariant loadPropertyValueFromDom( const QDomNode& node );
00338 
00340     KEXI_DB_EXPORT int loadIntPropertyValueFromDom( const QDomNode& node, bool* ok );
00341 
00343     KEXI_DB_EXPORT QString loadStringPropertyValueFromDom( const QDomNode& node, bool* ok );
00344 
00352     KEXI_DB_EXPORT QDomElement saveNumberElementToDom(QDomDocument& doc, QDomElement& parentEl, 
00353         const QString& elementName, int value);
00354 
00359     KEXI_DB_EXPORT QDomElement saveBooleanElementToDom(QDomDocument& doc, QDomElement& parentEl, 
00360         const QString& elementName, bool value);
00361 
00369     KEXI_DB_EXPORT QVariant emptyValueForType( Field::Type type );
00370 
00379     KEXI_DB_EXPORT QVariant notEmptyValueForType( Field::Type type );
00380 
00382     enum BLOBEscapingType {
00383         BLOBEscapeXHex = 1,        
00384         BLOBEscape0xHex,           
00385         BLOBEscapeHex,              
00386         BLOBEscapeOctal           
00387 
00388 
00389     };
00390 
00392 
00397     KEXI_DB_EXPORT QString escapeBLOB(const QByteArray& array, BLOBEscapingType type);
00398 
00403     KEXI_DB_EXPORT QByteArray pgsqlByteaToByteArray(const char* data, int length);
00404 
00411     KEXI_DB_EXPORT QString variantToString( const QVariant& v );
00412 
00416     KEXI_DB_EXPORT QVariant stringToVariant( const QString& s, QVariant::Type type, bool &ok );
00417 
00421     KEXI_DB_EXPORT bool isDefaultValueAllowed( Field* field );
00422 
00428 
00429     KEXI_DB_EXPORT void getLimitsForType(Field::Type type, int &minValue, int &maxValue);
00430 
00432     KEXI_DB_EXPORT void debugRowData(const RowData& rowData);
00433 
00436     KEXI_DB_EXPORT Field::Type maximumForIntegerTypes(Field::Type t1, Field::Type t2);
00437 
00440     inline QVariant cstringToVariant(const char* data, KexiDB::Field* f, int length = -1)
00441     {
00442         if (!data)
00443             return QVariant();
00444         // from mo st to least frequently used types:
00445 
00446         if (!f || f->isTextType())
00447             return QString::fromUtf8(data, length);
00448         if (f->isIntegerType()) {
00449             if (f->type()==KexiDB::Field::BigInteger)
00450                 return QVariant( QString::fromLatin1(data, length).toLongLong() );
00451             return QVariant( QString::fromLatin1(data, length).toInt() );
00452         }
00453         if (f->isFPNumericType())
00454             return QString::fromLatin1(data, length).toDouble();
00455         if (f->type()==KexiDB::Field::BLOB) {
00456             QByteArray ba;
00457             ba.duplicate(data, length);
00458             return ba;
00459         }
00460         // the default
00462         QVariant result(QString::fromUtf8(data, length));
00463         if (!result.cast( KexiDB::Field::variantType(f->type()) ))
00464             return QVariant();
00465         return result;
00466     }
00467 }
00468 
00469 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys