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