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) << "', \
322 change_policy_timeout = CAST('" << Int2TS(td.tariffConf.changePolicyTimeout) << "' AS TIMESTAMP)";
324 query << " WHERE pk_tariff = " << id;
326 result = PQexec(connection, query.str().c_str());
329 if (PQresultStatus(result) != PGRES_COMMAND_OK)
331 strError = PQresultErrorMessage(result);
333 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
334 if (RollbackTransaction())
336 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
343 for(int i = 0; i < DIR_NUM; i++)
345 double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
346 double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
350 if (td.dirPrice[i].singlePrice)
357 pna = td.dirPrice[i].priceNightA * 1024 * 1024;
358 pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
362 if (td.dirPrice[i].noDiscount)
364 threshold = 0xffFFffFF;
368 threshold = td.dirPrice[i].threshold;
372 std::ostringstream query;
373 query << "UPDATE tb_tariffs_params SET \
374 price_day_a = " << pda << ", \
375 price_day_b = " << pdb << ", \
376 price_night_a = " << pna << ", \
377 price_night_b = " << pnb << ", \
378 threshold = " << threshold << ", \
379 time_day_begins = CAST('" << td.dirPrice[i].hDay
381 << td.dirPrice[i].mDay << "' AS TIME), \
382 time_day_ends = CAST('" << td.dirPrice[i].hNight
384 << td.dirPrice[i].mNight << "' AS TIME) \
385 WHERE fk_tariff = " << id << " AND dir_num = " << i;
387 result = PQexec(connection, query.str().c_str());
390 if (PQresultStatus(result) != PGRES_COMMAND_OK)
392 strError = PQresultErrorMessage(result);
394 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
395 if (RollbackTransaction())
397 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
405 if (CommitTransaction())
407 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to commit transaction'\n");
413 //-----------------------------------------------------------------------------
414 int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
415 const std::string & tariffName) const
417 STG_LOCKER lock(&mutex);
419 if (PQstatus(connection) != CONNECTION_OK)
421 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
424 strError = "Connection lost";
425 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
432 if (StartTransaction())
434 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to start transaction'\n");
438 std::string ename = tariffName;
440 if (EscapeString(ename))
442 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
443 if (RollbackTransaction())
445 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
450 td->tariffConf.name = tariffName;
452 std::ostringstream query;
453 query << "SELECT pk_tariff, \
463 query << ", change_policy";
465 query << " FROM tb_tariffs WHERE name = '" << ename << "'";
467 result = PQexec(connection, query.str().c_str());
469 if (PQresultStatus(result) != PGRES_TUPLES_OK)
471 strError = PQresultErrorMessage(result);
473 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
474 if (RollbackTransaction())
476 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
481 int tuples = PQntuples(result);
485 strError = "Failed to fetch tariff data";
486 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
488 if (RollbackTransaction())
490 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
498 std::stringstream tuple;
499 tuple << PQgetvalue(result, 0, 0) << " ";
500 tuple << PQgetvalue(result, 0, 1) << " ";
501 tuple << PQgetvalue(result, 0, 2) << " ";
502 tuple << PQgetvalue(result, 0, 3) << " ";
503 tuple << PQgetvalue(result, 0, 4) << " ";
506 tuple >> td->tariffConf.fee;
507 tuple >> td->tariffConf.free;
508 tuple >> td->tariffConf.passiveCost;
509 tuple >> td->tariffConf.traffType;
513 td->tariffConf.period = TARIFF::StringToPeriod(PQgetvalue(result, 0, 5));
516 td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(PQgetvalue(result, 0, 6));
521 query << "SELECT dir_num, \
527 EXTRACT(hour FROM time_day_begins), \
528 EXTRACT(minute FROM time_day_begins), \
529 EXTRACT(hour FROM time_day_ends), \
530 EXTRACT(minute FROM time_day_ends) \
531 FROM tb_tariffs_params \
532 WHERE fk_tariff = " << id;
534 result = PQexec(connection, query.str().c_str());
536 if (PQresultStatus(result) != PGRES_TUPLES_OK)
538 strError = PQresultErrorMessage(result);
540 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
541 if (RollbackTransaction())
543 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
548 tuples = PQntuples(result);
550 if (tuples != DIR_NUM)
552 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
555 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
560 std::stringstream tuple;
561 tuple << PQgetvalue(result, i, 0) << " ";
562 tuple << PQgetvalue(result, i, 1) << " ";
563 tuple << PQgetvalue(result, i, 2) << " ";
564 tuple << PQgetvalue(result, i, 3) << " ";
565 tuple << PQgetvalue(result, i, 4) << " ";
566 tuple << PQgetvalue(result, i, 5) << " ";
567 tuple << PQgetvalue(result, i, 6) << " ";
568 tuple << PQgetvalue(result, i, 7) << " ";
569 tuple << PQgetvalue(result, i, 8) << " ";
570 tuple << PQgetvalue(result, i, 9) << " ";
573 tuple >> td->dirPrice[dir].priceDayA;
574 td->dirPrice[dir].priceDayA /= 1024 * 1024;
575 tuple >> td->dirPrice[dir].priceDayB;
576 td->dirPrice[dir].priceDayB /= 1024 * 1024;
577 tuple >> td->dirPrice[dir].priceNightA;
578 td->dirPrice[dir].priceNightA /= 1024 * 1024;
579 tuple >> td->dirPrice[dir].priceNightB;
580 td->dirPrice[dir].priceNightB /= 1024 * 1024;
581 tuple >> td->dirPrice[dir].threshold;
582 tuple >> td->dirPrice[dir].hDay;
583 tuple >> td->dirPrice[dir].mDay;
584 tuple >> td->dirPrice[dir].hNight;
585 tuple >> td->dirPrice[dir].mNight;
588 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
589 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
591 td->dirPrice[dir].singlePrice = true;
595 td->dirPrice[dir].singlePrice = false;
597 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
599 td->dirPrice[dir].noDiscount = true;
604 td->dirPrice[dir].noDiscount = false;
611 if (CommitTransaction())
613 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
619 //-----------------------------------------------------------------------------