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 $
34 #include "stg/common.h"
35 #include "postgresql_store.h"
37 int POSTGRESQL_STORE::StartTransaction() const
39 PGresult * result = PQexec(connection, "BEGIN");
41 if (PQresultStatus(result) != PGRES_COMMAND_OK)
43 strError = PQresultErrorMessage(result);
45 printfd(__FILE__, "POSTGRESQL_STORE::StartTransaction(): '%s'\n", strError.c_str());
54 int POSTGRESQL_STORE::CommitTransaction() const
56 PGresult * result = PQexec(connection, "COMMIT");
58 if (PQresultStatus(result) != PGRES_COMMAND_OK)
60 strError = PQresultErrorMessage(result);
62 printfd(__FILE__, "POSTGRESQL_STORE::CommitTransaction(): '%s'\n", strError.c_str());
71 int POSTGRESQL_STORE::RollbackTransaction() const
73 PGresult * result = PQexec(connection, "ROLLBACK");
75 if (PQresultStatus(result) != PGRES_COMMAND_OK)
77 strError = PQresultErrorMessage(result);
79 printfd(__FILE__, "POSTGRESQL_STORE::RollbackTransaction(): '%s'\n", strError.c_str());
88 int POSTGRESQL_STORE::EscapeString(std::string & value) const
91 char * buf = new char[(value.length() << 1) + 1];
93 PQescapeStringConn(connection,
101 strError = PQerrorMessage(connection);
102 printfd(__FILE__, "POSTGRESQL_STORE::EscapeString(): '%s'\n", strError.c_str());
113 std::string POSTGRESQL_STORE::Int2TS(uint32_t ts) const
116 struct tm brokenTime;
119 brokenTime.tm_wday = 0;
120 brokenTime.tm_yday = 0;
121 brokenTime.tm_isdst = 0;
123 gmtime_r(&tt, &brokenTime);
125 strftime(buf, 32, "%Y-%m-%d %H:%M:%S", &brokenTime);
130 uint32_t POSTGRESQL_STORE::TS2Int(const std::string & ts) const
132 struct tm brokenTime;
134 brokenTime.tm_wday = 0;
135 brokenTime.tm_yday = 0;
136 brokenTime.tm_isdst = 0;
138 stg_strptime(ts.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime);
140 return stg_timegm(&brokenTime);
143 void POSTGRESQL_STORE::MakeDate(std::string & date, int year, int month) const
145 struct tm brokenTime;
147 brokenTime.tm_wday = 0;
148 brokenTime.tm_yday = 0;
149 brokenTime.tm_isdst = 0;
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;
161 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);