Debian Codegraph @

src/backend.cpp

raw symbols SCIP
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <[email protected]>
4 //
5 // Distributed under:
6 //
7 // the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // or (at your opinion) under:
12 //
13 // The MIT License
14 // (See accompanying file MIT.txt or a copy at
15 // http://www.opensource.org/licenses/mit-license.php)
16 //
17 ///////////////////////////////////////////////////////////////////////////////
18 #define CPPDB_SOURCE
19 #include <cppdb/backend.h>
20 #include <cppdb/utils.h>
21 #include <cppdb/pool.h>
22
23 #include <map>
24 #include <list>
25
26 namespace cppdb {
27 namespace backend {
28 //result
29 struct result::data {};
30 result::result() {}
31 result::~result() {}
32
33 //statement
34 struct statement::data {};
35
36 statement::statement() : cache_(0)
37 {
38 }
39 statement::~statement()
40 {
41 }
42 void statement::cache(statements_cache *c)
43 {
44 cache_ = c;
45 }
46
47 void statement::dispose(statement *p)
48 {
49 if(!p)
50 return;
51 statements_cache *cache = p->cache_;
52 p->cache_ = 0;
53 if(cache)
54 cache->put(p);
55 else
56 delete p;
57 }
58
59
60 //statements cache//////////////
61
62 struct statements_cache::data {
63
64 data() :
65 size(0),
66 max_size(0)
67 {
68 }
69
70 struct entry;
71 typedef std::map<std::string,entry> statements_type;
72 typedef std::list<statements_type::iterator> lru_type;
73 struct entry {
74 ref_ptr<statement> stat;
75 lru_type::iterator lru_ptr;
76 };
77
78 statements_type statements;
79 lru_type lru;
80 size_t size;
81 size_t max_size;
82
83
84 void insert(ref_ptr<statement> st)
85 {
86 statements_type::iterator p;
87 if((p=statements.find(st->sql_query()))!=statements.end()) {
88 p->second.stat = st;
89 lru.erase(p->second.lru_ptr);
90 lru.push_front(p);
91 p->second.lru_ptr = lru.begin();
92 }
93 else {
94 if(size > 0 && size >= max_size) {
95 statements.erase(lru.back());
96 lru.pop_back();
97 size--;
98 }
99 std::pair<statements_type::iterator,bool> ins =
100 statements.insert(std::make_pair(st->sql_query(),entry()));
101 p = ins.first;
102 p->second.stat = st;
103 lru.push_front(p);
104 p->second.lru_ptr = lru.begin();
105 size ++;
106 }
107 }
108
109 ref_ptr<statement> fetch(std::string const &query)
110 {
111 ref_ptr<statement> st;
112 statements_type::iterator p = statements.find(query);
113 if(p==statements.end())
114 return st;
115 st=p->second.stat;
116 lru.erase(p->second.lru_ptr);
117 statements.erase(p);
118 size --;
119 return st;
120 }
121
122 void clear()
123 {
124 lru.clear();
125 statements.clear();
126 size=0;
127 }
128 }; // data
129
130 statements_cache::statements_cache()
131 {
132 }
133 void statements_cache::set_size(size_t n)
134 {
135 if(n!=0 && !active()) {
136 d.reset(new data());
137 d->max_size = n;
138 }
139 }
140 void statements_cache::put(statement *p_in)
141 {
142 if(!active()) {
143 delete p_in;
144 }
145 ref_ptr<statement> p(p_in);
146 p->reset();
147 d->insert(p);
148 }
149 ref_ptr<statement> statements_cache::fetch(std::string const &q)
150 {
151 if(!active())
152 return 0;
153 return d->fetch(q);
154 }
155 void statements_cache::clear()
156 {
157 d->clear();
158 }
159 statements_cache::~statements_cache()
160 {
161 }
162
163 bool statements_cache::active()
164 {
165 return d.get()!=0;
166 }
167
168 //////////////
169 //connection
170 //////////////
171
172 struct connection::data {
173 typedef std::list<connection_specific_data *> conn_specific_type;
174 conn_specific_type conn_specific;
175 ~data()
176 {
177 for(conn_specific_type::iterator p=conn_specific.begin();p!=conn_specific.end();++p)
178 delete *p;
179 }
180 };
181 ref_ptr<statement> connection::prepare(std::string const &q)
182 {
183 if(default_is_prepared_)
184 return get_prepared_statement(q);
185 else
186 return get_statement(q);
187 }
188
189 ref_ptr<statement> connection::get_statement(std::string const &q)
190 {
191 ref_ptr<statement> st = create_statement(q);
192 return st;
193 }
194
195 ref_ptr<statement> connection::get_prepared_statement(std::string const &q)
196 {
197 ref_ptr<statement> st;
198 if(!cache_.active()) {
199 st = prepare_statement(q);
200 return st;
201 }
202 st = cache_.fetch(q);
203 if(!st)
204 st = prepare_statement(q);
205 st->cache(&cache_);
206 return st;
207 }
208
209 ref_ptr<statement> connection::get_prepared_uncached_statement(std::string const &q)
210 {
211 ref_ptr<statement> st = prepare_statement(q);
212 return st;
213 }
214
215 connection::connection(connection_info const &info) :
216 d(new connection::data),
217 pool_(0),
218 once_called_(0),
219 recyclable_(1)
220 {
221 int cache_size = info.get("@stmt_cache_size",64);
222 if(cache_size > 0) {
223 cache_.set_size(cache_size);
224 }
225 std::string def_is_prep = info.get("@use_prepared","on");
226 if(def_is_prep == "on")
227 default_is_prepared_ = 1;
228 else if(def_is_prep == "off")
229 default_is_prepared_ = 0;
230 else
231 throw cppdb_error("cppdb::backend::connection: @use_prepared should be either 'on' or 'off'");
232 }
233 connection::~connection()
234 {
235 }
236
237 bool connection::once_called() const
238 {
239 return once_called_;
240 }
241 void connection::once_called(bool v)
242 {
243 once_called_ = v;
244 }
245 connection_specific_data *connection::connection_specific_get(std::type_info const &type) const
246 {
247 for(data::conn_specific_type::const_iterator p=d->conn_specific.begin();p!=d->conn_specific.end();++p) {
248 if(typeid(**p) == type)
249 return *p;
250 }
251 return 0;
252 }
253 connection_specific_data *connection::connection_specific_release(std::type_info const &type)
254 {
255 for(data::conn_specific_type::iterator p=d->conn_specific.begin();p!=d->conn_specific.end();++p) {
256 if(typeid(**p) == type) {
257 connection_specific_data *ptr = *p;
258 d->conn_specific.erase(p);
259 return ptr;
260 }
261 }
262 return 0;
263 }
264 void connection::connection_specific_reset(std::type_info const &type,connection_specific_data *ptr)
265 {
266 std::auto_ptr<connection_specific_data> tmp(ptr);
267 if(ptr && typeid(*ptr)!=type) {
268 throw cppdb_error(
269 std::string("cppdb::connection_specific::Inconsistent pointer type")
270 + typeid(*ptr).name()
271 + " and std::type_info reference:"
272 + type.name()
273 );
274 }
275 for(data::conn_specific_type::iterator p=d->conn_specific.begin();p!=d->conn_specific.end();++p) {
276 if(typeid(**p) == type) {
277 delete *p;
278 if(ptr)
279 *p = tmp.release();
280 else
281 d->conn_specific.erase(p);
282 return;
283 }
284 }
285 if(ptr) {
286 d->conn_specific.push_back(0);
287 d->conn_specific.back() = tmp.release();
288 }
289 }
290
291 ref_ptr<pool> connection::get_pool()
292 {
293 return pool_;
294 }
295 void connection::set_pool(ref_ptr<pool> p)
296 {
297 pool_ = p;
298 }
299 void connection::set_driver(ref_ptr<loadable_driver> p)
300 {
301 driver_ = p;
302 }
303 void connection::clear_cache()
304 {
305 cache_.clear();
306 }
307
308 void connection::recyclable(bool opt)
309 {
310 recyclable_ = opt;
311 }
312
313 bool connection::recyclable()
314 {
315 return recyclable_;
316 }
317
318 void connection::dispose(connection *c)
319 {
320 if(!c)
321 return;
322 ref_ptr<pool> p = c->pool_;
323 c->pool_ = 0;
324 if(p && c->recyclable())
325 p->put(c);
326 else {
327 c->clear_cache();
328 // Make sure that driver would not be
329 // destoryed destructor of connection exits
330 ref_ptr<loadable_driver> driver = c->driver_;
331 delete c;
332 driver.reset();
333 }
334 }
335
336 connection *driver::connect(connection_info const &cs)
337 {
338 return open(cs);
339 }
340 bool loadable_driver::in_use()
341 {
342 return use_count() > 1;
343 }
344 connection *loadable_driver::connect(connection_info const &cs)
345 {
346 connection *c = open(cs);
347 c->set_driver(ref_ptr<loadable_driver>(this));
348 return c;
349 }
350
351 static_driver::static_driver(connect_function_type c) : connect_(c)
352 {
353 }
354 static_driver::~static_driver()
355 {
356 }
357 bool static_driver::in_use()
358 {
359 return true;
360 }
361 backend::connection *static_driver::open(connection_info const &ci)
362 {
363 return connect_(ci);
364 }
365
366 } // backend
367
368 struct connection_specific_data::data {};
369
370 connection_specific_data::connection_specific_data()
371 {
372 }
373 connection_specific_data::~connection_specific_data()
374 {
375 }
376
377
378 } // cppdb
379
380