LiteSQL
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 
12 #include "litesql/database.hpp"
13 #include "litesql/selectquery.hpp"
14 
15 namespace litesql {
16 
21  SelectQuery selectObjectQuery(const std::vector<FieldType>& fdatas,
22  const Expr & e=Expr());
23 
27  template <class T>
29  std::vector<FieldType> fdatas;
30  T::getFieldTypes(fdatas);
31  return selectObjectQuery(fdatas, e);
32  }
34 template <class T>
35 class DataSource {
36 private:
38  const Database & db;
40  SelectQuery sel;
41 public:
44  DataSource(const Database & db_, const Expr& e = Expr())
45  : db(db_), sel(selectObjectQuery<T>(e)) {
46  }
49  DataSource(const Database & db_, const SelectQuery& sel_)
50  : db(db_), sel(sel_) {
51  }
52 
54  const Database & getDatabase() const {
55  return db;
56  }
58  SelectQuery idQuery() const {
59  SelectQuery idq(sel);
60  idq.clearResults();
61  idq.result(T::Id.fullName());
62  return idq;
63  }
65  size_t count() const {
66  SelectQuery cq(sel);
67  cq.clearResults();
68  cq.limit(0).offset(0);
69  cq.result("count(*)");
70  return atoi(db.query(cq)[0][0]);
71  }
74  return sel;
75  }
77  Cursor<T> cursor() const {
78  return db.template cursor<T>(sel);
79  }
82  T one() const {
83  return *cursor();
84  }
86  std::vector<T> all() const {
87  // \TODO a cursor is not appropriate here, because we fetch all results of the query
88  return cursor().dump();
89  }
94  DataSource& orderBy(FieldType f, bool asc=true) {
95  sel.orderBy(f.fullName(), asc);
96  return *this;
97  }
104  sel.source(id.table());
105  sel.where(id == T::Id);
106  sel.orderBy(f.fullName(), asc);
107  return *this;
108  }
109 };
110 
111 }
112 #endif
litesql::DataSource
template class which holds SelectQuery for selecting objects of type T
Definition: datasource.hpp:35
litesql::DataSource::one
T one() const
returns first object in result set.
Definition: datasource.hpp:82
litesql::DataSource::orderBy
DataSource & orderBy(FieldType f, bool asc=true)
modifies SelectQuery to order result set
Definition: datasource.hpp:94
litesql::DataSource::idQuery
SelectQuery idQuery() const
returns SelectQuery which selects ID-numbers of objects
Definition: datasource.hpp:58
litesql::SelectQuery
a class that helps creating SELECT-SQL statements.
Definition: selectquery.hpp:20
database.hpp
litesql::RawExpr
used to inject custom expression into WHERE-clause
Definition: expr.hpp:35
litesql::DataSource::all
std::vector< T > all() const
returns all objects in result set.
Definition: datasource.hpp:86
litesql::DataSource::orderByRelation
DataSource & orderByRelation(FieldType id, FieldType f, bool asc=true)
modifies SelectQuery to order result set by external table
Definition: datasource.hpp:103
litesql::DataSource::DataSource
DataSource(const Database &db_, const SelectQuery &sel_)
Definition: datasource.hpp:49
litesql::DataSource::cursor
Cursor< T > cursor() const
returns cursor for query
Definition: datasource.hpp:77
datasource.hpp
selectquery.hpp
litesql::join
std::string join(const std::vector< std::string > &strings, const std::string &delim=" ")
returns strings joined with delimiter
litesql::DataSource::getDatabase
const Database & getDatabase() const
returns database reference
Definition: datasource.hpp:54
litesql::Database
A base class of databases.
Definition: database.hpp:33
litesql::atoi
int atoi(const std::string &s)
string version of atoi
litesql::DataSource::count
size_t count() const
returns number of objects in result set
Definition: datasource.hpp:65
litesql::FieldType
Definition: field.hpp:25
litesql::Cursor
used to iterate results of SQL statement, creates objects of type T from retrieved records.
Definition: cursor.hpp:21
litesql::Expr
A base class for expression in WHERE - clause.
Definition: expr.hpp:18
litesql::Database::query
Records query(const std::string &query) const
executes SQL query
Definition: database.cpp:240
litesql::selectObjectQuery
SelectQuery selectObjectQuery(const std::vector< FieldType > &fdatas, const Expr &e=Expr())
returns SelectQuery which selects objects of type T
Definition: datasource.cpp:16
litesql::DataSource::objectQuery
SelectQuery objectQuery() const
returns SelectQuery which selects objects
Definition: datasource.hpp:73
litesql::DataSource::DataSource
DataSource(const Database &db_, const Expr &e=Expr())
Definition: datasource.hpp:44

SourceForge.net Logo