KDb

KDbUnaryExpression.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2016 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
30KDbUnaryExpressionData::KDbUnaryExpressionData()
32{
33 ExpressionDebug << "UnaryExpressionData" << ref;
34}
35
36KDbUnaryExpressionData::~KDbUnaryExpressionData()
37{
38 ExpressionDebug << "~UnaryExpressionData" << ref;
39}
40
41KDbUnaryExpressionData* KDbUnaryExpressionData::clone()
42{
43 ExpressionDebug << "UnaryExpressionData::clone" << *this;
44 return new KDbUnaryExpressionData(*this);
45}
46
48{
49 dbg.nospace() << "UnaryExp("
50 << token << ",";
51 if (children.isEmpty()) {
52 dbg.nospace() << "<NONE>";
53 }
54 else {
56 if (a.data()) {
57 a->debug(dbg, callStack);
58 }
59 else {
60 dbg.nospace() << "<NONE>";
61 }
62 }
63 dbg.nospace() << qPrintable(QString::fromLatin1(",type=%1)")
65}
66
67KDbEscapedString KDbUnaryExpressionData::toStringInternal(
68 const KDbDriver *driver,
70 KDb::ExpressionCallStack* callStack) const
71{
73 KDbEscapedString aString = a.constData()
74 ? a->toString(driver, params, callStack) : KDbEscapedString("<NULL>");
75 if (token == '(') { //parentheses (special case)
76 return "(" + aString + ")";
77 }
78 if (token.toChar() > 0) {
79 return token.toString(driver) + aString;
80 }
81 switch (token.value()) {
82 case NOT:
83 return "NOT " + aString;
84 case SQL_IS_NULL:
85 return aString + " IS NULL";
86 case SQL_IS_NOT_NULL:
87 return aString + " IS NOT NULL";
88 }
89 return KDbEscapedString("%1 %2").arg(token.toString(driver)).arg(aString);
90}
91
92void KDbUnaryExpressionData::getQueryParameters(QList<KDbQuerySchemaParameter>* params)
93{
94 Q_ASSERT(params);
96 if (a.constData())
97 a->getQueryParameters(params);
98}
99
101{
102 if (children.isEmpty()) {
104 }
106 if (!a.constData())
108
109 //NULL IS NOT NULL : BOOLEAN
110 //NULL IS NULL : BOOLEAN
111 switch (token.value()) {
112 case SQL_IS_NULL:
113 case SQL_IS_NOT_NULL:
114 //! @todo queryParameterExpressionData->m_type still is Text but can be any type
115 return KDbField::Boolean;
116 }
117
118 KDbQueryParameterExpressionData *queryParameterExpressionData = a->convert<KDbQueryParameterExpressionData>();
119 if (queryParameterExpressionData) {
120 switch (token.value()) {
121 case '+':
122 case '-':
123 case '~':
124 queryParameterExpressionData->m_type = KDbField::Integer;
125 break;
126 case '!':
127 case NOT:
128 queryParameterExpressionData->m_type = KDbField::Boolean;
129 break;
130 default:;
131 }
132 }
133 KDbField::Type t = a->type(callStack);
134 if (t == KDbField::Null)
135 return KDbField::Null;
136 if (token == KDbToken::NOT) {
138 }
139 return t;
140}
141
143{
145 if (!a.constData())
146 return false;
147
148 if (!a->validate(parseInfo, callStack))
149 return false;
150
151//! @todo compare types... e.g. NOT applied to Text makes no sense...
152
153 // update type
154//! @todo IMPORTANT: update type
155#if 0
156 if (a->toQueryParameter()) {
157 a->toQueryParameter()->setType(type());
158 }
159#endif
160
161 return typeInternal(callStack) != KDbField::InvalidType;
162#if 0
163 KDbExpression *n = l.at(0);
164
165 n->check();
166 /*typ wyniku:
167 const bool for "NOT <bool>" (negative)
168 int for "# <str>" (string length)
169 int for "+/- <int>"
170 */
171 if (is(NOT) && n->nodeTypeIs(TYP_BOOL)) {
172 node_type = new NConstType(TYP_BOOL);
173 } else if (is('#') && n->nodeTypeIs(TYP_STR)) {
174 node_type = new NConstType(TYP_INT);
175 } else if ((is('+') || is('-')) && n->nodeTypeIs(TYP_INT)) {
176 node_type = new NConstType(TYP_INT);
177 } else {
178 ERR("Invalid argument of type '%s' for operator '%s'",
179 n->nodeTypeName(), is(NOT) ? QString("not") : QChar(typ()));
180 }
181#endif
182}
183
184//=========================================
185
188{
189 ExpressionDebug << "KDbUnaryExpression() ctor" << *this;
190}
191
193 : KDbExpression(new KDbUnaryExpressionData, KDb::UnaryExpression, token)
194{
195 appendChild(arg.d);
196}
197
199 : KDbExpression(data)
200{
201 ExpressionDebug << "KDbUnaryExpression(KDbExpressionData*) ctor" << *this;
202}
203
208
210 : KDbExpression(ptr)
211{
212}
213
214KDbUnaryExpression::~KDbUnaryExpression()
215{
216}
217
219{
220 return KDbExpression(d->convertConst<KDbUnaryExpressionData>()->arg());
221}
222
224{
225 if (!d->children.isEmpty()) {
226 removeChild(0);
227 }
228 insertChild(0, arg);
229}
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
@ InvalidType
Definition KDbField.h:86
Internal data class used to implement implicitly shared class KDbQueryParameterExpression.
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:37
QString toString(const KDbDriver *driver=nullptr) const
Definition KDbToken.cpp:54
char toChar() const
Definition KDbToken.h:82
int value() const
Definition KDbToken.h:85
Internal data class used to implement implicitly shared class KDbUnaryExpression.
bool validateInternal(KDbParseInfo *parseInfo, KDb::ExpressionCallStack *callStack) override
void debugInternal(QDebug dbg, KDb::ExpressionCallStack *callStack) const override
Sends information about this expression to debug output dbg (internal).
KDbField::Type typeInternal(KDb::ExpressionCallStack *callStack) const override
The KDbUnaryExpression class represents unary expression (with a single argument).
void setArg(const KDbExpression &arg)
Sets expression argument expr for this unary expression.
KDbExpression arg() const
A database connectivity and creation framework.
QDebug & nospace()
const T * constData() const const
QString fromLatin1(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.