]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp
Ticket 37. '../stglibs/common.lib/strptime.cpp' added in the
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store_tariffs.cpp
index fb8caf9dee0b97b9d9a8a76724bd3f4595ae0301..fd0b38bca4f9a6056a52d9d3d9ca178290435c86 100644 (file)
 
 #include <libpq-fe.h>
 
+#include "stg/common.h"
 #include "postgresql_store.h"
 #include "stg/locker.h"
 
+namespace
+{
+
+const int pt_mega = 1024 * 1024;
+
+}
+
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -95,7 +103,7 @@ return 0;
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::AddTariff(const std::string & name) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -122,9 +130,9 @@ 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;
     }
 
@@ -158,7 +166,7 @@ return 0;
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::DelTariff(const std::string & name) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -185,9 +193,9 @@ 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;
     }
 
@@ -222,7 +230,7 @@ return 0;
 int POSTGRESQL_STORE::SaveTariff(const TARIFF_DATA & td,
                                  const std::string & tariffName) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -249,16 +257,12 @@ 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::ostringstream query;
     query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
@@ -286,12 +290,14 @@ 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;
     }
 
+int32_t id;
+
     {
     std::stringstream tuple;
     tuple << PQgetvalue(result, 0, 0);
@@ -307,8 +313,16 @@ if (tuples != 1)
                   fee = " << td.tariffConf.fee << ", \
                   free = " << td.tariffConf.free << ", \
                   passive_cost = " << td.tariffConf.passiveCost << ", \
-                  traff_type = " << td.tariffConf.traffType << " \
-              WHERE pk_tariff = " << id;
+                  traff_type = " << td.tariffConf.traffType;
+
+    if (version > 6)
+        query << ", period = '" << TARIFF::PeriodToString(td.tariffConf.period) << "'";
+
+    if (version > 7)
+        query << ", change_policy = '" << TARIFF::ChangePolicyToString(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());
     }
@@ -327,11 +341,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)
         {
@@ -344,6 +359,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;
@@ -399,7 +415,7 @@ return 0;
 int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
                                   const std::string & tariffName) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -426,9 +442,9 @@ 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;
     }
 
@@ -437,10 +453,18 @@ td->tariffConf.name = tariffName;
 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());
 
@@ -464,9 +488,9 @@ 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;
     }
 
@@ -487,21 +511,30 @@ int id;
     tuple >> td->tariffConf.traffType;
     }
 
+if (version > 6)
+    td->tariffConf.period = TARIFF::StringToPeriod(PQgetvalue(result, 0, 5));
+
+if (version > 7)
+    {
+    td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(PQgetvalue(result, 0, 6));
+    td->tariffConf.changePolicyTimeout = readTime(PQgetvalue(result, 0, 7));
+    }
+
 PQclear(result);
 
 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());
 
@@ -511,9 +544,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;
     }
 
@@ -557,8 +590,8 @@ for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
         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)
+    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;
         }