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 extern volatile time_t stgTime;
39 int POSTGRESQL_STORE::StartTransaction() const
41 PGresult * result = PQexec(connection, "BEGIN");
43 if (PQresultStatus(result) != PGRES_COMMAND_OK)
45 strError = PQresultErrorMessage(result);
47 printfd(__FILE__, "POSTGRESQL_STORE::StartTransaction(): '%s'\n", strError.c_str());
56 int POSTGRESQL_STORE::CommitTransaction() const
58 PGresult * result = PQexec(connection, "COMMIT");
60 if (PQresultStatus(result) != PGRES_COMMAND_OK)
62 strError = PQresultErrorMessage(result);
64 printfd(__FILE__, "POSTGRESQL_STORE::CommitTransaction(): '%s'\n", strError.c_str());
73 int POSTGRESQL_STORE::RollbackTransaction() const
75 PGresult * result = PQexec(connection, "ROLLBACK");
77 if (PQresultStatus(result) != PGRES_COMMAND_OK)
79 strError = PQresultErrorMessage(result);
81 printfd(__FILE__, "POSTGRESQL_STORE::RollbackTransaction(): '%s'\n", strError.c_str());
90 int POSTGRESQL_STORE::EscapeString(std::string & value) const
93 char * buf = new char[(value.length() << 1) + 1];
95 PQescapeStringConn(connection,
103 strError = PQerrorMessage(connection);
104 printfd(__FILE__, "POSTGRESQL_STORE::EscapeString(): '%s'\n", strError.c_str());
115 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);
126 strftime(buf, 32, "%Y-%m-%d %H:%M:%S", &brokenTime);
131 time_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);