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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 * Vairous utility methods
25 * $Date: 2009/10/22 10:01:08 $
29 #include "postgresql_store.h"
31 #include "stg/common.h"
38 extern volatile time_t stgTime;
40 int POSTGRESQL_STORE::StartTransaction() const
42 PGresult * result = PQexec(connection, "BEGIN");
44 if (PQresultStatus(result) != PGRES_COMMAND_OK)
46 strError = PQresultErrorMessage(result);
48 printfd(__FILE__, "POSTGRESQL_STORE::StartTransaction(): '%s'\n", strError.c_str());
57 int POSTGRESQL_STORE::CommitTransaction() const
59 PGresult * result = PQexec(connection, "COMMIT");
61 if (PQresultStatus(result) != PGRES_COMMAND_OK)
63 strError = PQresultErrorMessage(result);
65 printfd(__FILE__, "POSTGRESQL_STORE::CommitTransaction(): '%s'\n", strError.c_str());
74 int POSTGRESQL_STORE::RollbackTransaction() const
76 PGresult * result = PQexec(connection, "ROLLBACK");
78 if (PQresultStatus(result) != PGRES_COMMAND_OK)
80 strError = PQresultErrorMessage(result);
82 printfd(__FILE__, "POSTGRESQL_STORE::RollbackTransaction(): '%s'\n", strError.c_str());
91 int POSTGRESQL_STORE::EscapeString(std::string & value) const
94 char * buf = new char[(value.length() << 1) + 1];
96 PQescapeStringConn(connection,
104 strError = PQerrorMessage(connection);
105 printfd(__FILE__, "POSTGRESQL_STORE::EscapeString(): '%s'\n", strError.c_str());
116 std::string POSTGRESQL_STORE::Int2TS(time_t ts) const
118 struct tm brokenTime;
120 brokenTime.tm_wday = 0;
121 brokenTime.tm_yday = 0;
122 brokenTime.tm_isdst = 0;
124 gmtime_r(&ts, &brokenTime);
127 strftime(buf, 32, "%Y-%m-%d %H:%M:%S", &brokenTime);
132 time_t POSTGRESQL_STORE::TS2Int(const std::string & ts) const
134 struct tm brokenTime;
136 brokenTime.tm_wday = 0;
137 brokenTime.tm_yday = 0;
138 brokenTime.tm_isdst = 0;
140 stg_strptime(ts.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime);
142 return stg_timegm(&brokenTime);
145 void POSTGRESQL_STORE::MakeDate(std::string & date, int year, int month) const
147 struct tm brokenTime;
149 brokenTime.tm_wday = 0;
150 brokenTime.tm_yday = 0;
151 brokenTime.tm_isdst = 0;
155 brokenTime.tm_hour = 0;
156 brokenTime.tm_min = 0;
157 brokenTime.tm_sec = 0;
158 brokenTime.tm_year = year;
159 brokenTime.tm_mon = month;
163 time_t curTime = stgTime;
164 localtime_r(&curTime, &brokenTime);
167 brokenTime.tm_mday = DaysInMonth(brokenTime.tm_year + 1900, brokenTime.tm_mon);
171 strftime(buf, 32, "%Y-%m-%d", &brokenTime);