]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store_utils.cpp
Fix postgresql store plugin compilation errors
[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 <string>
30 #include <ctime>
31
32 #include <libpq-fe.h>
33
34 #include "stg/common.h"
35 #include "postgresql_store.h"
36
37 int POSTGRESQL_STORE::StartTransaction() const
38 {
39 PGresult * result = PQexec(connection, "BEGIN");
40
41 if (PQresultStatus(result) != PGRES_COMMAND_OK)
42     {
43     strError = PQresultErrorMessage(result);
44     PQclear(result);
45     printfd(__FILE__, "POSTGRESQL_STORE::StartTransaction(): '%s'\n", strError.c_str());
46     return -1;
47     }
48
49 PQclear(result);
50
51 return 0;
52 }
53
54 int POSTGRESQL_STORE::CommitTransaction() const
55 {
56 PGresult * result = PQexec(connection, "COMMIT");
57
58 if (PQresultStatus(result) != PGRES_COMMAND_OK)
59     {
60     strError = PQresultErrorMessage(result);
61     PQclear(result);
62     printfd(__FILE__, "POSTGRESQL_STORE::CommitTransaction(): '%s'\n", strError.c_str());
63     return -1;
64     }
65
66 PQclear(result);
67
68 return 0;
69 }
70
71 int POSTGRESQL_STORE::RollbackTransaction() const
72 {
73 PGresult * result = PQexec(connection, "ROLLBACK");
74
75 if (PQresultStatus(result) != PGRES_COMMAND_OK)
76     {
77     strError = PQresultErrorMessage(result);
78     PQclear(result);
79     printfd(__FILE__, "POSTGRESQL_STORE::RollbackTransaction(): '%s'\n", strError.c_str());
80     return -1;
81     }
82
83 PQclear(result);
84
85 return 0;
86 }
87
88 int POSTGRESQL_STORE::EscapeString(std::string & value) const
89 {
90 int error = 0;
91 char * buf = new char[(value.length() << 1) + 1];
92
93 PQescapeStringConn(connection,
94                    buf,
95                    value.c_str(),
96                    value.length(),
97                    &error);
98
99 if (error)
100     {
101     strError = PQerrorMessage(connection);
102     printfd(__FILE__, "POSTGRESQL_STORE::EscapeString(): '%s'\n", strError.c_str());
103     delete[] buf;
104     return -1;
105     }
106
107 value = buf;
108
109 delete[] buf;
110 return 0;
111 }
112
113 std::string POSTGRESQL_STORE::Int2TS(uint32_t ts) const
114 {
115 char buf[32];
116 struct tm brokenTime;
117 time_t tt = ts;
118
119 brokenTime.tm_wday = 0;
120 brokenTime.tm_yday = 0;
121 brokenTime.tm_isdst = 0;
122
123 gmtime_r(&tt, &brokenTime);
124
125 strftime(buf, 32, "%Y-%m-%d %H:%M:%S", &brokenTime);
126
127 return buf;
128 }
129
130 uint32_t POSTGRESQL_STORE::TS2Int(const std::string & ts) const
131 {
132 struct tm brokenTime;
133
134 brokenTime.tm_wday = 0;
135 brokenTime.tm_yday = 0;
136 brokenTime.tm_isdst = 0;
137
138 stg_strptime(ts.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime);
139
140 return stg_timegm(&brokenTime);
141 }
142
143 void POSTGRESQL_STORE::MakeDate(std::string & date, int year, int month) const
144 {
145 struct tm brokenTime;
146
147 brokenTime.tm_wday = 0;
148 brokenTime.tm_yday = 0;
149 brokenTime.tm_isdst = 0;
150
151 if (year)
152     {
153     brokenTime.tm_hour = 0;
154     brokenTime.tm_min = 0;
155     brokenTime.tm_sec = 0;
156     brokenTime.tm_year = year;
157     brokenTime.tm_mon = month;
158     }
159 else
160     {
161     time_t curTime = stgTime;
162     /*time(&curTime);*/
163
164     localtime_r(&curTime, &brokenTime);
165     }
166
167 brokenTime.tm_mday = DaysInMonth(brokenTime.tm_year + 1900, brokenTime.tm_mon);
168
169 char buf[32];
170
171 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
172
173 date = buf;
174 }
175