kexi

queryschema.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-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 #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, const QCString& _alias, bool _visible, QueryColumnInfo *foreignColumn = 0);
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         QueryColumnInfo *foreignColumn() const { return m_foreignColumn; }
00082 
00084         QString debugString() const;
00085 
00087         bool visible : 1;
00088 
00089     private:
00092         int m_indexForVisibleLookupValue;
00093 
00095         QueryColumnInfo *m_foreignColumn;
00096 };
00097 
00099 
00100 class KEXI_DB_EXPORT OrderByColumn
00101 {
00102     public:
00103         typedef QValueListConstIterator<OrderByColumn> ListConstIterator;
00104         OrderByColumn();
00105         OrderByColumn(QueryColumnInfo& column, bool ascending = true, int pos = -1);
00106         
00109         OrderByColumn(Field& field, bool ascending = true);
00110 
00111         ~OrderByColumn();
00112 
00114         inline QueryColumnInfo* column() const { return m_column; }
00115 
00119         inline int position() const { return m_pos; }
00120 
00122         inline Field *field() const { return m_field; }
00123 
00125         inline bool ascending() const { return m_ascending; }
00126 
00128         bool operator== ( const OrderByColumn& col ) const 
00129             { return m_column==col.m_column && m_field==col.m_field 
00130                 && m_ascending==col.m_ascending; }
00131 
00133         QString debugString() const;
00134 
00139         QString toSQLString(bool includeTableName = true, 
00140             Driver *drv = 0, int identifierEscaping = Driver::EscapeDriver|Driver::EscapeAsNecessary) const;
00141 
00142     protected:
00144         QueryColumnInfo* m_column; 
00145         int m_pos; 
00146 
00147 
00148         Field* m_field; 
00149 
00151         bool m_ascending : 1;
00152 };
00153 
00155 typedef QValueList<OrderByColumn> OrderByColumnListBase;
00156 
00158 class KEXI_DB_EXPORT OrderByColumnList : protected OrderByColumnListBase
00159 {
00160     public:
00162         OrderByColumnList();
00163 
00164         ~OrderByColumnList();
00165 
00170         bool appendFields(QuerySchema& querySchema,
00171             const QString& field1, bool ascending1 = true, 
00172             const QString& field2 = QString::null, bool ascending2 = true, 
00173             const QString& field3 = QString::null, bool ascending3 = true, 
00174             const QString& field4 = QString::null, bool ascending4 = true, 
00175             const QString& field5 = QString::null, bool ascending5 = true);
00176 
00178         void appendColumn(QueryColumnInfo& columnInfo, bool ascending = true);
00179         
00183         void appendField(Field& field, bool ascending = true);
00184 
00188         bool appendField(QuerySchema& querySchema, const QString& fieldName, 
00189             bool ascending = true);
00190 
00193         bool appendColumn(QuerySchema& querySchema, bool ascending = true, int pos = -1);
00194 
00196         void appendColumn(const OrderByColumn& column);
00197         
00199         bool isEmpty() const { return OrderByColumnListBase::isEmpty(); }
00200         
00202         uint count() const { return OrderByColumnListBase::count(); }
00203 
00205         void clear() { OrderByColumnListBase::clear(); }
00206 
00207         const_iterator constBegin () const { return OrderByColumnListBase::constBegin(); }
00208         const_iterator constEnd () const { return OrderByColumnListBase::constEnd(); }
00209 
00211         QString debugString() const;
00212         
00217         QString toSQLString(bool includeTableNames = true, 
00218             Driver *drv = 0, int identifierEscaping = Driver::EscapeDriver|Driver::EscapeAsNecessary) const;
00219 };
00220 
00222 
00225 class KEXI_DB_EXPORT QuerySchema : public FieldList, public SchemaData
00226 {
00227     public:
00229         QuerySchema();
00230 
00245         QuerySchema(TableSchema& tableSchema);
00246 
00249         QuerySchema(const QuerySchema& querySchema);
00250 
00251         virtual ~QuerySchema();
00252         
00267         virtual FieldList& insertField(uint position, Field *field);
00268 
00269         /* Like above method, but you can also set column's visibility. 
00270          New column is not bound explicitly to any table.
00271         */
00272         FieldList& insertField(uint position, Field *field, bool visible);
00273 
00274         /* Like above method, but you can also explicitly bound the new column
00275          to specific position on tables list. 
00276          If \a visible is true (the default), the field will be visible. 
00277          If bindToTable==-1, no particular table should be bound.
00278          @see tableBoundToColumn(uint columnPosition) */
00279         FieldList& insertField(uint position, Field *field, 
00280             int bindToTable, bool visible = true);
00281         
00285         FieldList& addField(Field* field, bool visible = true);
00286         
00293         FieldList& addField(Field* field, int bindToTable, 
00294             bool visible = true);
00295 
00297         virtual void removeField(Field *field);
00298 
00301         FieldList& addExpression(BaseExpr* expr, bool visible = true);
00302 
00305         bool isColumnVisible(uint position) const;
00306 
00308         void setColumnVisible(uint position, bool v);
00309 
00311         FieldList& addAsterisk(QueryAsterisk *asterisk, bool visible = true);
00312 
00318         virtual void clear();
00319 
00321         virtual QString debugString();
00322 
00325         Connection* connection() const;
00326         
00338         TableSchema* masterTable() const;
00339 
00344         void setMasterTable(TableSchema *table);
00345         
00349         TableSchema::List* tables() const;
00350 
00355         void addTable(TableSchema *table, const QCString& alias = QCString());
00356 
00361         void removeTable(TableSchema *table);
00362 
00364         TableSchema* table(const QString& tableName) const;
00365         
00367         bool contains(TableSchema *table) const;
00368 
00387         Field* findTableField(const QString &tableOrTableAndFieldName) const;
00388 
00395         QCString columnAlias(uint position) const;
00396         
00402         bool hasColumnAlias(uint position) const;
00403 
00406         void setColumnAlias(uint position, const QCString& alias);
00407 
00429         int tableBoundToColumn(uint columnPosition) const;
00430 
00434         QCString tableAlias(uint position) const;
00435         
00447         int tablePositionForAlias(const QCString& name) const;
00448 
00453         int tablePosition(const QString& tableName) const;
00454         
00461         QValueList<int> tablePositions(const QString& tableName) const;
00462 
00468         bool hasTableAlias(uint position) const;
00469 
00472         int columnPositionForAlias(const QCString& name) const;
00473 
00478         void setTableAlias(uint position, const QCString& alias);
00479 
00481         Relationship::List* relationships() const;
00482 
00490         Relationship* addRelationship( Field *field1, Field *field2 );
00491 
00493         Field::List* asterisks() const;
00494 
00519         virtual Field* field(const QString& name, bool expanded = true);
00520 
00522         inline Field* field(uint id) { return FieldList::field(id); }
00523 
00536         QueryColumnInfo* columnInfo(const QString& identifier, bool expanded = true);
00537 
00539         enum FieldsExpandedOptions {
00540             Default,                   
00541             Unique,                    
00542             WithInternalFields,        
00543             WithInternalFieldsAndRowID 
00544 
00545         };
00546 
00588         QueryColumnInfo::Vector fieldsExpanded(FieldsExpandedOptions options = Default);
00589 
00591         QueryColumnInfo::Vector internalFields();
00592 
00597         QueryColumnInfo* expandedOrInternalField(uint index);
00598 
00600         enum ColumnsOrderOptions {
00601             UnexpandedList,                 
00602             UnexpandedListWithoutAsterisks, 
00603             ExpandedList                    
00604         };
00605 
00635         QMap<QueryColumnInfo*,int> columnsOrder(ColumnsOrderOptions options = ExpandedList);
00636 
00659         QValueVector<int> pkeyFieldsOrder();
00660 
00679         uint pkeyFieldsCount();
00680 
00685         QueryColumnInfo::List* autoIncrementFields();
00686 
00688         QString statement() const;
00689 
00691         void setStatement(const QString &s);
00692 
00703         static QString sqlColumnsList(QueryColumnInfo::List* infolist, Driver *driver);
00704 
00707         QString autoIncrementSQLFieldsList(Driver *driver);
00708 
00712         void setWhereExpression(BaseExpr *expr);
00713 
00715         BaseExpr *whereExpression() const;
00716     
00720         void addToWhereExpression(KexiDB::Field *field, const QVariant& value, int relation = '=');
00721 
00734         void setOrderByColumnList(const OrderByColumnList& list);
00735 
00738         OrderByColumnList& orderByColumnList() const;
00739 
00742         QuerySchemaParameterList parameters();
00743 
00744     protected:
00745         void init();
00746 
00747         void computeFieldsExpanded();
00748 
00749         QuerySchemaPrivate *d;
00750         
00751     friend class Connection;
00752     friend class QuerySchemaPrivate;
00753 };
00754 
00756 
00778 class KEXI_DB_EXPORT QueryAsterisk : public Field
00779 {
00780     public:
00790         QueryAsterisk( QuerySchema *query, TableSchema *table = 0 );
00791         
00792         virtual ~QueryAsterisk();
00793 
00795         QuerySchema *query() const { return static_cast<QuerySchema*>(m_parent); }
00796 
00800         virtual TableSchema* table() const { return m_table; }
00801 
00804         virtual void setTable(TableSchema *table);
00805 
00807         virtual bool isQueryAsterisk() const  { return true; }
00808 
00811         bool isSingleTableAsterisk() const { return m_table!=NULL; }
00812         
00815         bool isAllTableAsterisk() const { return m_table==NULL; }
00816         
00818         virtual QString debugString() const;
00819 
00820     protected:
00822         virtual Field* copy() const;
00823 
00825         TableSchema* m_table;
00826 
00827     friend class QuerySchema;
00828 };
00829 
00830 } //namespace KexiDB
00831 
00832 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys