dcc67a58e502a1771c5e61544275077fbed55de4
[framework/web/wrt-commons.git] / modules / db / include / dpl / db / orm.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        orm.h
18  * @author      Bartosz Janiak (b.janiak@samsung.com)
19  * @version     1.0
20  * @brief       DPL-ORM: Object-relational mapping for sqlite database, written on top of DPL.
21  */
22
23 #include <cstdlib>
24 #include <cstdio>
25 #include <string>
26 #include <typeinfo>
27 #include <utility>
28 #include <set>
29 #include <memory>
30
31 #include <dpl/db/sql_connection.h>
32 #include <dpl/db/orm_interface.h>
33 #include <dpl/string.h>
34 #include <dpl/optional.h>
35 #include <dpl/type_list.h>
36 #include <dpl/assert.h>
37 #include <dpl/foreach.h>
38
39 #ifndef DPL_ORM_H
40 #define DPL_ORM_H
41
42 namespace DPL {
43 namespace DB {
44 namespace ORM {
45
46 //TODO move to type utils
47 #define DPL_CHECK_TYPE_INSTANTIABILITY(type) \
48     { \
49         type _ignored_; \
50         (void)_ignored_; \
51     }
52
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;
56
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;
62
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
71 }
72
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);
79 }
80 class __attribute__ ((visibility("hidden"))) Expression {
81 public:
82     virtual ~Expression() {}
83     virtual std::string GetString() const = 0;
84     virtual ArgumentIndex BindTo(DataCommand *command, ArgumentIndex index) = 0;
85 };
86
87 typedef std::shared_ptr<Expression> ExpressionPtr;
88
89 template<const char* Operator, typename LeftExpression, typename RightExpression>
90 class __attribute__ ((visibility("hidden"))) BinaryExpression : public Expression {
91 protected:
92     LeftExpression  m_leftExpression;
93     RightExpression m_rightExpression;
94     bool            m_outerParenthesis;
95 public:
96     BinaryExpression(const LeftExpression& leftExpression, const RightExpression& rightExpression, bool outerParenthesis = true) :
97         m_leftExpression(leftExpression),
98         m_rightExpression(rightExpression),
99         m_outerParenthesis(outerParenthesis)
100     {}
101
102     virtual std::string GetString() const
103     {
104         return  (m_outerParenthesis ? "( " : " " ) +
105                  m_leftExpression.GetString() + " " + Operator + " " + m_rightExpression.GetString() +
106                 (m_outerParenthesis ? " )" : " " ) ;
107     }
108
109     virtual ArgumentIndex BindTo(DataCommand *command, ArgumentIndex index)
110     {
111         index = m_leftExpression.BindTo(command, index);
112         return  m_rightExpression.BindTo(command, index);
113     }
114
115     template<typename TableDefinition>
116     struct ValidForTable {
117         typedef std::pair<typename LeftExpression ::template ValidForTable<TableDefinition>::Yes ,
118                           typename RightExpression::template ValidForTable<TableDefinition>::Yes >
119                 Yes;
120     };
121 };
122
123 template<typename LeftExpression, typename RightExpression>
124 BinaryExpression<RelationTypes::And, LeftExpression, RightExpression>
125     And(const LeftExpression& leftExpression, const RightExpression& rightExpression)
126 {
127     return BinaryExpression<RelationTypes::And, LeftExpression, RightExpression>
128             (leftExpression, rightExpression);
129 }
130
131 template<typename ArgumentType>
132 class __attribute__ ((visibility("hidden"))) ExpressionWithArgument : public Expression {
133 protected:
134     ArgumentType argument;
135
136 public:
137     explicit ExpressionWithArgument(const ArgumentType& _argument) : argument(_argument) {}
138
139     virtual ArgumentIndex BindTo(DataCommand *command, ArgumentIndex index)
140     {
141         DataCommandUtils::BindArgument(command, index, argument);
142         return index + 1;
143     }
144 };
145
146 template<typename ColumnData, const char* Relation>
147 class __attribute__ ((visibility("hidden"))) Compare : public ExpressionWithArgument<typename ColumnData::ColumnType> {
148 public:
149     explicit Compare(typename ColumnData::ColumnType column) :
150         ExpressionWithArgument<typename ColumnData::ColumnType>(column)
151     {}
152
153     virtual std::string GetString() const
154     {
155         std::string statement;
156         statement += ColumnData::GetTableName();
157         statement += ".";
158         statement += ColumnData::GetColumnName();
159         statement += " ";
160         statement += Relation;
161         statement += " ?";
162         return statement;
163     }
164
165     template<typename TableDefinition>
166     struct ValidForTable {
167         typedef typename TableDefinition::ColumnList::template Contains<ColumnData> Yes;
168     };
169 };
170 #define ORM_DEFINE_COMPARE_EXPRESSION(name, relationType)                      \
171     template<typename ColumnData>                                              \
172     class __attribute__ ((visibility("hidden"))) name : public Compare<ColumnData, RelationTypes::relationType> {     \
173     public:                                                                    \
174         name(typename ColumnData::ColumnType column) :                         \
175             Compare<ColumnData, RelationTypes::relationType>(column)           \
176         {}                                                                     \
177     };
178
179 ORM_DEFINE_COMPARE_EXPRESSION(Equals, Equal)
180 ORM_DEFINE_COMPARE_EXPRESSION(Is, Is)
181
182 template<typename ColumnData1, typename ColumnData2>
183 class __attribute__ ((visibility("hidden"))) CompareBinaryColumn {
184 private:
185     std::string m_relation;
186 public:
187     CompareBinaryColumn(const char* Relation) :
188       m_relation(Relation)
189     {}
190
191     virtual ~CompareBinaryColumn() {}
192
193     virtual std::string GetString() const
194     {
195         std::string statement;
196         statement += ColumnData1::GetTableName();
197         statement += ".";
198         statement += ColumnData1::GetColumnName();
199         statement += " ";
200         statement += m_relation;
201         statement += " ";
202         statement += ColumnData2::GetTableName();
203         statement += ".";
204         statement += ColumnData2::GetColumnName();
205
206         return statement;
207     }
208 };
209
210 template<typename ColumnData1, typename ColumnData2>
211 CompareBinaryColumn<ColumnData1, ColumnData2>
212     Equal()
213 {
214     return CompareBinaryColumn<ColumnData1, ColumnData2>(RelationTypes::Equal);
215 }
216
217 template<typename ColumnData, const char* Relation>
218 class __attribute__ ((visibility("hidden"))) NumerousArguments : public Expression {
219 protected:
220     std::set<typename ColumnData::ColumnType> m_argumentList;
221 public:
222     NumerousArguments(const std::set<typename ColumnData::ColumnType>& argumentList) : m_argumentList(argumentList) {}
223
224     virtual std::string GetString() const
225     {
226         std::string statement;
227         statement += ColumnData::GetColumnName();
228         statement += " ";
229         statement += Relation;
230         statement += " ( ";
231
232         int argumentCount = m_argumentList.size();
233         while(argumentCount)
234         {
235             statement += "?";
236             argumentCount--;
237             if (argumentCount)
238             {
239                 statement += ", ";
240             }
241         }
242
243         statement += " )";
244
245         return statement;
246     }
247
248     virtual ArgumentIndex BindTo(DataCommand *command, ArgumentIndex index)
249     {
250         ArgumentIndex argumentIndex = index;
251         FOREACH(argumentIt, m_argumentList)
252         {
253             DataCommandUtils::BindArgument(command, argumentIndex, *argumentIt);
254             argumentIndex++;
255         }
256         return  argumentIndex + 1;
257     }
258
259     template<typename TableDefinition>
260     struct ValidForTable {
261         typedef typename TableDefinition::ColumnList::template Contains<ColumnData> Yes;
262     };
263 };
264
265 #define ORM_DEFINE_COMPARE_EXPRESSION_NUMEROUS_ARGUMENTS(name, relationType)                      \
266     template<typename ColumnData>                                              \
267     class __attribute__ ((visibility("hidden"))) name : public NumerousArguments<ColumnData, RelationTypes::relationType> {     \
268     public:                                                                    \
269         name(std::set<typename ColumnData::ColumnType> column) :                         \
270             NumerousArguments<ColumnData, RelationTypes::relationType>(column)           \
271         {}                                                                     \
272     };
273
274 ORM_DEFINE_COMPARE_EXPRESSION_NUMEROUS_ARGUMENTS(In, In)
275
276 template<typename ColumnType>
277 ColumnType GetColumnFromCommand(ColumnIndex columnIndex, DataCommand *command);
278
279 class __attribute__ ((visibility("hidden"))) CustomColumnBase {
280 public:
281     CustomColumnBase() {}
282     virtual ~CustomColumnBase() {}
283 };
284
285 template<typename ColumnType>
286 class __attribute__ ((visibility("hidden"))) CustomColumn : public CustomColumnBase {
287 private:
288     ColumnType m_columnData;
289
290 public:
291     CustomColumn() {}
292     CustomColumn(ColumnType data)
293     {
294         m_columnData = data;
295     }
296
297     void SetColumnData(ColumnType data)
298     {
299         m_columnData = data;
300     }
301
302     ColumnType GetColumnData() const
303     {
304         return m_columnData;
305     }
306 };
307
308 template<typename ColumnList>
309 class __attribute__ ((visibility("hidden"))) CustomRowUtil {
310 public:
311     static void MakeColumnList(std::vector<CustomColumnBase*>& columnList)
312     {
313         typedef CustomColumn<typename ColumnList::Head::ColumnType> Type;
314         Type* pColumn = new Type();
315         columnList.push_back(pColumn);
316         CustomRowUtil<typename ColumnList::Tail>::MakeColumnList(columnList);
317     }
318
319     static void CopyColumnList(const std::vector<CustomColumnBase*>& srcList, std::vector<CustomColumnBase*>& dstList)
320     {
321         CopyColumnList(srcList, dstList, 0);
322     }
323
324     static ColumnIndex GetColumnIndex(const std::string& columnName)
325     {
326         return GetColumnIndex(columnName, 0);
327     }
328
329 private:
330     static void CopyColumnList(const std::vector<CustomColumnBase*>& srcList, std::vector<CustomColumnBase*>& dstList, ColumnIndex index)
331     {
332         typedef CustomColumn<typename ColumnList::Head::ColumnType> Type;
333         Type* pColumn = new Type(((Type*)(srcList.at(index)))->GetColumnData());
334         dstList.push_back(pColumn);
335         CustomRowUtil<typename ColumnList::Tail>::CopyColumnList(srcList, dstList, index + 1);
336     }
337
338     static ColumnIndex GetColumnIndex(const std::string& columnName, ColumnIndex index)
339     {
340         if (ColumnList::Head::GetColumnName() == columnName)
341             return index;
342
343         return CustomRowUtil<typename ColumnList::Tail>::GetColumnIndex(columnName, index + 1);
344     }
345
346 template<typename Other>
347 friend class CustomRowUtil;
348 };
349
350 template<>
351 class __attribute__ ((visibility("hidden"))) CustomRowUtil<DPL::TypeListGuard> {
352 public:
353     static void MakeColumnList(std::vector<CustomColumnBase*>&) {}
354 private:
355     static void CopyColumnList(const std::vector<CustomColumnBase*>&, std::vector<CustomColumnBase*>&, ColumnIndex) {}
356     static ColumnIndex GetColumnIndex(const std::string&, ColumnIndex) { return -1; }
357
358 template<typename Other>
359 friend class CustomRowUtil;
360 };
361
362 template<typename ColumnList>
363 class __attribute__ ((visibility("hidden"))) CustomRow {
364 private:
365     std::vector<CustomColumnBase*> m_columns;
366
367 public:
368     CustomRow()
369     {
370         CustomRowUtil<ColumnList>::MakeColumnList(m_columns);
371     }
372
373     CustomRow(const CustomRow& r)
374     {
375         CustomRowUtil<ColumnList>::CopyColumnList(r.m_columns, m_columns);
376     }
377
378     virtual ~CustomRow()
379     {
380         while (!m_columns.empty())
381         {
382             CustomColumnBase* pCustomColumn = m_columns.back();
383             m_columns.pop_back();
384             if (pCustomColumn)
385                 delete pCustomColumn;
386         }
387     }
388
389     template<typename ColumnType>
390     void SetColumnData(ColumnIndex columnIndex, ColumnType data)
391     {
392         typedef CustomColumn<ColumnType> Type;
393         Assert(columnIndex < m_columns.size());
394         Type* pColumn = dynamic_cast<Type*>(m_columns.at(columnIndex));
395         Assert(pColumn);
396         pColumn->SetColumnData(data);
397     }
398
399     template<typename ColumnData>
400     typename ColumnData::ColumnType GetColumnData()
401     {
402         typedef CustomColumn<typename ColumnData::ColumnType> Type;
403         ColumnIndex index = CustomRowUtil<ColumnList>::GetColumnIndex(ColumnData::GetColumnName());
404         Assert(index < m_columns.size());
405         Type* pColumn = dynamic_cast<Type*>(m_columns.at(index));
406         Assert(pColumn);
407         return pColumn->GetColumnData();
408     }
409 };
410
411 template<typename CustomRow, typename ColumnType>
412 void SetColumnData(CustomRow& row, ColumnType columnData, ColumnIndex columnIndex)
413 {
414     row.SetColumnData<ColumnType>(columnIndex, columnData);
415 }
416
417 template<typename ColumnList, typename CustomRow>
418 class  __attribute__ ((visibility("hidden"))) FillCustomRowUtil {
419 public:
420     static void FillCustomRow(CustomRow& row, DataCommand* command)
421     {
422         FillCustomRow(row, 0, command);
423     }
424
425 private:
426     static void FillCustomRow(CustomRow& row, ColumnIndex columnIndex, DataCommand* command)
427     {
428         typename ColumnList::Head::ColumnType columnData;
429         columnData = GetColumnFromCommand<typename ColumnList::Head::ColumnType>(columnIndex, command);
430         SetColumnData<CustomRow, typename ColumnList::Head::ColumnType>(row, columnData, columnIndex);
431         FillCustomRowUtil<typename ColumnList::Tail, CustomRow>::FillCustomRow(row, columnIndex + 1, command);
432     }
433
434 template<typename Other, typename OtherRow>
435 friend class FillCustomRowUtil;
436 };
437
438 template<typename CustomRow>
439 class  __attribute__ ((visibility("hidden"))) FillCustomRowUtil<DPL::TypeListGuard, CustomRow> {
440 private:
441     static void FillCustomRow(CustomRow&, ColumnIndex, DataCommand *)
442     { /* do nothing, we're past the last element of column list */ }
443
444 template<typename Other, typename OtherRow>
445 friend class FillCustomRowUtil;
446 };
447
448 template<typename ColumnList, typename Row>
449 class  __attribute__ ((visibility("hidden"))) FillRowUtil {
450 public:
451     static void FillRow(Row& row, DataCommand *command)
452     {
453         FillRow(row, 0, command);
454     }
455
456 private:
457     static void FillRow(Row& row, ColumnIndex columnIndex, DataCommand *command)
458     {
459         typename ColumnList::Head::ColumnType rowField;
460         rowField = GetColumnFromCommand<typename ColumnList::Head::ColumnType>(columnIndex, command);
461         ColumnList::Head::SetRowField(row, rowField);
462         FillRowUtil<typename ColumnList::Tail, Row>::FillRow(row, columnIndex + 1, command);
463     }
464
465 template<typename Other, typename OtherRow>
466 friend class FillRowUtil;
467 };
468
469 template<typename Row>
470 class  __attribute__ ((visibility("hidden"))) FillRowUtil<DPL::TypeListGuard, Row> {
471 private:
472     static void FillRow(Row&, ColumnIndex, DataCommand *)
473     { /* do nothing, we're past the last element of column list */ }
474
475 template<typename Other, typename OtherRow>
476 friend class FillRowUtil;
477 };
478
479 template<typename ColumnList>
480 class  __attribute__ ((visibility("hidden"))) JoinUtil {
481 public:
482     static std::string GetColumnNames()
483     {
484         std::string result;
485         result = ColumnList::Head::GetTableName();
486         result += ".";
487         result += ColumnList::Head::GetColumnName();
488         if (ColumnList::Tail::Size > 0)
489             result += ", ";
490
491         return result += JoinUtil<typename ColumnList::Tail>::GetColumnNames();
492     }
493
494     static std::string GetJoinTableName(const std::string& tableName)
495     {
496         std::string joinTableName = ColumnList::Head::GetTableName();
497         if (tableName.find(joinTableName) == std::string::npos)
498             return joinTableName;
499
500         return JoinUtil<typename ColumnList::Tail>::GetJoinTableName(tableName);
501     }
502 };
503
504 template<>
505 class  __attribute__ ((visibility("hidden"))) JoinUtil<DPL::TypeListGuard> {
506 public:
507     static std::string GetColumnNames() { return ""; }
508     static std::string GetJoinTableName(std::string) { return ""; }
509 };
510
511 class Exception {
512 public:
513     DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
514     DECLARE_EXCEPTION_TYPE(Base, SelectReuseWithDifferentQuerySignature)
515     DECLARE_EXCEPTION_TYPE(Base, RowFieldNotInitialized)
516     DECLARE_EXCEPTION_TYPE(Base, EmptyUpdateStatement)
517 };
518
519 template<typename TableDefinition>
520 class  __attribute__ ((visibility("hidden"))) Query
521 {
522 protected:
523     explicit Query(IOrmInterface* interface) :
524         m_interface(interface),
525         m_command(NULL)
526     {
527     }
528
529     virtual ~Query()
530     {
531         if (m_command == NULL)
532             return;
533
534         TableDefinition::FreeTableDataCommand(m_command, m_interface);
535     }
536
537     IOrmInterface* m_interface;
538     DataCommand *m_command;
539     std::string m_commandString;
540     ArgumentIndex m_bindArgumentIndex;
541 };
542
543 template<typename TableDefinition>
544 class  __attribute__ ((visibility("hidden"))) QueryWithWhereClause : public Query<TableDefinition>
545 {
546 protected:
547     ExpressionPtr m_whereExpression;
548
549     void Prepare()
550     {
551         if ( !!m_whereExpression )
552         {
553             this->m_commandString += " WHERE ";
554             this->m_commandString += m_whereExpression->GetString();
555         }
556     }
557
558     void Bind()
559     {
560         if ( !!m_whereExpression )
561         {
562             this->m_bindArgumentIndex = m_whereExpression->BindTo(
563                 this->m_command, this->m_bindArgumentIndex);
564         }
565     }
566
567 public:
568     explicit QueryWithWhereClause(IOrmInterface* interface) :
569         Query<TableDefinition>(interface)
570     {
571     }
572
573     template<typename Expression>
574     void Where(const Expression& expression)
575     {
576         DPL_CHECK_TYPE_INSTANTIABILITY(typename Expression::template ValidForTable<TableDefinition>::Yes);
577         if ( !!m_whereExpression && ( typeid(Expression) != typeid(*m_whereExpression) ) )
578         {
579             std::ostringstream str;
580             str << "Current ORM implementation doesn't allow to reuse Select"
581                     " instance with different query signature (particularly "
582                     "WHERE on different column).\n";
583             str << "Query: ";
584             str << this->m_commandString;
585             ThrowMsg(Exception::SelectReuseWithDifferentQuerySignature,
586                 str.str());
587         }
588         //TODO maybe don't make a copy here but just generate the string part of the query.
589         m_whereExpression.reset(new Expression(expression));
590     }
591
592 };
593
594 template<typename TableDefinition>
595 class  __attribute__ ((visibility("hidden"))) Delete : public QueryWithWhereClause<TableDefinition>
596 {
597 protected:
598     void Prepare()
599     {
600         if ( !this->m_command)
601         {
602             this->m_commandString  = "DELETE FROM ";
603             this->m_commandString += TableDefinition::GetName();
604
605             QueryWithWhereClause<TableDefinition>::Prepare();
606
607             this->m_command = TableDefinition::AllocTableDataCommand(
608                     this->m_commandString.c_str(),
609                     Query<TableDefinition>::m_interface);
610             LogPedantic("Prepared SQL command " << this->m_commandString);
611         }
612     }
613
614     void Bind()
615     {
616         this->m_bindArgumentIndex = 1;
617         QueryWithWhereClause<TableDefinition>::Bind();
618     }
619
620 public:
621     explicit Delete(IOrmInterface *interface = NULL) :
622         QueryWithWhereClause<TableDefinition>(interface)
623     {
624     }
625
626     void Execute()
627     {
628         Prepare();
629         Bind();
630         this->m_command->Step();
631         this->m_command->Reset();
632     }
633 };
634
635 namespace {
636 class BindVisitor {
637 private:
638     DataCommand *m_command;
639 public:
640     ArgumentIndex m_bindArgumentIndex;
641
642     BindVisitor(DataCommand *command) :
643         m_command(command),
644         m_bindArgumentIndex(1)
645     {}
646
647     template<typename ColumnType>
648     void Visit(const char*, const ColumnType& value, bool isSet)
649     {
650         if ( isSet )
651         {
652             DataCommandUtils::BindArgument(m_command, m_bindArgumentIndex, value);
653             m_bindArgumentIndex++;
654         }
655     }
656 };
657 } //anonymous namespace
658 template<typename TableDefinition>
659 class __attribute__ ((visibility("hidden"))) Insert : public Query<TableDefinition>
660 {
661 public:
662     typedef typename TableDefinition::Row Row;
663     typedef DPL::DB::SqlConnection::RowID RowID;
664
665 protected:
666     DPL::Optional<std::string> m_orClause;
667     Row m_row;
668
669     class PrepareVisitor {
670     public:
671         std::string m_columnNames;
672         std::string m_values;
673
674         template<typename ColumnType>
675         void Visit(const char* name, const ColumnType&, bool isSet)
676         {
677             if ( isSet )
678             {
679                 if ( !m_columnNames.empty() )
680                 {
681                     m_columnNames += ", ";
682                     m_values += ", ";
683                 }
684                 m_columnNames += name;
685                 m_values += "?";
686             }
687         }
688     };
689
690     void Prepare()
691     {
692         if ( !this->m_command )
693         {
694             this->m_commandString = "INSERT ";
695             if ( !!m_orClause )
696             {
697                 this->m_commandString += " OR " + *m_orClause + " ";
698             }
699             this->m_commandString += "INTO ";
700             this->m_commandString += TableDefinition::GetName();
701
702             PrepareVisitor visitor;
703             m_row.VisitColumns(visitor);
704
705             this->m_commandString += " ( " + visitor.m_columnNames + " ) ";
706             this->m_commandString += "VALUES ( " + visitor.m_values + " )";
707
708             LogPedantic("Prepared SQL command " << this->m_commandString);
709             this->m_command = TableDefinition::AllocTableDataCommand(
710                 this->m_commandString.c_str(),
711                 Query<TableDefinition>::m_interface);
712         }
713     }
714
715     void Bind()
716     {
717         BindVisitor visitor(this->m_command);
718         m_row.VisitColumns(visitor);
719     }
720
721 public:
722     explicit Insert(
723             IOrmInterface* interface = NULL,
724             const DPL::Optional<std::string>& orClause = DPL::Optional<std::string>::Null) :
725         Query<TableDefinition>(interface),
726         m_orClause(orClause)
727     {
728     }
729
730     void Values(const Row& row)
731     {
732         if ( this->m_command )
733         {
734             if ( !row.IsSignatureMatching(m_row) )
735             {
736                 ThrowMsg(Exception::SelectReuseWithDifferentQuerySignature,
737                     "Current ORM implementation doesn't allow to reuse Insert instance "
738                     "with different query signature.");
739             }
740         }
741         m_row = row;
742     }
743
744     RowID Execute()
745     {
746         Prepare();
747         Bind();
748         this->m_command->Step();
749
750         RowID result = TableDefinition::GetLastInsertRowID(
751             Query<TableDefinition>::m_interface);
752
753         this->m_command->Reset();
754         return result;
755     }
756 };
757
758 template<typename TableDefinition>
759 class __attribute__ ((visibility("hidden"))) Select : public QueryWithWhereClause<TableDefinition>
760 {
761 public:
762     typedef typename TableDefinition::ColumnList       ColumnList;
763     typedef typename TableDefinition::Row              Row;
764
765     typedef std::list<Row>                             RowList;
766 protected:
767     DPL::Optional<std::string> m_orderBy;
768     std::string m_JoinClause;
769     bool                       m_distinctResults;
770
771     void Prepare(const char* selectColumnName)
772     {
773         if ( !this->m_command )
774         {
775             this->m_commandString  = "SELECT ";
776             if (m_distinctResults)
777                 this->m_commandString += "DISTINCT ";
778             this->m_commandString += selectColumnName;
779             this->m_commandString += " FROM ";
780             this->m_commandString += TableDefinition::GetName();
781
782             this->m_commandString += m_JoinClause;
783
784             QueryWithWhereClause<TableDefinition>::Prepare();
785
786             if ( !m_orderBy.IsNull() )
787             {
788                 this->m_commandString += " ORDER BY " + *m_orderBy;
789             }
790
791             this->m_command = TableDefinition::AllocTableDataCommand(
792                 this->m_commandString.c_str(),
793                 Query<TableDefinition>::m_interface);
794
795             LogPedantic("Prepared SQL command " << this->m_commandString);
796         }
797     }
798
799     void Bind()
800     {
801         this->m_bindArgumentIndex = 1;
802         QueryWithWhereClause<TableDefinition>::Bind();
803     }
804
805     template<typename ColumnType>
806     ColumnType GetColumn(ColumnIndex columnIndex)
807     {
808         return GetColumnFromCommand<ColumnType>(columnIndex, this->m_command);
809     }
810
811     Row GetRow()
812     {
813         Row row;
814         FillRowUtil<ColumnList, Row>::FillRow(row, this->m_command);
815         return row;
816     }
817
818     template<typename ColumnList, typename CustomRow>
819     CustomRow GetCustomRow()
820     {
821         CustomRow row;
822         FillCustomRowUtil<ColumnList, CustomRow>::FillCustomRow(row, this->m_command);
823         return row;
824     }
825
826 public:
827
828     explicit Select(IOrmInterface *interface = NULL) :
829         QueryWithWhereClause<TableDefinition>(interface),
830         m_distinctResults(false)
831     {
832     }
833
834     void Distinct()
835     {
836         m_distinctResults = true;
837     }
838
839     void OrderBy(const std::string& orderBy)
840     {
841         m_orderBy = orderBy;
842     }
843
844     template<typename ColumnList, typename Expression>
845     void Join(const Expression& expression) {
846         std::string usedTableNames = TableDefinition::GetName();
847         if (!m_JoinClause.empty())
848             usedTableNames += m_JoinClause;
849
850         this->m_JoinClause += " JOIN ";
851         this->m_JoinClause += JoinUtil<ColumnList>::GetJoinTableName(usedTableNames);
852         this->m_JoinClause += " ON ";
853         this->m_JoinClause += expression.GetString();
854     }
855
856     template<typename ColumnData>
857     typename ColumnData::ColumnType GetSingleValue()
858     {
859         Prepare(ColumnData::GetColumnName());
860         Bind();
861         this->m_command->Step();
862
863         typename ColumnData::ColumnType result =
864             GetColumn<typename ColumnData::ColumnType>(0);
865
866         this->m_command->Reset();
867         return result;
868     }
869
870     //TODO return range - pair of custom iterators
871     template<typename ColumnData>
872     std::list<typename ColumnData::ColumnType> GetValueList()
873     {
874         Prepare(ColumnData::GetColumnName());
875         Bind();
876
877         std::list<typename ColumnData::ColumnType> resultList;
878
879         while (this->m_command->Step())
880             resultList.push_back(GetColumn<typename ColumnData::ColumnType>(0));
881
882         this->m_command->Reset();
883         return resultList;
884     }
885
886     Row GetSingleRow()
887     {
888         Prepare("*");
889         Bind();
890         this->m_command->Step();
891
892         Row result = GetRow();
893
894         this->m_command->Reset();
895         return result;
896     }
897
898     //TODO return range - pair of custom iterators
899     RowList GetRowList()
900     {
901         Prepare("*");
902         Bind();
903
904         RowList resultList;
905
906         while (this->m_command->Step())
907             resultList.push_back(GetRow());
908
909         this->m_command->Reset();
910         return resultList;
911     }
912
913     template<typename ColumnList, typename CustomRow>
914     CustomRow GetCustomSingleRow()
915     {
916         Prepare(JoinUtil<ColumnList>::GetColumnNames().c_str());
917         Bind();
918         this->m_command->Step();
919
920         CustomRow result = GetCustomRow<ColumnList, CustomRow>();
921
922         this->m_command->Reset();
923         return result;
924     }
925
926     template<typename ColumnList, typename CustomRow>
927     std::list<CustomRow> GetCustomRowList()
928     {
929         Prepare(JoinUtil<ColumnList>::GetColumnNames().c_str());
930         Bind();
931
932         std::list<CustomRow> resultList;
933
934         while (this->m_command->Step())
935             resultList.push_back(GetCustomRow<ColumnList, CustomRow>());
936
937         this->m_command->Reset();
938         return resultList;
939     }
940 };
941
942 template<typename TableDefinition>
943 class __attribute__ ((visibility("hidden"))) Update : public QueryWithWhereClause<TableDefinition> {
944 public:
945     typedef typename TableDefinition::Row Row;
946
947 protected:
948     DPL::Optional<std::string> m_orClause;
949     Row m_row;
950
951     class PrepareVisitor {
952     public:
953         std::string m_setExpressions;
954
955         template<typename ColumnType>
956         void Visit(const char* name, const ColumnType&, bool isSet)
957         {
958             if ( isSet )
959             {
960                 if ( !m_setExpressions.empty() )
961                 {
962                     m_setExpressions += ", ";
963                 }
964                 m_setExpressions += name;
965                 m_setExpressions += " = ";
966                 m_setExpressions += "?";
967             }
968         }
969     };
970
971     void Prepare()
972     {
973         if ( !this->m_command )
974         {
975             this->m_commandString = "UPDATE ";
976             if ( !!m_orClause )
977             {
978                 this->m_commandString += " OR " + *m_orClause + " ";
979             }
980             this->m_commandString += TableDefinition::GetName();
981             this->m_commandString += " SET ";
982
983             // got through row columns and values
984             PrepareVisitor visitor;
985             m_row.VisitColumns(visitor);
986
987             if(visitor.m_setExpressions.empty())
988             {
989                 ThrowMsg(Exception::EmptyUpdateStatement, "No SET expressions in update statement");
990             }
991
992             this->m_commandString += visitor.m_setExpressions;
993
994             // where
995             QueryWithWhereClause<TableDefinition>::Prepare();
996
997             this->m_command = TableDefinition::AllocTableDataCommand(
998                     this->m_commandString.c_str(),
999                     Query<TableDefinition>::m_interface);
1000             LogPedantic("Prepared SQL command " << this->m_commandString);
1001         }
1002     }
1003
1004     void Bind()
1005     {
1006         BindVisitor visitor(this->m_command);
1007         m_row.VisitColumns(visitor);
1008
1009         this->m_bindArgumentIndex = visitor.m_bindArgumentIndex;
1010         QueryWithWhereClause<TableDefinition>::Bind();
1011     }
1012
1013
1014 public:
1015     explicit Update(IOrmInterface *interface = NULL,
1016                     const DPL::Optional<std::string>& orClause = DPL::Optional<std::string>::Null) :
1017         QueryWithWhereClause<TableDefinition>(interface),
1018         m_orClause(orClause)
1019     {
1020     }
1021
1022     void Values(const Row& row)
1023     {
1024         if ( this->m_command )
1025         {
1026             if ( !row.IsSignatureMatching(m_row) )
1027             {
1028                 ThrowMsg(Exception::SelectReuseWithDifferentQuerySignature,
1029                     "Current ORM implementation doesn't allow to reuse Update instance "
1030                     "with different query signature.");
1031             }
1032         }
1033         m_row = row;
1034     }
1035
1036     void Execute()
1037     {
1038         Prepare();
1039         Bind();
1040         this->m_command->Step();
1041         this->m_command->Reset();
1042     }
1043 };
1044
1045 } //namespace ORM
1046 } //namespace DB
1047 } //namespace DPL
1048
1049 #endif // DPL_ORM_H