KDb

KDbQueryParameterExpression.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2003-2011 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 "KDbQuerySchemaParameter.h"
27 #include "KDbDriver.h"
28 #include "kdb_debug.h"
29 #include "generated/sqlparser.h"
30 
31 KDbQueryParameterExpressionData::KDbQueryParameterExpressionData()
33  , m_type(KDbField::InvalidType)
34 {
35  ExpressionDebug << "QueryParameterExpressionData" << ref;
36 }
37 
38 KDbQueryParameterExpressionData::KDbQueryParameterExpressionData(
39  KDbField::Type type, const QVariant& value)
40  : KDbConstExpressionData(value)
41  , m_type(type)
42 {
43  ExpressionDebug << "QueryParameterExpressionData" << ref;
44 }
45 
46 KDbQueryParameterExpressionData::~KDbQueryParameterExpressionData()
47 {
48  ExpressionDebug << "~QueryParameterExpressionData" << ref;
49 }
50 
51 KDbQueryParameterExpressionData* KDbQueryParameterExpressionData::clone()
52 {
53  ExpressionDebug << "QueryParameterExpressionData::clone" << *this;
54  return new KDbQueryParameterExpressionData(*this);
55 }
56 
58 {
59  Q_UNUSED(callStack);
60  dbg.nospace() << qPrintable(QString::fromLatin1("QueryParExp([%1],type=%2)")
61  .arg(value.toString(), KDbDriver::defaultSqlTypeName(type())));
62 }
63 
64 KDbEscapedString KDbQueryParameterExpressionData::toStringInternal(
65  const KDbDriver *driver,
67  KDb::ExpressionCallStack* callStack) const
68 {
69  Q_UNUSED(callStack);
70  return params
71  // Enclose in () because for example if the parameter is -1 and parent expression
72  // unary '-' then the result would be "--1" (a comment in SQL!).
73  // With the () the result will be a valid expression "-(-1)".
74  ? KDbEscapedString("(%1)").arg(driver->valueToSql(type(), params->previousValue()))
75  : KDbEscapedString("[%1]").arg(KDbEscapedString(value.toString()));
76 }
77 
78 void KDbQueryParameterExpressionData::getQueryParameters(QList<KDbQuerySchemaParameter>* params)
79 {
80  Q_ASSERT(params);
82  param.setMessage(value.toString());
83  param.setType(type());
84  params->append(param);
85 }
86 
87 bool KDbQueryParameterExpressionData::validateInternal(KDbParseInfo *parseInfo, KDb::ExpressionCallStack* callStack)
88 {
89  Q_UNUSED(parseInfo);
90  return typeInternal(callStack) != KDbField::InvalidType;
91 }
92 
93 KDbField::Type KDbQueryParameterExpressionData::typeInternal(KDb::ExpressionCallStack* callStack) const
94 {
95  Q_UNUSED(callStack);
96  return m_type;
97 }
98 
99 //=========================================
100 
103 {
104  ExpressionDebug << "KDbQueryParameterExpression() ctor" << *this;
105 }
106 
109  KDb::QueryParameterExpression, KDbToken::QUERY_PARAMETER)
110 {
111 }
112 
114  : KDbConstExpression(expr)
115 {
116 }
117 
119  : KDbConstExpression(data)
120 {
121  ExpressionDebug << "KDbQueryParameterExpression ctor (KDbExpressionData*)" << *this;
122 }
123 
125  : KDbConstExpression(ptr)
126 {
127 }
128 
129 KDbQueryParameterExpression::~KDbQueryParameterExpression()
130 {
131 }
132 
134 {
135  d->convert<KDbQueryParameterExpressionData>()->m_type = type;
136 }
void append(const T &value)
Internal data class used to implement implicitly shared class KDbExpression.
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
Type type(const QSqlDatabase &db)
void ref()
QDebug & nospace()
A database connectivity and creation framework.
KDbField::Type type() const
Specialized string for escaping.
A single parameter of a query schema.
Database driver's abstraction.
Definition: KDbDriver.h:49
Internal data class used to implement implicitly shared class KDbQueryParameterExpression.
The KDbQueryParameterExpression class represents query parameter expression.
static QString defaultSqlTypeName(KDbField::Type type)
Definition: KDbDriver.cpp:166
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).
ExplicitlySharedExpressionDataPointer d
virtual KDbEscapedString valueToSql(KDbField::Type ftype, const QVariant &v) const
Definition: KDbDriver.cpp:246
QString fromLatin1(const char *str, int size)
Meta-data for a field.
Definition: KDbField.h:71
The KDbConstExpression class represents const expression.
KDbField::Type type() const
QString message
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 04:07:51 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.