File indexing completed on 2024-12-08 12:44:47

0001 /****************************************************************************
0002  * Created by generate_parser_code.sh
0003  * WARNING! All changes made in this file will be lost!
0004  ****************************************************************************/
0005 /* This file is part of the KDE project
0006    Copyright (C) 2015-2018 Jarosław Staniek <staniek@kde.org>
0007 
0008    This library is free software; you can redistribute it and/or
0009    modify it under the terms of the GNU Library General Public
0010    License as published by the Free Software Foundation; either
0011    version 2 of the License, or (at your option) any later version.
0012 
0013    This library is distributed in the hope that it will be useful,
0014    but WITHOUT ANY WARRANTY; without even the implied warranty of
0015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016    Library General Public License for more details.
0017 
0018    You should have received a copy of the GNU Library General Public License
0019    along with this library; see the file COPYING.LIB.  If not, write to
0020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0021  * Boston, MA 02110-1301, USA.
0022 */
0023 
0024 #ifndef KDB_TOKEN_H
0025 #define KDB_TOKEN_H
0026 
0027 #include "kdb_export.h"
0028 
0029 #include <QDebug>
0030 
0031 class KDbDriver;
0032 
0033 /*! @brief A type-safe KDbSQL token
0034  It can be used in KDb expressions
0035  @see KDbExpression */
0036 class KDB_EXPORT KDbToken
0037 {
0038 public:
0039     //! @todo add KDbToken(const QByteArray &name)
0040 
0041     //! Creates an invalid token
0042     inline KDbToken() : v(0) {}
0043 
0044     KDbToken(const KDbToken &other) : v(other.v) {}
0045 
0046     //! Creates a single-character token
0047     //! Only characters that belong to the grammar are accepted:
0048     //!   ';'  ',' '.' '>' '<' '=' '+' '-' '&' '|' '/'  '*' '%' '~' '#' ':' '(' ')'
0049     //! Invalid KDbToken is created for character that is not accepted.
0050     KDbToken(char charToken);
0051 
0052     //! @return true if this token is valid
0053     inline bool isValid() const { return v != 0; }
0054 
0055     //! @return name of this token
0056     //! Useful for debugging.
0057     //! For example "NOT_EQUAL" string is returned for the NOT_EQUAL token.
0058     //! A single character is returned for printable single-character tokens.
0059     //! A number is returned for non-printable single-character.
0060     //! "<INVALID_TOKEN>" is returned for an invalid string.
0061     QString name() const;
0062 
0063     //! @return string interpretation of this token (as visibe to the user)
0064     //! For example "<>" is returned for the NOT_EQUAL token.
0065     //! Empty string is returned for an invalid string
0066     //! The result may depend on the optional @a driver parameter.
0067     //! If @a driver is @c nullptr, representation for portable KDbSQL dialect is returned.
0068     QString toString(const KDbDriver *driver = nullptr) const;
0069 
0070     //! Like toString(const KDbDriver *driver)
0071     static QString toString(KDbToken token, const KDbDriver *driver = nullptr);
0072 
0073     //! Maximum character token value (253)
0074     static const int maxCharTokenValue;
0075 
0076     //! Maximum character token value
0077     static const int maxTokenValue;
0078 
0079     //! @return character equivalent of this token
0080     //! Only character-based tokens are supported this way (toInt() <= maxCharTokenValue).
0081     //! For unsupported tokens @c nullptr is returned.
0082     inline char toChar() const { return v <= maxCharTokenValue ? v : 0; }
0083 
0084     //! @return numeric value of this token
0085     inline int value() const { return v; }
0086 
0087     //! @return true if this token is equal to @a other token
0088     inline bool operator==(KDbToken other) const { return v == other.v; }
0089 
0090     //! @return true if this token is not equal to @a other token
0091     inline bool operator!=(KDbToken other) const { return v != other.v; }
0092 
0093     //! @return true if this token is equal to @a other token
0094     inline bool operator==(char charToken) const { return v == charToken; }
0095 
0096     //! @return true if this token is not equal to @a other token
0097     inline bool operator!=(char charToken) const { return v != charToken; }
0098 
0099     //! Assigns a token
0100     inline void operator=(char charToken) { v = charToken; }
0101 
0102     static QList<KDbToken> allTokens();
0103 
0104     // -- constants go here --
0105     static const KDbToken SQL_TYPE;
0106     static const KDbToken AS;
0107     static const KDbToken AS_EMPTY;
0108     static const KDbToken ASC;
0109     static const KDbToken AUTO_INCREMENT;
0110     static const KDbToken BIT;
0111     static const KDbToken BITWISE_SHIFT_LEFT;
0112     static const KDbToken BITWISE_SHIFT_RIGHT;
0113     static const KDbToken BY;
0114     static const KDbToken CHARACTER_STRING_LITERAL;
0115     static const KDbToken CONCATENATION;
0116     static const KDbToken CREATE;
0117     static const KDbToken DESC;
0118     static const KDbToken DISTINCT;
0119     static const KDbToken DOUBLE_QUOTED_STRING;
0120     static const KDbToken FROM;
0121     static const KDbToken JOIN;
0122     static const KDbToken KEY;
0123     static const KDbToken LEFT;
0124     static const KDbToken LESS_OR_EQUAL;
0125     static const KDbToken GREATER_OR_EQUAL;
0126     static const KDbToken SQL_NULL;
0127     static const KDbToken SQL_IS;
0128     static const KDbToken SQL_IS_NULL;
0129     static const KDbToken SQL_IS_NOT_NULL;
0130     static const KDbToken ORDER;
0131     static const KDbToken PRIMARY;
0132     static const KDbToken SELECT;
0133     static const KDbToken INTEGER_CONST;
0134     static const KDbToken REAL_CONST;
0135     static const KDbToken RIGHT;
0136     static const KDbToken SQL_ON;
0137     static const KDbToken DATE_CONST;
0138     static const KDbToken DATETIME_CONST;
0139     static const KDbToken TIME_CONST;
0140     static const KDbToken TABLE;
0141     static const KDbToken IDENTIFIER;
0142     static const KDbToken IDENTIFIER_DOT_ASTERISK;
0143     static const KDbToken QUERY_PARAMETER;
0144     static const KDbToken VARCHAR;
0145     static const KDbToken WHERE;
0146     static const KDbToken SQL;
0147     static const KDbToken SQL_TRUE;
0148     static const KDbToken SQL_FALSE;
0149     static const KDbToken UNION;
0150     static const KDbToken SCAN_ERROR;
0151     static const KDbToken AND;
0152     static const KDbToken BETWEEN;
0153     static const KDbToken NOT_BETWEEN;
0154     static const KDbToken EXCEPT;
0155     static const KDbToken SQL_IN;
0156     static const KDbToken INTERSECT;
0157     static const KDbToken LIKE;
0158     static const KDbToken ILIKE;
0159     static const KDbToken NOT_LIKE;
0160     static const KDbToken NOT;
0161     static const KDbToken NOT_EQUAL;
0162     static const KDbToken NOT_EQUAL2;
0163     static const KDbToken OR;
0164     static const KDbToken SIMILAR_TO;
0165     static const KDbToken NOT_SIMILAR_TO;
0166     static const KDbToken XOR;
0167     static const KDbToken UMINUS;
0168     static const KDbToken TABS_OR_SPACES;
0169     static const KDbToken DATE_TIME_INTEGER;
0170     static const KDbToken TIME_AM;
0171     static const KDbToken TIME_PM;
0172     //! Custom tokens are not used in parser but used as an extension in expression classes.
0173     static const KDbToken BETWEEN_AND;
0174     static const KDbToken NOT_BETWEEN_AND;
0175     // -- end of constants --
0176 
0177     class List;
0178 private:
0179     inline KDbToken(int value) : v(value) {}
0180     int v;
0181 };
0182 
0183 //! Sends information about token @a token to debug output @a dbg.
0184 KDB_EXPORT QDebug operator<<(QDebug dbg, KDbToken token);
0185 
0186 #endif