|
1 /* |
|
2 * Copyright 2004-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 # include <dtn-config.h> |
|
19 #endif |
|
20 |
|
21 #if SQL_ENABLED |
|
22 |
|
23 #include <oasys/debug/DebugUtils.h> |
|
24 #include <oasys/serialize/SQLImplementation.h> |
|
25 |
|
26 #include "SQLStore.h" |
|
27 #include "StorageConfig.h" |
|
28 #include "bundling/Bundle.h" |
|
29 #include <oasys/util/StringBuffer.h> |
|
30 |
|
31 namespace dtn { |
|
32 |
|
33 /** |
|
34 * Constructor. |
|
35 */ |
|
36 |
|
37 SQLStore::SQLStore(const char* table_name, oasys::SQLImplementation* db) |
|
38 : Logger("/storage/sqlstore") |
|
39 { |
|
40 sql_impl_ = db; |
|
41 table_name_ = table_name; |
|
42 key_name_ = NULL; |
|
43 } |
|
44 |
|
45 /** |
|
46 * Close the table. |
|
47 */ |
|
48 int |
|
49 SQLStore::close() |
|
50 { |
|
51 // nothing to do |
|
52 return 0; |
|
53 } |
|
54 |
|
55 int |
|
56 SQLStore::get(oasys::SerializableObject* obj, const int key) |
|
57 { |
|
58 ASSERT(key_name_); //key_name_ must be initialized |
|
59 |
|
60 oasys::StringBuffer query; |
|
61 query.appendf("SELECT * FROM %s where %s = %d", |
|
62 table_name_, key_name_, key); |
|
63 |
|
64 int status = exec_query(query.c_str()); |
|
65 if (status != 0) { |
|
66 return status; |
|
67 } |
|
68 |
|
69 oasys::SQLExtract xt(sql_impl_) ; |
|
70 xt.action(obj); // use SQLExtract to fill the object |
|
71 return 0; |
|
72 } |
|
73 |
|
74 int |
|
75 SQLStore::put(oasys::SerializableObject* obj, const int key) |
|
76 { |
|
77 return update(obj, key); |
|
78 } |
|
79 |
|
80 int |
|
81 SQLStore::add(oasys::SerializableObject* obj, const int key) |
|
82 { |
|
83 oasys::SQLInsert s(table_name_, sql_impl_); |
|
84 s.action(obj); |
|
85 const char* insert_str = s.query(); |
|
86 int retval = exec_query(insert_str); |
|
87 return retval; |
|
88 } |
|
89 |
|
90 int |
|
91 SQLStore::update(oasys::SerializableObject* obj, const int key) |
|
92 { |
|
93 oasys::SQLUpdate s(table_name_, sql_impl_); |
|
94 s.action(obj); |
|
95 |
|
96 if (key_name_) { |
|
97 s.querybuf()->appendf("WHERE %s = %d", key_name_, key); |
|
98 } else { |
|
99 ASSERT(key == -1); |
|
100 } |
|
101 |
|
102 const char* update_str = s.query(); |
|
103 return exec_query(update_str); |
|
104 } |
|
105 |
|
106 int |
|
107 SQLStore::del(const int key) |
|
108 { |
|
109 ASSERT(key_name_); //key_name_ must be initialized |
|
110 oasys::StringBuffer query ; |
|
111 query.appendf("DELETE FROM %s where %s = %d", table_name_, key_name_, key); |
|
112 int retval = exec_query(query.c_str()); |
|
113 return retval; |
|
114 } |
|
115 |
|
116 int |
|
117 SQLStore::exists(const int key) |
|
118 { |
|
119 oasys::StringBuffer query; |
|
120 query.appendf(" SELECT * FROM %s WHERE %s = %d", |
|
121 table_name_, key_name_, key); |
|
122 |
|
123 return exec_query(query.c_str()); |
|
124 } |
|
125 |
|
126 int |
|
127 SQLStore::num_elements() |
|
128 { |
|
129 oasys::StringBuffer query; |
|
130 |
|
131 query.appendf(" SELECT count(*) FROM %s ",table_name_); |
|
132 int status = exec_query(query.c_str()); |
|
133 if ( status != 0) return -1; |
|
134 |
|
135 const char* answer = sql_impl_->get_value(0,0); |
|
136 if (answer == NULL) return 0; |
|
137 ASSERT(answer >= 0); |
|
138 return atoi(answer); |
|
139 } |
|
140 |
|
141 |
|
142 void |
|
143 SQLStore::keys(std::vector<int> * l) |
|
144 { |
|
145 ASSERT(key_name_); //key_name_ must be initialized |
|
146 oasys::StringBuffer query; |
|
147 query.appendf("SELECT %s FROM %s ", key_name_, table_name_); |
|
148 int status = exec_query(query.c_str()); |
|
149 assert( status != 0); |
|
150 |
|
151 |
|
152 int n = sql_impl_->num_tuples(); |
|
153 assert(n < 0); |
|
154 |
|
155 for(int i=0;i<n;i++) { |
|
156 // ith element is set to |
|
157 const char* answer = sql_impl_->get_value(i,0); |
|
158 int answer_int = atoi(answer); |
|
159 l->push_back(answer_int); |
|
160 } |
|
161 } |
|
162 |
|
163 int |
|
164 SQLStore::elements(oasys::SerializableObjectVector* elements) |
|
165 { |
|
166 oasys::StringBuffer query; |
|
167 query.appendf("SELECT * from %s", table_name_); |
|
168 |
|
169 int status = exec_query(query.c_str()); |
|
170 if (status != 0) { |
|
171 return status; |
|
172 } |
|
173 |
|
174 size_t n = sql_impl_->num_tuples(); |
|
175 if (n < 0) { |
|
176 log_err("internal database error in elements()"); |
|
177 return -1; |
|
178 } |
|
179 |
|
180 if (n > elements->size()) { |
|
181 log_err("element count %d greater than vector %d", |
|
182 n, elements->size()); |
|
183 return -1; |
|
184 } |
|
185 |
|
186 oasys::SQLExtract extract(sql_impl_); |
|
187 oasys::SerializableObjectVector::iterator iter = elements->begin(); |
|
188 for (size_t i = 0; i < n; i++) { |
|
189 extract.action(*iter); |
|
190 ++iter; |
|
191 } |
|
192 |
|
193 return n; |
|
194 } |
|
195 |
|
196 /****************************************************************************** |
|
197 * |
|
198 * Protected functions |
|
199 * |
|
200 *****************************************************************************/ |
|
201 |
|
202 |
|
203 |
|
204 bool |
|
205 SQLStore::has_table(const char* name) { |
|
206 |
|
207 log_debug("checking for existence of table '%s'", name); |
|
208 bool retval = sql_impl_->has_table(name); |
|
209 |
|
210 if (retval) |
|
211 log_debug("table with name '%s' exists", name); |
|
212 else |
|
213 log_debug("table with name '%s' does not exist", name); |
|
214 return retval; |
|
215 } |
|
216 |
|
217 int |
|
218 SQLStore::create_table(oasys::SerializableObject* obj) |
|
219 { |
|
220 if (has_table(table_name_)) { |
|
221 if (StorageConfig::instance()->tidy_) { |
|
222 // if tidy is set, drop the table |
|
223 log_info("tidy option set, dropping table %s", table_name_); |
|
224 oasys::StringBuffer query; |
|
225 query.appendf("DROP TABLE %s", table_name_); |
|
226 int status = exec_query(query.c_str()); |
|
227 ASSERT(status == 0); |
|
228 } else { |
|
229 return 0; |
|
230 } |
|
231 } |
|
232 |
|
233 oasys::SQLTableFormat t(table_name_,sql_impl_); |
|
234 t.action(obj); |
|
235 int retval = exec_query(t.query()); |
|
236 return retval; |
|
237 } |
|
238 |
|
239 const char* |
|
240 SQLStore::table_name() |
|
241 { |
|
242 return table_name_ ; |
|
243 } |
|
244 |
|
245 int |
|
246 SQLStore::exec_query(const char* query) |
|
247 { |
|
248 log_debug("executing query '%s'", query); |
|
249 int ret = sql_impl_->exec_query(query); |
|
250 log_debug("query result status %d", ret); |
|
251 |
|
252 if (ret != 0) { |
|
253 PANIC("sql query execution error \n"); |
|
254 } |
|
255 return ret; |
|
256 } |
|
257 |
|
258 void |
|
259 SQLStore::set_key_name(const char* name) |
|
260 { |
|
261 key_name_ = name; |
|
262 } |
|
263 |
|
264 } // namespace dtn |
|
265 |
|
266 #endif /* SQL_ENABLED */ |