kexi

queryschema.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2005 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 #ifndef KEXIDB_QUERY_H
00021 #define KEXIDB_QUERY_H
00022 
00023 #include <qvaluevector.h>
00024 #include <qstring.h>
00025 #include <qmap.h>
00026 #include <qptrlist.h>
00027 
00028 #include <kexidb/fieldlist.h>
00029 #include <kexidb/schemadata.h>
00030 #include <kexidb/tableschema.h>
00031 #include <kexidb/relationship.h>
00032 
00033 namespace KexiDB {
00034 
00035 class Connection;
00036 class QueryAsterisk;
00037 class QuerySchemaPrivate;
00038 
00045 class KEXI_DB_EXPORT QueryColumnInfo
00046 {
00047     public:
00048         typedef QPtrVector<QueryColumnInfo> Vector;
00049         typedef QPtrList<QueryColumnInfo> List;
00050         typedef QPtrListIterator<QueryColumnInfo> ListIterator;
00051 
00052         QueryColumnInfo(Field *f, QCString _alias, bool _visible)
00053          : field(f), alias(_alias), visible(_visible)
00054         {
00055         }
00056         ~QueryColumnInfo()
00057         {
00058         }
00060         inline QCString aliasOrName() const { 
00061             return alias.isEmpty() ? field->name().latin1() : (const char*)alias; 
00062         }
00063 
00066         inline QString captionOrAliasOrName() const {
00067             return field->caption().isEmpty() ? QString(aliasOrName()) : field->caption(); }
00068 
00069         Field *field;
00070         QCString alias;
00071         bool visible : 1;
00072 };
00073 
00077 class KEXI_DB_EXPORT QuerySchema : public FieldList, public SchemaData
00078 {
00079     public:
00081         QuerySchema();
00082 
00097         QuerySchema(TableSchema* tableSchema);
00098         
00099         virtual ~QuerySchema();
00100         
00115         virtual FieldList& insertField(uint position, Field *field);
00116 
00117         /* Like above method, but you can also set column's visibility. 
00118          New column isn't bound explicity to any table.
00119         */
00120         FieldList& insertField(uint position, Field *field, bool visible);
00121 
00122         /* Like above method, but you can also explicity bound the new column
00123          to specific position on tables list. 
00124          If bindToTable==-1, no particular table should be bound.
00125          @see tableBoundToColumn(uint columnPosition) */
00126         FieldList& insertField(uint position, Field *field, 
00127             int bindToTable, bool visible = true);
00128         
00131         FieldList& addField(Field* field, bool visible = true);
00132         
00138         FieldList& addField(Field* field, int bindToTable, 
00139             bool visible = true);
00140 
00142         virtual void removeField(Field *field);
00143 
00146         bool isColumnVisible(uint position) const;
00147 
00149         void setColumnVisible(uint position, bool v);
00150 
00152         FieldList& addAsterisk(QueryAsterisk *asterisk, bool visible = true);
00153 
00159         virtual void clear();
00160 
00162         virtual QString debugString();
00163 
00166         Connection* connection() const;
00167         
00179         TableSchema* masterTable() const;
00180 
00185         void setMasterTable(TableSchema *table);
00186         
00190         TableSchema::List* tables() const;
00191 
00196         void addTable(TableSchema *table, const QCString& alias = QCString());
00197 
00202         void removeTable(TableSchema *table);
00203 
00205         TableSchema* table(const QString& tableName) const;
00206         
00208         bool contains(TableSchema *table) const;
00209 
00224         Field* findTableField(const QString &tableDotFieldName) const;
00225 
00232         QCString columnAlias(uint position) const;
00233         
00239         bool hasColumnAlias(uint position) const;
00240 
00243         void setColumnAlias(uint position, const QCString& alias);
00244 
00266         int tableBoundToColumn(uint columnPosition) const;
00267 
00271         QCString tableAlias(uint position) const;
00272         
00284         int tablePositionForAlias(const QCString& name) const;
00285 
00290         int tablePosition(const QString& tableName) const;
00291         
00298         QValueList<int> tablePositions(const QString& tableName) const;
00299 
00305         bool hasTableAlias(uint position) const;
00306 
00309         int columnPositionForAlias(const QCString& name) const;
00310 
00315         void setTableAlias(uint position, const QCString& alias);
00316 
00318         Relationship::List* relationships() const;
00319 
00327         Relationship* addRelationship( Field *field1, Field *field2 );
00328 
00330         Field::List* asterisks() const;
00331 
00356         virtual Field* field(const QString& name);
00357 
00358         /* Like QuerySchema::field(const QString& name) but returns not only Field
00359          object for \a name but entire QueryColumnInfo object. */
00360         QueryColumnInfo* columnInfo(const QString& name);
00361 
00388         QueryColumnInfo::Vector fieldsExpanded(bool unique = false); //QValueList<bool> *detailedVisibility = 0);
00389 
00395         QMap<QueryColumnInfo*,int> fieldsOrder();
00396 
00419         QValueVector<int> pkeyFieldsOrder();
00420 
00439         uint pkeyFieldsCount();
00440 
00445         QueryColumnInfo::List* autoIncrementFields();
00446 
00448         QString statement() const;
00449 
00451         void setStatement(const QString &s);
00452 
00463         static QString sqlColumnsList(QueryColumnInfo::List* infolist, Driver *driver);
00464 
00467         QString autoIncrementSQLFieldsList(Driver *driver);
00468 
00472         void setWhereExpression(BaseExpr *expr);
00473 
00475         BaseExpr *whereExpression() const;
00476     
00480         void addToWhereExpression(KexiDB::Field *field, const QVariant& value, int relation = '=');
00481 
00487         void setOrderByColumnList(const QStringList& columnNames);
00488 
00490         void setOrderByColumnList(const QString& column1, const QString& column2 = QString::null, 
00491             const QString& column3 = QString::null, const QString& column4 = QString::null, 
00492             const QString& column5 = QString::null);
00493 
00496         QueryColumnInfo::Vector orderByColumnList() const;
00497 
00498     protected:
00499         void init();
00500 
00501         void computeFieldsExpanded();
00502 
00503     //      /*! Automatically retrieves query schema via connection. */
00504 //      QuerySchema(Connection *conn);
00505 
00506         QuerySchemaPrivate *d;
00507         
00508     friend class Connection;
00509     friend class QuerySchemaPrivate;
00510 };
00511 
00536 class KEXI_DB_EXPORT QueryAsterisk : protected Field
00537 {
00538     public:
00548         QueryAsterisk( QuerySchema *query, TableSchema *table = 0 );
00549         
00550         virtual ~QueryAsterisk();
00551 
00553         QuerySchema *query() const { return static_cast<QuerySchema*>(m_parent); }
00554 
00558         virtual TableSchema* table() const { return m_table; }
00559 
00562         virtual void setTable(TableSchema *table);
00563 
00566         bool isSingleTableAsterisk() const { return m_table!=NULL; }
00567         
00570         bool isAllTableAsterisk() const { return m_table==NULL; }
00571         
00573         virtual QString debugString();
00574 
00575     protected:
00577         TableSchema* m_table;
00578 
00579     friend class QuerySchema;
00580 };
00581 
00582 } //namespace KexiDB
00583 
00584 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys