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"
42 const int pt_mega = 1024 * 1024;
46 //-----------------------------------------------------------------------------
47 int POSTGRESQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
49 STG_LOCKER lock(&mutex);
51 if (PQstatus(connection) != CONNECTION_OK)
53 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
56 strError = "Connection lost";
57 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
64 if (StartTransaction())
66 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to start transaction'\n");
70 result = PQexec(connection, "SELECT name FROM tb_tariffs");
72 if (PQresultStatus(result) != PGRES_TUPLES_OK)
74 strError = PQresultErrorMessage(result);
76 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
77 if (RollbackTransaction())
79 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to rollback transaction'\n");
84 int tuples = PQntuples(result);
86 for (int i = 0; i < tuples; ++i)
88 tariffsList->push_back(PQgetvalue(result, i, 0));
93 if (CommitTransaction())
95 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to commit transaction'\n");
102 //-----------------------------------------------------------------------------
103 int POSTGRESQL_STORE::AddTariff(const std::string & name) const
105 STG_LOCKER lock(&mutex);
107 if (PQstatus(connection) != CONNECTION_OK)
109 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
112 strError = "Connection lost";
113 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
120 if (StartTransaction())
122 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to start transaction'\n");
126 std::string ename = name;
128 if (EscapeString(ename))
130 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
131 if (RollbackTransaction())
133 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
138 std::ostringstream query;
139 query << "SELECT sp_add_tariff('" << ename << "', " << DIR_NUM << ")";
141 result = PQexec(connection, query.str().c_str());
143 if (PQresultStatus(result) != PGRES_TUPLES_OK)
145 strError = PQresultErrorMessage(result);
147 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
148 if (RollbackTransaction())
150 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
157 if (CommitTransaction())
159 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to commit transaction'\n");
165 //-----------------------------------------------------------------------------
166 int POSTGRESQL_STORE::DelTariff(const std::string & name) const
168 STG_LOCKER lock(&mutex);
170 if (PQstatus(connection) != CONNECTION_OK)
172 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
175 strError = "Connection lost";
176 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
183 if (StartTransaction())
185 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to start transaction'\n");
189 std::string ename = name;
191 if (EscapeString(ename))
193 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
194 if (RollbackTransaction())
196 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
201 std::ostringstream query;
202 query << "DELETE FROM tb_tariffs WHERE name = '" << ename << "'";
204 result = PQexec(connection, query.str().c_str());
206 if (PQresultStatus(result) != PGRES_COMMAND_OK)
208 strError = PQresultErrorMessage(result);
210 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
211 if (RollbackTransaction())
213 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to rollback transaction'\n");
220 if (CommitTransaction())
222 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to commit transaction'\n");
228 //-----------------------------------------------------------------------------
229 int POSTGRESQL_STORE::SaveTariff(const TARIFF_DATA & td,
230 const std::string & tariffName) const
232 STG_LOCKER lock(&mutex);
234 if (PQstatus(connection) != CONNECTION_OK)
236 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
239 strError = "Connection lost";
240 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
247 if (StartTransaction())
249 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to start transaction'\n");
253 std::string ename = tariffName;
255 if (EscapeString(ename))
257 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to escape name'\n");
258 if (RollbackTransaction())
260 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
266 std::ostringstream query;
267 query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
269 result = PQexec(connection, query.str().c_str());
272 if (PQresultStatus(result) != PGRES_TUPLES_OK)
274 strError = PQresultErrorMessage(result);
276 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
277 if (RollbackTransaction())
279 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
284 int tuples = PQntuples(result);
288 strError = "Failed to fetch tariff ID";
289 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
291 if (RollbackTransaction())
293 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
301 std::stringstream tuple;
302 tuple << PQgetvalue(result, 0, 0);
310 std::ostringstream query;
311 query << "UPDATE tb_tariffs SET \
312 fee = " << td.tariffConf.fee << ", \
313 free = " << td.tariffConf.free << ", \
314 passive_cost = " << td.tariffConf.passiveCost << ", \
315 traff_type = " << td.tariffConf.traffType;
318 query << ", period = '" << TARIFF::PeriodToString(td.tariffConf.period) << "'";
321 query << ", change_policy = '" << TARIFF::ChangePolicyToString(td.tariffConf.changePolicy) << "'";
323 query << " WHERE pk_tariff = " << id;
325 result = PQexec(connection, query.str().c_str());
328 if (PQresultStatus(result) != PGRES_COMMAND_OK)
330 strError = PQresultErrorMessage(result);
332 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
333 if (RollbackTransaction())
335 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
342 for(int i = 0; i < DIR_NUM; i++)
344 double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
345 double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
349 if (td.dirPrice[i].singlePrice)
356 pna = td.dirPrice[i].priceNightA * 1024 * 1024;
357 pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
361 if (td.dirPrice[i].noDiscount)
363 threshold = 0xffFFffFF;
367 threshold = td.dirPrice[i].threshold;
371 std::ostringstream query;
372 query << "UPDATE tb_tariffs_params SET \
373 price_day_a = " << pda << ", \
374 price_day_b = " << pdb << ", \
375 price_night_a = " << pna << ", \
376 price_night_b = " << pnb << ", \
377 threshold = " << threshold << ", \
378 time_day_begins = CAST('" << td.dirPrice[i].hDay
380 << td.dirPrice[i].mDay << "' AS TIME), \
381 time_day_ends = CAST('" << td.dirPrice[i].hNight
383 << td.dirPrice[i].mNight << "' AS TIME) \
384 WHERE fk_tariff = " << id << " AND dir_num = " << i;
386 result = PQexec(connection, query.str().c_str());
389 if (PQresultStatus(result) != PGRES_COMMAND_OK)
391 strError = PQresultErrorMessage(result);
393 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
394 if (RollbackTransaction())
396 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
404 if (CommitTransaction())
406 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to commit transaction'\n");
412 //-----------------------------------------------------------------------------
413 int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
414 const std::string & tariffName) const
416 STG_LOCKER lock(&mutex);
418 if (PQstatus(connection) != CONNECTION_OK)
420 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
423 strError = "Connection lost";
424 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
431 if (StartTransaction())
433 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to start transaction'\n");
437 std::string ename = tariffName;
439 if (EscapeString(ename))
441 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
442 if (RollbackTransaction())
444 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
449 td->tariffConf.name = tariffName;
451 std::ostringstream query;
452 query << "SELECT pk_tariff, \
462 query << ", change_policy";
464 query << " FROM tb_tariffs WHERE name = '" << ename << "'";
466 result = PQexec(connection, query.str().c_str());
468 if (PQresultStatus(result) != PGRES_TUPLES_OK)
470 strError = PQresultErrorMessage(result);
472 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
473 if (RollbackTransaction())
475 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
480 int tuples = PQntuples(result);
484 strError = "Failed to fetch tariff data";
485 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
487 if (RollbackTransaction())
489 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
497 std::stringstream tuple;
498 tuple << PQgetvalue(result, 0, 0) << " ";
499 tuple << PQgetvalue(result, 0, 1) << " ";
500 tuple << PQgetvalue(result, 0, 2) << " ";
501 tuple << PQgetvalue(result, 0, 3) << " ";
502 tuple << PQgetvalue(result, 0, 4) << " ";
505 tuple >> td->tariffConf.fee;
506 tuple >> td->tariffConf.free;
507 tuple >> td->tariffConf.passiveCost;
508 tuple >> td->tariffConf.traffType;
512 td->tariffConf.period = TARIFF::StringToPeriod(PQgetvalue(result, 0, 5));
517 query << "SELECT dir_num, \
523 EXTRACT(hour FROM time_day_begins), \
524 EXTRACT(minute FROM time_day_begins), \
525 EXTRACT(hour FROM time_day_ends), \
526 EXTRACT(minute FROM time_day_ends) \
527 FROM tb_tariffs_params \
528 WHERE fk_tariff = " << id;
530 result = PQexec(connection, query.str().c_str());
532 if (PQresultStatus(result) != PGRES_TUPLES_OK)
534 strError = PQresultErrorMessage(result);
536 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
537 if (RollbackTransaction())
539 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
544 tuples = PQntuples(result);
546 if (tuples != DIR_NUM)
548 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
551 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
556 std::stringstream tuple;
557 tuple << PQgetvalue(result, i, 0) << " ";
558 tuple << PQgetvalue(result, i, 1) << " ";
559 tuple << PQgetvalue(result, i, 2) << " ";
560 tuple << PQgetvalue(result, i, 3) << " ";
561 tuple << PQgetvalue(result, i, 4) << " ";
562 tuple << PQgetvalue(result, i, 5) << " ";
563 tuple << PQgetvalue(result, i, 6) << " ";
564 tuple << PQgetvalue(result, i, 7) << " ";
565 tuple << PQgetvalue(result, i, 8) << " ";
566 tuple << PQgetvalue(result, i, 9) << " ";
569 tuple >> td->dirPrice[dir].priceDayA;
570 td->dirPrice[dir].priceDayA /= 1024 * 1024;
571 tuple >> td->dirPrice[dir].priceDayB;
572 td->dirPrice[dir].priceDayB /= 1024 * 1024;
573 tuple >> td->dirPrice[dir].priceNightA;
574 td->dirPrice[dir].priceNightA /= 1024 * 1024;
575 tuple >> td->dirPrice[dir].priceNightB;
576 td->dirPrice[dir].priceNightB /= 1024 * 1024;
577 tuple >> td->dirPrice[dir].threshold;
578 tuple >> td->dirPrice[dir].hDay;
579 tuple >> td->dirPrice[dir].mDay;
580 tuple >> td->dirPrice[dir].hNight;
581 tuple >> td->dirPrice[dir].mNight;
584 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
585 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
587 td->dirPrice[dir].singlePrice = true;
591 td->dirPrice[dir].singlePrice = false;
593 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
595 td->dirPrice[dir].noDiscount = true;
600 td->dirPrice[dir].noDiscount = false;
607 if (CommitTransaction())
609 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
615 //-----------------------------------------------------------------------------