LiteSQL
cursor.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 
7 #ifndef litesql_cursor_hpp
8 #define litesql_cursor_hpp
9 
10 #include "litesql/backend.hpp"
11 #include "litesql/except.hpp"
12 
15 namespace litesql {
16 class Database;
20 template <class T>
21 class Cursor {
22 private:
24  const Database& db;
26  Backend::Cursor * cursor;
28  bool done;
30  bool dataReady;
32  Record currentRow;
33 public:
34  Cursor(const Database& db, Backend::Cursor * c);
36  ~Cursor();
40  Cursor<T> & operator++(int) { return operator++();}
42  std::vector<T> dump();
44  T operator*();
46  inline bool rowsLeft() { return !done; }
47 };
48 
49 template <class T>
50 Cursor<T>::Cursor(const Database& db_, Backend::Cursor * c)
51  : db(db_), cursor(c), done(false), dataReady(false) {
52  operator++();
53 }
54 template <class T>
56  delete cursor;
57 }
58 template <class T>
60  if (done)
61  return *this;
62  currentRow = cursor->fetchOne();
63  if (currentRow.size() == 0) {
64  done = true;
65  dataReady = false;
66  }
67  else
68  dataReady = true;
69 
70  return *this;
71 }
72 
73 template <class T>
74 std::vector<T> Cursor<T>::dump() {
75  std::vector<T> res;
76  for (;!done;operator++())
77  res.push_back(operator*());
78  return res;
79 }
80 template <class T>
82  Record rec;
83 
84  if (!dataReady)
85  throw NotFound();
86 
87  return T(db,currentRow);
88 }
89 
90 }
91 
92 #endif // #ifndef litesql_cursor_hpp
93 
Classes Backend, Backend::Cursor and Backend::Result.
An abstract base class for cursors that iterate result sets returned by relational database.
Definition: backend.hpp:24
used to iterate results of SQL statement, creates objects of type T from retrieved records.
Definition: cursor.hpp:21
bool rowsLeft()
returns true if there are records left in the result set
Definition: cursor.hpp:46
~Cursor()
deletes Backend::Cursor
Definition: cursor.hpp:55
Cursor< T > & operator++()
steps to next record
Definition: cursor.hpp:59
std::vector< T > dump()
returns the rest of the result set in vector
Definition: cursor.hpp:74
Cursor< T > & operator++(int)
steps to next record
Definition: cursor.hpp:40
T operator*()
returns current record
Definition: cursor.hpp:81
A base class of databases.
Definition: database.hpp:33
exception thrown when a record is not found
Definition: except.hpp:33
SQL data row wrapper.
Definition: types.hpp:20
contains litesql's exception classes

SourceForge.net Logo