backend.cpp 8.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
///////////////////////////////////////////////////////////////////////////////
//                                                                             
//  Copyright (C) 2010-2011  Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>     
//                                                                             
//  Distributed under:
//
//                   the Boost Software License, Version 1.0.
//              (See accompanying file LICENSE_1_0.txt or copy at 
//                     http://www.boost.org/LICENSE_1_0.txt)
//
//  or (at your opinion) under:
//
//                               The MIT License
//                 (See accompanying file MIT.txt or a copy at
//              http://www.opensource.org/licenses/mit-license.php)
//
///////////////////////////////////////////////////////////////////////////////
#define CPPDB_SOURCE
#include <cppdb/backend.h>
#include <cppdb/utils.h>
#include <cppdb/pool.h>

#include <map>
#include <list>

namespace cppdb {
	namespace backend {
		//result
		struct result::data {};
		result::result() {}
		result::~result() {}
		
		//statement
		struct statement::data {};

		statement::statement() : cache_(0) 
		{
		}
		statement::~statement()
		{
		}
		void statement::cache(statements_cache *c)
		{
			cache_ = c;
		}

		void statement::dispose(statement *p)
		{
			if(!p)
				return;
			statements_cache *cache = p->cache_;
			p->cache_ = 0;
			if(cache) 
				cache->put(p);
			else
				delete p;
		}
		

		//statements cache//////////////

		struct statements_cache::data {

			data() : 
				size(0),
				max_size(0) 
			{
			}

			struct entry;
			typedef std::map<std::string,entry> statements_type;
			typedef std::list<statements_type::iterator> lru_type;
			struct entry {
				ref_ptr<statement> stat;
				lru_type::iterator lru_ptr;
			};
			
			statements_type statements;
			lru_type lru;
			size_t size;
			size_t max_size;


			void insert(ref_ptr<statement> st)
			{
				statements_type::iterator p;
				if((p=statements.find(st->sql_query()))!=statements.end()) {
					p->second.stat = st;
					lru.erase(p->second.lru_ptr);
					lru.push_front(p);
					p->second.lru_ptr = lru.begin();
				}
				else {
					if(size > 0 && size >= max_size) {
						statements.erase(lru.back());
						lru.pop_back();
						size--;
					}
					std::pair<statements_type::iterator,bool> ins = 
						statements.insert(std::make_pair(st->sql_query(),entry()));
					p = ins.first;
					p->second.stat = st;
					lru.push_front(p);
					p->second.lru_ptr = lru.begin();
					size ++;
				}
			}

			ref_ptr<statement> fetch(std::string const &query)
			{
				ref_ptr<statement> st;
				statements_type::iterator p = statements.find(query);
				if(p==statements.end())
					return st;
				st=p->second.stat;
				lru.erase(p->second.lru_ptr);
				statements.erase(p);
				size --;
				return st;
			}

			void clear()
			{
				lru.clear();
				statements.clear();
				size=0;
			}
		}; // data

		statements_cache::statements_cache() 
		{
		}
		void statements_cache::set_size(size_t n)
		{
			if(n!=0 && !active()) {
				d.reset(new data());
				d->max_size = n;
			}
		}
		void statements_cache::put(statement *p_in)
		{
			if(!active()) {
				delete p_in;
			}
			ref_ptr<statement> p(p_in);
			p->reset();
			d->insert(p);
		}
		ref_ptr<statement> statements_cache::fetch(std::string const &q)
		{
			if(!active())
				return 0;
			return d->fetch(q);
		}
		void statements_cache::clear()
		{
			d->clear();
		}
		statements_cache::~statements_cache()
		{
		}

		bool statements_cache::active()
		{
			return d.get()!=0;
		}

		//////////////
		//connection
		//////////////
		
		struct connection::data {
			typedef std::list<connection_specific_data *> conn_specific_type;
			conn_specific_type conn_specific;
			~data()
			{
				for(conn_specific_type::iterator p=conn_specific.begin();p!=conn_specific.end();++p)
					delete *p;
			}
		};
		ref_ptr<statement> connection::prepare(std::string const &q) 
		{
			if(default_is_prepared_)
				return get_prepared_statement(q);
			else
				return get_statement(q);
		}
		
		ref_ptr<statement> connection::get_statement(std::string const &q)
		{
			ref_ptr<statement> st = create_statement(q);
			return st;
		}

