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 $
29 #include "postgresql_store.h"
31 #include "stg/tariff_conf.h"
32 #include "stg/common.h"
33 #include "stg/locker.h"
45 const int pt_mega = 1024 * 1024;
49 //-----------------------------------------------------------------------------
50 int POSTGRESQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
52 STG_LOCKER lock(&mutex);
54 if (PQstatus(connection) != CONNECTION_OK)
56 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
59 strError = "Connection lost";
60 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
67 if (StartTransaction())
69 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to start transaction'\n");
73 result = PQexec(connection, "SELECT name FROM tb_tariffs");
75 if (PQresultStatus(result) != PGRES_TUPLES_OK)
77 strError = PQresultErrorMessage(result);
79 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
80 if (RollbackTransaction())
82 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to rollback transaction'\n");
87 int tuples = PQntuples(result);
89 for (int i = 0; i < tuples; ++i)
91 tariffsList->push_back(PQgetvalue(result, i, 0));
96 if (CommitTransaction())
98 printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to commit transaction'\n");
105 //-----------------------------------------------------------------------------
106 int POSTGRESQL_STORE::AddTariff(const std::string & name) const
108 STG_LOCKER lock(&mutex);
110 if (PQstatus(connection) != CONNECTION_OK)
112 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
115 strError = "Connection lost";
116 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
123 if (StartTransaction())
125 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to start transaction'\n");
129 std::string ename = name;
131 if (EscapeString(ename))
133 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
134 if (RollbackTransaction())
136 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
141 std::ostringstream query;
142 query << "SELECT sp_add_tariff('" << ename << "', " << DIR_NUM << ")";
144 result = PQexec(connection, query.str().c_str());
146 if (PQresultStatus(result) != PGRES_TUPLES_OK)
148 strError = PQresultErrorMessage(result);
150 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
151 if (RollbackTransaction())
153 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
160 if (CommitTransaction())
162 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to commit transaction'\n");
168 //-----------------------------------------------------------------------------
169 int POSTGRESQL_STORE::DelTariff(const std::string & name) const
171 STG_LOCKER lock(&mutex);
173 if (PQstatus(connection) != CONNECTION_OK)
175 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
178 strError = "Connection lost";
179 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
186 if (StartTransaction())
188 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to start transaction'\n");
192 std::string ename = name;
194 if (EscapeString(ename))
196 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
197 if (RollbackTransaction())
199 printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
204 std::ostringstream query;
205 query << "DELETE FROM tb_tariffs WHERE name = '" << ename << "'";
207 result = PQexec(connection, query.str().c_str());
209 if (PQresultStatus(result) != PGRES_COMMAND_OK)
211 strError = PQresultErrorMessage(result);
213 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
214 if (RollbackTransaction())
216 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to rollback transaction'\n");
223 if (CommitTransaction())
225 printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to commit transaction'\n");
231 //-----------------------------------------------------------------------------
232 int POSTGRESQL_STORE::SaveTariff(const STG::TariffData & td,
233 const std::string & tariffName) const
235 STG_LOCKER lock(&mutex);
237 if (PQstatus(connection) != CONNECTION_OK)
239 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
242 strError = "Connection lost";
243 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
250 if (StartTransaction())
252 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to start transaction'\n");
256 std::string ename = tariffName;
258 if (EscapeString(ename))
260 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to escape name'\n");
261 if (RollbackTransaction())
263 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
269 std::ostringstream query;
270 query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
272 result = PQexec(connection, query.str().c_str());
275 if (PQresultStatus(result) != PGRES_TUPLES_OK)
277 strError = PQresultErrorMessage(result);
279 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
280 if (RollbackTransaction())
282 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
287 int tuples = PQntuples(result);
291 strError = "Failed to fetch tariff ID";
292 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
294 if (RollbackTransaction())
296 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
304 std::stringstream tuple;
305 tuple << PQgetvalue(result, 0, 0);
313 std::ostringstream query;
314 query << "UPDATE tb_tariffs SET \
315 fee = " << td.tariffConf.fee << ", \
316 free = " << td.tariffConf.free << ", \
317 passive_cost = " << td.tariffConf.passiveCost << ", \
318 traff_type = " << td.tariffConf.traffType;
321 query << ", period = '" << STG::Tariff::toString(td.tariffConf.period) << "'";
324 query << ", change_policy = '" << STG::Tariff::toString(td.tariffConf.changePolicy) << "', \
325 change_policy_timeout = CAST('" << formatTime(td.tariffConf.changePolicyTimeout) << "' AS TIMESTAMP)";
327 query << " WHERE pk_tariff = " << id;
329 result = PQexec(connection, query.str().c_str());
332 if (PQresultStatus(result) != PGRES_COMMAND_OK)
334 strError = PQresultErrorMessage(result);
336 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
337 if (RollbackTransaction())
339 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
346 for(int i = 0; i < DIR_NUM; i++)
348 double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
349 double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
353 if (td.dirPrice[i].singlePrice)
360 pna = td.dirPrice[i].priceNightA * 1024 * 1024;
361 pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
365 if (td.dirPrice[i].noDiscount)
367 threshold = 0xffFFffFF;
371 threshold = td.dirPrice[i].threshold;
375 std::ostringstream query;
376 query << "UPDATE tb_tariffs_params SET \
377 price_day_a = " << pda << ", \
378 price_day_b = " << pdb << ", \
379 price_night_a = " << pna << ", \
380 price_night_b = " << pnb << ", \
381 threshold = " << threshold << ", \
382 time_day_begins = CAST('" << td.dirPrice[i].hDay
384 << td.dirPrice[i].mDay << "' AS TIME), \
385 time_day_ends = CAST('" << td.dirPrice[i].hNight
387 << td.dirPrice[i].mNight << "' AS TIME) \
388 WHERE fk_tariff = " << id << " AND dir_num = " << i;
390 result = PQexec(connection, query.str().c_str());
393 if (PQresultStatus(result) != PGRES_COMMAND_OK)
395 strError = PQresultErrorMessage(result);
397 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
398 if (RollbackTransaction())
400 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
408 if (CommitTransaction())
410 printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to commit transaction'\n");
416 //-----------------------------------------------------------------------------
417 int POSTGRESQL_STORE::RestoreTariff(STG::TariffData * td,
418 const std::string & tariffName) const
420 STG_LOCKER lock(&mutex);
422 if (PQstatus(connection) != CONNECTION_OK)
424 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
427 strError = "Connection lost";
428 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
435 if (StartTransaction())
437 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to start transaction'\n");
441 std::string ename = tariffName;
443 if (EscapeString(ename))
445 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
446 if (RollbackTransaction())
448 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
453 td->tariffConf.name = tariffName;
455 std::ostringstream query;
456 query << "SELECT pk_tariff, \
466 query << ", change_policy \
467 , change_policy_timeout";
469 query << " FROM tb_tariffs WHERE name = '" << ename << "'";
471 result = PQexec(connection, query.str().c_str());
473 if (PQresultStatus(result) != PGRES_TUPLES_OK)
475 strError = PQresultErrorMessage(result);
477 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
478 if (RollbackTransaction())
480 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
485 int tuples = PQntuples(result);
489 strError = "Failed to fetch tariff data";
490 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
492 if (RollbackTransaction())
494 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
502 std::stringstream tuple;
503 tuple << PQgetvalue(result, 0, 0) << " ";
504 tuple << PQgetvalue(result, 0, 1) << " ";
505 tuple << PQgetvalue(result, 0, 2) << " ";
506 tuple << PQgetvalue(result, 0, 3) << " ";
507 tuple << PQgetvalue(result, 0, 4) << " ";
510 tuple >> td->tariffConf.fee;
511 tuple >> td->tariffConf.free;
512 tuple >> td->tariffConf.passiveCost;
515 td->tariffConf.traffType = static_cast<STG::Tariff::TraffType>(traffType);
519 td->tariffConf.period = STG::Tariff::parsePeriod(PQgetvalue(result, 0, 5));
523 td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(PQgetvalue(result, 0, 6));
524 td->tariffConf.changePolicyTimeout = readTime(PQgetvalue(result, 0, 7));
530 query << "SELECT dir_num, \
536 EXTRACT(hour FROM time_day_begins), \
537 EXTRACT(minute FROM time_day_begins), \
538 EXTRACT(hour FROM time_day_ends), \
539 EXTRACT(minute FROM time_day_ends) \
540 FROM tb_tariffs_params \
541 WHERE fk_tariff = " << id;
543 result = PQexec(connection, query.str().c_str());
545 if (PQresultStatus(result) != PGRES_TUPLES_OK)
547 strError = PQresultErrorMessage(result);
549 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
550 if (RollbackTransaction())
552 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
557 tuples = PQntuples(result);
559 if (tuples != DIR_NUM)
561 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
564 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
569 std::stringstream tuple;
570 tuple << PQgetvalue(result, i, 0) << " ";
571 tuple << PQgetvalue(result, i, 1) << " ";
572 tuple << PQgetvalue(result, i, 2) << " ";
573 tuple << PQgetvalue(result, i, 3) << " ";
574 tuple << PQgetvalue(result, i, 4) << " ";
575 tuple << PQgetvalue(result, i, 5) << " ";
576 tuple << PQgetvalue(result, i, 6) << " ";
577 tuple << PQgetvalue(result, i, 7) << " ";
578 tuple << PQgetvalue(result, i, 8) << " ";
579 tuple << PQgetvalue(result, i, 9) << " ";
582 tuple >> td->dirPrice[dir].priceDayA;
583 td->dirPrice[dir].priceDayA /= 1024 * 1024;
584 tuple >> td->dirPrice[dir].priceDayB;
585 td->dirPrice[dir].priceDayB /= 1024 * 1024;
586 tuple >> td->dirPrice[dir].priceNightA;
587 td->dirPrice[dir].priceNightA /= 1024 * 1024;
588 tuple >> td->dirPrice[dir].priceNightB;
589 td->dirPrice[dir].priceNightB /= 1024 * 1024;
590 tuple >> td->dirPrice[dir].threshold;
591 tuple >> td->dirPrice[dir].hDay;
592 tuple >> td->dirPrice[dir].mDay;
593 tuple >> td->dirPrice[dir].hNight;
594 tuple >> td->dirPrice[dir].mNight;
597 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
598 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
600 td->dirPrice[dir].singlePrice = true;
604 td->dirPrice[dir].singlePrice = false;
606 if (td->dirPrice[dir].threshold == static_cast<int>(0xffFFffFF))
608 td->dirPrice[dir].noDiscount = true;
613 td->dirPrice[dir].noDiscount = false;
620 if (CommitTransaction())
622 printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
628 //-----------------------------------------------------------------------------