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 \
464 , change_policy_timeout";
466 query << " FROM tb_tariffs WHERE name = '" << ename << "'";
468 result = PQexec(connection, query.str().c_str());
470 if (PQresultStatus(result) != PGRES_TUPLES_OK)
472 strError = PQresultErrorMessage(result);
474 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
475 if (RollbackTransaction())
477 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
482 int tuples = PQntuples(result);
486 strError = "Failed to fetch tariff data";
487 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
489 if (RollbackTransaction())
491 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
499 std::stringstream tuple;
500 tuple << PQgetvalue(result, 0, 0) << " ";
501 tuple << PQgetvalue(result, 0, 1) << " ";
502 tuple << PQgetvalue(result, 0, 2) << " ";
503 tuple << PQgetvalue(result, 0, 3) << " ";
504 tuple << PQgetvalue(result, 0, 4) << " ";
507 tuple >> td->tariffConf.fee;
508 tuple >> td->tariffConf.free;
509 tuple >> td->tariffConf.passiveCost;
510 tuple >> td->tariffConf.traffType;
514 td->tariffConf.period = TARIFF::StringToPeriod(PQgetvalue(result, 0, 5));
518 td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(PQgetvalue(result, 0, 6));
519 td->tariffConf.changePolicyTimeout = TS2Int(PQgetvalue(result, 0, 7));
525 query << "SELECT dir_num, \
531 EXTRACT(hour FROM time_day_begins), \
532 EXTRACT(minute FROM time_day_begins), \
533 EXTRACT(hour FROM time_day_ends), \
534 EXTRACT(minute FROM time_day_ends) \
535 FROM tb_tariffs_params \
536 WHERE fk_tariff = " << id;
538 result = PQexec(connection, query.str().c_str());
540 if (PQresultStatus(result) != PGRES_TUPLES_OK)
542 strError = PQresultErrorMessage(result);
544 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
545 if (RollbackTransaction())
547 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
552 tuples = PQntuples(result);
554 if (tuples != DIR_NUM)
556 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
559 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
564 std::stringstream tuple;
565 tuple << PQgetvalue(result, i, 0) << " ";
566 tuple << PQgetvalue(result, i, 1) << " ";
567 tuple << PQgetvalue(result, i, 2) << " ";
568 tuple << PQgetvalue(result, i, 3) << " ";
569 tuple << PQgetvalue(result, i, 4) << " ";
570 tuple << PQgetvalue(result, i, 5) << " ";
571 tuple << PQgetvalue(result, i, 6) << " ";
572 tuple << PQgetvalue(result, i, 7) << " ";
573 tuple << PQgetvalue(result, i, 8) << " ";
574 tuple << PQgetvalue(result, i, 9) << " ";
577 tuple >> td->dirPrice[dir].priceDayA;
578 td->dirPrice[dir].priceDayA /= 1024 * 1024;
579 tuple >> td->dirPrice[dir].priceDayB;
580 td->dirPrice[dir].priceDayB /= 1024 * 1024;
581 tuple >> td->dirPrice[dir].priceNightA;
582 td->dirPrice[dir].priceNightA /= 1024 * 1024;
583 tuple >> td->dirPrice[dir].priceNightB;
584 td->dirPrice[dir].priceNightB /= 1024 * 1024;
585 tuple >> td->dirPrice[dir].threshold;
586 tuple >> td->dirPrice[dir].hDay;
587 tuple >> td->dirPrice[dir].mDay;
588 tuple >> td->dirPrice[dir].hNight;
589 tuple >> td->dirPrice[dir].mNight;
592 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
593 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
595 td->dirPrice[dir].singlePrice = true;
599 td->dirPrice[dir].singlePrice = false;
601 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
603 td->dirPrice[dir].noDiscount = true;
608 td->dirPrice[dir].noDiscount = false;
615 if (CommitTransaction())
617 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
623 //-----------------------------------------------------------------------------