		ref_ptr<statement> connection::get_prepared_statement(std::string const &q)
		{
			ref_ptr<statement> st;
			if(!cache_.active()) {
				st = prepare_statement(q);
				return st;
			}
			st = cache_.fetch(q);
			if(!st)
				st = prepare_statement(q);
			st->cache(&cache_);
			return st;
		}

		ref_ptr<statement> connection::get_prepared_uncached_statement(std::string const &q)
		{
			ref_ptr<statement> st = prepare_statement(q);
			return st;
		}

		connection::connection(connection_info const &info) :
			d(new connection::data),
			pool_(0),
			once_called_(0),
			recyclable_(1)
		{
			int cache_size = info.get("@stmt_cache_size",64);
			if(cache_size > 0) {
				cache_.set_size(cache_size);
			}
			std::string def_is_prep = info.get("@use_prepared","on");
			if(def_is_prep == "on")
				default_is_prepared_ = 1;
			else if(def_is_prep == "off") 
				default_is_prepared_ = 0;
			else
				throw cppdb_error("cppdb::backend::connection: @use_prepared should be either 'on' or 'off'");
		}
		connection::~connection()
		{
		}

		bool connection::once_called() const
		{
			return once_called_;
		}
		void connection::once_called(bool v)
		{
			once_called_ = v;
		}
		connection_specific_data *connection::connection_specific_get(std::type_info const &type) const
		{
			for(data::conn_specific_type::const_iterator p=d->conn_specific.begin();p!=d->conn_specific.end();++p) {
				if(typeid(**p) == type)
					return *p;
			}
			return 0;
		}
		connection_specific_data *connection::connection_specific_release(std::type_info const &type)
		{
			for(data::conn_specific_type::iterator p=d->conn_specific.begin();p!=d->conn_specific.end();++p) {
				if(typeid(**p) == type) {
					connection_specific_data *ptr = *p;
					d->conn_specific.erase(p);
					return ptr;
				}
			}
			return 0;
		}
		void connection::connection_specific_reset(std::type_info const &type,connection_specific_data *ptr)
		{
			std::auto_ptr<connection_specific_data> tmp(ptr);
			if(ptr && typeid(*ptr)!=type) {
				throw cppdb_error(
					std::string("cppdb::connection_specific::Inconsistent pointer type")
					+ typeid(*ptr).name() 
					+ " and std::type_info reference:"
					+ type.name()
				);
			}
			for(data::conn_specific_type::iterator p=d->conn_specific.begin();p!=d->conn_specific.end();++p) {
				if(typeid(**p) == type) {
					delete *p;
					if(ptr)
						*p = tmp.release();
					else
						d->conn_specific.erase(p);
					return;
				}
			}
			if(ptr) {
				d->conn_specific.push_back(0);
				d->conn_specific.back() = tmp.release();
			}
		}

		ref_ptr<pool> connection::get_pool()
		{
			return pool_;
		}
		void connection::set_pool(ref_ptr<pool> p)
		{
			pool_ = p;
		}
		void connection::set_driver(ref_ptr<loadable_driver> p)
		{
			driver_ = p;
		}
		void connection::clear_cache()
		{
			cache_.clear();
		}

		void connection::recyclable(bool opt)
		{
			recyclable_ = opt;
		}

		bool connection::recyclable()
		{
			return recyclable_;
		}
		
		void connection::dispose(connection *c)
		{
			if(!c)
				return;
			ref_ptr<pool> p = c->pool_;
			c->pool_ = 0;
			if(p && c->recyclable())
				p->put(c);
			else {
				c->clear_cache();
				// Make sure that driver would not be
				// destoryed destructor of connection exits
				ref_ptr<loadable_driver> driver = c->driver_;
				delete c;
				driver.reset();
			}
		}
		
		connection *driver::connect(connection_info const &cs)
		{
			return open(cs);
		}
		bool loadable_driver::in_use()
		{
			return use_count() > 1;
		}
		connection *loadable_driver::connect(connection_info const &cs)
		{
			connection *c = open(cs);
			c->set_driver(ref_ptr<loadable_driver>(this));
			return c;
		}

		static_driver::static_driver(connect_function_type c) : connect_(c)
		{
		}
		static_driver::~static_driver()
		{
		}
		bool static_driver::in_use()
		{
			return true;
		}
		backend::connection *static_driver::open(connection_info const &ci)
		{
			return connect_(ci);
		}

	} // backend

	struct connection_specific_data::data {};

	connection_specific_data::connection_specific_data()
	{
	}
	connection_specific_data::~connection_specific_data()
	{
	}
	
	
} // cppdb