]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store.cpp
index d9586b89f774052cedee341671d1dcb3aef414e1..e57382867d6169e00106ac7e3cc1e25315f6a683 100644 (file)
  *
  */
 
+#include "postgresql_store.h"
+
+#include "stg/common.h" // str2x, printfd
+
 #include <string>
 #include <vector>
-#include <algorithm>
 
 #include <libpq-fe.h>
 
-#include "postgresql_store.h"
-#include "postgresql_store_utils.h"
-#include "base_settings.h"
-
-class POSTGRESQL_STORE_CREATOR
-{
-public:
-    POSTGRESQL_STORE_CREATOR()
-        : pqStore(new POSTGRESQL_STORE())
-        {
-        };
-    ~POSTGRESQL_STORE_CREATOR()
-        {
-        delete pqStore;
-        };
-    POSTGRESQL_STORE * GetStore() { return pqStore; };
-private:
-    POSTGRESQL_STORE * pqStore;
-} pqStoreeCreator;
-
-//-----------------------------------------------------------------------------
-BASE_STORE * GetStore()
+extern "C" STG::Store* GetStore()
 {
-return pqStoreeCreator.GetStore();
+    static POSTGRESQL_STORE plugin;
+    return &plugin;
 }
 
 //-----------------------------------------------------------------------------
@@ -77,10 +60,12 @@ POSTGRESQL_STORE::POSTGRESQL_STORE()
       database("stargazer"),
       user("stg"),
       password("123456"),
+      clientEncoding("KOI8"),
       version(0),
-      connection(NULL)
+      retries(3),
+      connection(NULL),
+      logger(STG::PluginLogger::get("store_postgresql"))
 {
-pthread_mutex_init(&mutex, NULL);
 }
 //-----------------------------------------------------------------------------
 POSTGRESQL_STORE::~POSTGRESQL_STORE()
@@ -89,33 +74,41 @@ if (connection)
     {
     PQfinish(connection);
     }
-pthread_mutex_destroy(&mutex);
 }
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::ParseSettings()
 {
-std::vector<PARAM_VALUE>::iterator i;
-string s;
+std::vector<STG::ParamValue>::iterator i;
 
 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
     {
-    s = i->param;
-    std::transform(s.begin(), s.end(), s.begin(), ToLower());
+    if (i->value.empty())
+        continue;
+    std::string s = ToLower(i->param);
     if (s == "server")
         {
-        server = *(i->value.begin());
+        server = i->value.front();
         }
     if (s == "database")
         {
-        database = *(i->value.begin());
+        database = i->value.front();
         }
     if (s == "user")
         {
-        user = *(i->value.begin());
+        user = i->value.front();
         }
     if (s == "password")
         {
-        password = *(i->value.begin());
+        password = i->value.front();
+        }
+    if (s == "retries")
+        {
+        if (str2x(i->value.front(), retries))
+            {
+            strError = "Invalid 'retries' value";
+            printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
+            return -1;
+            }
         }
     }
 
@@ -138,7 +131,8 @@ if (PQstatus(connection) != CONNECTION_OK)
     {
     strError = PQerrorMessage(connection);
     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
-    return 1;
+    // Will try to connect later
+    return 0;
     }
 
 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
@@ -153,7 +147,12 @@ return CheckVersion();
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::Reset() const
 {
-PQreset(connection);
+for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
+    {
+    struct timespec ts = {1, 0};
+    nanosleep(&ts, NULL);
+    PQreset(connection);
+    }
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -166,7 +165,7 @@ if (PQsetClientEncoding(connection, clientEncoding.c_str()))
     {
     strError = PQerrorMessage(connection);
     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
-    return 1;
+    return -1;
     }
 
 return CheckVersion();
@@ -224,6 +223,8 @@ if (CommitTransaction())
     return -1;
     }
 
+logger("POSTGRESQL_STORE: Current DB schema version: %d", version);
+
 return 0;
 }
 //-----------------------------------------------------------------------------