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 namespace KexiDB
00031 {
00033     inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, TableSchema *table, 
00034         const QString &keyname, const QString &keyval)
00035     {
00036         return table!=0 && conn.executeSQL("DELETE FROM " + table->name() + " WHERE " 
00037             + keyname + "=" + conn.driver()->valueToSQL( Field::Text, QVariant(keyval) ));
00038     }
00039 
00040     inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, const QString &tableName, 
00041         const QString &keyname, const QString &keyval)
00042     {
00043         return conn.executeSQL("DELETE FROM " + tableName + " WHERE " 
00044             + keyname + "=" + conn.driver()->valueToSQL( Field::Text, QVariant(keyval) ));
00045     }
00046 
00047     inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, TableSchema *table, 
00048         const QString &keyname, int keyval)
00049     {
00050         return table!=0 && conn.executeSQL("DELETE FROM " + table->name() + " WHERE " 
00051             + keyname + "=" + conn.driver()->valueToSQL( Field::Integer, QVariant(keyval) ));
00052     }
00053 
00054     inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, const QString &tableName, 
00055         const QString &keyname, int keyval)
00056     {
00057         return conn.executeSQL("DELETE FROM " + tableName + " WHERE " 
00058             + keyname + "=" + conn.driver()->valueToSQL( Field::Integer, QVariant(keyval) ));
00059     }
00060 
00062     inline KEXI_DB_EXPORT bool deleteRow(Connection &conn, const QString &tableName, 
00063         const QString &keyname1, Field::Type keytype1, const QVariant& keyval1, 
00064         const QString &keyname2, Field::Type keytype2, const QVariant& keyval2)
00065     {
00066         return conn.executeSQL("DELETE FROM " + tableName + " WHERE " 
00067             + keyname1 + "=" + conn.driver()->valueToSQL( keytype1, keyval1 )
00068             + " AND " + keyname2 + "=" + conn.driver()->valueToSQL( keytype2, keyval2 ));
00069     }
00070 
00071     inline KEXI_DB_EXPORT bool replaceRow(Connection &conn, TableSchema *table, 
00072         const QString &keyname, const QString &keyval, const QString &valname, QVariant val, int ftype)
00073     {
00074         if (!table || !KexiDB::deleteRow(conn, table, keyname, keyval))
00075             return false;
00076         return conn.executeSQL("INSERT INTO " + table->name() + " (" + keyname + "," + valname + ") VALUES (" + conn.driver()->valueToSQL( Field::Text, QVariant(keyval) ) + "," + conn.driver()->valueToSQL( ftype, val) + ")");
00077     }
00078 
00079     typedef QValueList<uint> TypeGroupList;
00080 
00082     KEXI_DB_EXPORT const TypeGroupList typesForGroup(KexiDB::Field::TypeGroup typeGroup);
00083 
00085     KEXI_DB_EXPORT QStringList typeNamesForGroup(KexiDB::Field::TypeGroup typeGroup);
00086 
00088     KEXI_DB_EXPORT QStringList typeStringsForGroup(KexiDB::Field::TypeGroup typeGroup);
00089 
00094     KEXI_DB_EXPORT KexiDB::Field::Type defaultTypeForGroup(KexiDB::Field::TypeGroup typeGroup);
00095 
00098     inline bool isEmptyValue(KexiDB::Field *f, const QVariant &v) {
00099         if (f->hasEmptyProperty() && v.toString().isEmpty() && !v.toString().isNull())
00100             return true;
00101         return v.isNull();
00102     }
00103 
00110     KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, QString& msg, QString &details);
00111 
00114     KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, QString& msg);
00115 
00117     KEXI_DB_EXPORT void getHTMLErrorMesage(Object* obj, ResultInfo *result);
00118 
00123     inline KEXI_DB_EXPORT QString sqlWhere(KexiDB::Driver *drv, KexiDB::Field::Type t, 
00124         const QString fieldName, const QVariant value)
00125     {
00126         if (value.isNull())
00127             return fieldName + " is NULL";
00128         return fieldName + "=" + drv->valueToSQL( t, value );
00129     }
00130 
00133     KEXI_DB_EXPORT int idForObjectName( Connection &conn, const QString& objName, int objType );
00134 
00136     class KEXI_DB_EXPORT TableOrQuerySchema {
00137         public:
00140             TableOrQuerySchema(Connection *conn, const QCString& name, bool table);
00141 
00144             TableOrQuerySchema(FieldList &tableOrQuery);
00145             
00148             TableOrQuerySchema(Connection *conn, int id);
00149 
00150             TableOrQuerySchema(TableSchema* table);
00151             TableOrQuerySchema(QuerySchema* query);
00152 
00154             QuerySchema* query() const { return m_query; }
00155 
00157             TableSchema* table() const { return m_table; }
00158 
00160             QCString name() const;
00161 
00163             QString captionOrName() const;
00164 
00166             const QueryColumnInfo::Vector columns(bool unique = false);
00167 
00170             Field* field(const QString& name);
00171 
00174             QueryColumnInfo* columnInfo(const QString& name);
00175 
00177             Connection* connection() const;
00178 
00180             QString debugString();
00181 
00183             void debug();
00184 
00185         protected:
00186             QCString m_name; 
00187 
00188             TableSchema* m_table;
00189             QuerySchema* m_query;
00190     };
00191 
00193 
00199     KEXI_DB_EXPORT int rowCount(const TableSchema& tableSchema);
00200 
00202     KEXI_DB_EXPORT int rowCount(QuerySchema& querySchema);
00203 
00205     KEXI_DB_EXPORT int rowCount(TableOrQuerySchema& tableOrQuery);
00206 
00210     KEXI_DB_EXPORT int fieldCount(TableOrQuerySchema& tableOrQuery);
00211 
00216     KEXI_DB_EXPORT void connectionTestDialog(QWidget* parent, const KexiDB::ConnectionData& data, 
00217         KexiDB::MessageHandler& msgHandler);
00218 
00220     KEXI_DB_EXPORT QMap<QString,QString> toMap( const ConnectionData& data );
00221 
00223     KEXI_DB_EXPORT void fromMap( const QMap<QString,QString>& map, ConnectionData& data );
00224 
00226     enum SetFieldNameIfNoTableNameOptions {
00227         FailIfNoTableOrFieldName = 0, 
00228         SetFieldNameIfNoTableName = 1 
00229     };
00230 
00246     KEXI_DB_EXPORT bool splitToTableAndFieldParts(const QString& string, 
00247         QString& tableName, QString& fieldName, 
00248         SetFieldNameIfNoTableNameOptions option = FailIfNoTableOrFieldName);
00249 }
00250 
00251 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys