forked from PocketRent/hhvm-pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdo_pgsql_statement.cpp
467 lines (396 loc) · 16 KB
/
pdo_pgsql_statement.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "pdo_pgsql_resource.h"
#include "pdo_pgsql_statement.h"
#include "pdo_pgsql_connection.h"
#include "pdo_pgsql.h"
#include <iomanip>
#define STMT_HANDLE_ERROR(res) (*m_conn).handleError(this, (*m_conn).sqlstate(res), res.errorMessage())
namespace HPHP {
unsigned long PDOPgSqlStatement::m_stmtNameCounter = 0;
unsigned long PDOPgSqlStatement::m_cursorNameCounter = 0;
PDOPgSqlStatement::PDOPgSqlStatement(PDOPgSqlResource* conn, PQ::Connection* server)
: m_conn(conn->conn()), m_server(server),
m_result(), m_isPrepared(false), m_current_row(0) {
this->dbh = dynamic_cast<PDOResource*>(conn);
}
PDOPgSqlStatement::~PDOPgSqlStatement(){
sweep();
}
void PDOPgSqlStatement::sweep(){
if(m_stmtName.size() > 0){
if(m_isPrepared){
std::stringstream ss;
ss << "DEALLOCATE " << m_stmtName;
m_server->exec(ss.str());
}
}
if(m_cursorName.size() > 0){
// Do we need a check here maybe to see if we've actually got a cursor or not?
std::stringstream ss;
ss << "CLOSE " << m_cursorName;
m_server->exec(ss.str());
}
m_server = nullptr;
m_conn = nullptr;
}
bool PDOPgSqlStatement::create(const String& sql, const Array &options){
supports_placeholders = PDO_PLACEHOLDER_NAMED;
bool scrollable = pdo_attr_lval(options, PDO_ATTR_CURSOR, PDO_CURSOR_FWDONLY) == PDO_CURSOR_SCROLL;
if(scrollable){
m_cursorName = strprintf("pdo_crsr_%08x", ++m_cursorNameCounter);
// Disable prepared statements
supports_placeholders = PDO_PLACEHOLDER_NONE;
} else if (
m_conn->m_emulate_prepare ||
(!options.empty() && pdo_attr_lval(options, PDO_ATTR_EMULATE_PREPARES, 0))
){
supports_placeholders = PDO_PLACEHOLDER_NONE;
}
if(supports_placeholders != PDO_PLACEHOLDER_NONE && m_server->protocolVersion() > 2){
named_rewrite_template = "$%d";
String nsql;
int ret = pdo_parse_params(sp_PDOStatement(this), sql, nsql);
if(ret == 1){
// Query was rewritten
} else if (ret == -1){
// Query didn't parse - exception should have been thrown at this point
strncpy(m_conn->error_code, error_code, 6);
m_conn->error_code[5] = '\0';
return false;
} else {
// Original is great
nsql = sql;
}
m_stmtName = strprintf("pdo_stmt_%08x", ++m_stmtNameCounter);
m_resolvedQuery = (std::string)nsql;
}
return true;
}
bool PDOPgSqlStatement::executer(){
ExecStatusType status;
if(m_result){
m_result = PQ::Result();
}
m_current_row = 0;
if(m_cursorName.size() > 0){
if(m_isPrepared){
std::stringstream ss;
ss << "CLOSE " << m_cursorName;
m_result = m_server->exec(ss.str());
}
std::stringstream q;
q << "DECLARE " << m_cursorName << " SCROLL CURSOR WITH HOLD FOR " << active_query_string.data();
m_result = m_server->exec(q.str());
status = m_result.status();
if(status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK){
STMT_HANDLE_ERROR(m_result);
return false;
}
m_isPrepared = true;
q.str(std::string());
// Fetch to be able to get total number of rows
q << "FETCH FORWARD 0 FROM " << m_cursorName;
m_result = m_server->exec(q.str());
} else if(m_stmtName.size() > 0) {
if(!m_isPrepared){
stmt_retry:
m_result = m_server->prepare(m_stmtName.c_str(), m_resolvedQuery.c_str(), bound_params.size(), param_types.data());
status = m_result.status();
switch(status) {
case PGRES_COMMAND_OK:
case PGRES_TUPLES_OK:
// It worked!
m_isPrepared = true;
break;
default:
// Read Zend implementation for this one. I am not sure if this applies to hhvm as well or not
// but figure better leave it in here than not
if(!strcmp(m_conn->sqlstate(m_result), "42P05")){
std::stringstream q;
q << "DEALLOCATE " << m_stmtName;
m_server->exec(q.str());
goto stmt_retry;
} else {
STMT_HANDLE_ERROR(m_result);
return false;
}
}
}
std::vector<const char*> params;
params.reserve(bound_params.size());
for(auto it = param_values.begin(); it != param_values.end(); it++){
if((*it).isNull()){
params.push_back(nullptr);
} else {
params.push_back((*it).toString().c_str());
}
}
if(params.size() != bound_params.size()){
m_conn->handleError(this, "XX000", "Parameters not being bound correctly");
return false;
}
m_result = m_server->execPrepared(m_stmtName.c_str(), bound_params.size(), params.data(), param_lengths.data(), param_formats.data());
} else {
m_result = m_server->exec(active_query_string.data());
}
status = m_result.status();
if(status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK){
STMT_HANDLE_ERROR(m_result);
return false;
}
if(!executed && !column_count){
column_count = (long)m_result.numFields();
columns.reset();
m_pgsql_column_types.clear();
m_pgsql_column_types.reserve(column_count);
}
if(status == PGRES_COMMAND_OK){
row_count = m_result.lcmdTuples();
m_conn->pgoid = m_result.oidValue();
} else {
row_count = m_result.numTuples();
}
return true;
}
bool PDOPgSqlStatement::describer(int colno){
if(!m_result){
return false;
}
if(colno < 0 || colno >= column_count){
// Outside column ranges
return false;
}
if(columns.empty()){
for(int i = 0; i < column_count; i++){
columns.set(i, Resource(newres<PDOColumn>()));
}
}
PDOColumn *col = columns[colno].toResource().getTyped<PDOColumn>().get();
col->name = String(m_result.fieldName(colno));
col->maxlen = m_result.size(colno);
col->precision = m_result.precision(colno);
Oid oid = m_result.type(colno);
Oid *column_type = m_pgsql_column_types.data()+colno;
*column_type = oid;
switch(oid){
case BOOLOID:
col->param_type = PDO_PARAM_BOOL;
break;
case OIDOID:
// Let's get back to this later
col->param_type = PDO_PARAM_STR;
break;
case INT2OID:
case INT4OID:
col->param_type = PDO_PARAM_INT;
break;
case INT8OID:
if(sizeof(long) >= 8){
col->param_type = PDO_PARAM_INT;
} else {
col->param_type = PDO_PARAM_STR;
}
break;
case BYTEAOID:
col->param_type = PDO_PARAM_LOB;
break;
default:
col->param_type = PDO_PARAM_STR;
}
return true;
}
bool PDOPgSqlStatement::fetcher(PDOFetchOrientation ori, long offset){
if(m_cursorName.size() > 0){
bool oriOk;
std::string oriStr = oriToStr(ori, offset, oriOk);
if(!oriOk){
return false;
}
std::string q = strprintf("FETCH %s FROM %s", oriStr.c_str(), m_cursorName.c_str());
m_result = m_server->exec(q);
ExecStatusType status = m_result.status();
if(status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK){
STMT_HANDLE_ERROR(m_result);
return false;
}
if(m_result.numTuples()){
m_current_row = 1;
return true;
} else {
return false;
}
} else {
if(m_current_row < row_count){
m_current_row++;
return true;
} else {
return false;
}
}
}
bool PDOPgSqlStatement::getColumnMeta(int64_t colno, Array &return_value){
if (!m_result){
return false;
}
if(colno < 0 || colno >= column_count){
return false;
}
Oid coltype = m_pgsql_column_types[colno];
return_value.add(String("pgsql:oid"), (long)coltype);
std::string q = strprintf("SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", coltype);
PQ::Result res = m_server->exec(q);
ExecStatusType status = res.status();
if(status != PGRES_TUPLES_OK){
return true;
}
if(res.numTuples() != 1){
return true;
}
return_value.add(String("native_type"), String(res.getValue(0, 0)));
return true;
}
bool PDOPgSqlStatement::getColumn(int colno, Variant &value){
if(!m_result){
return false;
}
if(colno < 0 || colno >= column_count){
return false;
}
// We have already increased m_current_row by 1 in fetch
long current_row = m_current_row - 1;
if(m_result.fieldIsNull(current_row, colno)){
value = Variant(Variant::NullInit());
return true;
}
char* val = m_result.getValue(current_row, colno);
PDOColumn* col = columns[colno].toResource().getTyped<PDOColumn>().get();
switch(col->param_type){
case PDO_PARAM_INT:
value = Variant(atol(val));
return true;
case PDO_PARAM_BOOL:
value = Variant(*val == 't');
return true;
case PDO_PARAM_LOB:
// Not implemented
return false;
case PDO_PARAM_NULL:
case PDO_PARAM_STR:
case PDO_PARAM_STMT:
case PDO_PARAM_ZVAL:
default:
value = Variant(String(val, CopyString));
return true;
}
}
bool PDOPgSqlStatement::paramHook(PDOBoundParam* param, PDOParamEvent event_type){
if(m_stmtName.size() > 0 && param->is_param){
switch(event_type){
case PDO_PARAM_EVT_FREE:
param_values.clear();
param_values.resize(0);
param_lengths.clear();
param_lengths.resize(0);
param_formats.clear();
param_formats.resize(0);
param_types.clear();
param_types.resize(0);
m_pgsql_column_types.clear();
m_pgsql_column_types.resize(0);
break;
case PDO_PARAM_EVT_NORMALIZE:
// decode from $1, $2 into 0, 1, etc.
if(param->name){
if(param->name.data()[0] == '$'){
param->paramno = atoi(param->name.data() + 1)-1;
} else {
// resolve name
if(bound_param_map.exists(param->name, true)){
param->paramno = atoi(bound_param_map[param->name].asCStrRef().data() + 1)-1;
} else {
m_conn->handleError(this, "HY093", param->name.data());
return false;
}
}
}
break;
case PDO_PARAM_EVT_ALLOC:
case PDO_PARAM_EVT_EXEC_POST:
case PDO_PARAM_EVT_FETCH_PRE:
case PDO_PARAM_EVT_FETCH_POST:
// work is done in normalize
return true;
case PDO_PARAM_EVT_EXEC_PRE:
if(bound_param_map.isNull()){
return false;
}
int elems = bound_params.size();
if(param_values.size() == 0){
param_values.resize(elems);
param_lengths.resize(elems);
param_formats.resize(elems);
param_types.resize(elems);
}
if(param->paramno >= 0){
if(param->paramno >= elems){
m_conn->handleError(this, "HY105", "Too many parameters bound");
return false;
}
Oid* param_ts = param_types.data();
Variant* param_vals = param_values.data();
int* param_fs = param_formats.data();
int* param_ls = param_lengths.data();
if(PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB){
// Todo, implement LOBs
return false;
}
if(PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL || param->parameter.isNull()){
param_vals[param->paramno] = Variant(Variant::NullInit());
param_ls[param->paramno] = 0;
} else if(param->parameter.isBoolean()){
// Sadly we need to convert this to a 'real' pgsql boolean literal, ie a string
param_vals[param->paramno] = param->parameter.asBooleanVal() ? Variant("t") : Variant("f");
param_ls[param->paramno] = 1;
param_fs[param->paramno] = 0;
} else {
String str = param->parameter.toString();
param_vals[param->paramno] = str;
param_ls[param->paramno] = str.length();
param_fs[param->paramno] = 0;
}
if(PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB){
param_ts[param->paramno] = 0;
param_fs[param->paramno] = 1;
} else {
param_ts[param->paramno] = 0;
}
}
break;
}
} else {
if(param->is_param){
// Convert into a native pgsql boolean literal
if(PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)){
param->param_type = PDO_PARAM_STR;
param->parameter = param->parameter.asBooleanVal() ? String("t") : String("f");
}
}
}
return true;
}
bool PDOPgSqlStatement::cursorCloser(){
// For some reason, even without using a cursor, this is ok
// This is what Zend does
return true;
}
bool PDOPgSqlStatement::support(SupportedMethod method){
switch (method) {
case MethodSetAttribute:
case MethodGetAttribute:
case MethodNextRowset:
return false;
default:
return true;
}
}
}