File indexing completed on 2024-06-23 11:12:13

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004-2016 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KDB_PARSER_P_H
0021 #define KDB_PARSER_P_H
0022 
0023 #include "KDbParser.h"
0024 #include "KDbSqlTypes.h"
0025 
0026 #include <QList>
0027 #include <QHash>
0028 #include <QCache>
0029 #include <QString>
0030 
0031 class KDbQuerySchema;
0032 class KDbTableSchema;
0033 class KDbConnection;
0034 class KDbExpression;
0035 
0036 //! @internal
0037 class KDbParserPrivate
0038 {
0039 public:
0040     KDbParserPrivate();
0041     ~KDbParserPrivate();
0042 
0043     void reset();
0044 
0045     //! For use by parser's low level C functions
0046     inline static KDbParserPrivate* get(KDbParser *parser) {
0047         return parser->d;
0048     }
0049 
0050     /**
0051      * Sets the type of statement.
0052      */
0053     void setStatementType(KDbParser::StatementType type);
0054 
0055     /**
0056      * Sets @a table schema object.
0057      */
0058     void setTableSchema(KDbTableSchema *table);
0059 
0060     /**
0061      * Sets @a query schema object.
0062      */
0063     //! @todo Add other query types
0064     void setQuerySchema(KDbQuerySchema *query);
0065 
0066     /**
0067      * Sets a error.
0068      */
0069     void setError(const KDbParserError &err);
0070 
0071     /**
0072      * Creates a new query or provides the one provided by KDbParser::parse().
0073      */
0074     Q_REQUIRED_RESULT KDbQuerySchema *createQuery();
0075 
0076     friend class KDbParser;
0077 
0078 private:
0079     KDbParser::StatementType statementType;
0080     KDbTableSchema *table;
0081 
0082     //! Query created as a result of parse() (createQuery()); can be also predefined - passed as
0083     //! argument of parse()
0084     KDbQuerySchema *query;
0085 
0086     KDbConnection *connection;
0087     KDbEscapedString sql;
0088     KDbParserError error;
0089     bool initialized;
0090     Q_DISABLE_COPY(KDbParserPrivate)
0091 };
0092 
0093 /*! Info used on parsing. */
0094 class KDB_TESTING_EXPORT KDbParseInfo
0095 {
0096 public:
0097     ~KDbParseInfo();
0098 
0099     //! @return positions of tables/aliases having the same name @a tableOrAliasName.
0100     //! First tries to use information provided by appendPositionForTableOrAliasName(),
0101     //! then information from the query schema.
0102     QList<int> tablesAndAliasesForName(const QString &tableOrAliasName) const;
0103 
0104     //! @return query schema for this parsing
0105     KDbQuerySchema* querySchema() const;
0106 
0107     //! @return error message for the parsing process
0108     QString errorMessage() const;
0109 
0110     //! Sets error message for the parsing process to @a message
0111     void setErrorMessage(const QString &message);
0112 
0113     //! @return detailed error description for the parsing process
0114     QString errorDescription() const;
0115 
0116     //! Sets detailed error description for the parsing process to @a description
0117     void setErrorDescription(const QString &description);
0118 
0119 protected:
0120     //! Constructs parse info structure for query @a query.
0121     explicit KDbParseInfo(KDbQuerySchema *query);
0122 
0123     class Private;
0124     Private * const d;
0125 private:
0126     Q_DISABLE_COPY(KDbParseInfo)
0127 };
0128 
0129 class Q_DECL_HIDDEN KDbParseInfo::Private
0130 {
0131 public:
0132     Private() {}
0133     ~Private() {
0134         qDeleteAll(repeatedTablesAndAliases);
0135     }
0136 
0137     //! collects positions of tables/aliases with the same names
0138     QHash< QString, QList<int>* > repeatedTablesAndAliases;
0139 
0140     QString errorMessage, errorDescription; // helpers
0141 
0142     KDbQuerySchema *querySchema;
0143 private:
0144     Q_DISABLE_COPY(Private)
0145 };
0146 
0147 /*! Internal info used on parsing (writable). */
0148 class KDB_TESTING_EXPORT KDbParseInfoInternal : public KDbParseInfo
0149 {
0150 public:
0151     //! Constructs parse info structure for query @a query.
0152     explicit KDbParseInfoInternal(KDbQuerySchema *query);
0153 
0154     ~KDbParseInfoInternal();
0155 
0156     //! Appends position @a pos for table or alias @a tableOrAliasName.
0157     void appendPositionForTableOrAliasName(const QString &tableOrAliasName, int pos);
0158 
0159 private:
0160     Q_DISABLE_COPY(KDbParseInfoInternal)
0161 };
0162 
0163 KDB_TESTING_EXPORT const char* g_tokenName(unsigned int offset);
0164 
0165 void yyerror(const char *str);
0166 
0167 void setError(const QString& errName, const QString& errDesc);
0168 
0169 void setError(const QString& errDesc);
0170 
0171 bool addColumn(KDbParseInfo* parseInfo, KDbExpression* columnExpr);
0172 
0173 KDbQuerySchema* buildSelectQuery(
0174     KDbQuerySchema* querySchema, KDbNArgExpression* colViews,
0175     KDbNArgExpression* tablesList = nullptr, SelectOptionsInternal * options = nullptr);
0176 
0177 extern KDbParser *globalParser;
0178 extern KDbField *globalField;
0179 
0180 #endif