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 $
36 #include "postgresql_store.h"
38 int POSTGRESQL_STORE::StartTransaction() const
40 PGresult * result = PQexec(connection, "BEGIN");
42 if (PQresultStatus(result) != PGRES_COMMAND_OK)
44 strError = PQresultErrorMessage(result);
46 printfd(__FILE__, "POSTGRESQL_STORE::StartTransaction(): '%s'\n", strError.c_str());
55 int POSTGRESQL_STORE::CommitTransaction() const
57 PGresult * result = PQexec(connection, "COMMIT");
59 if (PQresultStatus(result) != PGRES_COMMAND_OK)
61 strError = PQresultErrorMessage(result);
63 printfd(__FILE__, "POSTGRESQL_STORE::CommitTransaction(): '%s'\n", strError.c_str());
72 int POSTGRESQL_STORE::RollbackTransaction() const
74 PGresult * result = PQexec(connection, "ROLLBACK");
76 if (PQresultStatus(result) != PGRES_COMMAND_OK)
78 strError = PQresultErrorMessage(result);
80 printfd(__FILE__, "POSTGRESQL_STORE::RollbackTransaction(): '%s'\n", strError.c_str());
89 int POSTGRESQL_STORE::EscapeString(std::string & value) const
92 char * buf = new char[(value.length() << 1) + 1];
94 PQescapeStringConn(connection,
102 strError = PQerrorMessage(connection);
103 printfd(__FILE__, "POSTGRESQL_STORE::EscapeString(): '%s'\n", strError.c_str());
114 std::string POSTGRESQL_STORE::Int2TS(uint32_t ts) const
117 struct tm brokenTime;
120 brokenTime.tm_wday = 0;
121 brokenTime.tm_yday = 0;
122 brokenTime.tm_isdst = 0;
124 gmtime_r(&tt, &brokenTime);
126 strftime(buf, 32, "%Y-%m-%d %H:%M:%S", &brokenTime);
131 uint32_t POSTGRESQL_STORE::TS2Int(const std::string & ts) const
133 struct tm brokenTime;
135 brokenTime.tm_wday = 0;
136 brokenTime.tm_yday = 0;
137 brokenTime.tm_isdst = 0;
139 stg_strptime(ts.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime);
141 return stg_timegm(&brokenTime);
144 void POSTGRESQL_STORE::MakeDate(std::string & date, int year, int month) const
146 struct tm brokenTime;
148 brokenTime.tm_wday = 0;
149 brokenTime.tm_yday = 0;
150 brokenTime.tm_isdst = 0;
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;
162 time_t curTime = stgTime;
165 localtime_r(&curTime, &brokenTime);
168 brokenTime.tm_mday = DaysInMonth(brokenTime.tm_year + 1900, brokenTime.tm_mon);
172 strftime(buf, 32, "%Y-%m-%d", &brokenTime);