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"
39 //-----------------------------------------------------------------------------
40 int POSTGRESQL_STORE::GetTariffsList(vector<string> * tariffsList) const
42 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
44 if (PQstatus(connection) != CONNECTION_OK)
46 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
49 strError = "Connection lost";
50 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
57 if (StartTransaction())
59 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to start transaction'\n");
63 result = PQexec(connection, "SELECT name FROM tb_tariffs");
65 if (PQresultStatus(result) != PGRES_TUPLES_OK)
67 strError = PQresultErrorMessage(result);
69 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
70 if (RollbackTransaction())
72 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to rollback transaction'\n");
77 int tuples = PQntuples(result);
79 for (int i = 0; i < tuples; ++i)
81 tariffsList->push_back(PQgetvalue(result, i, 0));
86 if (CommitTransaction())
88 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to commit transaction'\n");
95 //-----------------------------------------------------------------------------
96 int POSTGRESQL_STORE::AddTariff(const string & name) const
98 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
100 if (PQstatus(connection) != CONNECTION_OK)
102 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
105 strError = "Connection lost";
106 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
113 if (StartTransaction())
115 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to start transaction'\n");
119 std::string ename = name;
121 if (EscapeString(ename))
123 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
124 if (RollbackTransaction())
126 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
131 std::stringstream query;
132 query << "SELECT sp_add_tariff('" << ename << "', " << DIR_NUM << ")";
134 result = PQexec(connection, query.str().c_str());
136 if (PQresultStatus(result) != PGRES_TUPLES_OK)
138 strError = PQresultErrorMessage(result);
140 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
141 if (RollbackTransaction())
143 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
150 if (CommitTransaction())
152 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to commit transaction'\n");
158 //-----------------------------------------------------------------------------
159 int POSTGRESQL_STORE::DelTariff(const string & name) const
161 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
163 if (PQstatus(connection) != CONNECTION_OK)
165 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
168 strError = "Connection lost";
169 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
176 if (StartTransaction())
178 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to start transaction'\n");
182 std::string ename = name;
184 if (EscapeString(ename))
186 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
187 if (RollbackTransaction())
189 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
194 std::stringstream query;
195 query << "DELETE FROM tb_tariffs WHERE name = '" << ename << "'";
197 result = PQexec(connection, query.str().c_str());
199 if (PQresultStatus(result) != PGRES_COMMAND_OK)
201 strError = PQresultErrorMessage(result);
203 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
204 if (RollbackTransaction())
206 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to rollback transaction'\n");
213 if (CommitTransaction())
215 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to commit transaction'\n");
221 //-----------------------------------------------------------------------------
222 int POSTGRESQL_STORE::SaveTariff(const TARIFF_DATA & td,
223 const string & tariffName) const
225 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
227 if (PQstatus(connection) != CONNECTION_OK)
229 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
232 strError = "Connection lost";
233 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
240 if (StartTransaction())
242 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to start transaction'\n");
246 std::string ename = tariffName;
248 if (EscapeString(ename))
250 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to escape name'\n");
251 if (RollbackTransaction())
253 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
259 double pda, pdb, pna, pnb;
262 std::stringstream query;
263 query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
265 result = PQexec(connection, query.str().c_str());
267 if (PQresultStatus(result) != PGRES_TUPLES_OK)
269 strError = PQresultErrorMessage(result);
271 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
272 if (RollbackTransaction())
274 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
279 int tuples = PQntuples(result);
283 strError = "Failed to fetch tariff ID";
284 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
286 if (RollbackTransaction())
288 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
293 std::stringstream tuple;
294 tuple << PQgetvalue(result, 0, 0);
301 query << "UPDATE tb_tariffs SET \
302 fee = " << td.tariffConf.fee << ", \
303 free = " << td.tariffConf.free << ", \
304 passive_cost = " << td.tariffConf.passiveCost << ", \
305 traff_type = " << td.tariffConf.traffType << " \
306 WHERE pk_tariff = " << id;
308 result = PQexec(connection, query.str().c_str());
310 if (PQresultStatus(result) != PGRES_COMMAND_OK)
312 strError = PQresultErrorMessage(result);
314 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
315 if (RollbackTransaction())
317 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
324 for(i = 0; i < DIR_NUM; i++)
327 pda = td.dirPrice[i].priceDayA * 1024 * 1024;
328 pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
330 if (td.dirPrice[i].singlePrice)
337 pna = td.dirPrice[i].priceNightA * 1024 * 1024;
338 pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
341 if (td.dirPrice[i].noDiscount)
343 threshold = 0xffFFffFF;
347 threshold = td.dirPrice[i].threshold;
350 std::stringstream query;
351 query << "UPDATE tb_tariffs_params SET \
352 price_day_a = " << pda << ", \
353 price_day_b = " << pdb << ", \
354 price_night_a = " << pna << ", \
355 price_night_b = " << pnb << ", \
356 threshold = " << threshold << ", \
357 time_day_begins = CAST('" << td.dirPrice[i].hDay
359 << td.dirPrice[i].mDay << "' AS TIME), \
360 time_day_ends = CAST('" << td.dirPrice[i].hNight
362 << td.dirPrice[i].mNight << "' AS TIME) \
363 WHERE fk_tariff = " << id << " AND dir_num = " << i;
365 result = PQexec(connection, query.str().c_str());
367 if (PQresultStatus(result) != PGRES_COMMAND_OK)
369 strError = PQresultErrorMessage(result);
371 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
372 if (RollbackTransaction())
374 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
382 if (CommitTransaction())
384 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to commit transaction'\n");
390 //-----------------------------------------------------------------------------
391 int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
392 const string & tariffName) const
394 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
396 if (PQstatus(connection) != CONNECTION_OK)
398 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
401 strError = "Connection lost";
402 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
409 if (StartTransaction())
411 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to start transaction'\n");
415 std::string ename = tariffName;
417 if (EscapeString(ename))
419 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
420 if (RollbackTransaction())
422 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
427 td->tariffConf.name = tariffName;
429 std::stringstream query;
430 query << "SELECT pk_tariff, \
435 FROM tb_tariffs WHERE name = '" << ename << "'";
437 result = PQexec(connection, query.str().c_str());
439 if (PQresultStatus(result) != PGRES_TUPLES_OK)
441 strError = PQresultErrorMessage(result);
443 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
444 if (RollbackTransaction())
446 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
451 int tuples = PQntuples(result);
455 strError = "Failed to fetch tariff data";
456 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
458 if (RollbackTransaction())
460 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
465 std::stringstream tuple;
466 tuple << PQgetvalue(result, 0, 0) << " ";
467 tuple << PQgetvalue(result, 0, 1) << " ";
468 tuple << PQgetvalue(result, 0, 2) << " ";
469 tuple << PQgetvalue(result, 0, 3) << " ";
470 tuple << PQgetvalue(result, 0, 4) << " ";
474 tuple >> td->tariffConf.fee;
475 tuple >> td->tariffConf.free;
476 tuple >> td->tariffConf.passiveCost;
477 tuple >> td->tariffConf.traffType;
482 query << "SELECT dir_num, \
488 EXTRACT(hour FROM time_day_begins), \
489 EXTRACT(minute FROM time_day_begins), \
490 EXTRACT(hour FROM time_day_ends), \
491 EXTRACT(minute FROM time_day_ends) \
492 FROM tb_tariffs_params \
493 WHERE fk_tariff = " << id;
495 result = PQexec(connection, query.str().c_str());
497 if (PQresultStatus(result) != PGRES_TUPLES_OK)
499 strError = PQresultErrorMessage(result);
501 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
502 if (RollbackTransaction())
504 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
509 tuples = PQntuples(result);
511 if (tuples != DIR_NUM)
513 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
516 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
518 std::stringstream tuple;
519 tuple << PQgetvalue(result, i, 0) << " ";
520 tuple << PQgetvalue(result, i, 1) << " ";
521 tuple << PQgetvalue(result, i, 2) << " ";
522 tuple << PQgetvalue(result, i, 3) << " ";
523 tuple << PQgetvalue(result, i, 4) << " ";
524 tuple << PQgetvalue(result, i, 5) << " ";
525 tuple << PQgetvalue(result, i, 6) << " ";
526 tuple << PQgetvalue(result, i, 7) << " ";
527 tuple << PQgetvalue(result, i, 8) << " ";
528 tuple << PQgetvalue(result, i, 9) << " ";
533 tuple >> td->dirPrice[dir].priceDayA;
534 td->dirPrice[dir].priceDayA /= 1024 * 1024;
535 tuple >> td->dirPrice[dir].priceDayB;
536 td->dirPrice[dir].priceDayB /= 1024 * 1024;
537 tuple >> td->dirPrice[dir].priceNightA;
538 td->dirPrice[dir].priceNightA /= 1024 * 1024;
539 tuple >> td->dirPrice[dir].priceNightB;
540 td->dirPrice[dir].priceNightB /= 1024 * 1024;
541 tuple >> td->dirPrice[dir].threshold;
542 tuple >> td->dirPrice[dir].hDay;
543 tuple >> td->dirPrice[dir].mDay;
544 tuple >> td->dirPrice[dir].hNight;
545 tuple >> td->dirPrice[dir].mNight;
547 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) > 1.0e-3 &&
548 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) > 1.0e-3)
550 td->dirPrice[dir].singlePrice = true;
554 td->dirPrice[dir].singlePrice = false;
556 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
558 td->dirPrice[dir].noDiscount = true;
563 td->dirPrice[dir].noDiscount = false;
570 if (CommitTransaction())
572 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
578 //-----------------------------------------------------------------------------