kexi

queryschema.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-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 #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 "fieldlist.h"
00029 #include "schemadata.h"
00030 #include "tableschema.h"
00031 #include "relationship.h"
00032 
00033 namespace KexiDB {
00034 
00035 class Connection;
00036 class QueryAsterisk;
00037 class QuerySchemaPrivate;
00038 class QuerySchemaParameter;
00039 typedef QValueList<QuerySchemaParameter> QuerySchemaParameterList;
00040 
00042 
00048 class KEXI_DB_EXPORT QueryColumnInfo
00049 {
00050     public:
00051         typedef QPtrVector<QueryColumnInfo> Vector;
00052         typedef QPtrList<QueryColumnInfo> List;
00053         typedef QPtrListIterator<QueryColumnInfo> ListIterator;
00054 
00055         QueryColumnInfo(Field *f, QCString _alias, bool _visible);
00056         ~QueryColumnInfo();
00057 
00059         inline QCString aliasOrName() const { 
00060             return alias.isEmpty() ? field->name().latin1() : (const char*)alias; 
00061         }
00062 
00065         inline QString captionOrAliasOrName() const {
00066             return field->caption().isEmpty() ? QString(aliasOrName()) : field->caption(); }
00067 
00068         Field *field;
00069         QCString alias;
00070 
00075         inline int indexForVisibleLookupValue() const { return m_indexForVisibleLookupValue; }
00076 
00078         inline void setIndexForVisibleLookupValue(int index) { m_indexForVisibleLookupValue = index; }
00079 
00081         QString debugString() const;
00082 
00084         bool visible : 1;
00085 
00086     private:
00089         int m_indexForVisibleLookupValue;
00090 };
00091 
00093 
00094 class KEXI_DB_EXPORT OrderByColumn
00095 {
00096     public:
00097         typedef QValueListConstIterator<OrderByColumn> ListConstIterator;
00098         OrderByColumn();
00099         OrderByColumn(QueryColumnInfo& column, bool ascending = true, int pos = -1);
00100         
00103         OrderByColumn(Field& field, bool ascending = true);
00104 
00105         ~OrderByColumn();
00106 
00108         inline QueryColumnInfo* column() const { return m_column; }
00109 
00113         inline int position() const { return m_pos; }
00114 
00116         inline Field *field() const { return m_field; }
00117 
00119         inline bool ascending() const { return m_ascending; }
00120 
00122         bool operator== ( const OrderByColumn& col ) const 
00123             { return m_column==col.m_column && m_field==col.m_field 
00124                 && m_ascending==col.m_ascending; }
00125 
00127         QString debugString() const;
00128 
00132         QString toSQLString(bool includeTableName = true) const;
00133 
00134     protected:
00136         QueryColumnInfo* m_column; 
00137         int m_pos; 
00138 
00139 
00140         Field* m_field; 
00141 
00143         bool m_ascending : 1;
00144 };
00145 
00147 typedef QValueList<OrderByColumn> OrderByColumnListBase;
00148 
00150 class KEXI_DB_EXPORT OrderByColumnList : protected OrderByColumnListBase
00151 {
00152     public:
00154         OrderByColumnList();
00155 
00156         ~OrderByColumnList();
00157 
00162         bool appendFields(QuerySchema& querySchema,
00163             const QString& field1, bool ascending1 = true, 
00164             const QString& field2 = QString::null, bool ascending2 = true, 
00165             const QString& field3 = QString::null, bool ascending3 = true, 
00166             const QString& field4 = QString::null, bool ascending4 = true, 
00167             const QString& field5 = QString::null, bool ascending5 = true);
00168 
00170         void appendColumn(QueryColumnInfo& columnInfo, bool ascending = true);
00171         
00175         void appendField(Field& field, bool ascending = true);
00176 
00180         bool appendField(QuerySchema& querySchema, const QString& fieldName, 
00181             bool ascending = true);
00182 
00185         bool appendColumn(QuerySchema& querySchema, bool ascending = true, int pos = -1);
00186 
00188         void appendColumn(const OrderByColumn& column);
00189         
00191         bool isEmpty() const { return OrderByColumnListBase::isEmpty(); }
00192         
00194         uint count() const { return OrderByColumnListBase::count(); }
00195 
00197         void clear() { OrderByColumnListBase::clear(); }
00198 
00199         const_iterator constBegin () const { return OrderByColumnListBase::constBegin(); }
00200         const_iterator constEnd () const { return OrderByColumnListBase::constEnd(); }
00201 
00203         QString debugString() const;
00204         
00208         QString toSQLString(bool includeTableNames = true) const;
00209 };
00210 
00212 
00215 class KEXI_DB_EXPORT QuerySchema : public FieldList, public SchemaData
00216 {
00217     public:
00219         QuerySchema();
00220 
00235         QuerySchema(TableSchema* tableSchema);
00236         
00237         virtual ~QuerySchema();
00238         
00253         virtual FieldList& insertField(uint position, Field *field);
00254 
00255         /* Like above method, but you can also set column's visibility. 
00256          New column isn't bound explicity to any table.
00257         */
00258         FieldList& insertField(uint position, Field *field, bool visible);
00259 
00260         /* Like above method, but you can also explicity bound the new column
00261          to specific position on tables list. 
00262          If \a visible is true (the default), the field will be visible. 
00263          If bindToTable==-1, no particular table should be bound.
00264          @see tableBoundToColumn(uint columnPosition) */
00265         FieldList& insertField(uint position, Field *field, 
00266             int bindToTable, bool visible = true);
00267         
00271         FieldList& addField(Field* field, bool visible = true);
00272         
00279         FieldList& addField(Field* field, int bindToTable, 
00280             bool visible = true);
00281 
00283         virtual void removeField(Field *field);
00284 
00287         FieldList& addExpression(BaseExpr* expr, bool visible = true);
00288 
00291         bool isColumnVisible(uint position) const;
00292 
00294         void setColumnVisible(uint position, bool v);
00295 
00297         FieldList& addAsterisk(QueryAsterisk *asterisk, bool visible = true);
00298 
00304         virtual void clear();
00305 
00307         virtual QString debugString();
00308 
00311         Connection* connection() const;
00312         
00324         TableSchema* masterTable() const;
00325 
00330         void setMasterTable(TableSchema *table);
00331         
00335         TableSchema::List* tables() const;
00336 
00341         void addTable(TableSchema *table, const QCString& alias = QCString());
00342 
00347         void removeTable(TableSchema *table);
00348 
00350         TableSchema* table(const QString& tableName) const;
00351         
00353         bool contains(TableSchema *table) const;
00354 
00373         Field* findTableField(const QString &tableOrTableAndFieldName) const;
00374 
00381         QCString columnAlias(uint position) const;
00382         
00388         bool hasColumnAlias(uint position) const;
00389 
00392         void setColumnAlias(uint position, const QCString& alias);
00393 
00415         int tableBoundToColumn(uint columnPosition) const;
00416 
00420         QCString tableAlias(uint position) const;
00421         
00433         int tablePositionForAlias(const QCString& name) const;
00434 
00439         int tablePosition(const QString& tableName) const;
00440         
00447         QValueList<int> tablePositions(const QString& tableName) const;
00448 
00454         bool hasTableAlias(uint position) const;
00455 
00458         int columnPositionForAlias(const QCString& name) const;
00459 
00464         void setTableAlias(uint position, const QCString& alias);
00465 
00467         Relationship::List* relationships() const;
00468 
00476         Relationship* addRelationship( Field *field1, Field *field2 );
00477 
00479         Field::List* asterisks() const;
00480 
00505         virtual Field* field(const QString& name, bool expanded = true);
00506 
00508         inline Field* field(uint id) { return FieldList::field(id); }
00509 
00522         QueryColumnInfo* columnInfo(const QString& identifier, bool expanded = true);
00523 
00525         enum FieldsExpandedOptions {
00526             Default,                   
00527             Unique,                    
00528             WithInternalFields,        
00529             WithInternalFieldsAndRowID 
00530 
00531         };
00532 
00574         QueryColumnInfo::Vector fieldsExpanded(FieldsExpandedOptions options = Default);
00575 
00577         QueryColumnInfo::Vector internalFields();
00578 
00583         QueryColumnInfo* expandedOrInternalField(uint index);
00584 
00586         enum ColumnsOrderOptions {
00587             UnexpandedList,                 
00588             UnexpandedListWithoutAsterisks, 
00589             ExpandedList                    
00590         };
00591 
00621         QMap<QueryColumnInfo*,int> columnsOrder(ColumnsOrderOptions options = ExpandedList);
00622 
00645         QValueVector<int> pkeyFieldsOrder();
00646 
00665         uint pkeyFieldsCount();
00666 
00671         QueryColumnInfo::List* autoIncrementFields();
00672 
00674         QString statement() const;
00675 
00677         void setStatement(const QString &s);
00678 
00689         static QString sqlColumnsList(QueryColumnInfo::List* infolist, Driver *driver);
00690 
00693         QString autoIncrementSQLFieldsList(Driver *driver);
00694 
00698         void setWhereExpression(BaseExpr *expr);
00699 
00701         BaseExpr *whereExpression() const;
00702     
00706         void addToWhereExpression(KexiDB::Field *field, const QVariant& value, int relation = '=');
00707 
00720         void setOrderByColumnList(const OrderByColumnList& list);
00721 
00724         OrderByColumnList& orderByColumnList() const;
00725 
00728         QuerySchemaParameterList parameters();
00729 
00730     protected:
00731         void init();
00732 
00733         void computeFieldsExpanded();
00734 
00735         QuerySchemaPrivate *d;
00736         
00737     friend class Connection;
00738     friend class QuerySchemaPrivate;
00739 };
00740 
00742 
00764 class KEXI_DB_EXPORT QueryAsterisk : protected Field
00765 {
00766     public:
00776         QueryAsterisk( QuerySchema *query, TableSchema *table = 0 );
00777         
00778         virtual ~QueryAsterisk();
00779 
00781         QuerySchema *query() const { return static_cast<QuerySchema*>(m_parent); }
00782 
00786         virtual TableSchema* table() const { return m_table; }
00787 
00790         virtual void setTable(TableSchema *table);
00791 
00794         bool isSingleTableAsterisk() const { return m_table!=NULL; }
00795         
00798         bool isAllTableAsterisk() const { return m_table==NULL; }
00799         
00801         virtual QString debugString();
00802 
00803     protected:
00805         TableSchema* m_table;
00806 
00807     friend class QuerySchema;
00808 };
00809 
00810 } //namespace KexiDB
00811 
00812 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys