CppDB
cppdb/backend.h
00001 
00002 //                                                                             
00003 //  Copyright (C) 2010-2011  Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>     
00004 //                                                                             
00005 //  Distributed under:
00006 //
00007 //                   the Boost Software License, Version 1.0.
00008 //              (See accompanying file LICENSE_1_0.txt or copy at 
00009 //                     http://www.boost.org/LICENSE_1_0.txt)
00010 //
00011 //  or (at your opinion) under:
00012 //
00013 //                               The MIT License
00014 //                 (See accompanying file MIT.txt or a copy at
00015 //              http://www.opensource.org/licenses/mit-license.php)
00016 //
00018 #ifndef CPPDB_BACKEND_H
00019 #define CPPDB_BACKEND_H
00020 #include <iosfwd>
00021 #include <ctime>
00022 #include <string>
00023 #include <memory>
00024 #include <map>
00025 #include <typeinfo>
00026 #include <cppdb/defs.h>
00027 #include <cppdb/errors.h>
00028 #include <cppdb/ref_ptr.h>
00029 #include <cppdb/connection_specific.h>
00030 
00031 // Borland errors about unknown pool-type without this include.
00032 #ifdef __BORLANDC__
00033 #include <cppdb/pool.h>
00034 #endif
00035 
00036 namespace cppdb {
00037         class connection_info;
00038         // Borland needs pool.h, but not this forward declaration.
00039         #ifndef __BORLANDC__
00040         class pool;
00041         #endif
00042 
00046         namespace backend {     
00047 
00055                 class CPPDB_API result : public ref_counted {
00056                 public:
00057                         
00061                         typedef enum {
00062                                 last_row_reached, 
00063                                 next_row_exists,  
00064                                 next_row_unknown  
00065                         } next_row;
00066 
00071                         virtual next_row has_next() = 0;
00076                         virtual bool next() = 0;
00083                         virtual bool fetch(int col,short &v) = 0;
00091                         virtual bool fetch(int col,unsigned short &v) = 0;
00099                         virtual bool fetch(int col,int &v) = 0;
00107                         virtual bool fetch(int col,unsigned &v) = 0;
00115                         virtual bool fetch(int col,long &v) = 0;
00123                         virtual bool fetch(int col,unsigned long &v) = 0;
00131                         virtual bool fetch(int col,long long &v) = 0;
00139                         virtual bool fetch(int col,unsigned long long &v) = 0;
00147                         virtual bool fetch(int col,float &v) = 0;
00155                         virtual bool fetch(int col,double &v) = 0;
00163                         virtual bool fetch(int col,long double &v) = 0;
00171                         virtual bool fetch(int col,std::string &v) = 0;
00179                         virtual bool fetch(int col,std::ostream &v) = 0;
00187                         virtual bool fetch(int col,std::tm &v) = 0;
00191                         virtual bool is_null(int col) = 0;
00195                         virtual int cols() = 0;
00200                         virtual int name_to_column(std::string const &) = 0;
00206                         virtual std::string column_to_name(int) = 0;
00207 
00208                         result();
00209                         virtual ~result();
00210                 private:
00211                         struct data;
00212                         std::auto_ptr<data> d;
00213                 };
00214 
00215                 class statements_cache;
00216 
00220                 class CPPDB_API statement : public ref_counted {
00221                 public:
00222                         // Begin of API
00223 
00228                         virtual void reset() = 0;
00233                         virtual std::string const &sql_query() = 0;
00234 
00243                         virtual void bind(int col,std::string const &) = 0;
00252                         virtual void bind(int col,char const *s) = 0;
00261                         virtual void bind(int col,char const *b,char const *e) = 0;
00269                         virtual void bind(int col,std::tm const &) = 0;
00277                         virtual void bind(int col,std::istream &) = 0;
00285                         virtual void bind(int col,int v) = 0;
00295                         virtual void bind(int col,unsigned v) = 0;
00305                         virtual void bind(int col,long v) = 0;
00315                         virtual void bind(int col,unsigned long v) = 0;
00325                         virtual void bind(int col,long long v) = 0;
00335                         virtual void bind(int col,unsigned long long v) = 0;
00343                         virtual void bind(int col,double v) = 0;
00351                         virtual void bind(int col,long double v) = 0;
00359                         virtual void bind_null(int col) = 0;
00369                         virtual long long sequence_last(std::string const &sequence) = 0;
00375                         virtual unsigned long long affected() = 0;
00379                         virtual result *query() = 0;
00383                         virtual void exec() = 0;
00384 
00386                         // Caching support
00387                         static void dispose(statement *selfp);
00388                         
00389                         void cache(statements_cache *c);
00390                         statement();
00391                         virtual ~statement() ;
00393                 private:
00394                         struct data;
00395                         std::auto_ptr<data> d;
00396                         statements_cache *cache_;
00397                 };
00398         
00400                 class CPPDB_API statements_cache {
00401                         statements_cache(statements_cache const &);
00402                         void operator=(statements_cache const &);
00403                 public:
00404                         statements_cache();
00405                         bool active();
00406                         void set_size(size_t n);
00407                         void put(statement *p_in);
00408                         void clear();
00409                         ref_ptr<statement> fetch(std::string const &q);
00410                         ~statements_cache();
00411                 private:
00412                         struct data;
00413                         std::auto_ptr<data> d;
00414                 };
00415 
00417 
00418                 class connection;
00419 
00425                 class CPPDB_API driver : public ref_counted {
00426                         driver(driver const &);
00427                         void operator=(driver const &);
00428                 public:
00429                         driver() {}
00430                         virtual ~driver() {}
00435                         virtual bool in_use() = 0;
00439                         virtual connection *open(connection_info const &cs) = 0;
00444                         virtual connection *connect(connection_info const &cs);
00445                 };
00446         
00450                 class CPPDB_API loadable_driver : public driver {
00451                         loadable_driver(loadable_driver const &);
00452                         void operator=(loadable_driver const &);
00453                 public:
00454                         loadable_driver() {}
00458                         virtual bool in_use();
00459                         virtual ~loadable_driver() {}
00460 
00464                         virtual connection *connect(connection_info const &cs);
00465                 };
00466 
00467                 extern "C" {
00471                         typedef cppdb::backend::connection *cppdb_backend_connect_function(connection_info const &ci);
00472                 }
00473 
00474 
00478                 class CPPDB_API static_driver : public driver {
00479                 public:
00483                         typedef cppdb_backend_connect_function *connect_function_type;
00484 
00488                         static_driver(connect_function_type c);
00489                         ~static_driver();
00493                         bool in_use();
00497                         backend::connection *open(connection_info const &ci);
00498                 private:
00499                         connect_function_type connect_;
00500                 };
00501 
00502 
00506                 class CPPDB_API connection : public ref_counted {
00507                 public:
00511                         connection(connection_info const &info);
00512                         virtual ~connection();
00514                         void set_pool(ref_ptr<pool> p);
00515                         ref_ptr<pool> get_pool(); 
00516                         void set_driver(ref_ptr<loadable_driver> drv);
00517                         static void dispose(connection *c);
00518                         ref_ptr<statement> prepare(std::string const &q);
00519                         ref_ptr<statement> get_prepared_statement(std::string const &q);
00520                         ref_ptr<statement> get_prepared_uncached_statement(std::string const &q);
00521                         ref_ptr<statement> get_statement(std::string const &q);
00523 
00524                         // API 
00525 
00530                         virtual void begin() = 0;
00535                         virtual void commit() = 0;
00539                         virtual void rollback() = 0;
00544                         virtual statement *prepare_statement(std::string const &q) = 0;
00549                         virtual statement *create_statement(std::string const &q) = 0;
00553                         virtual std::string escape(std::string const &) = 0;
00557                         virtual std::string escape(char const *s) = 0;
00561                         virtual std::string escape(char const *b,char const *e) = 0;
00565                         virtual std::string driver() = 0;
00570                         virtual std::string engine() = 0;
00571 
00575                         void clear_cache();
00576 
00582                         bool once_called() const;
00583                         
00587                         void once_called(bool v);
00588 
00594                         connection_specific_data *connection_specific_get(std::type_info const &type) const;
00598                         connection_specific_data *connection_specific_release(std::type_info const &type);
00603                         void connection_specific_reset(std::type_info const &type,connection_specific_data *p = 0);
00604 
00613                         bool recyclable();
00614                         
00619                         void recyclable(bool value);
00620 
00621                 private:
00622 
00623                         struct data;
00624                         std::auto_ptr<data> d;
00625                         statements_cache cache_;
00626                         ref_ptr<loadable_driver> driver_;
00627                         ref_ptr<pool> pool_;
00628                         unsigned default_is_prepared_ : 1;
00629                         unsigned once_called_ : 1;
00630                         unsigned recyclable_ : 1;
00631                         unsigned reserverd_ : 29;
00632                 };
00633 
00634         } // backend
00635 } // cppdb
00636 
00637 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator