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 
00093         QuerySchema(TableSchema* tableSchema);
00094         
00095         virtual ~QuerySchema();
00096         
00111         virtual FieldList& insertField(uint position, Field *field);
00112 
00113         /* Like above method, but you can also set column's visibility. 
00114          New column isn't bound explicity to any table.
00115         */
00116         FieldList& insertField(uint position, Field *field, bool visible);
00117 
00118         /* Like above method, but you can also explicity bound the new column
00119          to specific position on tables list. 
00120          If bindToTable==-1, no particular table should be bound.
00121          @see tableBoundToColumn(uint columnPosition) */
00122         FieldList& insertField(uint position, Field *field, 
00123             int bindToTable, bool visible = true);
00124         
00127         FieldList& addField(Field* field, bool visible = true);
00128         
00134         FieldList& addField(Field* field, int bindToTable, 
00135             bool visible = true);
00136 
00138         virtual void removeField(Field *field);
00139 
00142         bool isColumnVisible(uint position) const;
00143 
00145         void setColumnVisible(uint position, bool v);
00146 
00148         FieldList& addAsterisk(QueryAsterisk *asterisk, bool visible = true);
00149 
00155         virtual void clear();
00156 
00158         virtual QString debugString();
00159 
00162         Connection* connection() const;
00163         
00175         TableSchema* masterTable() const;
00176 
00181         void setMasterTable(TableSchema *table);
00182         
00186         TableSchema::List* tables() const;
00187 
00192         void addTable(TableSchema *table, const QCString& alias = QCString());
00193 
00198         void removeTable(TableSchema *table);
00199 
00201         TableSchema* table(const QString& tableName) const;
00202         
00204         bool contains(TableSchema *table) const;
00205 
00220         Field* findTableField(const QString &tableDotFieldName) const;
00221 
00228         QCString columnAlias(uint position) const;
00229         
00235         bool hasColumnAlias(uint position) const;
00236 
00239         void setColumnAlias(uint position, const QCString& alias);
00240 
00262         int tableBoundToColumn(uint columnPosition) const;
00263 
00267         QCString tableAlias(uint position) const;
00268         
00280         int tablePositionForAlias(const QCString& name) const;
00281 
00286         int tablePosition(const QString& tableName) const;
00287         
00294         QValueList<int> tablePositions(const QString& tableName) const;
00295 
00301         bool hasTableAlias(uint position) const;
00302 
00305         int columnPositionForAlias(const QCString& name) const;
00306 
00311         void setTableAlias(uint position, const QCString& alias);
00312 
00314         Relationship::List* relationships() const;
00315 
00323         Relationship* addRelationship( Field *field1, Field *field2 );
00324 
00326         Field::List* asterisks() const;
00327 
00352         virtual Field* field(const QString& name);
00353 
00354         /* Like QuerySchema::field(const QString& name) but returns not only Field
00355          object for \a name but entire QueryColumnInfo object. */
00356         QueryColumnInfo* columnInfo(const QString& name);
00357 
00384         QueryColumnInfo::Vector fieldsExpanded(bool unique = false); //QValueList<bool> *detailedVisibility = 0);
00385 
00391         QMap<QueryColumnInfo*,int> fieldsOrder();
00392 
00415         QValueVector<int> pkeyFieldsOrder();
00416 
00435         uint pkeyFieldsCount();
00436 
00441         QueryColumnInfo::List* autoIncrementFields();
00442 
00444         QString statement() const;
00445 
00447         void setStatement(const QString &s);
00448 
00459         static QString sqlColumnsList(QueryColumnInfo::List* infolist, Driver *driver);
00460 
00463         QString autoIncrementSQLFieldsList(Driver *driver);
00464 
00468         void setWhereExpression(BaseExpr *expr);
00469 
00471         BaseExpr *whereExpression() const;
00472     
00476         void addToWhereExpression(KexiDB::Field *field, const QVariant& value, int relation = '=');
00477 
00483         void setOrderByColumnList(const QStringList& columnNames);
00484 
00486         void setOrderByColumnList(const QString& column1, const QString& column2 = QString::null, 
00487             const QString& column3 = QString::null, const QString& column4 = QString::null, 
00488             const QString& column5 = QString::null);
00489 
00492         QueryColumnInfo::Vector orderByColumnList() const;
00493 
00494     protected:
00495         void init();
00496 
00497         void computeFieldsExpanded();
00498 
00499     //      /*! Automatically retrieves query schema via connection. */
00500 //      QuerySchema(Connection *conn);
00501 
00502         QuerySchemaPrivate *d;
00503         
00504     friend class Connection;
00505     friend class QuerySchemaPrivate;
00506 };
00507 
00532 class KEXI_DB_EXPORT QueryAsterisk : protected Field
00533 {
00534     public:
00544         QueryAsterisk( QuerySchema *query, TableSchema *table = 0 );
00545         
00546         virtual ~QueryAsterisk();
00547 
00549         QuerySchema *query() const { return static_cast<QuerySchema*>(m_parent); }
00550 
00554         virtual TableSchema* table() const { return m_table; }
00555 
00558         virtual void setTable(TableSchema *table);
00559 
00562         bool isSingleTableAsterisk() const { return m_table!=NULL; }
00563         
00566         bool isAllTableAsterisk() const { return m_table==NULL; }
00567         
00569         virtual QString debugString();
00570 
00571     protected:
00573         TableSchema* m_table;
00574 
00575     friend class QuerySchema;
00576 };
00577 
00578 } //namespace KexiDB
00579 
00580 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys