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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 * Tariffs manipulation methods
25 * $Date: 2009/06/09 12:32:40 $
36 #include "postgresql_store.h"
37 #include "stg/locker.h"
39 //-----------------------------------------------------------------------------
40 int POSTGRESQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
42 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
44 if (PQstatus(connection) != CONNECTION_OK)
46 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
49 strError = "Connection lost";
50 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
57 if (StartTransaction())
59 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to start transaction'\n");
63 result = PQexec(connection, "SELECT name FROM tb_tariffs");
65 if (PQresultStatus(result) != PGRES_TUPLES_OK)
67 strError = PQresultErrorMessage(result);
69 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
70 if (RollbackTransaction())
72 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to rollback transaction'\n");
77 int tuples = PQntuples(result);
79 for (int i = 0; i < tuples; ++i)
81 tariffsList->push_back(PQgetvalue(result, i, 0));
86 if (CommitTransaction())
88 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to commit transaction'\n");
95 //-----------------------------------------------------------------------------
96 int POSTGRESQL_STORE::AddTariff(const std::string & name) const
98 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
100 if (PQstatus(connection) != CONNECTION_OK)
102 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
105 strError = "Connection lost";
106 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
113 if (StartTransaction())
115 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to start transaction'\n");
119 std::string ename = name;
121 if (EscapeString(ename))
123 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
124 if (RollbackTransaction())
126 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
131 std::ostringstream query;
132 query << "SELECT sp_add_tariff('" << ename << "', " << DIR_NUM << ")";
134 result = PQexec(connection, query.str().c_str());
136 if (PQresultStatus(result) != PGRES_TUPLES_OK)
138 strError = PQresultErrorMessage(result);
140 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
141 if (RollbackTransaction())
143 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
150 if (CommitTransaction())
152 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to commit transaction'\n");
158 //-----------------------------------------------------------------------------
159 int POSTGRESQL_STORE::DelTariff(const std::string & name) const
161 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
163 if (PQstatus(connection) != CONNECTION_OK)
165 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
168 strError = "Connection lost";
169 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
176 if (StartTransaction())
178 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to start transaction'\n");
182 std::string ename = name;
184 if (EscapeString(ename))
186 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
187 if (RollbackTransaction())
189 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
194 std::ostringstream query;
195 query << "DELETE FROM tb_tariffs WHERE name = '" << ename << "'";
197 result = PQexec(connection, query.str().c_str());
199 if (PQresultStatus(result) != PGRES_COMMAND_OK)
201 strError = PQresultErrorMessage(result);
203 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
204 if (RollbackTransaction())
206 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to rollback transaction'\n");
213 if (CommitTransaction())
215 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to commit transaction'\n");
221 //-----------------------------------------------------------------------------
222 int POSTGRESQL_STORE::SaveTariff(const TARIFF_DATA & td,
223 const std::string & tariffName) const
225 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
227 if (PQstatus(connection) != CONNECTION_OK)
229 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
232 strError = "Connection lost";
233 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
240 if (StartTransaction())
242 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to start transaction'\n");
246 std::string ename = tariffName;
248 if (EscapeString(ename))
250 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to escape name'\n");
251 if (RollbackTransaction())
253 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
259 double pda, pdb, pna, pnb;
263 std::ostringstream query;
264 query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
266 result = PQexec(connection, query.str().c_str());
269 if (PQresultStatus(result) != PGRES_TUPLES_OK)
271 strError = PQresultErrorMessage(result);
273 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
274 if (RollbackTransaction())
276 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
281 int tuples = PQntuples(result);
285 strError = "Failed to fetch tariff ID";
286 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
288 if (RollbackTransaction())
290 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
296 std::stringstream tuple;
297 tuple << PQgetvalue(result, 0, 0);
305 std::ostringstream query;
306 query << "UPDATE tb_tariffs SET \
307 fee = " << td.tariffConf.fee << ", \
308 free = " << td.tariffConf.free << ", \
309 passive_cost = " << td.tariffConf.passiveCost << ", \
310 traff_type = " << td.tariffConf.traffType << " \
311 WHERE pk_tariff = " << id;
313 result = PQexec(connection, query.str().c_str());
316 if (PQresultStatus(result) != PGRES_COMMAND_OK)
318 strError = PQresultErrorMessage(result);
320 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
321 if (RollbackTransaction())
323 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
330 for(i = 0; i < DIR_NUM; i++)
333 pda = td.dirPrice[i].priceDayA * 1024 * 1024;
334 pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
336 if (td.dirPrice[i].singlePrice)
343 pna = td.dirPrice[i].priceNightA * 1024 * 1024;
344 pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
347 if (td.dirPrice[i].noDiscount)
349 threshold = 0xffFFffFF;
353 threshold = td.dirPrice[i].threshold;
357 std::ostringstream query;
358 query << "UPDATE tb_tariffs_params SET \
359 price_day_a = " << pda << ", \
360 price_day_b = " << pdb << ", \
361 price_night_a = " << pna << ", \
362 price_night_b = " << pnb << ", \
363 threshold = " << threshold << ", \
364 time_day_begins = CAST('" << td.dirPrice[i].hDay
366 << td.dirPrice[i].mDay << "' AS TIME), \
367 time_day_ends = CAST('" << td.dirPrice[i].hNight
369 << td.dirPrice[i].mNight << "' AS TIME) \
370 WHERE fk_tariff = " << id << " AND dir_num = " << i;
372 result = PQexec(connection, query.str().c_str());
375 if (PQresultStatus(result) != PGRES_COMMAND_OK)
377 strError = PQresultErrorMessage(result);
379 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
380 if (RollbackTransaction())
382 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
390 if (CommitTransaction())
392 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to commit transaction'\n");
398 //-----------------------------------------------------------------------------
399 int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
400 const std::string & tariffName) const
402 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
404 if (PQstatus(connection) != CONNECTION_OK)
406 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
409 strError = "Connection lost";
410 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
417 if (StartTransaction())
419 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to start transaction'\n");
423 std::string ename = tariffName;
425 if (EscapeString(ename))
427 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
428 if (RollbackTransaction())
430 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
435 td->tariffConf.name = tariffName;
437 std::ostringstream query;
438 query << "SELECT pk_tariff, \
443 FROM tb_tariffs WHERE name = '" << ename << "'";
445 result = PQexec(connection, query.str().c_str());
447 if (PQresultStatus(result) != PGRES_TUPLES_OK)
449 strError = PQresultErrorMessage(result);
451 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
452 if (RollbackTransaction())
454 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
459 int tuples = PQntuples(result);
463 strError = "Failed to fetch tariff data";
464 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
466 if (RollbackTransaction())
468 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
476 std::stringstream tuple;
477 tuple << PQgetvalue(result, 0, 0) << " ";
478 tuple << PQgetvalue(result, 0, 1) << " ";
479 tuple << PQgetvalue(result, 0, 2) << " ";
480 tuple << PQgetvalue(result, 0, 3) << " ";
481 tuple << PQgetvalue(result, 0, 4) << " ";
484 tuple >> td->tariffConf.fee;
485 tuple >> td->tariffConf.free;
486 tuple >> td->tariffConf.passiveCost;
487 tuple >> td->tariffConf.traffType;
493 query << "SELECT dir_num, \
499 EXTRACT(hour FROM time_day_begins), \
500 EXTRACT(minute FROM time_day_begins), \
501 EXTRACT(hour FROM time_day_ends), \
502 EXTRACT(minute FROM time_day_ends) \
503 FROM tb_tariffs_params \
504 WHERE fk_tariff = " << id;
506 result = PQexec(connection, query.str().c_str());
508 if (PQresultStatus(result) != PGRES_TUPLES_OK)
510 strError = PQresultErrorMessage(result);
512 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
513 if (RollbackTransaction())
515 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
520 tuples = PQntuples(result);
522 if (tuples != DIR_NUM)
524 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
527 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
532 std::stringstream tuple;
533 tuple << PQgetvalue(result, i, 0) << " ";
534 tuple << PQgetvalue(result, i, 1) << " ";
535 tuple << PQgetvalue(result, i, 2) << " ";
536 tuple << PQgetvalue(result, i, 3) << " ";
537 tuple << PQgetvalue(result, i, 4) << " ";
538 tuple << PQgetvalue(result, i, 5) << " ";
539 tuple << PQgetvalue(result, i, 6) << " ";
540 tuple << PQgetvalue(result, i, 7) << " ";
541 tuple << PQgetvalue(result, i, 8) << " ";
542 tuple << PQgetvalue(result, i, 9) << " ";
545 tuple >> td->dirPrice[dir].priceDayA;
546 td->dirPrice[dir].priceDayA /= 1024 * 1024;
547 tuple >> td->dirPrice[dir].priceDayB;
548 td->dirPrice[dir].priceDayB /= 1024 * 1024;
549 tuple >> td->dirPrice[dir].priceNightA;
550 td->dirPrice[dir].priceNightA /= 1024 * 1024;
551 tuple >> td->dirPrice[dir].priceNightB;
552 td->dirPrice[dir].priceNightB /= 1024 * 1024;
553 tuple >> td->dirPrice[dir].threshold;
554 tuple >> td->dirPrice[dir].hDay;
555 tuple >> td->dirPrice[dir].mDay;
556 tuple >> td->dirPrice[dir].hNight;
557 tuple >> td->dirPrice[dir].mNight;
560 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) > 1.0e-3 &&
561 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) > 1.0e-3)
563 td->dirPrice[dir].singlePrice = true;
567 td->dirPrice[dir].singlePrice = false;
569 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
571 td->dirPrice[dir].noDiscount = true;
576 td->dirPrice[dir].noDiscount = false;
583 if (CommitTransaction())
585 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
591 //-----------------------------------------------------------------------------