]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp
Fix postgresql store plugin compilation errors
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store_tariffs.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  *  Tariffs manipulation methods
23  *
24  *  $Revision: 1.2 $
25  *  $Date: 2009/06/09 12:32:40 $
26  *
27  */
28
29 #include <string>
30 #include <vector>
31 #include <sstream>
32
33 #include <libpq-fe.h>
34
35 #include "postgresql_store.h"
36 #include "stg/stg_locker.h"
37
38 //-----------------------------------------------------------------------------
39 int POSTGRESQL_STORE::GetTariffsList(vector<string> * tariffsList) const
40 {
41 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
42
43 if (PQstatus(connection) != CONNECTION_OK)
44     {
45     printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
46     if (Reset())
47         {
48         strError = "Connection lost";
49         printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
50         return -1;
51         }
52     }
53
54 PGresult * result;
55
56 if (StartTransaction())
57     {
58     printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to start transaction'\n");
59     return -1;
60     }
61
62 result = PQexec(connection, "SELECT name FROM tb_tariffs");
63
64 if (PQresultStatus(result) != PGRES_TUPLES_OK)
65     {
66     strError = PQresultErrorMessage(result);
67     PQclear(result);
68     printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
69     if (RollbackTransaction())
70         {
71         printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to rollback transaction'\n");
72         }
73     return -1;
74     }
75
76 int tuples = PQntuples(result);
77
78 for (int i = 0; i < tuples; ++i)
79     {
80     tariffsList->push_back(PQgetvalue(result, i, 0));
81     }
82
83 PQclear(result);
84
85 if (CommitTransaction())
86     {
87     printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to commit transaction'\n");
88     return -1;
89     }
90
91 return 0;
92 }
93
94 //-----------------------------------------------------------------------------
95 int POSTGRESQL_STORE::AddTariff(const string & name) const
96 {
97 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
98
99 if (PQstatus(connection) != CONNECTION_OK)
100     {
101     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
102     if (Reset())
103         {
104         strError = "Connection lost";
105         printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
106         return -1;
107         }
108     }
109
110 PGresult * result;
111
112 if (StartTransaction())
113     {
114     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to start transaction'\n");
115     return -1;
116     }
117
118 std::string ename = name;
119
120 if (EscapeString(ename))
121     {
122     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
123     if (RollbackTransaction())
124         {
125         printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
126         }
127     return -1;
128     }
129
130 std::stringstream query;
131 query << "SELECT sp_add_tariff('" << ename << "', " << DIR_NUM << ")";
132
133 result = PQexec(connection, query.str().c_str());
134
135 if (PQresultStatus(result) != PGRES_TUPLES_OK)
136     {
137     strError = PQresultErrorMessage(result);
138     PQclear(result);
139     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
140     if (RollbackTransaction())
141         {
142         printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
143         }
144     return -1;
145     }
146
147 PQclear(result);
148
149 if (CommitTransaction())
150     {
151     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to commit transaction'\n");
152     return -1;
153     }
154
155 return 0;
156 }
157 //-----------------------------------------------------------------------------
158 int POSTGRESQL_STORE::DelTariff(const string & name) const
159 {
160 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
161
162 if (PQstatus(connection) != CONNECTION_OK)
163     {
164     printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
165     if (Reset())
166         {
167         strError = "Connection lost";
168         printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
169         return -1;
170         }
171     }
172
173 PGresult * result;
174
175 if (StartTransaction())
176     {
177     printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to start transaction'\n");
178     return -1;
179     }
180
181 std::string ename = name;
182
183 if (EscapeString(ename))
184     {
185     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
186     if (RollbackTransaction())
187         {
188         printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
189         }
190     return -1;
191     }
192
193 std::stringstream query;
194 query << "DELETE FROM tb_tariffs WHERE name = '" << ename << "'";
195
196 result = PQexec(connection, query.str().c_str());
197
198 if (PQresultStatus(result) != PGRES_COMMAND_OK)
199     {
200     strError = PQresultErrorMessage(result);
201     PQclear(result);
202     printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
203     if (RollbackTransaction())
204         {
205         printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to rollback transaction'\n");
206         }
207     return -1;
208     }
209
210 PQclear(result);
211
212 if (CommitTransaction())
213     {
214     printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to commit transaction'\n");
215     return -1;
216     }
217
218 return 0;
219 }
220 //-----------------------------------------------------------------------------
221 int POSTGRESQL_STORE::SaveTariff(const TARIFF_DATA & td,
222                                  const string & tariffName) const
223 {
224 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
225
226 if (PQstatus(connection) != CONNECTION_OK)
227     {
228     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
229     if (Reset())
230         {
231         strError = "Connection lost";
232         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
233         return -1;
234         }
235     }
236
237 PGresult * result;
238
239 if (StartTransaction())
240     {
241     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to start transaction'\n");
242     return -1;
243     }
244
245 std::string ename = tariffName;
246
247 if (EscapeString(ename))
248     {
249     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to escape name'\n");
250     if (RollbackTransaction())
251         {
252         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
253         }
254     return -1;
255     }
256
257 int32_t id, i;
258 double pda, pdb, pna, pnb;
259 int threshold;
260
261 std::stringstream query;
262 query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
263
264 result = PQexec(connection, query.str().c_str());
265
266 if (PQresultStatus(result) != PGRES_TUPLES_OK)
267     {
268     strError = PQresultErrorMessage(result);
269     PQclear(result);
270     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
271     if (RollbackTransaction())
272         {
273         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
274         }
275     return -1;
276     }
277
278 int tuples = PQntuples(result);
279
280 if (tuples != 1)
281     {
282     strError = "Failed to fetch tariff ID";
283     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
284     PQclear(result);
285     if (RollbackTransaction())
286         {
287         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
288         }
289     return -1;
290     }
291
292 std::stringstream tuple;
293 tuple << PQgetvalue(result, 0, 0);
294
295 PQclear(result);
296
297 tuple >> id;
298
299 query.str("");
300 query << "UPDATE tb_tariffs SET \
301               fee = " << td.tariffConf.fee << ", \
302               free = " << td.tariffConf.free << ", \
303               passive_cost = " << td.tariffConf.passiveCost << ", \
304               traff_type = " << td.tariffConf.traffType << " \
305           WHERE pk_tariff = " << id;
306
307 result = PQexec(connection, query.str().c_str());
308
309 if (PQresultStatus(result) != PGRES_COMMAND_OK)
310     {
311     strError = PQresultErrorMessage(result);
312     PQclear(result);
313     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
314     if (RollbackTransaction())
315         {
316         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
317         }
318     return -1;
319     }
320
321 PQclear(result);
322
323 for(i = 0; i < DIR_NUM; i++)
324     {
325
326     pda = td.dirPrice[i].priceDayA * 1024 * 1024;
327     pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
328
329     if (td.dirPrice[i].singlePrice)
330         {
331         pna = pda;
332         pnb = pdb;
333         }
334     else
335         {
336         pna = td.dirPrice[i].priceNightA * 1024 * 1024;
337         pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
338         }
339
340     if (td.dirPrice[i].noDiscount)
341         {
342         threshold = 0xffFFffFF;
343         }
344     else
345         {
346         threshold = td.dirPrice[i].threshold;
347         }
348
349     std::stringstream query;
350     query << "UPDATE tb_tariffs_params SET \
351                   price_day_a = " << pda << ", \
352                   price_day_b = " << pdb << ", \
353                   price_night_a = " << pna << ", \
354                   price_night_b = " << pnb << ", \
355                   threshold = " << threshold << ", \
356                   time_day_begins = CAST('" << td.dirPrice[i].hDay
357                                             << ":"
358                                             << td.dirPrice[i].mDay << "' AS TIME), \
359                   time_day_ends = CAST('" << td.dirPrice[i].hNight
360                                           << ":"
361                                           << td.dirPrice[i].mNight << "' AS TIME) \
362              WHERE fk_tariff = " << id << " AND dir_num = " << i;
363
364     result = PQexec(connection, query.str().c_str());
365
366     if (PQresultStatus(result) != PGRES_COMMAND_OK)
367         {
368         strError = PQresultErrorMessage(result);
369         PQclear(result);
370         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
371         if (RollbackTransaction())
372             {
373             printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
374             }
375         return -1;
376         }
377
378     PQclear(result);
379     }
380
381 if (CommitTransaction())
382     {
383     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to commit transaction'\n");
384     return -1;
385     }
386
387 return 0;
388 }
389 //-----------------------------------------------------------------------------
390 int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
391                                   const string & tariffName) const
392 {
393 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
394
395 if (PQstatus(connection) != CONNECTION_OK)
396     {
397     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
398     if (Reset())
399         {
400         strError = "Connection lost";
401         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
402         return -1;
403         }
404     }
405
406 PGresult * result;
407
408 if (StartTransaction())
409     {
410     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to start transaction'\n");
411     return -1;
412     }
413
414 std::string ename = tariffName;
415
416 if (EscapeString(ename))
417     {
418     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
419     if (RollbackTransaction())
420         {
421         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
422         }
423     return -1;
424     }
425
426 td->tariffConf.name = tariffName;
427
428 std::stringstream query;
429 query << "SELECT pk_tariff, \
430                  fee, \
431                  free, \
432                  passive_cost, \
433                  traff_type \
434           FROM tb_tariffs WHERE name = '" << ename << "'";
435
436 result = PQexec(connection, query.str().c_str());
437
438 if (PQresultStatus(result) != PGRES_TUPLES_OK)
439     {
440     strError = PQresultErrorMessage(result);
441     PQclear(result);
442     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
443     if (RollbackTransaction())
444         {
445         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
446         }
447     return -1;
448     }
449
450 int tuples = PQntuples(result);
451
452 if (tuples != 1)
453     {
454     strError = "Failed to fetch tariff data";
455     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
456     PQclear(result);
457     if (RollbackTransaction())
458         {
459         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
460         }
461     return -1;
462     }
463
464 std::stringstream tuple;
465 tuple << PQgetvalue(result, 0, 0) << " ";
466 tuple << PQgetvalue(result, 0, 1) << " ";
467 tuple << PQgetvalue(result, 0, 2) << " ";
468 tuple << PQgetvalue(result, 0, 3) << " ";
469 tuple << PQgetvalue(result, 0, 4) << " ";
470
471 int id;
472 tuple >> id;
473 tuple >> td->tariffConf.fee;
474 tuple >> td->tariffConf.free;
475 tuple >> td->tariffConf.passiveCost;
476 tuple >> td->tariffConf.traffType;
477
478 PQclear(result);
479
480 query.str("");
481 query << "SELECT dir_num, \
482                  price_day_a, \
483                  price_day_b, \
484                  price_night_a, \
485                  price_night_b, \
486                  threshold, \
487                  EXTRACT(hour FROM time_day_begins), \
488                  EXTRACT(minute FROM time_day_begins), \
489                  EXTRACT(hour FROM time_day_ends), \
490                  EXTRACT(minute FROM time_day_ends) \
491           FROM tb_tariffs_params \
492           WHERE fk_tariff = " << id;
493
494 result = PQexec(connection, query.str().c_str());
495
496 if (PQresultStatus(result) != PGRES_TUPLES_OK)
497     {
498     strError = PQresultErrorMessage(result);
499     PQclear(result);
500     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
501     if (RollbackTransaction())
502         {
503         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
504         }
505     return -1;
506     }
507
508 tuples = PQntuples(result);
509
510 if (tuples != DIR_NUM)
511     {
512     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
513     }
514
515 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
516     {
517     std::stringstream tuple;
518     tuple << PQgetvalue(result, i, 0) << " ";
519     tuple << PQgetvalue(result, i, 1) << " ";
520     tuple << PQgetvalue(result, i, 2) << " ";
521     tuple << PQgetvalue(result, i, 3) << " ";
522     tuple << PQgetvalue(result, i, 4) << " ";
523     tuple << PQgetvalue(result, i, 5) << " ";
524     tuple << PQgetvalue(result, i, 6) << " ";
525     tuple << PQgetvalue(result, i, 7) << " ";
526     tuple << PQgetvalue(result, i, 8) << " ";
527     tuple << PQgetvalue(result, i, 9) << " ";
528
529     int dir;
530
531     tuple >> dir;
532     tuple >> td->dirPrice[dir].priceDayA;
533     td->dirPrice[dir].priceDayA /= 1024 * 1024;
534     tuple >> td->dirPrice[dir].priceDayB;
535     td->dirPrice[dir].priceDayB /= 1024 * 1024;
536     tuple >> td->dirPrice[dir].priceNightA;
537     td->dirPrice[dir].priceNightA /= 1024 * 1024;
538     tuple >> td->dirPrice[dir].priceNightB;
539     td->dirPrice[dir].priceNightB /= 1024 * 1024;
540     tuple >> td->dirPrice[dir].threshold;
541     tuple >> td->dirPrice[dir].hDay;
542     tuple >> td->dirPrice[dir].mDay;
543     tuple >> td->dirPrice[dir].hNight;
544     tuple >> td->dirPrice[dir].mNight;
545
546     if (td->dirPrice[dir].priceDayA == td->dirPrice[dir].priceNightA &&
547         td->dirPrice[dir].priceDayB == td->dirPrice[dir].priceNightB)
548         {
549         td->dirPrice[dir].singlePrice = true;
550         }
551     else
552         {
553         td->dirPrice[dir].singlePrice = false;
554         }
555     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
556         {
557         td->dirPrice[dir].noDiscount = true;
558         }
559     else
560         {
561
562         td->dirPrice[dir].noDiscount = false;
563         }
564
565     }
566
567 PQclear(result);
568
569 if (CommitTransaction())
570     {
571     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
572     return -1;
573     }
574
575 return 0;
576 }
577 //-----------------------------------------------------------------------------
578