-
Notifications
You must be signed in to change notification settings - Fork 46
/
pgsql.cpp
1792 lines (1352 loc) · 45.1 KB
/
pgsql.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
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 <queue>
#include "pq.h"
#include "hphp/runtime/base/array-iterator.h"
#include "hphp/runtime/base/zend-string.h"
#include "hphp/runtime/base/runtime-option.h"
#include "hphp/runtime/server/server-stats.h"
#include "hphp/runtime/ext/string/ext_string.h"
#define PGSQL_ASSOC 1
#define PGSQL_NUM 2
#define PGSQL_BOTH (PGSQL_ASSOC | PGSQL_NUM)
#define PGSQL_STATUS_LONG 1
#define PGSQL_STATUS_STRING 2
#ifdef HACK_FRIENDLY
#define FAIL_RETURN return null_variant
#else
#define FAIL_RETURN return false
#endif
namespace HPHP {
namespace { // Anonymous namespace
struct ScopeNonBlocking {
ScopeNonBlocking(PQ::Connection& conn, bool mode) :
m_conn(conn), m_mode(mode) {}
~ScopeNonBlocking() {
m_conn.setNonBlocking(m_mode);
}
PQ::Connection& m_conn;
bool m_mode;
};
class PGSQLConnectionPool;
static class PGSQLConnectionPoolContainer {
private:
std::map<std::string, PGSQLConnectionPool*> m_pools;
Mutex m_lock;
public:
PGSQLConnectionPoolContainer();
PGSQLConnectionPoolContainer(PGSQLConnectionPoolContainer const&);
void operator=(PGSQLConnectionPoolContainer const&);
~PGSQLConnectionPoolContainer();
PGSQLConnectionPool& GetPool(const std::string);
std::vector<PGSQLConnectionPool *> &GetPools();
} s_connectionPoolContainer;
class PGSQLConnectionPool {
private:
int m_maximumConnections;
Mutex m_lock;
std::string m_connectionString;
std::string m_cleanedConnectionString;
std::queue<PQ::Connection*> m_availableConnections;
std::vector<PQ::Connection*> m_connections;
long m_sweepedConnections = 0;
long m_openedConnections = 0;
long m_requestedConnections = 0;
long m_releasedConnections = 0;
long m_errors = 0;
public:
long SweepedConnections() const { return m_sweepedConnections; }
long OpenedConnections() const { return m_openedConnections; }
long RequestedConnections() const { return m_requestedConnections; }
long ReleasedConnections() const { return m_releasedConnections; }
long Errors() const { return m_errors; }
int TotalConnectionsCount() const { return m_connections.size(); }
int FreeConnectionsCount() const { return m_availableConnections.size(); }
PGSQLConnectionPool(std::string connectionString, int maximumConnections = -1);
~PGSQLConnectionPool();
PQ::Connection& GetConnection();
void Release(PQ::Connection& connection);
std::string GetConnectionString() const { return m_connectionString; }
std::string GetCleanedConnectionString() const { return m_cleanedConnectionString; }
void CloseAllConnections();
void CloseFreeConnections();
int MaximumConnections() const { return m_maximumConnections; }
void SweepConnection(PQ::Connection& connection);
};
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 req::ptr<PGSQL> Get(const Variant& conn_id);
public:
PGSQL(String conninfo);
PGSQL(PGSQLConnectionPool& connectionPool);
~PGSQL();
void ReleaseConnection();
static StaticString s_class_name;
virtual const String& o_getClassNameHook() const { return s_class_name; }
virtual bool isResource() const { return m_conn != nullptr; }
PQ::Connection &get() { return *m_conn; }
ScopeNonBlocking asNonBlocking() {
auto mode = m_conn->isNonBlocking();
return ScopeNonBlocking(*m_conn, mode);
}
bool IsConnectionPooled() const { return m_connectionPool != nullptr; }
private:
PQ::Connection* m_conn;
PGSQLConnectionPool* m_connectionPool = nullptr;
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;
void SetupInformation();
};
class PGSQLResult : public SweepableResourceData {
DECLARE_RESOURCE_ALLOCATION(PGSQLResult);
public:
static req::ptr<PGSQLResult> Get(const Variant& result);
public:
PGSQLResult(req::ptr<PGSQL> conn, PQ::Result res);
~PGSQLResult();
static StaticString s_class_name;
virtual const String& o_getClassNameHook() const { return s_class_name; }
virtual bool isResource() const { return (bool)m_res; }
void close();
PQ::Result& get() { return m_res; }
int getFieldNumber(const Variant& field);
int getNumFields();
int getNumRows();
bool convertFieldRow(const Variant& row, const Variant& field,
int *out_row, int *out_field, const char *fn_name = nullptr);
Variant fieldIsNull(const Variant& row, const Variant& field, const char *fn_name = nullptr);
Variant getFieldVal(const Variant& row, const Variant& field, const char *fn_name = nullptr);
String getFieldVal(int row, int field, const char *fn_name = nullptr);
req::ptr<PGSQL> getConn() { return m_conn; }
public:
int m_current_row;
private:
PQ::Result m_res;
int m_num_fields;
int m_num_rows;
req::ptr<PGSQL> m_conn;
};
}
//////////////////////////////////////////////////////////////////////////////////
StaticString PGSQL::s_class_name("pgsql connection");
StaticString PGSQLResult::s_class_name("pgsql result");
req::ptr<PGSQL> PGSQL::Get(const Variant& conn_id) {
if (conn_id.isNull()) {
return nullptr;
}
req::ptr<PGSQL> pgsql = dyn_cast_or_null<PGSQL>(conn_id.toResource());
return pgsql;
}
static void notice_processor(PGSQL *pgsql, const char *message) {
if (pgsql != nullptr) {
pgsql->m_last_notice = message;
if (PGSQL::LogNotice) {
raise_notice("%s", message);
}
}
}
void PGSQL::SetupInformation()
{
if (!m_conn) return;
m_db = m_conn->db();
m_user = m_conn->user();
m_pass = m_conn->pass();
m_host = m_conn->host();
m_port = m_conn->port();
m_options = m_conn->options();
if (!PGSQL::IgnoreNotice) {
m_conn->setNoticeProcessor(notice_processor, this);
} else {
m_conn->setNoticeProcessor<PGSQL>(notice_processor, nullptr);
}
}
PGSQL::PGSQL(String conninfo)
: m_conn_string(conninfo.data()), m_last_notice("") {
m_conn = new PQ::Connection(conninfo.data());
if (RuntimeOption::EnableStats && RuntimeOption::EnableSQLStats) {
ServerStats::Log("sql.conn", 1);
}
ConnStatusType st = m_conn->status();
if (m_conn && st == CONNECTION_OK) {
// Load up the fixed information
SetupInformation();
} else if (st == CONNECTION_BAD) {
m_conn->finish();
}
}
PGSQL::PGSQL(PGSQLConnectionPool &connectionPool)
: m_conn_string(connectionPool.GetConnectionString()),
m_last_notice("")
{
m_conn = &(connectionPool.GetConnection());
m_connectionPool = &connectionPool;
SetupInformation();
}
PGSQL::~PGSQL() {
ReleaseConnection();
}
void PGSQL::sweep() {
ReleaseConnection();
}
void PGSQL::ReleaseConnection()
{
if (!m_conn) return;
if (!IsConnectionPooled())
{
m_conn->finish();
}
else
{
m_connectionPool->Release(*m_conn);
m_connectionPool = nullptr;
m_conn = nullptr;
}
}
req::ptr<PGSQLResult> PGSQLResult::Get(const Variant& result) {
if (result.isNull()) {
return nullptr;
}
req::ptr<PGSQLResult> res = dyn_cast_or_null<PGSQLResult>(result.toResource());
return res;
}
PGSQLResult::PGSQLResult(req::ptr<PGSQL> conn, PQ::Result res)
: m_current_row(0), m_res(std::move(res)),
m_num_fields(-1), m_num_rows(-1), m_conn(conn) {
m_conn->incRefCount();
}
void PGSQLResult::close() {
m_res.clear();
}
PGSQLResult::~PGSQLResult() {
close();
}
void PGSQLResult::sweep() {
close();
}
int PGSQLResult::getFieldNumber(const Variant& field) {
int n;
if (field.isNumeric(true)) {
n = field.toInt32();
} else if (field.isString()){
n = m_res.fieldNumber(field.asCStrRef().data());
} else {
n = -1;
}
return n;
}
int PGSQLResult::getNumFields() {
if (m_num_fields == -1) {
m_num_fields = m_res.numFields();
}
return m_num_fields;
}
int PGSQLResult::getNumRows() {
if (m_num_rows == -1) {
m_num_rows = m_res.numTuples();
}
return m_num_rows;
}
bool PGSQLResult::convertFieldRow(const Variant& row, const Variant& 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) {
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(const Variant& row, const Variant& field, const char *fn_name) {
int r, f;
if (convertFieldRow(row, field, &r, &f, fn_name)) {
return m_res.fieldIsNull(r, f) ? 1 : 0;
}
return false;
}
Variant PGSQLResult::getFieldVal(const Variant& row, const Variant& 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 (m_res.fieldIsNull(row, field)) {
return null_string;
} else {
char * value = m_res.getValue(row, field);
int length = m_res.getLength(row, field);
return String(value, length, CopyString);
}
}
//////////////////////////////////////////////////////////////////////////////////
PGSQLConnectionPool::PGSQLConnectionPool(std::string connectionString, int maximumConnections)
:m_maximumConnections(maximumConnections),
m_connectionString(connectionString),
m_availableConnections(),
m_connections()
{
}
PGSQLConnectionPool::~PGSQLConnectionPool()
{
CloseAllConnections();
}
PQ::Connection& PGSQLConnectionPool::GetConnection()
{
Lock lock(m_lock);
// 1) m_availableConnections
// 2) newconn, max 1
m_requestedConnections++;
while (!m_availableConnections.empty())
{
PQ::Connection* pconn = m_availableConnections.front();
PQ::Connection& conn = *pconn;
m_availableConnections.pop();
ConnStatusType st = conn.status();
if (conn && st == CONNECTION_OK)
{
return conn;
}
else if (st == CONNECTION_BAD)
{
SweepConnection(conn);
conn.finish();
};
}
if (RuntimeOption::EnableStats && RuntimeOption::EnableSQLStats) {
ServerStats::Log("sql.conn", 1);
}
int maxConnections = MaximumConnections();
int connections = m_connections.size();
if (maxConnections > 0 && connections < maxConnections)
raise_error("The connection pool is full, cannot open new connection.");
PQ::Connection& conn = *(new PQ::Connection(GetConnectionString()));
ConnStatusType st = conn.status();
if (st == CONNECTION_OK)
{
m_openedConnections++;
m_connections.push_back(&conn);
}
else if (st == CONNECTION_BAD)
{
m_errors++;
conn.finish();
raise_error("Getting connection from pool failed.");
}
if (m_cleanedConnectionString == "")
{
m_cleanedConnectionString.append("host=");
m_cleanedConnectionString.append(conn.host());
m_cleanedConnectionString.append(" port=");
m_cleanedConnectionString.append(conn.port());
m_cleanedConnectionString.append(" user=");
m_cleanedConnectionString.append(conn.user());
m_cleanedConnectionString.append(" dbname=");
m_cleanedConnectionString.append(conn.db());
}
return conn;
}
void PGSQLConnectionPool::SweepConnection(PQ::Connection& connection)
{
auto p = std::find(m_connections.begin(), m_connections.end(), &connection);
if (p != m_connections.end())
m_connections.erase(p);
m_sweepedConnections++;
}
void PGSQLConnectionPool::Release(PQ::Connection& connection)
{
Lock lock(m_lock);
m_releasedConnections++;
ConnStatusType st = connection.status();
if (connection && st == CONNECTION_OK) {
m_availableConnections.push(&connection);
} else if (st == CONNECTION_BAD) {
connection.finish();
SweepConnection(connection);
}
}
void PGSQLConnectionPool::CloseAllConnections()
{
Lock lock(m_lock);
while (!m_availableConnections.empty())
{
PQ::Connection* pconn = m_availableConnections.front();
pconn->finish();
m_availableConnections.pop();
}
for (PQ::Connection* conn : m_connections)
conn->finish();
m_connections.clear();
}
void PGSQLConnectionPool::CloseFreeConnections()
{
Lock lock(m_lock);
while (!m_availableConnections.empty())
{
PQ::Connection* pconn = m_availableConnections.front();
pconn->finish();
m_availableConnections.pop();
}
}
PGSQLConnectionPoolContainer::PGSQLConnectionPoolContainer()
:m_pools() {
}
PGSQLConnectionPoolContainer::~PGSQLConnectionPoolContainer() {
for (auto & any : m_pools) {
PGSQLConnectionPool* pool = any.second;
pool->CloseAllConnections();
}
}
PGSQLConnectionPool& PGSQLConnectionPoolContainer::GetPool(const std::string connString)
{
Lock lock(m_lock);
auto pool = m_pools[connString];
if (!pool)
{
pool = new PGSQLConnectionPool(connString);
m_pools[connString] = pool;
}
return *pool;
}
std::vector<PGSQLConnectionPool*>& PGSQLConnectionPoolContainer::GetPools()
{
Lock lock(m_lock);
std::vector<PGSQLConnectionPool*>* v = new std::vector<PGSQLConnectionPool*>();
for (auto it : m_pools)
{
v->push_back(it.second);
}
return *v;
}
//////////////////////////////////////////////////////////////////////////////////
// Simple RAII helper to convert an array 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(const Array& arr) {
int size = arr.size();
m_strings.reserve(size);
m_c_strs.reserve(size);
for (ArrayIter iter(arr); iter; ++iter) {
const Variant ¶m = iter.secondRef();
if (param.isNull()) {
m_strings.push_back(null_string);
m_c_strs.push_back(nullptr);
} else {
m_strings.push_back(param.toString());
m_c_strs.push_back(m_strings.back().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 */) {
auto pgsql = req::make<PGSQL>(connection_string);
if (!pgsql->get()) {
FAIL_RETURN;
}
return Resource(pgsql);
}
static Variant HHVM_FUNCTION(pg_pconnect, const String& connection_string, int connect_type /* = 0 */) {
PGSQLConnectionPool& pool = s_connectionPoolContainer.GetPool(connection_string.toCppString());
auto pgsql = req::make<PGSQL>(pool);
if (!pgsql->get()) {
FAIL_RETURN;
}
return Resource(pgsql);
}
static bool HHVM_FUNCTION(pg_close, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (pgsql) {
pgsql->ReleaseConnection();
return true;
} else {
return false;
}
}
static bool HHVM_FUNCTION(pg_ping, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql->get()) {
return false;
}
PGPing response = PQping(pgsql->m_conn_string.data());
if (response == PQPING_OK) {
if (pgsql->get().status() == CONNECTION_BAD) {
pgsql->get().reset();
return pgsql->get().status() != CONNECTION_BAD;
} else {
return true;
}
}
return false;
}
static bool HHVM_FUNCTION(pg_connection_reset, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql->get()) {
return false;
}
pgsql->get().reset();
return pgsql->get().status() != CONNECTION_BAD;
}
//////////////////// Connection Pool functions /////////////////////////
const StaticString
s_connection_string("connection_string"),
s_sweeped_connections("sweeped_connections"),
s_opened_connections("opened_connections"),
s_requested_connections("requested_connections"),
s_released_connections("released_connections"),
s_errors("errors"),
s_total_connections("total_connections"),
s_free_connections("free_connections");
static Variant HHVM_FUNCTION(pg_connection_pool_stat) {
auto pools = s_connectionPoolContainer.GetPools();
Array arr;
int i = 0;
for (auto pool : pools)
{
Array poolArr;
String poolName(pool->GetCleanedConnectionString().c_str(), CopyString);
poolArr.set(s_connection_string, poolName);
poolArr.set(s_sweeped_connections, pool->SweepedConnections());
poolArr.set(s_opened_connections, pool->OpenedConnections());
poolArr.set(s_requested_connections, pool->RequestedConnections());
poolArr.set(s_released_connections, pool->ReleasedConnections());
poolArr.set(s_errors, pool->Errors());
poolArr.set(s_total_connections, pool->TotalConnectionsCount());
poolArr.set(s_free_connections, pool->FreeConnectionsCount());
arr.set(i, poolArr);
i++;
}
return arr;
}
static void HHVM_FUNCTION(pg_connection_pool_sweep_free) {
auto pools = s_connectionPoolContainer.GetPools();
for (auto pool : pools)
{
pool->CloseFreeConnections();
}
}
///////////// Interrogation Functions ////////////////////
static int64_t HHVM_FUNCTION(pg_connection_status, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) return CONNECTION_BAD;
return (int64_t)pgsql->get().status();
}
static bool HHVM_FUNCTION(pg_connection_busy, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
return false;
}
auto blocking = pgsql->asNonBlocking();
pgsql->get().consumeInput();
return pgsql->get().isBusy();
}
static Variant HHVM_FUNCTION(pg_dbname, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
FAIL_RETURN;
}
return pgsql->m_db;
}
static Variant HHVM_FUNCTION(pg_host, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
FAIL_RETURN;
}
return pgsql->m_host;
}
static Variant HHVM_FUNCTION(pg_port, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
FAIL_RETURN;
}
String ret = pgsql->m_port;
if (ret.isNumeric()) {
return ret.toInt32();
} else {
return ret;
}
}
static Variant HHVM_FUNCTION(pg_options, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
FAIL_RETURN;
}
return pgsql->m_options;
}
static Variant HHVM_FUNCTION(pg_parameter_status, const Resource& connection, const String& param_name) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
return false;
}
String ret(pgsql->get().parameterStatus(param_name.data()), CopyString);
return ret;
}
static Variant HHVM_FUNCTION(pg_client_encoding, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
FAIL_RETURN;
}
String ret(pgsql->get().clientEncoding(), CopyString);
return ret;
}
static int64_t HHVM_FUNCTION(pg_transaction_status, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
return PQTRANS_UNKNOWN;
}
return (int64_t)pgsql->get().transactionStatus();
}
static Variant HHVM_FUNCTION(pg_last_error, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
FAIL_RETURN;
}
String ret(pgsql->get().errorMessage(), CopyString);
return f_trim(ret);
}
static Variant HHVM_FUNCTION(pg_last_notice, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
FAIL_RETURN;
}
return pgsql->m_last_notice;
}
static Variant HHVM_FUNCTION(pg_version, const Resource& connection) {
static StaticString client_key("client");
static StaticString protocol_key("protocol");
static StaticString server_key("server");
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
FAIL_RETURN;
}
Array ret;
int proto_ver = pgsql->get().protocolVersion();
if (proto_ver) {
ret.set(protocol_key, String(proto_ver) + ".0");
}
int server_ver = pgsql->get().serverVersion();
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, const Resource& connection) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
return -1;
}
return (int64_t)pgsql->get().backendPID();
}
//////////////// Escaping Functions ///////////////////////////
static String HHVM_FUNCTION(pg_escape_bytea, const Resource& connection, const String& data) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
return null_string;
}
std::string escaped = pgsql->get().escapeByteA(data.data(), data.size());
if (escaped.empty()) {
raise_warning("pg_escape_bytea(): %s", pgsql->get().errorMessage());
return null_string;
}
String ret(escaped);
return ret;
}
static String HHVM_FUNCTION(pg_escape_identifier, const Resource& connection, const String& data) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
return null_string;
}
std::string escaped = pgsql->get().escapeIdentifier(data.data(), data.size());
if (escaped.empty()) {
raise_warning("pg_escape_identifier(): %s", pgsql->get().errorMessage());
return null_string;
}
String ret(escaped);
return ret;
}
static String HHVM_FUNCTION(pg_escape_literal, const Resource& connection, const String& data) {
auto pgsql = PGSQL::Get(connection);
if (!pgsql) {
return null_string;
}