File indexing completed on 2024-05-19 04:42:45

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2011 Jarosław Staniek <staniek@kde.org>
0003 
0004    Based on nexp.cpp : Parser module of Python-like language
0005    (C) 2001 Jarosław Staniek, MIMUW (www.mimuw.edu.pl)
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This library is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021  */
0022 
0023 #include "KDbExpression.h"
0024 #include "KDb.h"
0025 #include "KDbQuerySchema.h"
0026 #include "KDbQuerySchemaParameter.h"
0027 #include "KDbDriver.h"
0028 #include "kdb_debug.h"
0029 #include "generated/sqlparser.h"
0030 
0031 KDbQueryParameterExpressionData::KDbQueryParameterExpressionData()
0032  : KDbConstExpressionData()
0033  , m_type(KDbField::InvalidType)
0034 {
0035     ExpressionDebug << "QueryParameterExpressionData" << ref;
0036 }
0037 
0038 KDbQueryParameterExpressionData::KDbQueryParameterExpressionData(
0039     KDbField::Type type, const QVariant& value)
0040  : KDbConstExpressionData(value)
0041  , m_type(type)
0042 {
0043    ExpressionDebug << "QueryParameterExpressionData" << ref;
0044 }
0045 
0046 KDbQueryParameterExpressionData::~KDbQueryParameterExpressionData()
0047 {
0048     ExpressionDebug << "~QueryParameterExpressionData" << ref;
0049 }
0050 
0051 KDbQueryParameterExpressionData* KDbQueryParameterExpressionData::clone()
0052 {
0053     ExpressionDebug << "QueryParameterExpressionData::clone" << *this;
0054     return new KDbQueryParameterExpressionData(*this);
0055 }
0056 
0057 void KDbQueryParameterExpressionData::debugInternal(QDebug dbg, KDb::ExpressionCallStack* callStack) const
0058 {
0059     Q_UNUSED(callStack);
0060     dbg.nospace() << qPrintable(QString::fromLatin1("QueryParExp([%1],type=%2)")
0061         .arg(value.toString(), KDbDriver::defaultSqlTypeName(type())));
0062 }
0063 
0064 KDbEscapedString KDbQueryParameterExpressionData::toStringInternal(
0065                                         const KDbDriver *driver,
0066                                         KDbQuerySchemaParameterValueListIterator* params,
0067                                         KDb::ExpressionCallStack* callStack) const
0068 {
0069     Q_UNUSED(callStack);
0070     return params
0071            // Enclose in () because for example if the parameter is -1 and parent expression
0072            // unary '-' then the result would be "--1" (a comment in SQL!).
0073            // With the () the result will be a valid expression "-(-1)".
0074            ? KDbEscapedString("(%1)").arg(driver->valueToSql(type(), params->previousValue()))
0075            : KDbEscapedString("[%1]").arg(KDbEscapedString(value.toString()));
0076 }
0077 
0078 void KDbQueryParameterExpressionData::getQueryParameters(QList<KDbQuerySchemaParameter>* params)
0079 {
0080     Q_ASSERT(params);
0081     KDbQuerySchemaParameter param;
0082     param.setMessage(value.toString());
0083     param.setType(type());
0084     params->append(param);
0085 }
0086 
0087 bool KDbQueryParameterExpressionData::validateInternal(KDbParseInfo *parseInfo, KDb::ExpressionCallStack* callStack)
0088 {
0089     Q_UNUSED(parseInfo);
0090     return typeInternal(callStack) != KDbField::InvalidType;
0091 }
0092 
0093 KDbField::Type KDbQueryParameterExpressionData::typeInternal(KDb::ExpressionCallStack* callStack) const
0094 {
0095     Q_UNUSED(callStack);
0096     return m_type;
0097 }
0098 
0099 //=========================================
0100 
0101 KDbQueryParameterExpression::KDbQueryParameterExpression()
0102  : KDbConstExpression(new KDbQueryParameterExpressionData)
0103 {
0104     ExpressionDebug << "KDbQueryParameterExpression() ctor" << *this;
0105 }
0106 
0107 KDbQueryParameterExpression::KDbQueryParameterExpression(const QString& message)
0108         : KDbConstExpression(new KDbQueryParameterExpressionData(KDbField::Text, message),
0109               KDb::QueryParameterExpression, KDbToken::QUERY_PARAMETER)
0110 {
0111 }
0112 
0113 KDbQueryParameterExpression::KDbQueryParameterExpression(const KDbQueryParameterExpression& expr)
0114         : KDbConstExpression(expr)
0115 {
0116 }
0117 
0118 KDbQueryParameterExpression::KDbQueryParameterExpression(KDbExpressionData* data)
0119     : KDbConstExpression(data)
0120 {
0121     ExpressionDebug << "KDbQueryParameterExpression ctor (KDbExpressionData*)" << *this;
0122 }
0123 
0124 KDbQueryParameterExpression::KDbQueryParameterExpression(const ExplicitlySharedExpressionDataPointer &ptr)
0125     : KDbConstExpression(ptr)
0126 {
0127 }
0128 
0129 KDbQueryParameterExpression::~KDbQueryParameterExpression()
0130 {
0131 }
0132 
0133 void KDbQueryParameterExpression::setType(KDbField::Type type)
0134 {
0135     d->convert<KDbQueryParameterExpressionData>()->m_type = type;
0136 }