00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef db_h
00024 #define db_h
00025
00026 #ifndef _REENTRANT
00027 #define _REENTRANT
00028 #endif
00029
00030 #include "config.h"
00031 #include "rock.h"
00032 #include "log.h"
00033 #include "util.h"
00034 #include <pqxx/pqxx>
00035 #include <vector>
00036 using std::vector;
00037
00038 namespace karoo {
00039
00040 class db: public referable {
00041 private:
00042 log logger;
00043 text conf;
00044 rock* parent;
00045
00046 class conn_data: public referable {
00047 private:
00048 pqxx::connection* c;
00049 text conf;
00050 public:
00051 conn_data(const text& conf);
00052 virtual ~conn_data();
00053 operator pqxx::connection_base&();
00054 text getConf() { return conf; }
00055 };
00056
00057 class conn_cleanup: public pebble {
00058 private:
00059 db* parent;
00060 mutable karoo_mutex sem;
00061
00062 public:
00063 conn_cleanup(db* parent);
00064 virtual ~conn_cleanup();
00065 void run();
00066 void stop();
00067 };
00068
00069 public:
00070 class conn {
00071 private:
00072 conn_data* c;
00073 public:
00074 conn(conn_data* c);
00075 conn(const conn& ref);
00076 virtual ~conn();
00077
00078 const conn& operator=(const conn& ref);
00079 conn_data* getData() { return c; }
00080 operator pqxx::connection_base&();
00081 text getConf() { return c->getConf(); }
00082 };
00083
00084 private:
00085 mutable karoo_mutex pool_sem;
00086 mutable karoo_mutex sem;
00087 bool stopping;
00088 size_t pool_sz;
00089 size_t prev_pool_sz;
00090 vector<db::conn*> pool;
00091 conn_cleanup* cleaner;
00092 mutable karoo_mutex types_sem;
00093 map<pqxx::oid,db_data_type> types;
00094 protected:
00095 public:
00096 db(rock* parent, const text& conf, size_t pool_sz = 1);
00097 virtual ~db();
00098
00103 void setLogger(const log& logger);
00104
00105 void setPoolSize(size_t sz);
00106 void configure(const text& conf, size_t pool_sz);
00107
00112 db::conn getConnection();
00113 void cleanup();
00114 bool isStopping() const;
00115 void stop();
00116 void start();
00117 rock* getParent() { return parent; }
00118 db_data_type getColumnType(pqxx::oid id) const;
00119 };
00120 }
00121
00122 #endif