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