KDb

KDbConstExpression.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2018 Jarosław Staniek <staniek@kde.org>
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
32KDbConstExpressionData::KDbConstExpressionData(const QVariant& aValue)
34 , value(aValue)
35{
36 ExpressionDebug << "ConstExpressionData" << ref;
37}
38
39KDbConstExpressionData::~KDbConstExpressionData()
40{
41 ExpressionDebug << "~ConstExpressionData" << ref;
42}
43
44KDbConstExpressionData* 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 }
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 }
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
153void KDbConstExpressionData::getQueryParameters(QList<KDbQuerySchemaParameter>* params)
154{
155 Q_UNUSED(params);
156}
157
158bool 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
197
198KDbConstExpression::~KDbConstExpression()
199{
200}
201
202QVariant KDbConstExpression::value() const
203{
204 return d->convert<const KDbConstExpressionData>()->value;
205}
206
207void KDbConstExpression::setValue(const QVariant& value)
208{
209 d->convert<KDbConstExpressionData>()->value = value;
210}
Internal data class used to implement implicitly shared class KDbConstExpression.
void debugInternal(QDebug dbg, KDb::ExpressionCallStack *callStack) const override
Sends information about this expression to debug output dbg (internal).
KDbEscapedString toStringInternal(const KDbDriver *driver, KDbQuerySchemaParameterValueListIterator *params, KDb::ExpressionCallStack *callStack) const override
KDbField::Type typeInternal(KDb::ExpressionCallStack *callStack) const override
The KDbConstExpression class represents const expression.
Generic date/time constant.
bool isValid() const
Returns true if the date/time is valid.
Generic date constant.
bool isValid() const
Returns true if the date is valid.
Database driver's abstraction.
Definition KDbDriver.h:50
static QString defaultSqlTypeName(KDbField::Type type)
Specialized string for escaping.
Internal data class used to implement implicitly shared class KDbExpression.
KDbField::Type type() const
The KDbExpression class represents a base class for all expressions.
ExplicitlySharedExpressionDataPointer d
@ Integer
Definition KDbField.h:90
@ Boolean
Definition KDbField.h:92
@ ShortInteger
Definition KDbField.h:89
@ InvalidType
Definition KDbField.h:86
@ BigInteger
Definition KDbField.h:91
@ LongText
Definition KDbField.h:99
static int defaultMaxLength()
Definition KDbField.cpp:645
An iterator for a list of values of query schema parameters Allows to iterate over parameters and ret...
Generic time constant.
bool isValid() const
Returns true if the time is valid.
A type-safe KDbSQL token It can be used in KDb expressions.
Definition KDbToken.h:37
int value() const
Definition KDbToken.h:85
QString name() const
Definition KDbToken.cpp:38
A database connectivity and creation framework.
KDB_EXPORT KDbEscapedString valueToSql(KDbField::Type ftype, const QVariant &v)
ExpressionClass
Classes of expressions.
bool isValid(int year, int month, int day)
bool isValid() const const
QDebug & nospace()
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
qsizetype length() const const
bool isValid(int h, int m, int s, int ms)
Type type() const const
bool canConvert() const const
QByteArray toByteArray() const const
QDate toDate() const const
QDateTime toDateTime() const const
int toInt(bool *ok) const const
QString toString() const const
QTime toTime() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 12 2024 12:05:27 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.