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));
515 td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(PQgetvalue(result, 0, 6));
520 query << "SELECT dir_num, \
526 EXTRACT(hour FROM time_day_begins), \
527 EXTRACT(minute FROM time_day_begins), \
528 EXTRACT(hour FROM time_day_ends), \
529 EXTRACT(minute FROM time_day_ends) \
530 FROM tb_tariffs_params \
531 WHERE fk_tariff = " << id;
533 result = PQexec(connection, query.str().c_str());
535 if (PQresultStatus(result) != PGRES_TUPLES_OK)
537 strError = PQresultErrorMessage(result);
539 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
540 if (RollbackTransaction())
542 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
547 tuples = PQntuples(result);
549 if (tuples != DIR_NUM)
551 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
554 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
559 std::stringstream tuple;
560 tuple << PQgetvalue(result, i, 0) << " ";
561 tuple << PQgetvalue(result, i, 1) << " ";
562 tuple << PQgetvalue(result, i, 2) << " ";
563 tuple << PQgetvalue(result, i, 3) << " ";
564 tuple << PQgetvalue(result, i, 4) << " ";
565 tuple << PQgetvalue(result, i, 5) << " ";
566 tuple << PQgetvalue(result, i, 6) << " ";
567 tuple << PQgetvalue(result, i, 7) << " ";
568 tuple << PQgetvalue(result, i, 8) << " ";
569 tuple << PQgetvalue(result, i, 9) << " ";
572 tuple >> td->dirPrice[dir].priceDayA;
573 td->dirPrice[dir].priceDayA /= 1024 * 1024;
574 tuple >> td->dirPrice[dir].priceDayB;
575 td->dirPrice[dir].priceDayB /= 1024 * 1024;
576 tuple >> td->dirPrice[dir].priceNightA;
577 td->dirPrice[dir].priceNightA /= 1024 * 1024;
578 tuple >> td->dirPrice[dir].priceNightB;
579 td->dirPrice[dir].priceNightB /= 1024 * 1024;
580 tuple >> td->dirPrice[dir].threshold;
581 tuple >> td->dirPrice[dir].hDay;
582 tuple >> td->dirPrice[dir].mDay;
583 tuple >> td->dirPrice[dir].hNight;
584 tuple >> td->dirPrice[dir].mNight;
587 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
588 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
590 td->dirPrice[dir].singlePrice = true;
594 td->dirPrice[dir].singlePrice = false;
596 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
598 td->dirPrice[dir].noDiscount = true;
603 td->dirPrice[dir].noDiscount = false;
610 if (CommitTransaction())
612 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
618 //-----------------------------------------------------------------------------