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 : Maksym Mamontov <stg@madf.info>
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 void POSTGRESQL_STORE::MakeDate(std::string & date, int year, int month) const
117 struct tm brokenTime;
119 brokenTime.tm_wday = 0;
120 brokenTime.tm_yday = 0;
121 brokenTime.tm_isdst = 0;
125 brokenTime.tm_hour = 0;
126 brokenTime.tm_min = 0;
127 brokenTime.tm_sec = 0;
128 brokenTime.tm_year = year;
129 brokenTime.tm_mon = month;
133 time_t curTime = stgTime;
136 localtime_r(&curTime, &brokenTime);
139 brokenTime.tm_mday = DaysInMonth(brokenTime.tm_year + 1900, brokenTime.tm_mon);
143 strftime(buf, 32, "%Y-%m-%d", &brokenTime);