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