ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/Common/sqlite3.cpp
Revision: 47
Committed: 2008-03-07T03:08:31-08:00 (17 years, 4 months ago) by douglas
File size: 2632 byte(s)
Log Message:
Progress, needs moar workz.

File Contents

# Content
1 // Truck Computer Dooom!
2 //
3 // Douglas Thrift
4 //
5 // $Id$
6
7 #include "sqlite3.hpp"
8
9 namespace SQLite3
10 {
11
12 Connection::Connection(const std::string &db)
13 {
14 CheckError(::sqlite3_open(db.c_str(), &connection), connection);
15 }
16
17 Connection::~Connection()
18 {
19 CheckError(::sqlite3_close(connection), connection);
20 }
21
22 Statement Connection::Prepare(const std::string &statement) const
23 {
24 return Statement(connection, statement);
25 }
26
27 Statement::Statement(::sqlite3 *connection, const std::string &statement) : connection(connection)
28 {
29 CheckError(::sqlite3_prepare_v2(connection, statement.data(), statement.size(), &this->statement, NULL), connection);
30 }
31
32 Statement::~Statement()
33 {
34 ::sqlite3_finalize(statement);
35 }
36
37 void Statement::Bind(int index, double value) const
38 {
39 CheckError(::sqlite3_bind_double(statement, ++index, value), connection);
40 }
41
42 void Statement::Bind(int index, int value) const
43 {
44 CheckError(::sqlite3_bind_int(statement, ++index, value), connection);
45 }
46
47 void Statement::Bind(int index, int64_t value) const
48 {
49 CheckError(::sqlite3_bind_int64(statement, ++index, value), connection);
50 }
51
52 void Statement::Bind(int index, const Null &value) const
53 {
54 CheckError(::sqlite3_bind_null(statement, ++index), connection);
55 }
56
57 void Statement::Bind(int index, const std::string &value) const
58 {
59 CheckError(::sqlite3_bind_text(statement, ++index, value.data(), value.size(), SQLITE_TRANSIENT), connection);
60 }
61
62 Results Statement::Execute() const
63 {
64 return Results(connection, statement);
65 }
66
67 void Statement::Clear() const
68 {
69 CheckError(::sqlite3_clear_bindings(statement), connection);
70 }
71
72 Results::Results(::sqlite3 *connection, ::sqlite3_stmt *statement) : connection(connection), statement(statement), next(Begin)
73 {
74 if (MoveNext())
75 next = True;
76 else
77 next = False;
78 }
79
80 bool Results::MoveNext()
81 {
82 if (next == Begin || next == Ignore)
83 {
84 int code(::sqlite3_step(statement));
85
86 if (code == SQLITE_ROW)
87 return true;
88 else if (code == SQLITE_DONE)
89 {
90 CheckError(::sqlite3_reset(statement), connection);
91
92 return false;
93 }
94
95 CheckError(code, connection);
96
97 return false;
98 }
99 else
100 {
101 bool next_(next);
102
103 next = Ignore;
104
105 return next_;
106 }
107 }
108
109 template <>
110 double Results::Get(int index)
111 {
112 return ::sqlite3_column_double(statement, index);
113 }
114
115 template <>
116 int Results::Get(int index)
117 {
118 return ::sqlite3_column_int(statement, index);
119 }
120
121 template <>
122 int64_t Results::Get(int index)
123 {
124 return ::sqlite3_column_int64(statement, index);
125 }
126
127 template <>
128 std::string Results::Get(int index)
129 {
130 return std::string(reinterpret_cast<const char *>(::sqlite3_column_text(statement, index)), ::sqlite3_column_bytes(statement, index));
131 }
132
133 }

Properties

Name Value
svn:keywords Id