summary refs log tree commit diff stats
path: root/lib/query.h
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2017-01-24 21:50:39 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2017-01-24 21:50:39 -0500
commite1fa4a088dd95caef22045f905a9d5d22b71bef0 (patch)
tree09f30eadcbb5a7352f19a6070dc363298f16aff8 /lib/query.h
parent0ba0fff06fb679f5cabedd52257fc0c38a600279 (diff)
downloadverbly-e1fa4a088dd95caef22045f905a9d5d22b71bef0.tar.gz
verbly-e1fa4a088dd95caef22045f905a9d5d22b71bef0.tar.bz2
verbly-e1fa4a088dd95caef22045f905a9d5d22b71bef0.zip
Whitespace changes
Diffstat (limited to 'lib/query.h')
-rw-r--r--lib/query.h48
1 files changed, 24 insertions, 24 deletions
diff --git a/lib/query.h b/lib/query.h index e31be3d..352748f 100644 --- a/lib/query.h +++ b/lib/query.h
@@ -11,36 +11,36 @@
11#include "binding.h" 11#include "binding.h"
12 12
13namespace verbly { 13namespace verbly {
14 14
15 class database_error : public std::logic_error { 15 class database_error : public std::logic_error {
16 public: 16 public:
17 17
18 database_error(std::string msg, std::string sqlMsg) : std::logic_error(msg + " (" + sqlMsg + ")") 18 database_error(std::string msg, std::string sqlMsg) : std::logic_error(msg + " (" + sqlMsg + ")")
19 { 19 {
20 } 20 }
21 }; 21 };
22 22
23 template <typename Object> 23 template <typename Object>
24 class query { 24 class query {
25 public: 25 public:
26 26
27 query(const database& db, sqlite3* ppdb, filter queryFilter, bool random, int limit) : db_(&db) 27 query(const database& db, sqlite3* ppdb, filter queryFilter, bool random, int limit) : db_(&db)
28 { 28 {
29 statement stmt(Object::objectType, std::move(queryFilter)); 29 statement stmt(Object::objectType, std::move(queryFilter));
30 30
31 std::string queryString = stmt.getQueryString(Object::select, random, limit); 31 std::string queryString = stmt.getQueryString(Object::select, random, limit);
32 std::list<binding> bindings = stmt.getBindings(); 32 std::list<binding> bindings = stmt.getBindings();
33 33
34 std::cout << queryString << std::endl; 34 std::cout << queryString << std::endl;
35 35
36 if (sqlite3_prepare_v2(ppdb, queryString.c_str(), queryString.length(), &ppstmt_, NULL) != SQLITE_OK) 36 if (sqlite3_prepare_v2(ppdb, queryString.c_str(), queryString.length(), &ppstmt_, NULL) != SQLITE_OK)
37 { 37 {
38 std::string errorMsg = sqlite3_errmsg(ppdb); 38 std::string errorMsg = sqlite3_errmsg(ppdb);
39 sqlite3_finalize(ppstmt_); 39 sqlite3_finalize(ppstmt_);
40 40
41 throw database_error("Error preparing query", errorMsg); 41 throw database_error("Error preparing query", errorMsg);
42 } 42 }
43 43
44 int i = 1; 44 int i = 1;
45 for (const binding& value : bindings) 45 for (const binding& value : bindings)
46 { 46 {
@@ -52,52 +52,52 @@ namespace verbly {
52 { 52 {
53 std::string errorMsg = sqlite3_errmsg(ppdb); 53 std::string errorMsg = sqlite3_errmsg(ppdb);
54 sqlite3_finalize(ppstmt_); 54 sqlite3_finalize(ppstmt_);
55 55
56 throw database_error("Error binding value to query", errorMsg); 56 throw database_error("Error binding value to query", errorMsg);
57 } 57 }
58 58
59 break; 59 break;
60 } 60 }
61 61
62 case binding::type::string: 62 case binding::type::string:
63 { 63 {
64 if (sqlite3_bind_text(ppstmt_, i, value.getString().c_str(), value.getString().length(), SQLITE_TRANSIENT) != SQLITE_OK) 64 if (sqlite3_bind_text(ppstmt_, i, value.getString().c_str(), value.getString().length(), SQLITE_TRANSIENT) != SQLITE_OK)
65 { 65 {
66 std::string errorMsg = sqlite3_errmsg(ppdb); 66 std::string errorMsg = sqlite3_errmsg(ppdb);
67 sqlite3_finalize(ppstmt_); 67 sqlite3_finalize(ppstmt_);
68 68
69 throw database_error("Error binding value to query", errorMsg); 69 throw database_error("Error binding value to query", errorMsg);
70 } 70 }
71 71
72 break; 72 break;
73 } 73 }
74 74
75 case binding::type::invalid: 75 case binding::type::invalid:
76 { 76 {
77 throw std::logic_error("Cannot use invalid bindings"); 77 throw std::logic_error("Cannot use invalid bindings");
78 } 78 }
79 } 79 }
80 80
81 i++; 81 i++;
82 } 82 }
83 } 83 }
84 84
85 ~query() 85 ~query()
86 { 86 {
87 sqlite3_finalize(ppstmt_); 87 sqlite3_finalize(ppstmt_);
88 } 88 }
89 89
90 std::vector<Object> all() const 90 std::vector<Object> all() const
91 { 91 {
92 std::vector<Object> result; 92 std::vector<Object> result;
93 93
94 while (sqlite3_step(ppstmt_) == SQLITE_ROW) 94 while (sqlite3_step(ppstmt_) == SQLITE_ROW)
95 { 95 {
96 result.emplace_back(*db_, ppstmt_); 96 result.emplace_back(*db_, ppstmt_);
97 } 97 }
98 98
99 sqlite3_reset(ppstmt_); 99 sqlite3_reset(ppstmt_);
100 100
101 return result; 101 return result;
102 } 102 }
103 103
@@ -111,13 +111,13 @@ namespace verbly {
111 throw std::logic_error("query returned empty dataset"); 111 throw std::logic_error("query returned empty dataset");
112 } 112 }
113 } 113 }
114 114
115 private: 115 private:
116 const database* db_; 116 const database* db_;
117 sqlite3_stmt* ppstmt_; 117 sqlite3_stmt* ppstmt_;
118 118
119 }; 119 };
120 120
121}; 121};
122 122
123#endif /* end of include guard: QUERY_H_7CC5284C */ 123#endif /* end of include guard: QUERY_H_7CC5284C */