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(std::vector<std::string> * tariffsList) const
42 STG_LOCKER lock(&mutex);
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 std::string & name) const
98 STG_LOCKER lock(&mutex);
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::ostringstream 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 std::string & name) const
161 STG_LOCKER lock(&mutex);
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::ostringstream 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 std::string & tariffName) const
225 STG_LOCKER lock(&mutex);
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 std::ostringstream query;
260 query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
262 result = PQexec(connection, query.str().c_str());
265 if (PQresultStatus(result) != PGRES_TUPLES_OK)
267 strError = PQresultErrorMessage(result);
269 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
270 if (RollbackTransaction())
272 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
277 int tuples = PQntuples(result);
281 strError = "Failed to fetch tariff ID";
282 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
284 if (RollbackTransaction())
286 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
294 std::stringstream tuple;
295 tuple << PQgetvalue(result, 0, 0);
303 std::ostringstream query;
304 query << "UPDATE tb_tariffs SET \
305 fee = " << td.tariffConf.fee << ", \
306 free = " << td.tariffConf.free << ", \
307 passive_cost = " << td.tariffConf.passiveCost << ", \
308 traff_type = " << td.tariffConf.traffType;
311 query << ", period = '" << TARIFF::PeriodToString(td.tariffConf.period) << "'";
313 query << " WHERE pk_tariff = " << id;
315 result = PQexec(connection, query.str().c_str());
318 if (PQresultStatus(result) != PGRES_COMMAND_OK)
320 strError = PQresultErrorMessage(result);
322 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
323 if (RollbackTransaction())
325 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
332 for(int i = 0; i < DIR_NUM; i++)
334 double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
335 double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
339 if (td.dirPrice[i].singlePrice)
346 pna = td.dirPrice[i].priceNightA * 1024 * 1024;
347 pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
351 if (td.dirPrice[i].noDiscount)
353 threshold = 0xffFFffFF;
357 threshold = td.dirPrice[i].threshold;
361 std::ostringstream query;
362 query << "UPDATE tb_tariffs_params SET \
363 price_day_a = " << pda << ", \
364 price_day_b = " << pdb << ", \
365 price_night_a = " << pna << ", \
366 price_night_b = " << pnb << ", \
367 threshold = " << threshold << ", \
368 time_day_begins = CAST('" << td.dirPrice[i].hDay
370 << td.dirPrice[i].mDay << "' AS TIME), \
371 time_day_ends = CAST('" << td.dirPrice[i].hNight
373 << td.dirPrice[i].mNight << "' AS TIME) \
374 WHERE fk_tariff = " << id << " AND dir_num = " << i;
376 result = PQexec(connection, query.str().c_str());
379 if (PQresultStatus(result) != PGRES_COMMAND_OK)
381 strError = PQresultErrorMessage(result);
383 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
384 if (RollbackTransaction())
386 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
394 if (CommitTransaction())
396 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to commit transaction'\n");
402 //-----------------------------------------------------------------------------
403 int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
404 const std::string & tariffName) const
406 STG_LOCKER lock(&mutex);
408 if (PQstatus(connection) != CONNECTION_OK)
410 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
413 strError = "Connection lost";
414 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
421 if (StartTransaction())
423 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to start transaction'\n");
427 std::string ename = tariffName;
429 if (EscapeString(ename))
431 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
432 if (RollbackTransaction())
434 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
439 td->tariffConf.name = tariffName;
441 std::ostringstream query;
442 query << "SELECT pk_tariff, \
451 query << " FROM tb_tariffs WHERE name = '" << ename << "'";
453 result = PQexec(connection, query.str().c_str());
455 if (PQresultStatus(result) != PGRES_TUPLES_OK)
457 strError = PQresultErrorMessage(result);
459 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
460 if (RollbackTransaction())
462 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
467 int tuples = PQntuples(result);
471 strError = "Failed to fetch tariff data";
472 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
474 if (RollbackTransaction())
476 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
484 std::stringstream tuple;
485 tuple << PQgetvalue(result, 0, 0) << " ";
486 tuple << PQgetvalue(result, 0, 1) << " ";
487 tuple << PQgetvalue(result, 0, 2) << " ";
488 tuple << PQgetvalue(result, 0, 3) << " ";
489 tuple << PQgetvalue(result, 0, 4) << " ";
492 tuple >> td->tariffConf.fee;
493 tuple >> td->tariffConf.free;
494 tuple >> td->tariffConf.passiveCost;
495 tuple >> td->tariffConf.traffType;
499 td->tariffConf.period = TARIFF::StringToPeriod(PQgetvalue(result, 0, 5));
504 query << "SELECT dir_num, \
510 EXTRACT(hour FROM time_day_begins), \
511 EXTRACT(minute FROM time_day_begins), \
512 EXTRACT(hour FROM time_day_ends), \
513 EXTRACT(minute FROM time_day_ends) \
514 FROM tb_tariffs_params \
515 WHERE fk_tariff = " << id;
517 result = PQexec(connection, query.str().c_str());
519 if (PQresultStatus(result) != PGRES_TUPLES_OK)
521 strError = PQresultErrorMessage(result);
523 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
524 if (RollbackTransaction())
526 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
531 tuples = PQntuples(result);
533 if (tuples != DIR_NUM)
535 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
538 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
543 std::stringstream tuple;
544 tuple << PQgetvalue(result, i, 0) << " ";
545 tuple << PQgetvalue(result, i, 1) << " ";
546 tuple << PQgetvalue(result, i, 2) << " ";
547 tuple << PQgetvalue(result, i, 3) << " ";
548 tuple << PQgetvalue(result, i, 4) << " ";
549 tuple << PQgetvalue(result, i, 5) << " ";
550 tuple << PQgetvalue(result, i, 6) << " ";
551 tuple << PQgetvalue(result, i, 7) << " ";
552 tuple << PQgetvalue(result, i, 8) << " ";
553 tuple << PQgetvalue(result, i, 9) << " ";
556 tuple >> td->dirPrice[dir].priceDayA;
557 td->dirPrice[dir].priceDayA /= 1024 * 1024;
558 tuple >> td->dirPrice[dir].priceDayB;
559 td->dirPrice[dir].priceDayB /= 1024 * 1024;
560 tuple >> td->dirPrice[dir].priceNightA;
561 td->dirPrice[dir].priceNightA /= 1024 * 1024;
562 tuple >> td->dirPrice[dir].priceNightB;
563 td->dirPrice[dir].priceNightB /= 1024 * 1024;
564 tuple >> td->dirPrice[dir].threshold;
565 tuple >> td->dirPrice[dir].hDay;
566 tuple >> td->dirPrice[dir].mDay;
567 tuple >> td->dirPrice[dir].hNight;
568 tuple >> td->dirPrice[dir].mNight;
571 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) > 1.0e-3 &&
572 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) > 1.0e-3)
574 td->dirPrice[dir].singlePrice = true;
578 td->dirPrice[dir].singlePrice = false;
580 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
582 td->dirPrice[dir].noDiscount = true;
587 td->dirPrice[dir].noDiscount = false;
594 if (CommitTransaction())
596 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
602 //-----------------------------------------------------------------------------