KDb

KDbToken.cpp
1 /****************************************************************************
2  * Created by generate_parser_code.sh
3  * WARNING! All changes made in this file will be lost!
4  ****************************************************************************/
5 /* This file is part of the KDE project
6  Copyright (C) 2015-2018 JarosÅ‚aw Staniek <[email protected]>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22 */
23 
24 #include "KDbToken.h"
25 #include "KDbDriver.h"
26 #include "KDbDriver_p.h"
27 #include "KDbDriverBehavior.h"
28 #include "sqlparser.h"
29 #include "parser/KDbParser_p.h"
30 
31 #include <QGlobalStatic>
32 
33 KDbToken::KDbToken(char charToken)
34  : v(g_tokenName(charToken) == nullptr ? 0 : charToken)
35 {
36 }
37 
39 {
40  if (!isValid()) {
41  return QLatin1String("<INVALID_TOKEN>");
42  }
43  if (v > maxCharTokenValue) {
44  return QLatin1String(g_tokenName(v));
45  }
46  if (isprint(v)) {
47  return QString(QLatin1Char(char(v)));
48  }
49  else {
51  }
52 }
53 
54 QString KDbToken::toString(const KDbDriver *driver) const
55 {
56  if (toChar() > 0) {
57  return name();
58  }
59  // other arithmetic operations: << >>
60  // NOTE: only include cases that have toString() != name() or are dependent on driver
61  switch (v) {
62  case ::BITWISE_SHIFT_RIGHT: return QLatin1String(">>");
63  case ::BITWISE_SHIFT_LEFT: return QLatin1String("<<");
64  // other relational operations: <= >= <> (or !=) LIKE IN
65  case ::NOT_EQUAL: return QLatin1String("<>");
66  case ::NOT_EQUAL2: return QLatin1String("!=");
67  case ::LESS_OR_EQUAL: return QLatin1String("<=");
68  case ::GREATER_OR_EQUAL: return QLatin1String(">=");
69  case ::LIKE: return driver ? KDbDriverPrivate::behavior(driver)->LIKE_OPERATOR : QLatin1String("LIKE");
70  case ::NOT_LIKE:
71  return driver
72  ? (QString::fromLatin1("NOT ") + KDbDriverPrivate::behavior(driver)->LIKE_OPERATOR)
73  : QString::fromLatin1("NOT LIKE");
74  case ::SQL_IN: return QLatin1String("IN");
75  // other logical operations: OR (or ||) AND (or &&) XOR
76  case ::SIMILAR_TO: return QLatin1String("SIMILAR TO");
77  case ::NOT_SIMILAR_TO: return QLatin1String("NOT SIMILAR TO");
78  // other string operations: || (as CONCATENATION)
79  case ::CONCATENATION: return QLatin1String("||");
80  // SpecialBinary "pseudo operators":
81  /* not handled here */
82  default:;
83  }
84  const QString s = name();
85  if (!s.isEmpty()) {
86  return s;
87  }
88  return QString::fromLatin1("<INVALID_TOKEN#%1> ").arg(v);
89 }
90 
91 //static
93 {
94  return token.toString(driver);
95 }
96 
97 KDB_EXPORT QDebug operator<<(QDebug dbg, KDbToken token)
98 {
99  QDebugStateSaver saver(dbg);
100  dbg.nospace().noquote() << token.name();
101  return dbg.maybeSpace();
102 }
103 
104 //! @internal
105 class KDbToken::List
106 {
107 public:
108  List()
109  {
110  for (int i = 0; i < KDbToken::maxTokenValue; ++i) {
111  if (g_tokenName(i)) {
112  data.append(KDbToken(i));
113  }
114  }
115  }
116  QList<KDbToken> data;
117 };
118 
119 Q_GLOBAL_STATIC(KDbToken::List, g_allTokens)
120 
121 //static
122 QList<KDbToken> KDbToken::allTokens()
123 {
124  return g_allTokens->data;
125 }
126 
127 const KDbToken KDbToken::SQL_TYPE(::SQL_TYPE);
128 const KDbToken KDbToken::AS(::AS);
129 const KDbToken KDbToken::AS_EMPTY(::AS_EMPTY);
130 const KDbToken KDbToken::ASC(::ASC);
131 const KDbToken KDbToken::AUTO_INCREMENT(::AUTO_INCREMENT);
132 const KDbToken KDbToken::BIT(::BIT);
133 const KDbToken KDbToken::BITWISE_SHIFT_LEFT(::BITWISE_SHIFT_LEFT);
134 const KDbToken KDbToken::BITWISE_SHIFT_RIGHT(::BITWISE_SHIFT_RIGHT);
135 const KDbToken KDbToken::BY(::BY);
136 const KDbToken KDbToken::CHARACTER_STRING_LITERAL(::CHARACTER_STRING_LITERAL);
137 const KDbToken KDbToken::CONCATENATION(::CONCATENATION);
138 const KDbToken KDbToken::CREATE(::CREATE);
139 const KDbToken KDbToken::DESC(::DESC);
140 const KDbToken KDbToken::DISTINCT(::DISTINCT);
141 const KDbToken KDbToken::DOUBLE_QUOTED_STRING(::DOUBLE_QUOTED_STRING);
142 const KDbToken KDbToken::FROM(::FROM);
143 const KDbToken KDbToken::JOIN(::JOIN);
144 const KDbToken KDbToken::KEY(::KEY);
145 const KDbToken KDbToken::LEFT(::LEFT);
146 const KDbToken KDbToken::LESS_OR_EQUAL(::LESS_OR_EQUAL);
147 const KDbToken KDbToken::GREATER_OR_EQUAL(::GREATER_OR_EQUAL);
148 const KDbToken KDbToken::SQL_NULL(::SQL_NULL);
149 const KDbToken KDbToken::SQL_IS(::SQL_IS);
150 const KDbToken KDbToken::SQL_IS_NULL(::SQL_IS_NULL);
151 const KDbToken KDbToken::SQL_IS_NOT_NULL(::SQL_IS_NOT_NULL);
152 const KDbToken KDbToken::ORDER(::ORDER);
153 const KDbToken KDbToken::PRIMARY(::PRIMARY);
154 const KDbToken KDbToken::SELECT(::SELECT);
155 const KDbToken KDbToken::INTEGER_CONST(::INTEGER_CONST);
156 const KDbToken KDbToken::REAL_CONST(::REAL_CONST);
157 const KDbToken KDbToken::RIGHT(::RIGHT);
158 const KDbToken KDbToken::SQL_ON(::SQL_ON);
159 const KDbToken KDbToken::DATE_CONST(::DATE_CONST);
160 const KDbToken KDbToken::DATETIME_CONST(::DATETIME_CONST);
161 const KDbToken KDbToken::TIME_CONST(::TIME_CONST);
162 const KDbToken KDbToken::TABLE(::TABLE);
163 const KDbToken KDbToken::IDENTIFIER(::IDENTIFIER);
164 const KDbToken KDbToken::IDENTIFIER_DOT_ASTERISK(::IDENTIFIER_DOT_ASTERISK);
165 const KDbToken KDbToken::QUERY_PARAMETER(::QUERY_PARAMETER);
166 const KDbToken KDbToken::VARCHAR(::VARCHAR);
167 const KDbToken KDbToken::WHERE(::WHERE);
168 const KDbToken KDbToken::SQL(::SQL);
169 const KDbToken KDbToken::SQL_TRUE(::SQL_TRUE);
170 const KDbToken KDbToken::SQL_FALSE(::SQL_FALSE);
171 const KDbToken KDbToken::UNION(::UNION);
172 const KDbToken KDbToken::SCAN_ERROR(::SCAN_ERROR);
173 const KDbToken KDbToken::AND(::AND);
174 const KDbToken KDbToken::BETWEEN(::BETWEEN);
175 const KDbToken KDbToken::NOT_BETWEEN(::NOT_BETWEEN);
176 const KDbToken KDbToken::EXCEPT(::EXCEPT);
177 const KDbToken KDbToken::SQL_IN(::SQL_IN);
178 const KDbToken KDbToken::INTERSECT(::INTERSECT);
179 const KDbToken KDbToken::LIKE(::LIKE);
180 const KDbToken KDbToken::ILIKE(::ILIKE);
181 const KDbToken KDbToken::NOT_LIKE(::NOT_LIKE);
182 const KDbToken KDbToken::NOT(::NOT);
183 const KDbToken KDbToken::NOT_EQUAL(::NOT_EQUAL);
184 const KDbToken KDbToken::NOT_EQUAL2(::NOT_EQUAL2);
185 const KDbToken KDbToken::OR(::OR);
186 const KDbToken KDbToken::SIMILAR_TO(::SIMILAR_TO);
187 const KDbToken KDbToken::NOT_SIMILAR_TO(::NOT_SIMILAR_TO);
188 const KDbToken KDbToken::XOR(::XOR);
189 const KDbToken KDbToken::UMINUS(::UMINUS);
190 const KDbToken KDbToken::TABS_OR_SPACES(::TABS_OR_SPACES);
191 const KDbToken KDbToken::DATE_TIME_INTEGER(::DATE_TIME_INTEGER);
192 const KDbToken KDbToken::TIME_AM(::TIME_AM);
193 const KDbToken KDbToken::TIME_PM(::TIME_PM);
194 const KDbToken KDbToken::BETWEEN_AND(0x1001);
195 const KDbToken KDbToken::NOT_BETWEEN_AND(0x1002);
bool isValid() const
Definition: KDbToken.h:53
A type-safe KDbSQL token It can be used in KDb expressions.
Definition: KDbToken.h:36
QDebug & nospace()
KDbToken()
Creates an invalid token.
Definition: KDbToken.h:42
QByteArray number(int n, int base)
QDataStream & operator<<(QDataStream &out, const KDateTime &dateTime)
Database driver's abstraction.
Definition: KDbDriver.h:49
static const KDbToken BETWEEN_AND
Custom tokens are not used in parser but used as an extension in expression classes.
Definition: KDbToken.h:170
QDebug & maybeSpace()
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
QDebug & noquote()
bool isEmpty() const const
char toChar() const
Definition: KDbToken.h:82
QString name() const
Definition: KDbToken.cpp:38
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString toString(const KDbDriver *driver=nullptr) const
Definition: KDbToken.cpp:54
static const int maxTokenValue
Maximum character token value.
Definition: KDbToken.h:77
QString fromLatin1(const char *str, int size)
static const int maxCharTokenValue
Maximum character token value (253)
Definition: KDbToken.h:74
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:21:34 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.