]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store_utils.cpp
Ticket 37. Int2TS() and TS2Int() methods define removed.
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store_utils.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  Vairous utility methods
23  *
24  *  $Revision: 1.3 $
25  *  $Date: 2009/10/22 10:01:08 $
26  *
27  */
28
29 #include <string>
30 #include <ctime>
31
32 #include <libpq-fe.h>
33
34 #include "stg/common.h"
35 #include "postgresql_store.h"
36
37 extern volatile time_t stgTime;
38
39 int POSTGRESQL_STORE::StartTransaction() const
40 {
41 PGresult * result = PQexec(connection, "BEGIN");
42
43 if (PQresultStatus(result) != PGRES_COMMAND_OK)
44     {
45     strError = PQresultErrorMessage(result);
46     PQclear(result);
47     printfd(__FILE__, "POSTGRESQL_STORE::StartTransaction(): '%s'\n", strError.c_str());
48     return -1;
49     }
50
51 PQclear(result);
52
53 return 0;
54 }
55
56 int POSTGRESQL_STORE::CommitTransaction() const
57 {
58 PGresult * result = PQexec(connection, "COMMIT");
59
60 if (PQresultStatus(result) != PGRES_COMMAND_OK)
61     {
62     strError = PQresultErrorMessage(result);
63     PQclear(result);
64     printfd(__FILE__, "POSTGRESQL_STORE::CommitTransaction(): '%s'\n", strError.c_str());
65     return -1;
66     }
67
68 PQclear(result);
69
70 return 0;
71 }
72
73 int POSTGRESQL_STORE::RollbackTransaction() const
74 {
75 PGresult * result = PQexec(connection, "ROLLBACK");
76
77 if (PQresultStatus(result) != PGRES_COMMAND_OK)
78     {
79     strError = PQresultErrorMessage(result);
80     PQclear(result);
81     printfd(__FILE__, "POSTGRESQL_STORE::RollbackTransaction(): '%s'\n", strError.c_str());
82     return -1;
83     }
84
85 PQclear(result);
86
87 return 0;
88 }
89
90 int POSTGRESQL_STORE::EscapeString(std::string & value) const
91 {
92 int error = 0;
93 char * buf = new char[(value.length() << 1) + 1];
94
95 PQescapeStringConn(connection,
96                    buf,
97                    value.c_str(),
98                    value.length(),
99                    &error);
100
101 if (error)
102     {
103     strError = PQerrorMessage(connection);
104     printfd(__FILE__, "POSTGRESQL_STORE::EscapeString(): '%s'\n", strError.c_str());
105     delete[] buf;
106     return -1;
107     }
108
109 value = buf;
110
111 delete[] buf;
112 return 0;
113 }
114
115 void POSTGRESQL_STORE::MakeDate(std::string & date, int year, int month) const
116 {
117 struct tm brokenTime;
118
119 brokenTime.tm_wday = 0;
120 brokenTime.tm_yday = 0;
121 brokenTime.tm_isdst = 0;
122
123 if (year)
124     {
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;
130     }
131 else
132     {
133     time_t curTime = stgTime;
134     /*time(&curTime);*/
135
136     localtime_r(&curTime, &brokenTime);
137     }
138
139 brokenTime.tm_mday = DaysInMonth(brokenTime.tm_year + 1900, brokenTime.tm_mon);
140
141 char buf[32];
142
143 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
144
145 date = buf;
146 }
147