]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store_utils.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store_utils.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  Vairous utility methods
23  *
24  *  $Revision: 1.3 $
25  *  $Date: 2009/10/22 10:01:08 $
26  *
27  */
28
29 #include "postgresql_store.h"
30
31 #include "stg/common.h"
32
33 #include <string>
34 #include <ctime>
35
36 #include <libpq-fe.h>
37
38 extern volatile time_t stgTime;
39
40 int POSTGRESQL_STORE::StartTransaction() const
41 {
42 PGresult * result = PQexec(connection, "BEGIN");
43
44 if (PQresultStatus(result) != PGRES_COMMAND_OK)
45     {
46     strError = PQresultErrorMessage(result);
47     PQclear(result);
48     printfd(__FILE__, "POSTGRESQL_STORE::StartTransaction(): '%s'\n", strError.c_str());
49     return -1;
50     }
51
52 PQclear(result);
53
54 return 0;
55 }
56
57 int POSTGRESQL_STORE::CommitTransaction() const
58 {
59 PGresult * result = PQexec(connection, "COMMIT");
60
61 if (PQresultStatus(result) != PGRES_COMMAND_OK)
62     {
63     strError = PQresultErrorMessage(result);
64     PQclear(result);
65     printfd(__FILE__, "POSTGRESQL_STORE::CommitTransaction(): '%s'\n", strError.c_str());
66     return -1;
67     }
68
69 PQclear(result);
70
71 return 0;
72 }
73
74 int POSTGRESQL_STORE::RollbackTransaction() const
75 {
76 PGresult * result = PQexec(connection, "ROLLBACK");
77
78 if (PQresultStatus(result) != PGRES_COMMAND_OK)
79     {
80     strError = PQresultErrorMessage(result);
81     PQclear(result);
82     printfd(__FILE__, "POSTGRESQL_STORE::RollbackTransaction(): '%s'\n", strError.c_str());
83     return -1;
84     }
85
86 PQclear(result);
87
88 return 0;
89 }
90
91 int POSTGRESQL_STORE::EscapeString(std::string & value) const
92 {
93 int error = 0;
94 char * buf = new char[(value.length() << 1) + 1];
95
96 PQescapeStringConn(connection,
97                    buf,
98                    value.c_str(),
99                    value.length(),
100                    &error);
101
102 if (error)
103     {
104     strError = PQerrorMessage(connection);
105     printfd(__FILE__, "POSTGRESQL_STORE::EscapeString(): '%s'\n", strError.c_str());
106     delete[] buf;
107     return -1;
108     }
109
110 value = buf;
111
112 delete[] buf;
113 return 0;
114 }
115
116 void POSTGRESQL_STORE::MakeDate(std::string & date, int year, int month) const
117 {
118 struct tm brokenTime;
119
120 brokenTime.tm_wday = 0;
121 brokenTime.tm_yday = 0;
122 brokenTime.tm_isdst = 0;
123
124 if (year)
125     {
126     brokenTime.tm_hour = 0;
127     brokenTime.tm_min = 0;
128     brokenTime.tm_sec = 0;
129     brokenTime.tm_year = year;
130     brokenTime.tm_mon = month;
131     }
132 else
133     {
134     time_t curTime = stgTime;
135     localtime_r(&curTime, &brokenTime);
136     }
137
138 brokenTime.tm_mday = DaysInMonth(brokenTime.tm_year + 1900, brokenTime.tm_mon);
139
140 char buf[32];
141
142 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
143
144 date = buf;
145 }