]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store_utils.cpp
В утилите экранирования строки модуля store_postgresql исправлено
[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 "common.h"
35
36 #include "postgresql_store.h"
37
38 int POSTGRESQL_STORE::StartTransaction() const
39 {
40 PGresult * result = PQexec(connection, "BEGIN");
41
42 if (PQresultStatus(result) != PGRES_COMMAND_OK)
43     {
44     strError = PQresultErrorMessage(result);
45     PQclear(result);
46     printfd(__FILE__, "POSTGRESQL_STORE::StartTransaction(): '%s'\n", strError.c_str());
47     return -1;
48     }
49
50 PQclear(result);
51
52 return 0;
53 }
54
55 int POSTGRESQL_STORE::CommitTransaction() const
56 {
57 PGresult * result = PQexec(connection, "COMMIT");
58
59 if (PQresultStatus(result) != PGRES_COMMAND_OK)
60     {
61     strError = PQresultErrorMessage(result);
62     PQclear(result);
63     printfd(__FILE__, "POSTGRESQL_STORE::CommitTransaction(): '%s'\n", strError.c_str());
64     return -1;
65     }
66
67 PQclear(result);
68
69 return 0;
70 }
71
72 int POSTGRESQL_STORE::RollbackTransaction() const
73 {
74 PGresult * result = PQexec(connection, "ROLLBACK");
75
76 if (PQresultStatus(result) != PGRES_COMMAND_OK)
77     {
78     strError = PQresultErrorMessage(result);
79     PQclear(result);
80     printfd(__FILE__, "POSTGRESQL_STORE::RollbackTransaction(): '%s'\n", strError.c_str());
81     return -1;
82     }
83
84 PQclear(result);
85
86 return 0;
87 }
88
89 int POSTGRESQL_STORE::EscapeString(std::string & value) const
90 {
91 int error = 0;
92 char * buf = new char[(value.length() << 1) + 1];
93
94 PQescapeStringConn(connection,
95                    buf,
96                    value.c_str(),
97                    value.length(),
98                    &error);
99
100 if (error)
101     {
102     strError = PQerrorMessage(connection);
103     printfd(__FILE__, "POSTGRESQL_STORE::EscapeString(): '%s'\n", strError.c_str());
104     delete[] buf;
105     return -1;
106     }
107
108 value = buf;
109
110 delete[] buf;
111 return 0;
112 }
113
114 std::string POSTGRESQL_STORE::Int2TS(uint32_t ts) const
115 {
116 char buf[32];
117 struct tm brokenTime;
118 time_t tt = ts;
119
120 brokenTime.tm_wday = 0;
121 brokenTime.tm_yday = 0;
122 brokenTime.tm_isdst = 0;
123
124 gmtime_r(&tt, &brokenTime);
125
126 strftime(buf, 32, "%Y-%m-%d %H:%M:%S", &brokenTime);
127
128 return buf;
129 }
130
131 uint32_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