-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSqlConditionalQueryBuilderBase.cpp
101 lines (88 loc) · 3.61 KB
/
SqlConditionalQueryBuilderBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright (C) 2011-2013 Klarälvdalens Datakonsult AB,
a KDAB Group company, [email protected],
author Volker Krause <[email protected]>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "SqlConditionalQueryBuilderBase.h"
#include "SqlExceptions.h"
#include <QStringList>
SqlConditionalQueryBuilderBase::SqlConditionalQueryBuilderBase(const QSqlDatabase& db) :
SqlQueryBuilderBase( db ),
m_bindedValuesOffset(0)
{
}
SqlCondition& SqlConditionalQueryBuilderBase::whereCondition()
{
return m_whereCondition;
}
QString SqlConditionalQueryBuilderBase::registerBindValue(const QVariant& value)
{
m_bindValues.push_back( value );
return QLatin1Char( ':' ) + QString::number( m_bindedValuesOffset + m_bindValues.size() - 1 );
}
static QString logicOperatorToString( SqlCondition::LogicOperator op )
{
switch ( op ) {
case SqlCondition::And: return QLatin1String( " AND " );
case SqlCondition::Or: return QLatin1String( " OR " );
}
qFatal( "Unknown logic operator" );
return QString();
}
static QString compareOperatorToString( SqlCondition::CompareOperator op )
{
switch ( op ) {
case SqlCondition::Equals: return QLatin1String( " = " );
case SqlCondition::NotEquals: return QLatin1String( " <> " );
case SqlCondition::Is: return QLatin1String( " IS " );
case SqlCondition::IsNot: return QLatin1String( " IS NOT " );
case SqlCondition::Less: return QLatin1String( " < " );
case SqlCondition::LessOrEqual: return QLatin1String( " <= " );
case SqlCondition::Greater: return QLatin1String( " > " );
case SqlCondition::GreaterOrEqual: return QLatin1String( " >= " );
case SqlCondition::Like: return QLatin1String( " LIKE " );
}
qFatal( "Unknown compare operator." );
return QString();
}
QString SqlConditionalQueryBuilderBase::conditionToString(const SqlCondition& condition)
{
if ( condition.hasSubConditions() ) {
QStringList conds;
foreach ( const SqlCondition &c, condition.subConditions() )
conds << conditionToString( c );
if ( conds.size() == 1 )
return conds.first();
return QLatin1Char( '(' ) + conds.join( logicOperatorToString( condition.m_logicOp ) ) + QLatin1Char( ')' );
} else {
QString stmt = condition.m_column;
stmt += compareOperatorToString( condition.m_compareOp );
if ( condition.m_comparedColumn.isEmpty() ) {
if ( condition.m_comparedValue.isValid() ) {
if ( condition.m_comparedValue.userType() == qMetaTypeId<SqlNowType>() )
stmt += currentDateTime();
else
stmt += registerBindValue( condition.m_comparedValue );
} else if ( !condition.m_placeholder.isEmpty() ) {
stmt += condition.m_placeholder;
} else {
stmt += QLatin1String( "NULL" );
}
} else {
stmt += condition.m_comparedColumn;
}
return stmt;
}
}