KDb

KDbConstExpression.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2003-2018 JarosÅ‚aw Staniek <[email protected]>
3 
4  Based on nexp.cpp : Parser module of Python-like language
5  (C) 2001 JarosÅ‚aw Staniek, MIMUW (www.mimuw.edu.pl)
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "KDbExpression.h"
24 #include "KDb.h"
25 #include "KDbQuerySchema.h"
26 #include "KDbDriver.h"
27 #include "kdb_debug.h"
28 #include "generated/sqlparser.h"
29 
30 #include <QPoint>
31 
32 KDbConstExpressionData::KDbConstExpressionData(const QVariant& aValue)
34  , value(aValue)
35 {
36  ExpressionDebug << "ConstExpressionData" << ref;
37 }
38 
39 KDbConstExpressionData::~KDbConstExpressionData()
40 {
41  ExpressionDebug << "~ConstExpressionData" << ref;
42 }
43 
44 KDbConstExpressionData* KDbConstExpressionData::clone()
45 {
46  ExpressionDebug << "ConstExpressionData::clone" << *this;
47  return new KDbConstExpressionData(*this);
48 }
49 
51 {
52  Q_UNUSED(callStack);
53  switch (token.value()) {
54  case SQL_NULL:
55  return KDbField::Null;
56  case INTEGER_CONST:
57 //! @todo ok?
58 //! @todo add sign info?
59  if (value.type() == QVariant::Int || value.type() == QVariant::UInt) {
60  qint64 v = value.toInt();
61  if (v <= 0xff && v > -0x80)
62  return KDbField::Byte;
63  if (v <= 0xffff && v > -0x8000)
65  return KDbField::Integer;
66  }
67  return KDbField::BigInteger;
68  case CHARACTER_STRING_LITERAL:
71  {
72  return KDbField::LongText;
73  }
74  else {
75  return KDbField::Text;
76  }
77  case SQL_TRUE:
78  case SQL_FALSE:
79  return KDbField::Boolean;
80  case REAL_CONST:
81  return KDbField::Double;
82  case DATE_CONST:
83  if (value.canConvert<KDbDate>() && value.value<KDbDate>().isValid()) {
84  return KDbField::Date;
85  } else if (value.canConvert<QDate>() && value.toDate().isValid()) {
86  return KDbField::Date;
87  }
88  break;
89  case DATETIME_CONST:
90  if (value.canConvert<KDbDateTime>() && value.value<KDbDateTime>().isValid()) {
91  return KDbField::DateTime;
92  } else if (value.canConvert<QDateTime>() && value.toDateTime().isValid()) {
93  return KDbField::DateTime;
94  }
95  break;
96  case TIME_CONST:
97  if (value.canConvert<KDbTime>() && value.value<KDbTime>().isValid()) {
98  return KDbField::Time;
99  } else if (value.canConvert<QTime>() && value.toTime().isValid()) {
100  return KDbField::Time;
101  }
102  break;
103  }
104  return KDbField::InvalidType;
105 }
106 
108 {
109  Q_UNUSED(callStack);
110  QString res = QLatin1String("ConstExp(")
111  + token.name()
112  + QLatin1String(",") + toString(nullptr).toString()
114  if (value.type() == QVariant::Point && token.value() == REAL_CONST) {
115  res += QLatin1String(",DECIMAL");
116  }
117  res += QLatin1String(")");
118  dbg.nospace() << qPrintable(res);
119 }
120 
122  const KDbDriver *driver,
124  KDb::ExpressionCallStack* callStack) const
125 {
126  Q_UNUSED(params);
127  Q_UNUSED(callStack);
128  switch (token.value()) {
129  case SQL_NULL:
130  return KDb::valueToSql(driver, KDbField::Null, QVariant());
131  case CHARACTER_STRING_LITERAL:
132 //! @todo better escaping!
133  return KDb::valueToSql(driver, KDbField::Text, value);
134  case SQL_TRUE:
135  return KDb::valueToSql(driver, KDbField::Boolean, 1);
136  case SQL_FALSE:
137  return KDb::valueToSql(driver, KDbField::Boolean, 0);
138  case REAL_CONST:
139  return KDbEscapedString(value.toByteArray());
140  case DATE_CONST:
141  return KDb::valueToSql(driver, KDbField::Date, value);
142  case DATETIME_CONST:
143  return KDb::valueToSql(driver, KDbField::Date, value);
144  case TIME_CONST:
145  return KDb::valueToSql(driver, KDbField::Time, value);
146  case INTEGER_CONST:
147  default:
148  break;
149  }
150  return KDbEscapedString(value.toByteArray());
151 }
152 
153 void KDbConstExpressionData::getQueryParameters(QList<KDbQuerySchemaParameter>* params)
154 {
155  Q_UNUSED(params);
156 }
157 
158 bool KDbConstExpressionData::validateInternal(KDbParseInfo *parseInfo, KDb::ExpressionCallStack* callStack)
159 {
160  Q_UNUSED(parseInfo);
161  return typeInternal(callStack) != KDbField::InvalidType;
162 }
163 
164 //=========================================
165 
168 {
169  ExpressionDebug << "KDbConstExpression() ctor" << *this;
170 }
171 
173  : KDbExpression(new KDbConstExpressionData(value), KDb::ConstExpression, token)
174 {
175 }
176 
178  KDbToken token)
179  : KDbExpression(data, aClass, token)
180 {
181 }
182 
184  : KDbExpression(data)
185 {
186 }
187 
189  : KDbExpression(ptr)
190 {
191 }
192 
194  : KDbExpression(expr)
195 {
196 }
197 
198 KDbConstExpression::~KDbConstExpression()
199 {
200 }
201 
202 QVariant KDbConstExpression::value() const
203 {
204  return d->convert<const KDbConstExpressionData>()->value;
205 }
206 
207 void KDbConstExpression::setValue(const QVariant& value)
208 {
209  d->convert<KDbConstExpressionData>()->value = value;
210 }
static int defaultMaxLength()
Definition: KDbField.cpp:645
Internal data class used to implement implicitly shared class KDbExpression.
KDbField::Type typeInternal(KDb::ExpressionCallStack *callStack) const override
An iterator for a list of values of query schema parameters Allows to iterate over parameters and ret...
A type-safe KDbSQL token It can be used in KDb expressions.
Definition: KDbToken.h:36
@ InvalidType
Definition: KDbField.h:86
T value() const const
@ Text
Definition: KDbField.h:98
void ref()
QDebug & nospace()
A database connectivity and creation framework.
KDbField::Type type() const
Specialized string for escaping.
void debugInternal(QDebug dbg, KDb::ExpressionCallStack *callStack) const override
Sends information about this expression to debug output dbg (internal).
Database driver's abstraction.
Definition: KDbDriver.h:49
bool isValid() const const
QByteArray toByteArray() const const
QDate toDate() const const
bool isValid() const
Returns true if the time is valid.
static QString defaultSqlTypeName(KDbField::Type type)
Definition: KDbDriver.cpp:166
QVariant::Type type() const const
bool isValid() const
Returns true if the date is valid.
@ Integer
Definition: KDbField.h:90
Internal data class used to implement implicitly shared class KDbConstExpression.
int length() const const
Generic time constant.
Definition: KDbDateTime.h:260
int toInt(bool *ok) const const
bool isValid() const const
ExplicitlySharedExpressionDataPointer d
@ BigInteger
Definition: KDbField.h:91
ExpressionClass
Classes of expressions.
@ Double
Definition: KDbField.h:97
KDB_EXPORT KDbEscapedString valueToSql(KDbField::Type ftype, const QVariant &v)
Definition: KDb.cpp:2201
@ LongText
Definition: KDbField.h:99
@ Byte
Definition: KDbField.h:87
bool canConvert(int targetTypeId) const const
Generic date/time constant.
Definition: KDbDateTime.h:403
QString name() const
Definition: KDbToken.cpp:38
QDateTime toDateTime() const const
@ Boolean
Definition: KDbField.h:92
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
QTime toTime() const const
bool isValid() const const
KDbEscapedString toStringInternal(const KDbDriver *driver, KDbQuerySchemaParameterValueListIterator *params, KDb::ExpressionCallStack *callStack) const override
int value() const
Definition: KDbToken.h:85
Generic date constant.
Definition: KDbDateTime.h:157
The KDbConstExpression class represents const expression.
The KDbExpression class represents a base class for all expressions.
Definition: KDbExpression.h:51
@ ShortInteger
Definition: KDbField.h:89
bool isValid() const
Returns true if the date/time is valid.
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Nov 28 2023 04:08:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.