forked from PocketRent/hhvm-pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgsql.cpp
More file actions
1528 lines (1203 loc) · 39.6 KB
/
pgsql.cpp
File metadata and controls
1528 lines (1203 loc) · 39.6 KB
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "hphp/runtime/base/base-includes.h"
#include "hphp/runtime/base/zend-string.h"
#include <libpq-fe.h>
#include "hphp/runtime/base/runtime-option.h"
#include "hphp/runtime/server/server-stats.h"
#include "hphp/runtime/ext/ext_string.h"
#define PGSQL_ASSOC 1
#define PGSQL_NUM 2
#define PGSQL_BOTH (PGSQL_ASSOC | PGSQL_NUM)
#define k_PGSQL_BOTH PGSQL_BOTH
#define PGSQL_STATUS_LONG 1
#define PGSQL_STATUS_STRING 2
namespace HPHP {
namespace { // Anonymous namespace
class PGSQL : public SweepableResourceData {
DECLARE_RESOURCE_ALLOCATION(PGSQL);
public:
static bool AllowPersistent;
static int MaxPersistent;
static int MaxLinks;
static bool AutoResetPersistent;
static bool IgnoreNotice;
static bool LogNotice;
static PGSQL *Get(CVarRef conn_id);
static PGconn *GetConn(CVarRef conn_id, PGSQL **rconn = NULL);
public:
PGSQL(String conninfo, bool async);
~PGSQL();
void close();
static StaticString s_class_name;
virtual const String& o_getClassNameHook() const { return s_class_name; }
virtual bool isResource() const { return m_conn != NULL; }
PGconn * get() { return m_conn; }
bool isNonBlocking() { return PQisnonblocking(m_conn) == 1; }
int setNonBlocking(bool val=true) { return PQsetnonblocking(m_conn, (int)val); }
private:
PGconn *m_conn;
public:
std::string m_conn_string;
std::string m_db;
std::string m_user;
std::string m_pass;
std::string m_host;
std::string m_port;
std::string m_options;
std::string m_last_notice;
};
class PGSQLResult : public SweepableResourceData {
DECLARE_RESOURCE_ALLOCATION(PGSQLResult);
public:
static PGSQLResult *Get(CVarRef result);
static PGresult *GetResult(CVarRef result, PGSQLResult **rres = NULL);
public:
PGSQLResult(PGSQL* conn, PGresult *res);
~PGSQLResult();
static StaticString s_class_name;
virtual const String& o_getClassNameHook() const { return s_class_name; }
virtual bool isResource() const { return m_res != NULL; }
void close();
PGresult *get() { return m_res; }
int getFieldNumber(CVarRef field);
int getNumFields();
int getNumRows();
bool convertFieldRow(CVarRef row, CVarRef field,
int *out_row, int *out_field, const char *fn_name = NULL);
Variant fieldIsNull(CVarRef row, CVarRef field, const char *fn_name = NULL);
Variant getFieldVal(CVarRef row, CVarRef field, const char *fn_name = NULL);
String getFieldVal(int row, int field, const char *fn_name = NULL);
PGSQL * getConn() { return m_conn; }
public:
int m_current_row;
private:
PGresult *m_res;
int m_num_fields;
int m_num_rows;
PGSQL * m_conn;
};
}
//////////////////////////////////////////////////////////////////////////////////
StaticString PGSQL::s_class_name("pgsql connection");
StaticString PGSQLResult::s_class_name("pgsql result");
PGSQL *PGSQL::Get(CVarRef conn_id) {
if (conn_id.isNull()) {
return NULL;
}
PGSQL *pgsql = conn_id.toResource().getTyped<PGSQL>
(!RuntimeOption::ThrowBadTypeExceptions,
!RuntimeOption::ThrowBadTypeExceptions);
return pgsql;
}
PGconn *PGSQL::GetConn(CVarRef conn_id, PGSQL **rconn /* = NULL */) {
PGSQL *pgsql = Get(conn_id);
PGconn *ret = NULL;
if (pgsql) {
ret = pgsql->get();
}
if (ret == NULL) {
raise_warning("supplied argument is not a valid Postgres connection resource");
}
if (rconn) {
*rconn = pgsql;
}
return ret;
}
static void notice_processor(void *arg, const char *message) {
if (arg != NULL) {
auto pgsql = static_cast<PGSQL *>(arg);
pgsql->m_last_notice = message;
if (PGSQL::LogNotice) {
raise_notice("%s", message);
}
}
}
PGSQL::PGSQL(String conninfo, bool async)
: m_conn_string(conninfo.data()), m_last_notice("") {
if (RuntimeOption::EnableStats && RuntimeOption::EnableSQLStats) {
ServerStats::Log("sql.conn", 1);
}
if (async) {
m_conn = PQconnectStart(conninfo.data());
} else {
m_conn = PQconnectdb(conninfo.data());
ConnStatusType st = PQstatus(m_conn);
if (m_conn && st == CONNECTION_OK) {
// Load up the fixed information
m_db = PQdb(m_conn);
m_user = PQuser(m_conn);
m_pass = PQpass(m_conn);
m_host = PQhost(m_conn);
m_port = PQport(m_conn);
m_options = PQoptions(m_conn);
} else if (m_conn) {
raise_warning("pg_connect(): Connection failed: %s", PQerrorMessage(m_conn));
PQfinish(m_conn);
m_conn = NULL;
}
}
if (m_conn) {
if (!PGSQL::IgnoreNotice) {
PQsetNoticeProcessor(m_conn, notice_processor, static_cast<void *>(this));
} else {
PQsetNoticeProcessor(m_conn, notice_processor, NULL);
}
}
}
void PGSQL::close() {
if (m_conn != NULL) {
PQfinish(m_conn);
m_conn = NULL;
}
}
PGSQL::~PGSQL() {
close();
}
void PGSQL::sweep() {
close();
}
PGSQLResult *PGSQLResult::Get(CVarRef result) {
if (result.isNull()) {
return NULL;
}
auto *res = result.toResource().getTyped<PGSQLResult>
(!RuntimeOption::ThrowBadTypeExceptions,
!RuntimeOption::ThrowBadTypeExceptions);
return res;
}
PGresult *PGSQLResult::GetResult(CVarRef result, PGSQLResult **rres) {
auto *res = Get(result);
PGresult *ret = NULL;
if (res) {
ret = res->get();
}
if (ret == NULL) {
raise_warning("supplied argument is not a valid Postgres result resource");
}
if (rres) {
*rres = res;
}
return ret;
}
PGSQLResult::PGSQLResult(PGSQL * conn, PGresult * res)
: m_current_row(0), m_res(res),
m_num_fields(-1), m_num_rows(-1), m_conn(conn) {
m_conn->incRefCount();
}
void PGSQLResult::close() {
if (m_res) {
PQclear(m_res);
m_res = NULL;
}
}
PGSQLResult::~PGSQLResult() {
m_conn->decRefCount();
close();
}
void PGSQLResult::sweep() {
close();
}
int PGSQLResult::getFieldNumber(CVarRef field) {
int n;
if (field.isNumeric(true)) {
n = field.toInt32();
} else if (field.isString()){
n = PQfnumber(m_res, field.asCStrRef().data());
} else {
n = -1;
}
return n;
}
int PGSQLResult::getNumFields() {
if (m_num_fields == -1) {
m_num_fields = PQnfields(m_res);
}
return m_num_fields;
}
int PGSQLResult::getNumRows() {
if (m_num_rows == -1) {
m_num_rows = PQntuples(m_res);
}
return m_num_rows;
}
bool PGSQLResult::convertFieldRow(CVarRef row, CVarRef field,
int *out_row, int *out_field, const char *fn_name) {
Variant actual_field;
int actual_row;
assert(out_row && out_field && "Output parameters cannot be null");
if (fn_name == NULL) {
fn_name = "__internal_pgsql_func";
}
if (field.isInitialized()) {
actual_row = row.toInt64();
actual_field = field;
} else {
actual_row = m_current_row;
actual_field = row;
}
int field_number = getFieldNumber(actual_field);
if (field_number < 0 || field_number >= getNumFields()) {
if (actual_field.isString()) {
raise_warning("%s(): Unknown column name \"%s\"",
fn_name, actual_field.asCStrRef().data());
} else {
raise_warning("%s(): Column offset `%d` out of range", fn_name, field_number);
}
return false;
}
if (actual_row < 0 || actual_row >= getNumRows()) {
raise_warning("%s(): Row `%d` out of range", fn_name, actual_row);
return false;
}
*out_row = actual_row;
*out_field = field_number;
return true;
}
Variant PGSQLResult::fieldIsNull(CVarRef row, CVarRef field, const char *fn_name) {
int r, f;
if (convertFieldRow(row, field, &r, &f, fn_name)) {
return PQgetisnull(m_res, r, f);
}
return false;
}
Variant PGSQLResult::getFieldVal(CVarRef row, CVarRef field, const char *fn_name) {
int r, f;
if (convertFieldRow(row, field, &r, &f, fn_name)) {
return getFieldVal(r, f, fn_name);
}
return false;
}
String PGSQLResult::getFieldVal(int row, int field, const char *fn_name) {
if (PQgetisnull(m_res, row, field) == 1) {
return null_string;
} else {
char * value = PQgetvalue(m_res, row, field);
int length = PQgetlength(m_res, row, field);
return String(value, length, CopyString);
}
}
// Simple RAII helper to convert a CArrRef to a
// list of C strings to pass to pgsql functions. Needs
// to be like this because string conversion may-or-may
// not allocate and therefore needs to ensure that the
// underlying data lasts long enough.
struct CStringArray {
std::vector<String> m_strings;
std::vector<const char *> m_c_strs;
public:
CStringArray(CArrRef arr) {
int size = arr.size();
m_strings.reserve(size);
for(int i = 0; i < size; i++) {
Variant param = arr[i];
if (param.isNull()) {
m_strings.push_back(null_string);
} else {
m_strings.push_back(param.toString());
}
}
m_c_strs.reserve(size);
for (int i = 0; i < size; i++) {
if (m_strings[i].isNull()) {
m_c_strs.push_back(NULL);
} else {
m_c_strs.push_back(m_strings[i].data());
}
}
}
const char * const *data() {
return m_c_strs.data();
}
};
//////////////////// Connection functions /////////////////////////
static Variant HHVM_FUNCTION(pg_connect, const String& connection_string, int connect_type /* = 0 */) {
PGSQL * pgsql = NULL;
MemoryManager* mm = MemoryManager::TlsWrapper::getNoCheck();
pgsql = new (mm->smartMallocSize(sizeof(PGSQL))) PGSQL(connection_string, false);
if (pgsql->get() == NULL) {
delete pgsql;
return false;
}
return Resource(pgsql);
}
static Variant HHVM_FUNCTION(pg_async_connect, const String& connection_string, int connect_type /* = 0 */) {
PGSQL * pgsql = NULL;
pgsql = NEWOBJ(PGSQL)(connection_string, true);
if (pgsql->get() == NULL) {
delete pgsql;
return false;
}
return Resource(pgsql);
}
static bool HHVM_FUNCTION(pg_close, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
pgsql->close();
return true;
}
static bool HHVM_FUNCTION(pg_ping, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql->get() == NULL) {
return false;
}
PGPing response = PQping(pgsql->m_conn_string.data());
if (response == PQPING_OK) {
if (PQstatus(pgsql->get()) == CONNECTION_BAD) {
PQreset(pgsql->get());
return PQstatus(pgsql->get()) != CONNECTION_BAD;
} else {
return true;
}
}
return false;
}
static bool HHVM_FUNCTION(pg_connection_reset, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql->get() == NULL) {
return false;
}
PQreset(pgsql->get());
return PQstatus(pgsql->get()) != CONNECTION_BAD;
}
///////////// Interrogation Functions ////////////////////
static int64_t HHVM_FUNCTION(pg_connection_status, CResRef connection) {
PGconn *conn = PGSQL::GetConn(connection);
if (!conn) return CONNECTION_BAD;
return (int64_t)PQstatus(conn);
}
static bool HHVM_FUNCTION(pg_connection_busy, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
bool mode = pgsql->isNonBlocking();
pgsql->setNonBlocking();
PQconsumeInput(pgsql->get());
bool ret = (bool)PQisBusy(pgsql->get());
pgsql->setNonBlocking(mode);
return ret;
}
static Variant HHVM_FUNCTION(pg_dbname, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
if (pgsql->m_db.empty()) {
const char * db = PQdb(pgsql->get());
if (db == NULL) {
return false;
} else {
pgsql->m_db = db;
}
}
return pgsql->m_db;
}
static Variant HHVM_FUNCTION(pg_host, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
if (pgsql->m_host.empty()) {
const char * host = PQhost(pgsql->get());
if (host == NULL) {
return false;
} else {
pgsql->m_host = host;
}
}
return pgsql->m_host;
}
static Variant HHVM_FUNCTION(pg_port, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
if (pgsql->m_port.empty()) {
const char * port = PQport(pgsql->get());
if (port == NULL) {
return false;
} else {
pgsql->m_port = port;
}
}
String ret = pgsql->m_port;
if (ret.isNumeric()) {
return ret.toInt32();
} else {
return ret;
}
}
static Variant HHVM_FUNCTION(pg_options, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
if (pgsql->m_options.empty()) {
const char * options = PQoptions(pgsql->get());
if (options == NULL) {
return false;
} else {
pgsql->m_options = options;
}
}
return pgsql->m_options;
}
static Variant HHVM_FUNCTION(pg_parameter_status, CResRef connection, const String& param_name) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
String ret(PQparameterStatus(pgsql->get(), param_name.data()), CopyString);
return ret;
}
static Variant HHVM_FUNCTION(pg_client_encoding, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
int enc = PQclientEncoding(pgsql->get());
String ret(pg_encoding_to_char(enc), CopyString);
return ret;
}
static int64_t HHVM_FUNCTION(pg_transaction_status, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return PQTRANS_UNKNOWN;
}
return (int64_t)PQtransactionStatus(pgsql->get());
}
static Variant HHVM_FUNCTION(pg_last_error, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
String ret(PQerrorMessage(pgsql->get()), CopyString);
return f_trim(ret);
}
static Variant HHVM_FUNCTION(pg_last_notice, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
return pgsql->m_last_notice;
}
static Variant HHVM_FUNCTION(pg_version, CResRef connection) {
static StaticString client_key("client");
static StaticString protocol_key("protocol");
static StaticString server_key("server");
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return false;
}
Array ret;
int proto_ver = PQprotocolVersion(pgsql->get());
if (proto_ver) {
ret.set(protocol_key, String(proto_ver) + ".0");
}
int server_ver = PQserverVersion(pgsql->get());
if (server_ver) {
int revision = server_ver % 100;
int minor = (server_ver / 100) % 100;
int major = server_ver / 10000;
ret.set(server_key, String(major) + "." + String(minor) + "." + String(revision));
}
int client_ver = PQlibVersion();
if (client_ver) {
int revision = client_ver % 100;
int minor = (client_ver / 100) % 100;
int major = client_ver / 10000;
ret.set(client_key, String(major) + "." + String(minor) + "." + String(revision));
}
return ret;
}
static int64_t HHVM_FUNCTION(pg_get_pid, CResRef connection) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return -1;
}
return (int64_t)PQbackendPID(pgsql->get());
}
//////////////// Escaping Functions ///////////////////////////
static String HHVM_FUNCTION(pg_escape_bytea, CResRef connection, const String& data) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return null_string;
}
size_t escape_size = 0;
char * escaped = (char *)PQescapeByteaConn(pgsql->get(),
(unsigned char *)data.data(), data.size(), &escape_size);
if (escaped == NULL) {
raise_warning("pg_escape_bytea(): %s", PQerrorMessage(pgsql->get()));
return null_string;
}
String ret(escaped, escape_size, CopyString);
PQfreemem(escaped);
return ret;
}
static String HHVM_FUNCTION(pg_escape_identifier, CResRef connection, const String& data) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return null_string;
}
char * escaped = (char *)PQescapeIdentifier(pgsql->get(), data.data(), data.size());
if (escaped == NULL) {
raise_warning("pg_escape_identifier(): %s", PQerrorMessage(pgsql->get()));
return null_string;
}
String ret(escaped, CopyString);
PQfreemem(escaped);
return ret;
}
static String HHVM_FUNCTION(pg_escape_literal, CResRef connection, const String& data) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return null_string;
}
char * escaped = (char *)PQescapeLiteral(pgsql->get(), data.data(), data.size());
if (escaped == NULL) {
raise_warning("pg_escape_literal(): %s", PQerrorMessage(pgsql->get()));
return null_string;
}
String ret(escaped, CopyString);
PQfreemem(escaped);
return ret;
}
static String HHVM_FUNCTION(pg_escape_string, CResRef connection, const String& data) {
PGSQL * pgsql = PGSQL::Get(connection);
if (pgsql == NULL) {
return null_string;
}
String ret((data.size()*2)+1, ReserveString); // Reserve enough space as defined by PQescapeStringConn
int error = 0;
size_t size = PQescapeStringConn(pgsql->get(),
ret.get()->mutableData(), data.data(), data.size(),
&error);
if (error) {
return null_string;
}
ret.shrink(size); // Shrink to the returned size, `shrink` may re-alloc and free up space
return ret;
}
static String HHVM_FUNCTION(pg_unescape_bytea, const String& data) {
size_t to_len = 0;
char * unesc = (char *)PQunescapeBytea((unsigned char *)data.data(), &to_len);
String ret = String(unesc, to_len, CopyString);
PQfreemem(unesc);
return ret;
}
///////////// Command Execution / Querying /////////////////////////////
static int64_t HHVM_FUNCTION(pg_affected_rows, CResRef result) {
PGresult *res = PGSQLResult::GetResult(result);
return (int64_t)PQcmdTuples(res);
}
static Variant HHVM_FUNCTION(pg_result_status, CResRef result, int64_t type /* = PGSQL_STATUS_LONG */) {
PGresult *res = PGSQLResult::GetResult(result);
if (type == PGSQL_STATUS_LONG) {
return (int64_t)PQresultStatus(res);
} else {
String ret(PQcmdStatus(res), CopyString);
return ret;
}
}
static bool HHVM_FUNCTION(pg_free_result, CResRef result) {
PGSQLResult *res = PGSQLResult::Get(result);
if (res) {
res->close();
return true;
} else {
return false;
}
}
static Variant HHVM_FUNCTION(pg_query, CResRef connection, const String& query) {
PGSQL *conn = PGSQL::Get(connection);
if (conn == NULL) {
return false;
}
PGresult *res = PQexec(conn->get(), query.data());
if (res == NULL) {
char * err = PQerrorMessage(conn->get());
raise_warning("pg_query(): Query Failed: %s", err);
return false;
} else {
int st = PQresultStatus(res);
switch (st) {
default:
break;
case PGRES_EMPTY_QUERY:
case PGRES_BAD_RESPONSE:
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
char * msg = PQresultErrorMessage(res);
raise_warning("pg_query(): Query Failed: %s", msg);
PQclear(res);
return false;
}
}
PGSQLResult *pgresult = NEWOBJ(PGSQLResult)(conn, res);
return Resource(pgresult);
}
static Variant HHVM_FUNCTION(pg_query_params, CResRef connection, const String& query, CArrRef params) {
PGSQL *conn = PGSQL::Get(connection);
if (conn == NULL) {
return false;
}
CStringArray str_array(params);
PGresult * res = PQexecParams(conn->get(), query.data(),
params.size(), NULL, str_array.data(), NULL, NULL, 0);
if (res == NULL) {
char * err = PQerrorMessage(conn->get());
raise_warning("pg_query_params(): Query Failed: %s", err);
return false;
} else {
int st = PQresultStatus(res);
switch (st) {
default:
break;
case PGRES_EMPTY_QUERY:
case PGRES_BAD_RESPONSE:
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
char * msg = PQresultErrorMessage(res);
raise_warning("pg_query_params(): Query Failed: %s", msg);
PQclear(res);
return false;
}
}
PGSQLResult *pgresult = NEWOBJ(PGSQLResult)(conn, res);
return Resource(pgresult);
}
static Variant HHVM_FUNCTION(pg_prepare, CResRef connection, const String& stmtname, const String& query) {
PGSQL *conn = PGSQL::Get(connection);
if (conn == NULL) {
return false;
}
PGresult *res = PQprepare(conn->get(), stmtname.data(), query.data(), 0, NULL);
if (res == NULL) {
char * err = PQerrorMessage(conn->get());
raise_warning("pg_prepare(): Query Failed: %s", err);
return false;
} else {
int st = PQresultStatus(res);
switch (st) {
default:
break;
case PGRES_EMPTY_QUERY:
case PGRES_BAD_RESPONSE:
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
char * msg = PQresultErrorMessage(res);
raise_warning("pg_prepare(): Query Failed: %s", msg);
PQclear(res);
return false;
}
}
PGSQLResult *pgres = NEWOBJ(PGSQLResult)(conn, res);
return Resource(pgres);
}
static Variant HHVM_FUNCTION(pg_execute, CResRef connection, const String& stmtname, CArrRef params) {
PGSQL *conn = PGSQL::Get(connection);
if (conn == NULL) {
return false;
}
CStringArray str_array(params);
PGresult *res = PQexecPrepared(conn->get(), stmtname.data(), params.size(),
str_array.data(), NULL, NULL, 0);
PGSQLResult *pgres = NEWOBJ(PGSQLResult)(conn, res);
return Resource(pgres);
}
static bool HHVM_FUNCTION(pg_send_query, CResRef connection, const String& query) {
PGSQL *conn = PGSQL::Get(connection);
if (conn == NULL) {
return false;
}
bool origBlock = conn->isNonBlocking();
conn->setNonBlocking();
PGresult *res;
bool empty = true;
while ((res = PQgetResult(conn->get()))) {
PQclear(res);
empty = false;
}
if (!empty) {
raise_notice("There are results on this connection."
" Call pg_get_result() until it returns FALSE");
}
if (PQsendQuery(conn->get(), query.data()) == 0) {
// TODO: Do persistent auto-reconnect
return false;
}
int ret;
while ((ret = PQflush(conn->get()))) {
if (ret == -1) {
raise_notice("Could not empty PostgreSQL send buffer");
break;
}
usleep(5000);
}
conn->setNonBlocking(origBlock);
return true;
}
static Variant HHVM_FUNCTION(pg_get_result, CResRef connection) {
PGSQL *conn = PGSQL::Get(connection);
if (conn == NULL) {
return false;
}
PGresult *res = PQgetResult(conn->get());
if (res == NULL) {
return false;
}
PGSQLResult *pgresult = NEWOBJ(PGSQLResult)(conn, res);
return Resource(pgresult);
}
static bool HHVM_FUNCTION(pg_send_query_params, CResRef connection, const String& query, CArrRef params) {
PGSQL *conn = PGSQL::Get(connection);
if (conn == NULL) {
return false;
}
bool origBlock = conn->isNonBlocking();
conn->setNonBlocking();
PGresult *res;
bool empty = true;
while ((res = PQgetResult(conn->get()))) {
PQclear(res);
empty = false;
}
if (!empty) {
raise_notice("There are results on this connection."
" Call pg_get_result() until it returns FALSE");
}
CStringArray str_array(params);
if (PQsendQueryParams(conn->get(), query.data(),
params.size(), NULL, str_array.data(), NULL, NULL, 0) == 0) {
return false;
}
int ret;
while ((ret = PQflush(conn->get()))) {
if (ret == -1) {
raise_notice("Could not empty PostgreSQL send buffer");
break;
}
usleep(5000);
}