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) << "'";
320 query << " WHERE pk_tariff = " << id;
322 result = PQexec(connection, query.str().c_str());
325 if (PQresultStatus(result) != PGRES_COMMAND_OK)
327 strError = PQresultErrorMessage(result);
329 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
330 if (RollbackTransaction())
332 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
339 for(int i = 0; i < DIR_NUM; i++)
341 double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
342 double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
346 if (td.dirPrice[i].singlePrice)
353 pna = td.dirPrice[i].priceNightA * 1024 * 1024;
354 pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
358 if (td.dirPrice[i].noDiscount)
360 threshold = 0xffFFffFF;
364 threshold = td.dirPrice[i].threshold;
368 std::ostringstream query;
369 query << "UPDATE tb_tariffs_params SET \
370 price_day_a = " << pda << ", \
371 price_day_b = " << pdb << ", \
372 price_night_a = " << pna << ", \
373 price_night_b = " << pnb << ", \
374 threshold = " << threshold << ", \
375 time_day_begins = CAST('" << td.dirPrice[i].hDay
377 << td.dirPrice[i].mDay << "' AS TIME), \
378 time_day_ends = CAST('" << td.dirPrice[i].hNight
380 << td.dirPrice[i].mNight << "' AS TIME) \
381 WHERE fk_tariff = " << id << " AND dir_num = " << i;
383 result = PQexec(connection, query.str().c_str());
386 if (PQresultStatus(result) != PGRES_COMMAND_OK)
388 strError = PQresultErrorMessage(result);
390 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
391 if (RollbackTransaction())
393 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
401 if (CommitTransaction())
403 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to commit transaction'\n");
409 //-----------------------------------------------------------------------------
410 int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
411 const std::string & tariffName) const
413 STG_LOCKER lock(&mutex);
415 if (PQstatus(connection) != CONNECTION_OK)
417 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
420 strError = "Connection lost";
421 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
428 if (StartTransaction())
430 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to start transaction'\n");
434 std::string ename = tariffName;
436 if (EscapeString(ename))
438 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
439 if (RollbackTransaction())
441 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
446 td->tariffConf.name = tariffName;
448 std::ostringstream query;
449 query << "SELECT pk_tariff, \
458 query << " FROM tb_tariffs WHERE name = '" << ename << "'";
460 result = PQexec(connection, query.str().c_str());
462 if (PQresultStatus(result) != PGRES_TUPLES_OK)
464 strError = PQresultErrorMessage(result);
466 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
467 if (RollbackTransaction())
469 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
474 int tuples = PQntuples(result);
478 strError = "Failed to fetch tariff data";
479 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
481 if (RollbackTransaction())
483 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
491 std::stringstream tuple;
492 tuple << PQgetvalue(result, 0, 0) << " ";
493 tuple << PQgetvalue(result, 0, 1) << " ";
494 tuple << PQgetvalue(result, 0, 2) << " ";
495 tuple << PQgetvalue(result, 0, 3) << " ";
496 tuple << PQgetvalue(result, 0, 4) << " ";
499 tuple >> td->tariffConf.fee;
500 tuple >> td->tariffConf.free;
501 tuple >> td->tariffConf.passiveCost;
502 tuple >> td->tariffConf.traffType;
506 td->tariffConf.period = TARIFF::StringToPeriod(PQgetvalue(result, 0, 5));
511 query << "SELECT dir_num, \
517 EXTRACT(hour FROM time_day_begins), \
518 EXTRACT(minute FROM time_day_begins), \
519 EXTRACT(hour FROM time_day_ends), \
520 EXTRACT(minute FROM time_day_ends) \
521 FROM tb_tariffs_params \
522 WHERE fk_tariff = " << id;
524 result = PQexec(connection, query.str().c_str());
526 if (PQresultStatus(result) != PGRES_TUPLES_OK)
528 strError = PQresultErrorMessage(result);
530 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
531 if (RollbackTransaction())
533 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
538 tuples = PQntuples(result);
540 if (tuples != DIR_NUM)
542 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
545 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
550 std::stringstream tuple;
551 tuple << PQgetvalue(result, i, 0) << " ";
552 tuple << PQgetvalue(result, i, 1) << " ";
553 tuple << PQgetvalue(result, i, 2) << " ";
554 tuple << PQgetvalue(result, i, 3) << " ";
555 tuple << PQgetvalue(result, i, 4) << " ";
556 tuple << PQgetvalue(result, i, 5) << " ";
557 tuple << PQgetvalue(result, i, 6) << " ";
558 tuple << PQgetvalue(result, i, 7) << " ";
559 tuple << PQgetvalue(result, i, 8) << " ";
560 tuple << PQgetvalue(result, i, 9) << " ";
563 tuple >> td->dirPrice[dir].priceDayA;
564 td->dirPrice[dir].priceDayA /= 1024 * 1024;
565 tuple >> td->dirPrice[dir].priceDayB;
566 td->dirPrice[dir].priceDayB /= 1024 * 1024;
567 tuple >> td->dirPrice[dir].priceNightA;
568 td->dirPrice[dir].priceNightA /= 1024 * 1024;
569 tuple >> td->dirPrice[dir].priceNightB;
570 td->dirPrice[dir].priceNightB /= 1024 * 1024;
571 tuple >> td->dirPrice[dir].threshold;
572 tuple >> td->dirPrice[dir].hDay;
573 tuple >> td->dirPrice[dir].mDay;
574 tuple >> td->dirPrice[dir].hNight;
575 tuple >> td->dirPrice[dir].mNight;
578 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
579 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
581 td->dirPrice[dir].singlePrice = true;
585 td->dirPrice[dir].singlePrice = false;
587 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
589 td->dirPrice[dir].noDiscount = true;
594 td->dirPrice[dir].noDiscount = false;
601 if (CommitTransaction())
603 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
609 //-----------------------------------------------------------------------------