]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store.cpp
Ticket 37. The long int policyTime type changed to int64_t policyTime.
[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/ibpp.h"
32 #include "stg/plugin_creator.h"
33
34 #include <string>
35 #include <vector>
36
37 namespace
38 {
39 PLUGIN_CREATOR<FIREBIRD_STORE> frsc;
40 }
41
42 extern "C" STORE * GetStore();
43 //-----------------------------------------------------------------------------
44 STORE * GetStore()
45 {
46 return frsc.GetPlugin();
47 }
48 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
51 FIREBIRD_STORE::FIREBIRD_STORE()
52     : version("firebird_store v.1.4"),
53       db_server("localhost"),
54       db_database("/var/stg/stargazer.fdb"),
55       db_user("stg"),
56       db_password("123456"),
57       til(IBPP::ilConcurrency),
58       tlr(IBPP::lrWait),
59       schemaVersion(0),
60       logger(GetPluginLogger(GetStgLogger(), "store_firebird"))
61 {
62 pthread_mutex_init(&mutex, NULL);
63 }
64 //-----------------------------------------------------------------------------
65 FIREBIRD_STORE::~FIREBIRD_STORE()
66 {
67 db->Disconnect();
68 }
69 //-----------------------------------------------------------------------------
70 int FIREBIRD_STORE::ParseSettings()
71 {
72 std::vector<PARAM_VALUE>::iterator i;
73 std::string s;
74
75 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
76     {
77     if (i->value.empty())
78         continue;
79     s = ToLower(i->param);
80
81     if (s == "server")
82         db_server = i->value.front();
83
84     if (s == "database")
85         db_database = i->value.front();
86
87     if (s == "user")
88         db_user = i->value.front();
89
90     if (s == "password")
91         db_password = i->value.front();
92
93     // Advanced settings block
94
95     if (s == "isolationLevel")
96         {
97         if (i->value.front() == "Concurrency")
98             til = IBPP::ilConcurrency;
99         else if (i->value.front() == "DirtyRead")
100             til = IBPP::ilReadDirty;
101         else if (i->value.front() == "ReadCommitted")
102             til = IBPP::ilReadCommitted;
103         else if (i->value.front() == "Consistency")
104             til = IBPP::ilConsistency;
105         }
106
107     if (s == "lockResolution")
108         {
109         if (i->value.front() == "Wait")
110             tlr = IBPP::lrWait;
111         else if (i->value.front() == "NoWait")
112             tlr = IBPP::lrNoWait;
113         }
114     }
115
116 try
117     {
118     db = IBPP::DatabaseFactory(db_server, db_database, db_user, db_password, "", "KOI8U", "");
119     db->Connect();
120     return CheckVersion();
121     }
122 catch (IBPP::Exception & ex)
123     {
124     strError = "IBPP exception";
125     printfd(__FILE__, ex.what());
126     return -1;
127     }
128
129 return 0;
130 }
131 //-----------------------------------------------------------------------------
132 int FIREBIRD_STORE::CheckVersion()
133 {
134 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
135 IBPP::Statement st = IBPP::StatementFactory(db, tr);
136
137 try
138     {
139     tr->Start();
140     st->Execute("SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG=0 AND RDB$RELATION_NAME = 'TB_INFO'");
141     if (!st->Fetch())
142         {
143         schemaVersion = 0;
144         }
145     else
146         {
147         st->Execute("SELECT version FROM tb_info");
148         while (st->Fetch())
149             st->Get(1, schemaVersion);
150         }
151     tr->Commit();
152     logger("FIREBIRD_STORE: Current DB schema version: %d", schemaVersion);
153     }
154
155 catch (IBPP::Exception & ex)
156     {
157     tr->Rollback();
158     strError = "IBPP exception";
159     printfd(__FILE__, ex.what());
160     return -1;
161     }
162
163 return 0;
164 }
165 //-----------------------------------------------------------------------------