]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store_tariffs.cpp
index 7d63b57420cd2f4973f02a29a2413fde10094896..085675c3dc0bb41f80a7b3bc56011aad60980a40 100644 (file)
  *
  */
 
+#include "postgresql_store.h"
+
+#include "stg/tariff_conf.h"
+#include "stg/common.h"
+
 #include <string>
 #include <vector>
 #include <sstream>
 
 #include <libpq-fe.h>
 
-#include "postgresql_store.h"
-#include "stg/locker.h"
+namespace
+{
+
+const int pt_mega = 1024 * 1024;
+
+}
 
 //-----------------------------------------------------------------------------
-int POSTGRESQL_STORE::GetTariffsList(vector<string> * tariffsList) const
+int POSTGRESQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+std::lock_guard lock(m_mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -93,9 +102,9 @@ return 0;
 }
 
 //-----------------------------------------------------------------------------
-int POSTGRESQL_STORE::AddTariff(const string & name) const
+int POSTGRESQL_STORE::AddTariff(const std::string & name) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+std::lock_guard lock(m_mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -122,13 +131,13 @@ if (EscapeString(ename))
     {
     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
-std::stringstream query;
+std::ostringstream query;
 query << "SELECT sp_add_tariff('" << ename << "', " << DIR_NUM << ")";
 
 result = PQexec(connection, query.str().c_str());
@@ -156,9 +165,9 @@ if (CommitTransaction())
 return 0;
 }
 //-----------------------------------------------------------------------------
-int POSTGRESQL_STORE::DelTariff(const string & name) const
+int POSTGRESQL_STORE::DelTariff(const std::string & name) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+std::lock_guard lock(m_mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -185,13 +194,13 @@ if (EscapeString(ename))
     {
     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
-std::stringstream query;
+std::ostringstream query;
 query << "DELETE FROM tb_tariffs WHERE name = '" << ename << "'";
 
 result = PQexec(connection, query.str().c_str());
@@ -219,10 +228,10 @@ if (CommitTransaction())
 return 0;
 }
 //-----------------------------------------------------------------------------
-int POSTGRESQL_STORE::SaveTariff(const TARIFF_DATA & td,
-                                 const string & tariffName) const
+int POSTGRESQL_STORE::SaveTariff(const STG::TariffData & td,
+                                 const std::string & tariffName) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+std::lock_guard lock(m_mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -249,20 +258,18 @@ if (EscapeString(ename))
     {
     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to escape name'\n");
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
-int32_t id, i;
-double pda, pdb, pna, pnb;
-int threshold;
-
-std::stringstream query;
-query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
+    {
+    std::ostringstream query;
+    query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
 
-result = PQexec(connection, query.str().c_str());
+    result = PQexec(connection, query.str().c_str());
+    }
 
 if (PQresultStatus(result) != PGRES_TUPLES_OK)
     {
@@ -284,28 +291,42 @@ if (tuples != 1)
     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
     PQclear(result);
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
-std::stringstream tuple;
-tuple << PQgetvalue(result, 0, 0);
+int32_t id;
 
-PQclear(result);
+    {
+    std::stringstream tuple;
+    tuple << PQgetvalue(result, 0, 0);
 
-tuple >> id;
+    PQclear(result);
 
-query.str("");
-query << "UPDATE tb_tariffs SET \
-              fee = " << td.tariffConf.fee << ", \
-              free = " << td.tariffConf.free << ", \
-              passive_cost = " << td.tariffConf.passiveCost << ", \
-              traff_type = " << td.tariffConf.traffType << " \
-          WHERE pk_tariff = " << id;
+    tuple >> id;
+    }
 
-result = PQexec(connection, query.str().c_str());
+    {
+    std::ostringstream query;
+    query << "UPDATE tb_tariffs SET \
+                  fee = " << td.tariffConf.fee << ", \
+                  free = " << td.tariffConf.free << ", \
+                  passive_cost = " << td.tariffConf.passiveCost << ", \
+                  traff_type = " << td.tariffConf.traffType;
+
+    if (version > 6)
+        query << ", period = '" << STG::Tariff::toString(td.tariffConf.period) << "'";
+
+    if (version > 7)
+        query << ", change_policy = '" << STG::Tariff::toString(td.tariffConf.changePolicy) << "', \
+                  change_policy_timeout = CAST('" << formatTime(td.tariffConf.changePolicyTimeout) << "' AS TIMESTAMP)";
+
+    query << " WHERE pk_tariff = " << id;
+
+    result = PQexec(connection, query.str().c_str());
+    }
 
 if (PQresultStatus(result) != PGRES_COMMAND_OK)
     {
@@ -321,11 +342,12 @@ if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
 PQclear(result);
 
-for(i = 0; i < DIR_NUM; i++)
+for(int i = 0; i < DIR_NUM; i++)
     {
-
-    pda = td.dirPrice[i].priceDayA * 1024 * 1024;
-    pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
+    double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
+    double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
+    double pna = 0;
+    double pnb = 0;
 
     if (td.dirPrice[i].singlePrice)
         {
@@ -338,6 +360,7 @@ for(i = 0; i < DIR_NUM; i++)
         pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
         }
 
+    int threshold = 0;
     if (td.dirPrice[i].noDiscount)
         {
         threshold = 0xffFFffFF;
@@ -347,22 +370,24 @@ for(i = 0; i < DIR_NUM; i++)
         threshold = td.dirPrice[i].threshold;
         }
 
-    std::stringstream query;
-    query << "UPDATE tb_tariffs_params SET \
-                  price_day_a = " << pda << ", \
-                  price_day_b = " << pdb << ", \
-                  price_night_a = " << pna << ", \
-                  price_night_b = " << pnb << ", \
-                  threshold = " << threshold << ", \
-                  time_day_begins = CAST('" << td.dirPrice[i].hDay
-                                            << ":"
-                                            << td.dirPrice[i].mDay << "' AS TIME), \
-                  time_day_ends = CAST('" << td.dirPrice[i].hNight
-                                          << ":"
-                                          << td.dirPrice[i].mNight << "' AS TIME) \
-             WHERE fk_tariff = " << id << " AND dir_num = " << i;
-
-    result = PQexec(connection, query.str().c_str());
+        {
+        std::ostringstream query;
+        query << "UPDATE tb_tariffs_params SET \
+                      price_day_a = " << pda << ", \
+                      price_day_b = " << pdb << ", \
+                      price_night_a = " << pna << ", \
+                      price_night_b = " << pnb << ", \
+                      threshold = " << threshold << ", \
+                      time_day_begins = CAST('" << td.dirPrice[i].hDay
+                                                << ":"
+                                                << td.dirPrice[i].mDay << "' AS TIME), \
+                      time_day_ends = CAST('" << td.dirPrice[i].hNight
+                                              << ":"
+                                              << td.dirPrice[i].mNight << "' AS TIME) \
+                 WHERE fk_tariff = " << id << " AND dir_num = " << i;
+
+        result = PQexec(connection, query.str().c_str());
+        }
 
     if (PQresultStatus(result) != PGRES_COMMAND_OK)
         {
@@ -388,10 +413,10 @@ if (CommitTransaction())
 return 0;
 }
 //-----------------------------------------------------------------------------
-int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
-                                  const string & tariffName) const
+int POSTGRESQL_STORE::RestoreTariff(STG::TariffData * td,
+                                    const std::string & tariffName) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+std::lock_guard lock(m_mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -418,21 +443,29 @@ if (EscapeString(ename))
     {
     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
 td->tariffConf.name = tariffName;
 
-std::stringstream query;
+std::ostringstream query;
 query << "SELECT pk_tariff, \
                  fee, \
-                free, \
-                passive_cost, \
-                traff_type \
-         FROM tb_tariffs WHERE name = '" << ename << "'";
+                 free, \
+                 passive_cost, \
+                 traff_type";
+
+if (version > 6)
+    query << ", period";
+
+if (version > 7)
+    query << ", change_policy \
+              , change_policy_timeout";
+
+query << " FROM tb_tariffs WHERE name = '" << ename << "'";
 
 result = PQexec(connection, query.str().c_str());
 
@@ -456,25 +489,39 @@ if (tuples != 1)
     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
     PQclear(result);
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
-std::stringstream tuple;
-tuple << PQgetvalue(result, 0, 0) << " ";
-tuple << PQgetvalue(result, 0, 1) << " ";
-tuple << PQgetvalue(result, 0, 2) << " ";
-tuple << PQgetvalue(result, 0, 3) << " ";
-tuple << PQgetvalue(result, 0, 4) << " ";
-
 int id;
-tuple >> id;
-tuple >> td->tariffConf.fee;
-tuple >> td->tariffConf.free;
-tuple >> td->tariffConf.passiveCost;
-tuple >> td->tariffConf.traffType;
+
+    {
+    std::stringstream tuple;
+    tuple << PQgetvalue(result, 0, 0) << " ";
+    tuple << PQgetvalue(result, 0, 1) << " ";
+    tuple << PQgetvalue(result, 0, 2) << " ";
+    tuple << PQgetvalue(result, 0, 3) << " ";
+    tuple << PQgetvalue(result, 0, 4) << " ";
+
+    tuple >> id;
+    tuple >> td->tariffConf.fee;
+    tuple >> td->tariffConf.free;
+    tuple >> td->tariffConf.passiveCost;
+    unsigned traffType;
+    tuple >> traffType;
+    td->tariffConf.traffType = static_cast<STG::Tariff::TraffType>(traffType);
+    }
+
+if (version > 6)
+    td->tariffConf.period = STG::Tariff::parsePeriod(PQgetvalue(result, 0, 5));
+
+if (version > 7)
+    {
+    td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(PQgetvalue(result, 0, 6));
+    td->tariffConf.changePolicyTimeout = readTime(PQgetvalue(result, 0, 7));
+    }
 
 PQclear(result);
 
@@ -482,15 +529,15 @@ query.str("");
 query << "SELECT dir_num, \
                  price_day_a, \
                  price_day_b, \
-                price_night_a, \
-                price_night_b, \
-                threshold, \
-                EXTRACT(hour FROM time_day_begins), \
-                EXTRACT(minute FROM time_day_begins), \
-                EXTRACT(hour FROM time_day_ends), \
-                EXTRACT(minute FROM time_day_ends) \
-         FROM tb_tariffs_params \
-         WHERE fk_tariff = " << id;
+                 price_night_a, \
+                 price_night_b, \
+                 threshold, \
+                 EXTRACT(hour FROM time_day_begins), \
+                 EXTRACT(minute FROM time_day_begins), \
+                 EXTRACT(hour FROM time_day_ends), \
+                 EXTRACT(minute FROM time_day_ends) \
+          FROM tb_tariffs_params \
+          WHERE fk_tariff = " << id;
 
 result = PQexec(connection, query.str().c_str());
 
@@ -500,9 +547,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK)
     PQclear(result);
     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
     if (RollbackTransaction())
-       {
-       printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
-       }
+        {
+        printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
+        }
     return -1;
     }
 
@@ -515,37 +562,39 @@ if (tuples != DIR_NUM)
 
 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
     {
-    std::stringstream tuple;
-    tuple << PQgetvalue(result, i, 0) << " ";
-    tuple << PQgetvalue(result, i, 1) << " ";
-    tuple << PQgetvalue(result, i, 2) << " ";
-    tuple << PQgetvalue(result, i, 3) << " ";
-    tuple << PQgetvalue(result, i, 4) << " ";
-    tuple << PQgetvalue(result, i, 5) << " ";
-    tuple << PQgetvalue(result, i, 6) << " ";
-    tuple << PQgetvalue(result, i, 7) << " ";
-    tuple << PQgetvalue(result, i, 8) << " ";
-    tuple << PQgetvalue(result, i, 9) << " ";
-
     int dir;
 
-    tuple >> dir;
-    tuple >> td->dirPrice[dir].priceDayA;
-    td->dirPrice[dir].priceDayA /= 1024 * 1024;
-    tuple >> td->dirPrice[dir].priceDayB;
-    td->dirPrice[dir].priceDayB /= 1024 * 1024;
-    tuple >> td->dirPrice[dir].priceNightA;
-    td->dirPrice[dir].priceNightA /= 1024 * 1024;
-    tuple >> td->dirPrice[dir].priceNightB;
-    td->dirPrice[dir].priceNightB /= 1024 * 1024;
-    tuple >> td->dirPrice[dir].threshold;
-    tuple >> td->dirPrice[dir].hDay;
-    tuple >> td->dirPrice[dir].mDay;
-    tuple >> td->dirPrice[dir].hNight;
-    tuple >> td->dirPrice[dir].mNight;
-
-    if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) > 1.0e-3 &&
-        std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) > 1.0e-3)
+        {
+        std::stringstream tuple;
+        tuple << PQgetvalue(result, i, 0) << " ";
+        tuple << PQgetvalue(result, i, 1) << " ";
+        tuple << PQgetvalue(result, i, 2) << " ";
+        tuple << PQgetvalue(result, i, 3) << " ";
+        tuple << PQgetvalue(result, i, 4) << " ";
+        tuple << PQgetvalue(result, i, 5) << " ";
+        tuple << PQgetvalue(result, i, 6) << " ";
+        tuple << PQgetvalue(result, i, 7) << " ";
+        tuple << PQgetvalue(result, i, 8) << " ";
+        tuple << PQgetvalue(result, i, 9) << " ";
+
+        tuple >> dir;
+        tuple >> td->dirPrice[dir].priceDayA;
+        td->dirPrice[dir].priceDayA /= 1024 * 1024;
+        tuple >> td->dirPrice[dir].priceDayB;
+        td->dirPrice[dir].priceDayB /= 1024 * 1024;
+        tuple >> td->dirPrice[dir].priceNightA;
+        td->dirPrice[dir].priceNightA /= 1024 * 1024;
+        tuple >> td->dirPrice[dir].priceNightB;
+        td->dirPrice[dir].priceNightB /= 1024 * 1024;
+        tuple >> td->dirPrice[dir].threshold;
+        tuple >> td->dirPrice[dir].hDay;
+        tuple >> td->dirPrice[dir].mDay;
+        tuple >> td->dirPrice[dir].hNight;
+        tuple >> td->dirPrice[dir].mNight;
+        }
+
+    if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
+        std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
         {
         td->dirPrice[dir].singlePrice = true;
         }
@@ -553,7 +602,7 @@ for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
         {
         td->dirPrice[dir].singlePrice = false;
         }
-    if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
+    if (td->dirPrice[dir].threshold == static_cast<int>(0xffFFffFF))
         {
         td->dirPrice[dir].noDiscount = true;
         }