LiteSQL  0.3.10
datasource.hpp
Go to the documentation of this file.
1 /* LiteSQL
2  *
3  * The list of contributors at http://litesql.sf.net/
4  *
5  * See LICENSE for copyright information. */
6 
9 #ifndef litesql_datasource_hpp
10 #define litesql_datasource_hpp
11 #include <set>
12 #include <string>
13 #include "litesql/database.hpp"
14 #include "litesql/selectquery.hpp"
15 #include "litesql/expr.hpp"
16 namespace litesql {
17 using namespace std;
21 SelectQuery selectObjectQuery(const std::vector<FieldType>& fdatas,
22  const Expr & e=Expr());
23 
26 template <class T>
28  std::vector<FieldType> fdatas;
29  T::getFieldTypes(fdatas);
30  return selectObjectQuery(fdatas, e);
31 }
33 template <class T>
34 class DataSource {
35 private:
37  const Database & db;
39  SelectQuery sel;
40 public:
43  DataSource(const Database & db_, const Expr& e = Expr())
44  : db(db_), sel(selectObjectQuery<T>(e)) {
45  }
48  DataSource(const Database & db_, const SelectQuery& sel_)
49  : db(db_), sel(sel_) {
50  }
51 
53  const Database & getDatabase() const {
54  return db;
55  }
57  SelectQuery idQuery() const {
58  SelectQuery idq(sel);
59  idq.clearResults();
60  idq.result(T::Id.fullName());
61  return idq;
62  }
64  size_t count() const {
65  SelectQuery cq(sel);
66  cq.clearResults();
67  cq.limit(0).offset(0);
68  cq.result("count(*)");
69  return atoi(db.query(cq)[0][0]);
70  }
73  return sel;
74  }
76  Cursor<T> cursor() const {
77  return db.template cursor<T>(sel);
78  }
81  T one() const {
82  return *cursor();
83  }
85  std::vector<T> all() const {
86  // \TODO a cursor is not appropriate here, because we fetch all results of the query
87  return cursor().dump();
88  }
93  DataSource& orderBy(FieldType f, bool asc=true) {
94  sel.orderBy(f.fullName(), asc);
95  return *this;
96  }
103  sel.source(id.table());
104  sel.where(id == T::Id);
105  sel.orderBy(f.fullName(), asc);
106  return *this;
107  }
108 };
109 
110 }
111 #endif
Definition: backend.hpp:14
DataSource(const Database &db_, const Expr &e=Expr())
Definition: datasource.hpp:43
int atoi(const std::string &s)
string version of atoi
A base class of databases.
Definition: database.hpp:37
A base class for expression in WHERE - clause.
Definition: expr.hpp:19
Contains Expr-class hierarchy and operator overloadings for them.
DataSource & orderBy(FieldType f, bool asc=true)
modifies SelectQuery to order result set
Definition: datasource.hpp:93
Records query(const string &query) const
executes SQL query
Definition: database.cpp:219
size_t count() const
returns number of objects in result set
Definition: datasource.hpp:64
a class that helps creating SELECT-SQL statements.
Definition: selectquery.hpp:18
SelectQuery selectObjectQuery(const std::vector< FieldType > &fdatas, const Expr &e=Expr())
returns SelectQuery which selects objects of type T
Definition: datasource.cpp:10
T one() const
returns first object in result set.
Definition: datasource.hpp:81
template class which holds SelectQuery for selecting objects of type T
Definition: datasource.hpp:34
const Database & getDatabase() const
returns database reference
Definition: datasource.hpp:53
used to iterate results of SQL statement, creates objects of type T from retrieved records...
Definition: cursor.hpp:22
std::vector< T > all() const
returns all objects in result set.
Definition: datasource.hpp:85
Database-class.
Cursor< T > cursor() const
returns cursor for query
Definition: datasource.hpp:76
SelectQuery objectQuery() const
returns SelectQuery which selects objects
Definition: datasource.hpp:72
contains SelectQuery-class.
SelectQuery idQuery() const
returns SelectQuery which selects ID-numbers of objects
Definition: datasource.hpp:57
DataSource(const Database &db_, const SelectQuery &sel_)
Definition: datasource.hpp:48
Definition: field.hpp:24
DataSource & orderByRelation(FieldType id, FieldType f, bool asc=true)
modifies SelectQuery to order result set by external table
Definition: datasource.hpp:102

SourceForge.net Logo