]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store.cpp
Stargazer (#6)
[stg.git] / projects / stargazer / plugins / store / firebird / firebird_store.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  This file contains a realization of a base firebird-storage plugin class
23  *
24  *  $Revision: 1.18 $
25  *  $Date: 2010/01/08 16:00:45 $
26  *
27  */
28
29 #include "firebird_store.h"
30
31 #include "stg/common.h"
32
33 #include <string>
34 #include <vector>
35
36 extern "C" STG::Store* GetStore()
37 {
38     static FIREBIRD_STORE plugin;
39     return &plugin;
40 }
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 FIREBIRD_STORE::FIREBIRD_STORE()
45     : version("firebird_store v.1.4"),
46       db_server("localhost"),
47       db_database("/var/stg/stargazer.fdb"),
48       db_user("stg"),
49       db_password("123456"),
50       til(IBPP::ilConcurrency),
51       tlr(IBPP::lrWait),
52       schemaVersion(0),
53       logger(STG::PluginLogger::get("store_firebird"))
54 {
55 }
56 //-----------------------------------------------------------------------------
57 FIREBIRD_STORE::~FIREBIRD_STORE()
58 {
59 db->Disconnect();
60 }
61 //-----------------------------------------------------------------------------
62 int FIREBIRD_STORE::ParseSettings()
63 {
64 std::vector<STG::ParamValue>::iterator i;
65 std::string s;
66
67 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
68     {
69     if (i->value.empty())
70         continue;
71     s = ToLower(i->param);
72
73     if (s == "server")
74         db_server = i->value.front();
75
76     if (s == "database")
77         db_database = i->value.front();
78
79     if (s == "user")
80         db_user = i->value.front();
81
82     if (s == "password")
83         db_password = i->value.front();
84
85     // Advanced settings block
86
87     if (s == "isolationLevel")
88         {
89         if (i->value.front() == "Concurrency")
90             til = IBPP::ilConcurrency;
91         else if (i->value.front() == "DirtyRead")
92             til = IBPP::ilReadDirty;
93         else if (i->value.front() == "ReadCommitted")
94             til = IBPP::ilReadCommitted;
95         else if (i->value.front() == "Consistency")
96             til = IBPP::ilConsistency;
97         }
98
99     if (s == "lockResolution")
100         {
101         if (i->value.front() == "Wait")
102             tlr = IBPP::lrWait;
103         else if (i->value.front() == "NoWait")
104             tlr = IBPP::lrNoWait;
105         }
106     }
107
108 try
109     {
110     db = IBPP::DatabaseFactory(db_server, db_database, db_user, db_password, "", "KOI8U", "");
111     db->Connect();
112     return CheckVersion();
113     }
114 catch (IBPP::Exception & ex)
115     {
116     strError = "IBPP exception";
117     printfd(__FILE__, ex.what());
118     return -1;
119     }
120
121 return 0;
122 }
123 //-----------------------------------------------------------------------------
124 int FIREBIRD_STORE::CheckVersion()
125 {
126 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
127 IBPP::Statement st = IBPP::StatementFactory(db, tr);
128
129 try
130     {
131     tr->Start();
132     st->Execute("SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG=0 AND RDB$RELATION_NAME = 'TB_INFO'");
133     if (!st->Fetch())
134         {
135         schemaVersion = 0;
136         }
137     else
138         {
139         st->Execute("SELECT version FROM tb_info");
140         while (st->Fetch())
141             st->Get(1, schemaVersion);
142         }
143     tr->Commit();
144     logger("FIREBIRD_STORE: Current DB schema version: %d", schemaVersion);
145     }
146
147 catch (IBPP::Exception & ex)
148     {
149     tr->Rollback();
150     strError = "IBPP exception";
151     printfd(__FILE__, ex.what());
152     return -1;
153     }
154
155 return 0;
156 }
157 //-----------------------------------------------------------------------------