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