-
Notifications
You must be signed in to change notification settings - Fork 2
/
db-pg.cpp
177 lines (160 loc) · 2.96 KB
/
db-pg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "db-pg.h"
#include "errlist.h"
DatabasePostgreSQL::DatabasePostgreSQL()
: conn(NULL)
{
errmsg = "";
type = "postgresql";
}
DatabasePostgreSQL::~DatabasePostgreSQL()
{
if (conn) {
close();
}
}
/**
* @brief Connset to PostgreSQL
* @param login not used
* @param password not used
*/
int DatabasePostgreSQL::open(
const std::string &connection,
const std::string &login,
const std::string &password,
const std::string &db,
int port
)
{
conn = PQconnectdb(connection.c_str());
ConnStatusType r = PQstatus(conn);
if (r != CONNECTION_OK)
{
conn = NULL;
errmsg = std::string(PQerrorMessage(conn));
}
return r;
}
int DatabasePostgreSQL::close()
{
PQfinish(conn);
conn = NULL;
return 0;
}
int DatabasePostgreSQL::exec(
const std::string &statement
)
{
PGresult *r = PQexec(conn, statement.c_str());
ExecStatusType status = PQresultStatus(r);
int rc;
if (status == PGRES_TUPLES_OK || status == PGRES_COMMAND_OK ) {
rc = 0;
} else {
errmsg = std::string(PQerrorMessage(conn));
rc = ERR_CODE_DB_SELECT;
}
PQclear(r);
return rc;
}
int DatabasePostgreSQL::select(
std::vector<std::vector<std::string>> &retval,
const std::string &statement
)
{
PGresult *r = PQexec(conn, statement.c_str());
ExecStatusType status = PQresultStatus(r);
int rc;
if (status == PGRES_TUPLES_OK || status == PGRES_COMMAND_OK ) {
rc = 0;
} else {
rc = ERR_CODE_DB_SELECT;
errmsg = std::string(PQerrorMessage(conn));
}
for (int row = 0; row < PQntuples(r); row++)
{
std::vector<std::string> line;
for (int fld = 0; fld < PQnfields(r); fld++)
{
std::string v(PQgetvalue(r, row, fld));
line.push_back(v);
}
retval.push_back(line);
}
PQclear(r);
return rc;
}
//
// cursor
//
int DatabasePostgreSQL::cursorOpen(
void **retStmt,
std::string &statement
)
{
PGresult *r = PQexec(conn, statement.c_str());
ExecStatusType status = PQresultStatus(r);
int rc;
if (status == PGRES_TUPLES_OK || status == PGRES_COMMAND_OK ) {
rc = 0;
} else {
rc = ERR_CODE_DB_SELECT;
errmsg = std::string(PQerrorMessage(conn));
}
*retStmt = r;
return rc;
}
int DatabasePostgreSQL::cursorBindText(
void *stmt,
int position1,
const std::string &value
)
{
// int r = sqlite3_bind_text((sqlite3_stmt *) stmt, position1, value.c_str(), -1, SQLITE_STATIC);
long int v = strtol(value.c_str(), NULL, 10);
int r = ERR_CODE_DB_EXEC;
if (r)
errmsg = ERR_DB_EXEC;
return r;
}
int DatabasePostgreSQL::cursorColumnCount(
void *stmt
)
{
return PQnfields((const PGresult *) stmt);
}
std::string DatabasePostgreSQL::cursorColumnName(
void *stmt,
int column0
)
{
std::string r;
return r;
}
std::string DatabasePostgreSQL::cursorColumnText(
void *stmt,
int column0
)
{
std::string r;
return r;
}
DB_FIELD_TYPE DatabasePostgreSQL::cursorColumnType(
void *stmt,
int column0
)
{
return (DB_FIELD_TYPE) 0;
}
bool DatabasePostgreSQL::cursorNext(
void *stmt
)
{
return false;
}
int DatabasePostgreSQL::cursorClose(
void *stmt
)
{
PQclear((PGresult *) stmt);
return 0;
}