2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Bartosz Janiak (b.janiak@samsung.com)
20 * @brief DPL-ORM: Object-relational mapping for sqlite database, written on top of DPL.
30 #include <dpl/db/sql_connection.h>
31 #include <dpl/db/orm_interface.h>
32 #include <dpl/string.h>
33 #include <dpl/optional.h>
34 #include <dpl/shared_ptr.h>
35 #include <dpl/type_list.h>
36 #include <dpl/assert.h>
37 #include <dpl/foreach.h>
46 //TODO move to type utils
47 #define DPL_CHECK_TYPE_INSTANTIABILITY(type) \
53 #define DECLARE_COLUMN_TYPE_LIST() typedef DPL::TypeListDecl<
54 #define SELECTED_COLUMN(table_name, column_name) table_name::column_name,
55 #define DECLARE_COLUMN_TYPE_LIST_END(name) DPL::TypeListGuard>::Type name;
57 typedef size_t ColumnIndex;
58 typedef size_t ArgumentIndex;
59 typedef DPL::Optional<DPL::String> OptionalString;
60 typedef DPL::Optional<int> OptionalInteger;
61 typedef DPL::DB::SqlConnection::DataCommand DataCommand;
63 namespace RelationTypes {
64 extern const char Equal[];
65 extern const char LessThan[];
66 extern const char And[];
67 extern const char Or[];
68 extern const char Is[];
69 extern const char In[];
70 //TODO define more relation types
73 namespace DataCommandUtils {
74 //TODO move to DPL::DataCommand?
75 void BindArgument(DataCommand *command, ArgumentIndex index, int argument);
76 void BindArgument(DataCommand *command, ArgumentIndex index, const OptionalInteger& argument);
77 void BindArgument(DataCommand *command, ArgumentIndex index, const DPL::String& argument);
78 void BindArgument(DataCommand *command, ArgumentIndex index, const OptionalString& argument);
82 virtual ~Expression() {}
83 virtual std::string GetString() const = 0;
84 virtual ArgumentIndex BindTo(DataCommand *command, ArgumentIndex index) = 0;
87 typedef DPL::SharedPtr<Expression> ExpressionPtr;
89 template<const char* Operator, typename LeftExpression, typename RightExpression>
90 class BinaryExpression : public Expression {
92 LeftExpression m_leftExpression;
93 RightExpression m_rightExpression;
94 bool m_outerParenthesis;
96 BinaryExpression(const LeftExpression& leftExpression, const RightExpression& rightExpression, bool outerParenthesis = true) :
97 m_leftExpression(leftExpression),
98 m_rightExpression(rightExpression),
99 m_outerParenthesis(outerParenthesis)
102 virtual std::string GetString() const
104 return (m_outerParenthesis ? "( " : " " ) +
105 m_leftExpression.GetString() + " " + Operator + " " + m_rightExpression.GetString() +
106 (m_outerParenthesis ? " )" : " " ) ;
109 virtual ArgumentIndex BindTo(DataCommand *command, ArgumentIndex index)
111 index = m_leftExpression.BindTo(command, index);
112 return m_rightExpression.BindTo(command, index);
115 template<typename TableDefinition>
116 struct ValidForTable {
117 typedef std::pair<typename LeftExpression ::template ValidForTable<TableDefinition>::Yes ,
118 typename RightExpression::template ValidForTable<TableDefinition>::Yes >
123 template<typename LeftExpression, typename RightExpression>
124 BinaryExpression<RelationTypes::And, LeftExpression, RightExpression>
125 And(const LeftExpression& leftExpression, const RightExpression& rightExpression)
127 return BinaryExpression<RelationTypes::And, LeftExpression, RightExpression>
128 (leftExpression, rightExpression);
131 template<typename ArgumentType>
132 class ExpressionWithArgument : public Expression {
134 ArgumentType argument;
137 explicit ExpressionWithArgument(const ArgumentType& _argument) : argument(_argument) {}
139 virtual ArgumentIndex BindTo(DataCommand *command, ArgumentIndex index)
141 DataCommandUtils::BindArgument(command, index, argument);
147 template<typename ColumnData, const char* Relation>
148 class Compare : public ExpressionWithArgument<typename ColumnData::ColumnType> {
150 explicit Compare(typename ColumnData::ColumnType column) :
151 ExpressionWithArgument<typename ColumnData::ColumnType>(column)
154 virtual std::string GetString() const
156 std::string statement;
157 statement += ColumnData::GetTableName();
159 statement += ColumnData::GetColumnName();
161 statement += Relation;
166 template<typename TableDefinition>
167 struct ValidForTable {
168 typedef typename TableDefinition::ColumnList::template Contains<ColumnData> Yes;
171 #define ORM_DEFINE_COMPARE_EXPRESSION(name, relationType) \
172 template<typename ColumnData> \
173 class name : public Compare<ColumnData, RelationTypes::relationType> { \
175 name(typename ColumnData::ColumnType column) : \
176 Compare<ColumnData, RelationTypes::relationType>(column) \
180 ORM_DEFINE_COMPARE_EXPRESSION(Equals, Equal)
181 ORM_DEFINE_COMPARE_EXPRESSION(Is, Is)
183 template<typename ColumnData1, typename ColumnData2>
184 class CompareBinaryColumn {
186 std::string m_relation;
188 CompareBinaryColumn(const char* Relation) :
192 virtual ~CompareBinaryColumn() {}
194 virtual std::string GetString() const
196 std::string statement;
197 statement += ColumnData1::GetTableName();
199 statement += ColumnData1::GetColumnName();
201 statement += m_relation;
203 statement += ColumnData2::GetTableName();
205 statement += ColumnData2::GetColumnName();
211 template<typename ColumnData1, typename ColumnData2>
212 CompareBinaryColumn<ColumnData1, ColumnData2>
215 return CompareBinaryColumn<ColumnData1, ColumnData2>(RelationTypes::Equal);
218 template<typename ColumnData, const char* Relation>
219 class NumerousArguments : public Expression {
221 std::set<typename ColumnData::ColumnType> m_argumentList;
223 NumerousArguments(const std::set<typename ColumnData::ColumnType>& argumentList) : m_argumentList(argumentList) {}
225 virtual std::string GetString() const
227 std::string statement;
228 statement += ColumnData::GetColumnName();
230 statement += Relation;
233 int argumentCount = m_argumentList.size();
249 virtual ArgumentIndex BindTo(DataCommand *command, ArgumentIndex index)
251 ArgumentIndex argumentIndex = index;
252 FOREACH(argumentIt, m_argumentList)
254 DataCommandUtils::BindArgument(command, argumentIndex, *argumentIt);
257 return argumentIndex + 1;
260 template<typename TableDefinition>
261 struct ValidForTable {
262 typedef typename TableDefinition::ColumnList::template Contains<ColumnData> Yes;
266 #define ORM_DEFINE_COMPARE_EXPRESSION_NUMEROUS_ARGUMENTS(name, relationType) \
267 template<typename ColumnData> \
268 class name : public NumerousArguments<ColumnData, RelationTypes::relationType> { \
270 name(std::set<typename ColumnData::ColumnType> column) : \
271 NumerousArguments<ColumnData, RelationTypes::relationType>(column) \
275 ORM_DEFINE_COMPARE_EXPRESSION_NUMEROUS_ARGUMENTS(In, In)
277 template<typename ColumnType>
278 ColumnType GetColumnFromCommand(ColumnIndex columnIndex, DataCommand *command);
280 class CustomColumnBase {
282 CustomColumnBase() {}
283 virtual ~CustomColumnBase() {}
286 template<typename ColumnType>
287 class CustomColumn : public CustomColumnBase {
289 ColumnType m_columnData;
293 CustomColumn(ColumnType data)
298 void SetColumnData(ColumnType data)
303 ColumnType GetColumnData() const
309 template<typename ColumnList>
310 class CustomRowUtil {
312 static void MakeColumnList(std::vector<CustomColumnBase*>& columnList)
314 typedef CustomColumn<typename ColumnList::Head::ColumnType> Type;
315 Type* pColumn = new Type();
316 columnList.push_back(pColumn);
317 CustomRowUtil<typename ColumnList::Tail>::MakeColumnList(columnList);
320 static void CopyColumnList(const std::vector<CustomColumnBase*>& srcList, std::vector<CustomColumnBase*>& dstList)
322 CopyColumnList(srcList, dstList, 0);
325 static ColumnIndex GetColumnIndex(const std::string& columnName)
327 return GetColumnIndex(columnName, 0);
331 static void CopyColumnList(const std::vector<CustomColumnBase*>& srcList, std::vector<CustomColumnBase*>& dstList, ColumnIndex index)
333 typedef CustomColumn<typename ColumnList::Head::ColumnType> Type;
334 Type* pColumn = new Type(((Type*)(srcList.at(index)))->GetColumnData());
335 dstList.push_back(pColumn);
336 CustomRowUtil<typename ColumnList::Tail>::CopyColumnList(srcList, dstList, index + 1);
339 static ColumnIndex GetColumnIndex(const std::string& columnName, ColumnIndex index)
341 if (ColumnList::Head::GetColumnName() == columnName)
344 return CustomRowUtil<typename ColumnList::Tail>::GetColumnIndex(columnName, index + 1);
347 template<typename Other>
348 friend class CustomRowUtil;
352 class CustomRowUtil<DPL::TypeListGuard> {
354 static void MakeColumnList(std::vector<CustomColumnBase*>&) {}
356 static void CopyColumnList(const std::vector<CustomColumnBase*>&, std::vector<CustomColumnBase*>&, ColumnIndex) {}
357 static ColumnIndex GetColumnIndex(const std::string&, ColumnIndex) { return -1; }
359 template<typename Other>
360 friend class CustomRowUtil;
363 template<typename ColumnList>
366 std::vector<CustomColumnBase*> m_columns;
371 CustomRowUtil<ColumnList>::MakeColumnList(m_columns);
374 CustomRow(const CustomRow& r)
376 CustomRowUtil<ColumnList>::CopyColumnList(r.m_columns, m_columns);
381 while (!m_columns.empty())
383 CustomColumnBase* pCustomColumn = m_columns.back();
384 m_columns.pop_back();
386 delete pCustomColumn;
390 template<typename ColumnType>
391 void SetColumnData(ColumnIndex columnIndex, ColumnType data)
393 typedef CustomColumn<ColumnType> Type;
394 Assert(columnIndex < m_columns.size());
395 Type* pColumn = dynamic_cast<Type*>(m_columns.at(columnIndex));
397 pColumn->SetColumnData(data);
400 template<typename ColumnData>
401 typename ColumnData::ColumnType GetColumnData()
403 typedef CustomColumn<typename ColumnData::ColumnType> Type;
404 ColumnIndex index = CustomRowUtil<ColumnList>::GetColumnIndex(ColumnData::GetColumnName());
405 Assert(index < m_columns.size());
406 Type* pColumn = dynamic_cast<Type*>(m_columns.at(index));
408 return pColumn->GetColumnData();
412 template<typename CustomRow, typename ColumnType>
413 void SetColumnData(CustomRow& row, ColumnType columnData, ColumnIndex columnIndex)
415 row.SetColumnData<ColumnType>(columnIndex, columnData);
418 template<typename ColumnList, typename CustomRow>
419 class FillCustomRowUtil {
421 static void FillCustomRow(CustomRow& row, DataCommand* command)
423 FillCustomRow(row, 0, command);
427 static void FillCustomRow(CustomRow& row, ColumnIndex columnIndex, DataCommand* command)
429 typename ColumnList::Head::ColumnType columnData;
430 columnData = GetColumnFromCommand<typename ColumnList::Head::ColumnType>(columnIndex, command);
431 SetColumnData<CustomRow, typename ColumnList::Head::ColumnType>(row, columnData, columnIndex);
432 FillCustomRowUtil<typename ColumnList::Tail, CustomRow>::FillCustomRow(row, columnIndex + 1, command);
435 template<typename Other, typename OtherRow>
436 friend class FillCustomRowUtil;
439 template<typename CustomRow>
440 class FillCustomRowUtil<DPL::TypeListGuard, CustomRow> {
442 static void FillCustomRow(CustomRow&, ColumnIndex, DataCommand *)
443 { /* do nothing, we're past the last element of column list */ }
445 template<typename Other, typename OtherRow>
446 friend class FillCustomRowUtil;
449 template<typename ColumnList, typename Row>
452 static void FillRow(Row& row, DataCommand *command)
454 FillRow(row, 0, command);
458 static void FillRow(Row& row, ColumnIndex columnIndex, DataCommand *command)
460 typename ColumnList::Head::ColumnType rowField;
461 rowField = GetColumnFromCommand<typename ColumnList::Head::ColumnType>(columnIndex, command);
462 ColumnList::Head::SetRowField(row, rowField);
463 FillRowUtil<typename ColumnList::Tail, Row>::FillRow(row, columnIndex + 1, command);
466 template<typename Other, typename OtherRow>
467 friend class FillRowUtil;
470 template<typename Row>
471 class FillRowUtil<DPL::TypeListGuard, Row> {
473 static void FillRow(Row&, ColumnIndex, DataCommand *)
474 { /* do nothing, we're past the last element of column list */ }
476 template<typename Other, typename OtherRow>
477 friend class FillRowUtil;
480 template<typename ColumnList>
483 static std::string GetColumnNames()
486 result = ColumnList::Head::GetTableName();
488 result += ColumnList::Head::GetColumnName();
489 if (ColumnList::Tail::Size > 0)
492 return result += JoinUtil<typename ColumnList::Tail>::GetColumnNames();
495 static std::string GetJoinTableName(const std::string& tableName)
497 std::string joinTableName = ColumnList::Head::GetTableName();
498 if (tableName.find(joinTableName) == std::string::npos)
499 return joinTableName;
501 return JoinUtil<typename ColumnList::Tail>::GetJoinTableName(tableName);
506 class JoinUtil<DPL::TypeListGuard> {
508 static std::string GetColumnNames() { return ""; }
509 static std::string GetJoinTableName(std::string) { return ""; }
514 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
515 DECLARE_EXCEPTION_TYPE(Base, SelectReuseWithDifferentQuerySignature)
516 DECLARE_EXCEPTION_TYPE(Base, RowFieldNotInitialized)
517 DECLARE_EXCEPTION_TYPE(Base, EmptyUpdateStatement)
520 template<typename TableDefinition>
524 explicit Query(IOrmInterface* interface) :
525 m_interface(interface),
532 if (m_command == NULL)
535 TableDefinition::FreeTableDataCommand(m_command, m_interface);
538 IOrmInterface* m_interface;
539 DataCommand *m_command;
540 std::string m_commandString;
541 ArgumentIndex m_bindArgumentIndex;
544 template<typename TableDefinition>
545 class QueryWithWhereClause : public Query<TableDefinition>
548 ExpressionPtr m_whereExpression;
552 if ( !!m_whereExpression )
554 this->m_commandString += " WHERE ";
555 this->m_commandString += m_whereExpression->GetString();
561 if ( !!m_whereExpression )
563 this->m_bindArgumentIndex = m_whereExpression->BindTo(
564 this->m_command, this->m_bindArgumentIndex);
569 explicit QueryWithWhereClause(IOrmInterface* interface) :
570 Query<TableDefinition>(interface)
574 template<typename Expression>
575 void Where(const Expression& expression)
577 DPL_CHECK_TYPE_INSTANTIABILITY(typename Expression::template ValidForTable<TableDefinition>::Yes);
578 if ( !!m_whereExpression && ( typeid(Expression) != typeid(*m_whereExpression) ) )
580 std::ostringstream str;
581 str << "Current ORM implementation doesn't allow to reuse Select"
582 " instance with different query signature (particularly "
583 "WHERE on different column).\n";
585 str << this->m_commandString;
586 ThrowMsg(Exception::SelectReuseWithDifferentQuerySignature,
589 //TODO maybe don't make a copy here but just generate the string part of the query.
590 m_whereExpression.Reset(new Expression(expression));
595 template<typename TableDefinition>
596 class Delete : public QueryWithWhereClause<TableDefinition>
601 if ( !this->m_command)
603 this->m_commandString = "DELETE FROM ";
604 this->m_commandString += TableDefinition::GetName();
606 QueryWithWhereClause<TableDefinition>::Prepare();
608 this->m_command = TableDefinition::AllocTableDataCommand(
609 this->m_commandString.c_str(),
610 Query<TableDefinition>::m_interface);
611 LogPedantic("Prepared SQL command " << this->m_commandString);
617 this->m_bindArgumentIndex = 1;
618 QueryWithWhereClause<TableDefinition>::Bind();
622 explicit Delete(IOrmInterface *interface = NULL) :
623 QueryWithWhereClause<TableDefinition>(interface)
631 this->m_command->Step();
632 this->m_command->Reset();
636 template<typename TableDefinition>
637 class Insert : public Query<TableDefinition>
640 typedef typename TableDefinition::Row Row;
641 typedef DPL::DB::SqlConnection::RowID RowID;
644 DPL::Optional<std::string> m_orClause;
647 class PrepareVisitor {
649 std::string m_columnNames;
650 std::string m_values;
652 template<typename ColumnType>
653 void Visit(const char* name, const ColumnType&, bool isSet)
657 if ( !m_columnNames.empty() )
659 m_columnNames += ", ";
662 m_columnNames += name;
670 if ( !this->m_command )
672 this->m_commandString = "INSERT ";
675 this->m_commandString += " OR " + *m_orClause + " ";
677 this->m_commandString += "INTO ";
678 this->m_commandString += TableDefinition::GetName();
680 PrepareVisitor visitor;
681 m_row.VisitColumns(visitor);
683 this->m_commandString += " ( " + visitor.m_columnNames + " ) ";
684 this->m_commandString += "VALUES ( " + visitor.m_values + " )";
686 LogPedantic("Prepared SQL command " << this->m_commandString);
687 this->m_command = TableDefinition::AllocTableDataCommand(
688 this->m_commandString.c_str(),
689 Query<TableDefinition>::m_interface);
695 DataCommand *m_command;
696 ArgumentIndex m_bindArgumentIndex;
698 BindVisitor(DataCommand *command) :
700 m_bindArgumentIndex(1)
703 template<typename ColumnType>
704 void Visit(const char*, const ColumnType& value, bool isSet)
708 DataCommandUtils::BindArgument(m_command, m_bindArgumentIndex, value);
709 m_bindArgumentIndex++;
716 BindVisitor visitor(this->m_command);
717 m_row.VisitColumns(visitor);
722 IOrmInterface* interface = NULL,
723 const DPL::Optional<std::string>& orClause = DPL::Optional<std::string>::Null) :
724 Query<TableDefinition>(interface),
729 void Values(const Row& row)
731 if ( this->m_command )
733 if ( !row.IsSignatureMatching(m_row) )
735 ThrowMsg(Exception::SelectReuseWithDifferentQuerySignature,
736 "Current ORM implementation doesn't allow to reuse Insert instance "
737 "with different query signature.");
747 this->m_command->Step();
749 RowID result = TableDefinition::GetLastInsertRowID(
750 Query<TableDefinition>::m_interface);
752 this->m_command->Reset();
757 template<typename TableDefinition>
758 class Select : public QueryWithWhereClause<TableDefinition>
761 typedef typename TableDefinition::ColumnList ColumnList;
762 typedef typename TableDefinition::Row Row;
764 typedef std::list<Row> RowList;
766 DPL::Optional<std::string> m_orderBy;
767 std::string m_JoinClause;
768 bool m_distinctResults;
770 void Prepare(const char* selectColumnName)
772 if ( !this->m_command )
774 this->m_commandString = "SELECT ";
775 if (m_distinctResults)
776 this->m_commandString += "DISTINCT ";
777 this->m_commandString += selectColumnName;
778 this->m_commandString += " FROM ";
779 this->m_commandString += TableDefinition::GetName();
781 this->m_commandString += m_JoinClause;
783 QueryWithWhereClause<TableDefinition>::Prepare();
785 if ( !m_orderBy.IsNull() )
787 this->m_commandString += " ORDER BY " + *m_orderBy;
790 this->m_command = TableDefinition::AllocTableDataCommand(
791 this->m_commandString.c_str(),
792 Query<TableDefinition>::m_interface);
794 LogPedantic("Prepared SQL command " << this->m_commandString);
800 this->m_bindArgumentIndex = 1;
801 QueryWithWhereClause<TableDefinition>::Bind();
804 template<typename ColumnType>
805 ColumnType GetColumn(ColumnIndex columnIndex)
807 return GetColumnFromCommand<ColumnType>(columnIndex, this->m_command);
813 FillRowUtil<ColumnList, Row>::FillRow(row, this->m_command);
817 template<typename ColumnList, typename CustomRow>
818 CustomRow GetCustomRow()
821 FillCustomRowUtil<ColumnList, CustomRow>::FillCustomRow(row, this->m_command);
827 explicit Select(IOrmInterface *interface = NULL) :
828 QueryWithWhereClause<TableDefinition>(interface),
829 m_distinctResults(false)
835 m_distinctResults = true;
838 void OrderBy(const std::string& orderBy)
843 template<typename ColumnList, typename Expression>
844 void Join(const Expression& expression) {
845 std::string usedTableNames = TableDefinition::GetName();
846 if (!m_JoinClause.empty())
847 usedTableNames += m_JoinClause;
849 this->m_JoinClause += " JOIN ";
850 this->m_JoinClause += JoinUtil<ColumnList>::GetJoinTableName(usedTableNames);
851 this->m_JoinClause += " ON ";
852 this->m_JoinClause += expression.GetString();
855 template<typename ColumnData>
856 typename ColumnData::ColumnType GetSingleValue()
858 Prepare(ColumnData::GetColumnName());
860 this->m_command->Step();
862 typename ColumnData::ColumnType result =
863 GetColumn<typename ColumnData::ColumnType>(0);
865 this->m_command->Reset();
869 //TODO return range - pair of custom iterators
870 template<typename ColumnData>
871 std::list<typename ColumnData::ColumnType> GetValueList()
873 Prepare(ColumnData::GetColumnName());
876 std::list<typename ColumnData::ColumnType> resultList;
878 while (this->m_command->Step())
879 resultList.push_back(GetColumn<typename ColumnData::ColumnType>(0));
881 this->m_command->Reset();
889 this->m_command->Step();
891 Row result = GetRow();
893 this->m_command->Reset();
897 //TODO return range - pair of custom iterators
905 while (this->m_command->Step())
906 resultList.push_back(GetRow());
908 this->m_command->Reset();
912 template<typename ColumnList, typename CustomRow>
913 CustomRow GetCustomSingleRow()
915 Prepare(JoinUtil<ColumnList>::GetColumnNames().c_str());
917 this->m_command->Step();
919 CustomRow result = GetCustomRow<ColumnList, CustomRow>();
921 this->m_command->Reset();
925 template<typename ColumnList, typename CustomRow>
926 std::list<CustomRow> GetCustomRowList()
928 Prepare(JoinUtil<ColumnList>::GetColumnNames().c_str());
931 std::list<CustomRow> resultList;
933 while (this->m_command->Step())
934 resultList.push_back(GetCustomRow<ColumnList, CustomRow>());
936 this->m_command->Reset();
941 template<typename TableDefinition>
942 class Update : public QueryWithWhereClause<TableDefinition> {
944 typedef typename TableDefinition::Row Row;
947 DPL::Optional<std::string> m_orClause;
950 class PrepareVisitor {
952 std::string m_setExpressions;
954 template<typename ColumnType>
955 void Visit(const char* name, const ColumnType&, bool isSet)
959 if ( !m_setExpressions.empty() )
961 m_setExpressions += ", ";
963 m_setExpressions += name;
964 m_setExpressions += " = ";
965 m_setExpressions += "?";
972 if ( !this->m_command )
974 this->m_commandString = "UPDATE ";
977 this->m_commandString += " OR " + *m_orClause + " ";
979 this->m_commandString += TableDefinition::GetName();
980 this->m_commandString += " SET ";
982 // got through row columns and values
983 PrepareVisitor visitor;
984 m_row.VisitColumns(visitor);
986 if(visitor.m_setExpressions.empty())
988 ThrowMsg(Exception::EmptyUpdateStatement, "No SET expressions in update statement");
991 this->m_commandString += visitor.m_setExpressions;
994 QueryWithWhereClause<TableDefinition>::Prepare();
996 this->m_command = TableDefinition::AllocTableDataCommand(
997 this->m_commandString.c_str(),
998 Query<TableDefinition>::m_interface);
999 LogPedantic("Prepared SQL command " << this->m_commandString);
1005 DataCommand *m_command;
1008 ArgumentIndex m_bindArgumentIndex;
1010 BindVisitor(DataCommand *command) :
1012 m_bindArgumentIndex(1)
1015 template<typename ColumnType>
1016 void Visit(const char*, const ColumnType& value, bool isSet)
1020 DataCommandUtils::BindArgument(m_command, m_bindArgumentIndex, value);
1021 m_bindArgumentIndex++;
1028 BindVisitor visitor(this->m_command);
1029 m_row.VisitColumns(visitor);
1031 this->m_bindArgumentIndex = visitor.m_bindArgumentIndex;
1032 QueryWithWhereClause<TableDefinition>::Bind();
1037 explicit Update(IOrmInterface *interface = NULL,
1038 const DPL::Optional<std::string>& orClause = DPL::Optional<std::string>::Null) :
1039 QueryWithWhereClause<TableDefinition>(interface),
1040 m_orClause(orClause)
1044 void Values(const Row& row)
1046 if ( this->m_command )
1048 if ( !row.IsSignatureMatching(m_row) )
1050 ThrowMsg(Exception::SelectReuseWithDifferentQuerySignature,
1051 "Current ORM implementation doesn't allow to reuse Update instance "
1052 "with different query signature.");
1062 this->m_command->Step();
1063 this->m_command->Reset();