1 |
/* |
2 |
* This file is a part of morm |
3 |
* and is distributed under the 2-Clause BSD licence. |
4 |
* Author: Tomasz Sowa <t.sowa@ttmath.org> |
5 |
*/ |
6 |
|
7 |
/* |
8 |
* Copyright (c) 2018-2019, Tomasz Sowa |
9 |
* All rights reserved. |
10 |
* |
11 |
* Redistribution and use in source and binary forms, with or without |
12 |
* modification, are permitted provided that the following conditions are met: |
13 |
* |
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
15 |
* this list of conditions and the following disclaimer. |
16 |
* |
17 |
* 2. Redistributions in binary form must reproduce the above copyright |
18 |
* notice, this list of conditions and the following disclaimer in the |
19 |
* documentation and/or other materials provided with the distribution. |
20 |
* |
21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
22 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
25 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
26 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
27 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
29 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
30 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 |
* POSSIBILITY OF SUCH DAMAGE. |
32 |
* |
33 |
*/ |
34 |
|
35 |
#include "postgresqlqueryresult.h" |
36 |
|
37 |
|
38 |
|
39 |
namespace morm |
40 |
{ |
41 |
|
42 |
|
43 |
PostgreSQLQueryResult::PostgreSQLQueryResult() |
44 |
{ |
45 |
psql_result = nullptr; |
46 |
psql_status = PGRES_EMPTY_QUERY; |
47 |
} |
48 |
|
49 |
|
50 |
void PostgreSQLQueryResult::clear() |
51 |
{ |
52 |
if( psql_result ) |
53 |
{ |
54 |
PQclear(psql_result); |
55 |
} |
56 |
|
57 |
psql_result = nullptr; |
58 |
psql_status = PGRES_EMPTY_QUERY; |
59 |
|
60 |
QueryResult::clear(); |
61 |
} |
62 |
|
63 |
|
64 |
bool PostgreSQLQueryResult::has_db_result() |
65 |
{ |
66 |
return psql_result != nullptr; |
67 |
} |
68 |
|
69 |
|
70 |
const char * PostgreSQLQueryResult::get_field_string_value(int column_index) |
71 |
{ |
72 |
const char * value_str = nullptr; |
73 |
|
74 |
if( psql_result && column_index >= 0 && (size_t)column_index < result_cols ) |
75 |
{ |
76 |
if( cur_row < result_rows ) |
77 |
{ |
78 |
value_str = PQgetvalue(psql_result, cur_row, column_index); |
79 |
} |
80 |
} |
81 |
|
82 |
return value_str; |
83 |
} |
84 |
|
85 |
|
86 |
|
87 |
const char * PostgreSQLQueryResult::get_field_string_value(const char * column_name) |
88 |
{ |
89 |
const char * value_str = nullptr; |
90 |
|
91 |
if( psql_result ) |
92 |
{ |
93 |
int col_index = PQfnumber(psql_result, column_name); |
94 |
|
95 |
if( col_index != -1 ) |
96 |
{ |
97 |
if( cur_row < result_rows ) |
98 |
{ |
99 |
value_str = PQgetvalue(psql_result, cur_row, col_index); |
100 |
} |
101 |
} |
102 |
} |
103 |
|
104 |
return value_str; |
105 |
} |
106 |
|
107 |
|
108 |
|
109 |
int PostgreSQLQueryResult::get_column_index(const char * column_name) |
110 |
{ |
111 |
int col_index = -1; |
112 |
|
113 |
if( psql_result ) |
114 |
{ |
115 |
col_index = PQfnumber(psql_result, column_name); |
116 |
// returns -1 if there is no such a column |
117 |
} |
118 |
|
119 |
return col_index; |
120 |
} |
121 |
|
122 |
|
123 |
bool PostgreSQLQueryResult::is_null(int column_index) |
124 |
{ |
125 |
bool res = false; |
126 |
|
127 |
if( column_index >= 0 && (size_t)column_index < result_cols ) |
128 |
{ |
129 |
if( cur_row < result_rows ) |
130 |
{ |
131 |
res = is_null(cur_row, column_index); |
132 |
} |
133 |
} |
134 |
|
135 |
return res; |
136 |
} |
137 |
|
138 |
|
139 |
/* |
140 |
* in the future we can use single row mode: |
141 |
* https://www.postgresql.org/docs/10/libpq-single-row-mode.html |
142 |
* |
143 |
* or just cursors from database: |
144 |
* https://www.postgresql.org/docs/current/sql-fetch.html |
145 |
* |
146 |
*/ |
147 |
const char * PostgreSQLQueryResult::get_value_from_result(int row, int col) |
148 |
{ |
149 |
const char * value_str = nullptr; |
150 |
|
151 |
if( psql_result ) |
152 |
{ |
153 |
value_str = PQgetvalue(psql_result, row, col); |
154 |
// can return a null pointer if there is no such an item in the last psql_result |
155 |
} |
156 |
|
157 |
return value_str; |
158 |
} |
159 |
|
160 |
|
161 |
int PostgreSQLQueryResult::get_value_length(int row, int col) |
162 |
{ |
163 |
int len = 0; |
164 |
|
165 |
if( psql_result ) |
166 |
{ |
167 |
len = PQgetlength(psql_result, row, col); |
168 |
} |
169 |
|
170 |
return len; |
171 |
} |
172 |
|
173 |
|
174 |
bool PostgreSQLQueryResult::is_null(int row, int col) |
175 |
{ |
176 |
bool is_null = false; |
177 |
|
178 |
if( psql_result ) |
179 |
{ |
180 |
is_null = (PQgetisnull(psql_result, row, col) == 1); |
181 |
} |
182 |
|
183 |
return is_null; |
184 |
} |
185 |
|
186 |
|
187 |
void PostgreSQLQueryResult::dump_column_names(PT::Log & log) |
188 |
{ |
189 |
if( psql_result ) |
190 |
{ |
191 |
int cols = PQnfields(psql_result); |
192 |
|
193 |
for(int i = 0 ; i < cols ; ++i) |
194 |
{ |
195 |
log << i << ' ' << PQfname(psql_result, i) << PT::Log::logend; |
196 |
} |
197 |
} |
198 |
} |
199 |
|
200 |
|
201 |
//int PostgreSQLQueryResult::Rows(PGresult * r) |
202 |
//{ |
203 |
// // PQntuples - Returns the number of rows (tuples) in the query result. Because it returns |
204 |
// // an integer result, large result sets might overflow the return value on 32-bit operating systems. |
205 |
// return PQntuples(r); |
206 |
//} |
207 |
|
208 |
|
209 |
//int PostgreSQLQueryResult::Cols(PGresult * r) |
210 |
//{ |
211 |
// // PQnfields - Returns the number of columns (fields) in each row of the query result. |
212 |
// return PQnfields(r); |
213 |
//} |
214 |
|
215 |
//long PostgreSQLQueryResult::AffectedRows(PGresult * r) |
216 |
//{ |
217 |
// // PQcmdTuples - This function returns a string containing the number of rows affected by the SQL |
218 |
// // statement that generated the PGresult. This function can only be used following the execution |
219 |
// // of an INSERT, UPDATE, DELETE, MOVE, FETCH, or COPY statement, or [...] |
220 |
// char * rows_str = PQcmdTuples(r); // can be an empty string |
221 |
// long rows = 0; |
222 |
// |
223 |
// if( rows_str ) |
224 |
// { |
225 |
// rows = strtol(rows_str, 0, 10); |
226 |
// // strtol - If an overflow or underflow occurs, errno is set to ERANGE |
227 |
// // and the function return value is clamped according to the following table: |
228 |
// // Function underflow overflow |
229 |
// // strtol() LONG_MIN LONG_MAX |
230 |
// |
231 |
// if( rows < 0 ) |
232 |
// rows = 0; |
233 |
// } |
234 |
// |
235 |
//return rows; |
236 |
//} |
237 |
|
238 |
|
239 |
//void PostgreSQLConnector::get_value_bin(int row, int col, std::string & result, bool clear_string) |
240 |
//{ |
241 |
// if( clear_string ) |
242 |
// result.clear(); |
243 |
// |
244 |
// const char * raw_result = get_value(row, col); |
245 |
// |
246 |
// if( raw_result ) |
247 |
// { |
248 |
// int len = PQgetlength(last_result, row, col); |
249 |
// |
250 |
// if( len > 0 ) |
251 |
// { |
252 |
// unescape_bin(raw_result, len, result); |
253 |
// } |
254 |
// } |
255 |
//} |
256 |
|
257 |
|
258 |
//bool PostgreSQLConnector::AssertValueSpace(PGresult * r, int row, int col, PT::Space & space) |
259 |
//{ |
260 |
// const char * res = AssertValue(r, row, col); |
261 |
// |
262 |
// conf_parser.SetSpace(space); |
263 |
// space.Clear(); |
264 |
// |
265 |
// PT::SpaceParser::Status status = conf_parser.ParseString(res); |
266 |
// |
267 |
// if( status != PT::SpaceParser::ok ) |
268 |
// { |
269 |
// log << log1 << "Morm: a problem with parsing a PT::Space"; |
270 |
// |
271 |
// if( status == PT::SpaceParser::syntax_error ) |
272 |
// log << ", syntax error at line: " << conf_parser.line; |
273 |
// |
274 |
// log << logend; |
275 |
// |
276 |
// space.Clear(); |
277 |
// return false; |
278 |
// } |
279 |
// |
280 |
//return true; |
281 |
//} |
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
} // namespace |
288 |
|