-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSqlInsertQueryBuilder.h
91 lines (74 loc) · 3.23 KB
/
SqlInsertQueryBuilder.h
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
/*
Copyright (C) 2011-2013 Klarälvdalens Datakonsult AB,
a KDAB Group company, [email protected],
author Volker Krause <[email protected]>
author Andras Mantia <[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.
*/
#ifndef SQLINSERTQUERYBUILDER_H
#define SQLINSERTQUERYBUILDER_H
#include "sqlate_export.h"
#include "SqlQueryBuilderBase.h"
#include "SqlInternals_p.h"
#include <QDateTime>
#include <QStringList>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/not.hpp>
#include <boost/type_traits/is_same.hpp>
/** API to create INSERT queries in a safe way from code, without the risk of
* introducing SQL injection vulnerabilities or typos.
*/
class SQLATE_EXPORT SqlInsertQueryBuilder : public SqlQueryBuilderBase
{
public:
/// Create a new query builder for the given database
explicit SqlInsertQueryBuilder( const QSqlDatabase &db = QSqlDatabase::database() );
/// INSERT INTO table ( @p columnName , ... ) VALUES( @p value )
void addColumnValue( const QString &columnName, const QVariant &value );
/// INSERT INTO table ( @p columnName , ... ) VALUES( DEFAULT )
void addColumn( const QString& columnName );
template <typename Column>
void addColumnValue( const Column &, const typename Column::type &value )
{
Sql::warning<boost::is_same<typename Column::type, QDateTime>, UsageOfClientSideTime>::print();
addColumnValue( Column::sqlName(), QVariant::fromValue( value ) );
}
template <typename Column>
void addColumnValue( const Column &, SqlNullType )
{
BOOST_MPL_ASSERT(( boost::mpl::not_<typename Column::notNull> ));
addColumnValue( Column::sqlName(), QVariant() );
}
template <typename Column>
void addColumnValue( const Column &, SqlNowType now )
{
BOOST_MPL_ASSERT(( boost::is_same<typename Column::type, QDateTime> ));
addColumnValue( Column::sqlName(), QVariant::fromValue(now) );
}
template <typename Column>
void addColumn( const Column & )
{
addColumn( Column::sqlName() );
}
/// INSERT INTO ... DEFAULT VALUES
void setToDefaultValues();
/// Returns the created query object, when called first, the query object is assembled and prepared
SqlQuery& query();
private:
friend class InsertQueryBuilderTest;
friend class InsertTest;
QStringList m_columnNames; //holds the column names, used for unit testing
QMap<QString, QVariant> m_values; //holds the inserted values, used for unit testing
};
#endif