-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathpostgresql.cc
More file actions
973 lines (839 loc) · 45.1 KB
/
postgresql.cc
File metadata and controls
973 lines (839 loc) · 45.1 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// A libpq-based PostgreSQL driver for ADBC.
// For #warning Please include winsock2.h before windows.h on RTools/msys2
#ifdef _WIN32
#include <winsock2.h>
#endif
#include <cstring>
#include <memory>
#include <arrow-adbc/adbc.h>
#include "connection.h"
#include "database.h"
#include "driver/common/utils.h"
#include "driver/framework/status.h"
#include "statement.h"
using adbc::driver::Status;
using adbcpq::PostgresConnection;
using adbcpq::PostgresDatabase;
using adbcpq::PostgresStatement;
// ---------------------------------------------------------------------
// ADBC interface implementation - as private functions so that these
// don't get replaced by the dynamic linker. If we implemented these
// under the Adbc* names, then in DriverInit, the linker may resolve
// functions to the address of the functions provided by the driver
// manager instead of our functions.
//
// We could also:
// - Play games with RTLD_DEEPBIND - but this doesn't work with ASan
// - Use __attribute__((visibility("protected"))) - but this is
// apparently poorly supported by some linkers
// - Play with -Bsymbolic(-functions) - but this has other
// consequences and complicates the build setup
//
// So in the end some manual effort here was chosen.
// ---------------------------------------------------------------------
// AdbcError
namespace {
const struct AdbcError* PostgresErrorFromArrayStream(struct ArrowArrayStream* stream,
AdbcStatusCode* status) {
// Currently only valid for TupleReader
return adbcpq::TupleReader::ErrorFromArrayStream(stream, status);
}
int PostgresErrorGetDetailCount(const struct AdbcError* error) {
if (InternalAdbcIsCommonError(error)) {
return InternalAdbcCommonErrorGetDetailCount(error);
}
if (error->vendor_code != ADBC_ERROR_VENDOR_CODE_PRIVATE_DATA) {
return 0;
}
auto error_obj = reinterpret_cast<Status*>(error->private_data);
return error_obj->CDetailCount();
}
struct AdbcErrorDetail PostgresErrorGetDetail(const struct AdbcError* error, int index) {
if (InternalAdbcIsCommonError(error)) {
return InternalAdbcCommonErrorGetDetail(error, index);
}
auto error_obj = reinterpret_cast<Status*>(error->private_data);
return error_obj->CDetail(index);
}
} // namespace
#if !defined(ADBC_NO_COMMON_ENTRYPOINTS)
int AdbcErrorGetDetailCount(const struct AdbcError* error) {
return PostgresErrorGetDetailCount(error);
}
struct AdbcErrorDetail AdbcErrorGetDetail(const struct AdbcError* error, int index) {
return PostgresErrorGetDetail(error, index);
}
const struct AdbcError* AdbcErrorFromArrayStream(struct ArrowArrayStream* stream,
AdbcStatusCode* status) {
return PostgresErrorFromArrayStream(stream, status);
}
#endif // ADBC_NO_COMMON_ENTRYPOINTS
// ---------------------------------------------------------------------
// AdbcDatabase
namespace {
AdbcStatusCode PostgresDatabaseInit(struct AdbcDatabase* database,
struct AdbcError* error) {
if (!database || !database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
return (*ptr)->Init(error);
}
AdbcStatusCode PostgresDatabaseNew(struct AdbcDatabase* database,
struct AdbcError* error) {
if (!database) {
InternalAdbcSetError(error, "%s", "[libpq] database must not be null");
return ADBC_STATUS_INVALID_STATE;
}
if (database->private_data) {
InternalAdbcSetError(error, "%s", "[libpq] database is already initialized");
return ADBC_STATUS_INVALID_STATE;
}
auto impl = std::make_shared<PostgresDatabase>();
database->private_data = new std::shared_ptr<PostgresDatabase>(impl);
return ADBC_STATUS_OK;
}
AdbcStatusCode PostgresDatabaseRelease(struct AdbcDatabase* database,
struct AdbcError* error) {
if (!database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
AdbcStatusCode status = (*ptr)->Release(error);
delete ptr;
database->private_data = nullptr;
return status;
}
AdbcStatusCode PostgresDatabaseGetOption(struct AdbcDatabase* database, const char* key,
char* value, size_t* length,
struct AdbcError* error) {
if (!database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
return (*ptr)->GetOption(key, value, length, error);
}
AdbcStatusCode PostgresDatabaseGetOptionBytes(struct AdbcDatabase* database,
const char* key, uint8_t* value,
size_t* length, struct AdbcError* error) {
if (!database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
return (*ptr)->GetOptionBytes(key, value, length, error);
}
AdbcStatusCode PostgresDatabaseGetOptionDouble(struct AdbcDatabase* database,
const char* key, double* value,
struct AdbcError* error) {
if (!database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
return (*ptr)->GetOptionDouble(key, value, error);
}
AdbcStatusCode PostgresDatabaseGetOptionInt(struct AdbcDatabase* database,
const char* key, int64_t* value,
struct AdbcError* error) {
if (!database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
return (*ptr)->GetOptionInt(key, value, error);
}
AdbcStatusCode PostgresDatabaseSetOption(struct AdbcDatabase* database, const char* key,
const char* value, struct AdbcError* error) {
if (!database || !database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
return (*ptr)->SetOption(key, value, error);
}
AdbcStatusCode PostgresDatabaseSetOptionBytes(struct AdbcDatabase* database,
const char* key, const uint8_t* value,
size_t length, struct AdbcError* error) {
if (!database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
return (*ptr)->SetOptionBytes(key, value, length, error);
}
AdbcStatusCode PostgresDatabaseSetOptionDouble(struct AdbcDatabase* database,
const char* key, double value,
struct AdbcError* error) {
if (!database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
return (*ptr)->SetOptionDouble(key, value, error);
}
AdbcStatusCode PostgresDatabaseSetOptionInt(struct AdbcDatabase* database,
const char* key, int64_t value,
struct AdbcError* error) {
if (!database->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr = reinterpret_cast<std::shared_ptr<PostgresDatabase>*>(database->private_data);
return (*ptr)->SetOptionInt(key, value, error);
}
} // namespace
#if !defined(ADBC_NO_COMMON_ENTRYPOINTS)
AdbcStatusCode AdbcDatabaseGetOption(struct AdbcDatabase* database, const char* key,
char* value, size_t* length,
struct AdbcError* error) {
return PostgresDatabaseGetOption(database, key, value, length, error);
}
AdbcStatusCode AdbcDatabaseGetOptionBytes(struct AdbcDatabase* database, const char* key,
uint8_t* value, size_t* length,
struct AdbcError* error) {
return PostgresDatabaseGetOptionBytes(database, key, value, length, error);
}
AdbcStatusCode AdbcDatabaseGetOptionInt(struct AdbcDatabase* database, const char* key,
int64_t* value, struct AdbcError* error) {
return PostgresDatabaseGetOptionInt(database, key, value, error);
}
AdbcStatusCode AdbcDatabaseGetOptionDouble(struct AdbcDatabase* database, const char* key,
double* value, struct AdbcError* error) {
return PostgresDatabaseGetOptionDouble(database, key, value, error);
}
AdbcStatusCode AdbcDatabaseInit(struct AdbcDatabase* database, struct AdbcError* error) {
return PostgresDatabaseInit(database, error);
}
AdbcStatusCode AdbcDatabaseNew(struct AdbcDatabase* database, struct AdbcError* error) {
return PostgresDatabaseNew(database, error);
}
AdbcStatusCode AdbcDatabaseRelease(struct AdbcDatabase* database,
struct AdbcError* error) {
return PostgresDatabaseRelease(database, error);
}
AdbcStatusCode AdbcDatabaseSetOption(struct AdbcDatabase* database, const char* key,
const char* value, struct AdbcError* error) {
return PostgresDatabaseSetOption(database, key, value, error);
}
AdbcStatusCode AdbcDatabaseSetOptionBytes(struct AdbcDatabase* database, const char* key,
const uint8_t* value, size_t length,
struct AdbcError* error) {
return PostgresDatabaseSetOptionBytes(database, key, value, length, error);
}
AdbcStatusCode AdbcDatabaseSetOptionInt(struct AdbcDatabase* database, const char* key,
int64_t value, struct AdbcError* error) {
return PostgresDatabaseSetOptionInt(database, key, value, error);
}
AdbcStatusCode AdbcDatabaseSetOptionDouble(struct AdbcDatabase* database, const char* key,
double value, struct AdbcError* error) {
return PostgresDatabaseSetOptionDouble(database, key, value, error);
}
#endif // ADBC_NO_COMMON_ENTRYPOINTS
// ---------------------------------------------------------------------
// AdbcConnection
namespace {
AdbcStatusCode PostgresConnectionCancel(struct AdbcConnection* connection,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->Cancel(error);
}
AdbcStatusCode PostgresConnectionCommit(struct AdbcConnection* connection,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->Commit(error);
}
AdbcStatusCode PostgresConnectionGetInfo(struct AdbcConnection* connection,
const uint32_t* info_codes,
size_t info_codes_length,
struct ArrowArrayStream* stream,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetInfo(connection, info_codes, info_codes_length, stream, error);
}
AdbcStatusCode PostgresConnectionGetObjects(
struct AdbcConnection* connection, int depth, const char* catalog,
const char* db_schema, const char* table_name, const char** table_types,
const char* column_name, struct ArrowArrayStream* stream, struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetObjects(connection, depth, catalog, db_schema, table_name,
table_types, column_name, stream, error);
}
AdbcStatusCode PostgresConnectionGetOption(struct AdbcConnection* connection,
const char* key, char* value, size_t* length,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetOption(key, value, length, error);
}
AdbcStatusCode PostgresConnectionGetOptionBytes(struct AdbcConnection* connection,
const char* key, uint8_t* value,
size_t* length, struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetOptionBytes(key, value, length, error);
}
AdbcStatusCode PostgresConnectionGetOptionDouble(struct AdbcConnection* connection,
const char* key, double* value,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetOptionDouble(key, value, error);
}
AdbcStatusCode PostgresConnectionGetOptionInt(struct AdbcConnection* connection,
const char* key, int64_t* value,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetOptionInt(key, value, error);
}
AdbcStatusCode PostgresConnectionGetStatistics(struct AdbcConnection* connection,
const char* catalog, const char* db_schema,
const char* table_name, char approximate,
struct ArrowArrayStream* out,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetStatistics(catalog, db_schema, table_name, approximate == 1, out,
error);
}
AdbcStatusCode PostgresConnectionGetStatisticNames(struct AdbcConnection* connection,
struct ArrowArrayStream* out,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetStatisticNames(out, error);
}
AdbcStatusCode PostgresConnectionGetTableSchema(
struct AdbcConnection* connection, const char* catalog, const char* db_schema,
const char* table_name, struct ArrowSchema* schema, struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetTableSchema(catalog, db_schema, table_name, schema, error);
}
AdbcStatusCode PostgresConnectionGetTableTypes(struct AdbcConnection* connection,
struct ArrowArrayStream* stream,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->GetTableTypes(connection, stream, error);
}
AdbcStatusCode PostgresConnectionInit(struct AdbcConnection* connection,
struct AdbcDatabase* database,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->Init(database, error);
}
AdbcStatusCode PostgresConnectionNew(struct AdbcConnection* connection,
struct AdbcError* error) {
auto impl = std::make_shared<PostgresConnection>();
connection->private_data = new std::shared_ptr<PostgresConnection>(impl);
return ADBC_STATUS_OK;
}
AdbcStatusCode PostgresConnectionReadPartition(struct AdbcConnection* connection,
const uint8_t* serialized_partition,
size_t serialized_length,
struct ArrowArrayStream* out,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
return ADBC_STATUS_NOT_IMPLEMENTED;
}
AdbcStatusCode PostgresConnectionRelease(struct AdbcConnection* connection,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
AdbcStatusCode status = (*ptr)->Release(error);
delete ptr;
connection->private_data = nullptr;
return status;
}
AdbcStatusCode PostgresConnectionRollback(struct AdbcConnection* connection,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->Rollback(error);
}
AdbcStatusCode PostgresConnectionSetOption(struct AdbcConnection* connection,
const char* key, const char* value,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->SetOption(key, value, error);
}
AdbcStatusCode PostgresConnectionSetOptionBytes(struct AdbcConnection* connection,
const char* key, const uint8_t* value,
size_t length, struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->SetOptionBytes(key, value, length, error);
}
AdbcStatusCode PostgresConnectionSetOptionDouble(struct AdbcConnection* connection,
const char* key, double value,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->SetOptionDouble(key, value, error);
}
AdbcStatusCode PostgresConnectionSetOptionInt(struct AdbcConnection* connection,
const char* key, int64_t value,
struct AdbcError* error) {
if (!connection->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresConnection>*>(connection->private_data);
return (*ptr)->SetOptionInt(key, value, error);
}
} // namespace
#if !defined(ADBC_NO_COMMON_ENTRYPOINTS)
AdbcStatusCode AdbcConnectionCancel(struct AdbcConnection* connection,
struct AdbcError* error) {
return PostgresConnectionCancel(connection, error);
}
AdbcStatusCode AdbcConnectionCommit(struct AdbcConnection* connection,
struct AdbcError* error) {
return PostgresConnectionCommit(connection, error);
}
AdbcStatusCode AdbcConnectionGetInfo(struct AdbcConnection* connection,
const uint32_t* info_codes, size_t info_codes_length,
struct ArrowArrayStream* stream,
struct AdbcError* error) {
return PostgresConnectionGetInfo(connection, info_codes, info_codes_length, stream,
error);
}
AdbcStatusCode AdbcConnectionGetObjects(struct AdbcConnection* connection, int depth,
const char* catalog, const char* db_schema,
const char* table_name, const char** table_types,
const char* column_name,
struct ArrowArrayStream* stream,
struct AdbcError* error) {
return PostgresConnectionGetObjects(connection, depth, catalog, db_schema, table_name,
table_types, column_name, stream, error);
}
AdbcStatusCode AdbcConnectionGetOption(struct AdbcConnection* connection, const char* key,
char* value, size_t* length,
struct AdbcError* error) {
return PostgresConnectionGetOption(connection, key, value, length, error);
}
AdbcStatusCode AdbcConnectionGetOptionBytes(struct AdbcConnection* connection,
const char* key, uint8_t* value,
size_t* length, struct AdbcError* error) {
return PostgresConnectionGetOptionBytes(connection, key, value, length, error);
}
AdbcStatusCode AdbcConnectionGetOptionInt(struct AdbcConnection* connection,
const char* key, int64_t* value,
struct AdbcError* error) {
return PostgresConnectionGetOptionInt(connection, key, value, error);
}
AdbcStatusCode AdbcConnectionGetOptionDouble(struct AdbcConnection* connection,
const char* key, double* value,
struct AdbcError* error) {
return PostgresConnectionGetOptionDouble(connection, key, value, error);
}
AdbcStatusCode AdbcConnectionGetStatistics(struct AdbcConnection* connection,
const char* catalog, const char* db_schema,
const char* table_name, char approximate,
struct ArrowArrayStream* out,
struct AdbcError* error) {
return PostgresConnectionGetStatistics(connection, catalog, db_schema, table_name,
approximate, out, error);
}
AdbcStatusCode AdbcConnectionGetStatisticNames(struct AdbcConnection* connection,
struct ArrowArrayStream* out,
struct AdbcError* error) {
return PostgresConnectionGetStatisticNames(connection, out, error);
}
AdbcStatusCode AdbcConnectionGetTableSchema(struct AdbcConnection* connection,
const char* catalog, const char* db_schema,
const char* table_name,
struct ArrowSchema* schema,
struct AdbcError* error) {
return PostgresConnectionGetTableSchema(connection, catalog, db_schema, table_name,
schema, error);
}
AdbcStatusCode AdbcConnectionGetTableTypes(struct AdbcConnection* connection,
struct ArrowArrayStream* stream,
struct AdbcError* error) {
return PostgresConnectionGetTableTypes(connection, stream, error);
}
AdbcStatusCode AdbcConnectionInit(struct AdbcConnection* connection,
struct AdbcDatabase* database,
struct AdbcError* error) {
return PostgresConnectionInit(connection, database, error);
}
AdbcStatusCode AdbcConnectionNew(struct AdbcConnection* connection,
struct AdbcError* error) {
return PostgresConnectionNew(connection, error);
}
AdbcStatusCode AdbcConnectionReadPartition(struct AdbcConnection* connection,
const uint8_t* serialized_partition,
size_t serialized_length,
struct ArrowArrayStream* out,
struct AdbcError* error) {
return PostgresConnectionReadPartition(connection, serialized_partition,
serialized_length, out, error);
}
AdbcStatusCode AdbcConnectionRelease(struct AdbcConnection* connection,
struct AdbcError* error) {
return PostgresConnectionRelease(connection, error);
}
AdbcStatusCode AdbcConnectionRollback(struct AdbcConnection* connection,
struct AdbcError* error) {
return PostgresConnectionRollback(connection, error);
}
AdbcStatusCode AdbcConnectionSetOption(struct AdbcConnection* connection, const char* key,
const char* value, struct AdbcError* error) {
return PostgresConnectionSetOption(connection, key, value, error);
}
AdbcStatusCode AdbcConnectionSetOptionBytes(struct AdbcConnection* connection,
const char* key, const uint8_t* value,
size_t length, struct AdbcError* error) {
return PostgresConnectionSetOptionBytes(connection, key, value, length, error);
}
AdbcStatusCode AdbcConnectionSetOptionInt(struct AdbcConnection* connection,
const char* key, int64_t value,
struct AdbcError* error) {
return PostgresConnectionSetOptionInt(connection, key, value, error);
}
AdbcStatusCode AdbcConnectionSetOptionDouble(struct AdbcConnection* connection,
const char* key, double value,
struct AdbcError* error) {
return PostgresConnectionSetOptionDouble(connection, key, value, error);
}
#endif // ADBC_NO_COMMON_ENTRYPOINTS
// ---------------------------------------------------------------------
// AdbcStatement
namespace {
AdbcStatusCode PostgresStatementBind(struct AdbcStatement* statement,
struct ArrowArray* values,
struct ArrowSchema* schema,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->Bind(values, schema, error);
}
AdbcStatusCode PostgresStatementBindStream(struct AdbcStatement* statement,
struct ArrowArrayStream* stream,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->Bind(stream, error);
}
AdbcStatusCode PostgresStatementCancel(struct AdbcStatement* statement,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->Cancel(error);
}
AdbcStatusCode PostgresStatementExecutePartitions(struct AdbcStatement* statement,
struct ArrowSchema* schema,
struct AdbcPartitions* partitions,
int64_t* rows_affected,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
return ADBC_STATUS_NOT_IMPLEMENTED;
}
AdbcStatusCode PostgresStatementExecuteQuery(struct AdbcStatement* statement,
struct ArrowArrayStream* output,
int64_t* rows_affected,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->ExecuteQuery(output, rows_affected, error);
}
AdbcStatusCode PostgresStatementExecuteSchema(struct AdbcStatement* statement,
struct ArrowSchema* schema,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->ExecuteSchema(schema, error);
}
AdbcStatusCode PostgresStatementGetOption(struct AdbcStatement* statement,
const char* key, char* value, size_t* length,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->GetOption(key, value, length, error);
}
AdbcStatusCode PostgresStatementGetOptionBytes(struct AdbcStatement* statement,
const char* key, uint8_t* value,
size_t* length, struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->GetOptionBytes(key, value, length, error);
}
AdbcStatusCode PostgresStatementGetOptionDouble(struct AdbcStatement* statement,
const char* key, double* value,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->GetOptionDouble(key, value, error);
}
AdbcStatusCode PostgresStatementGetOptionInt(struct AdbcStatement* statement,
const char* key, int64_t* value,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->GetOptionInt(key, value, error);
}
AdbcStatusCode PostgresStatementGetParameterSchema(struct AdbcStatement* statement,
struct ArrowSchema* schema,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->GetParameterSchema(schema, error);
}
AdbcStatusCode PostgresStatementNew(struct AdbcConnection* connection,
struct AdbcStatement* statement,
struct AdbcError* error) {
auto impl = std::make_shared<PostgresStatement>();
statement->private_data = new std::shared_ptr<PostgresStatement>(impl);
return impl->New(connection, error);
}
AdbcStatusCode PostgresStatementPrepare(struct AdbcStatement* statement,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->Prepare(error);
}
AdbcStatusCode PostgresStatementRelease(struct AdbcStatement* statement,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
auto status = (*ptr)->Release(error);
delete ptr;
statement->private_data = nullptr;
return status;
}
AdbcStatusCode PostgresStatementSetOption(struct AdbcStatement* statement,
const char* key, const char* value,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->SetOption(key, value, error);
}
AdbcStatusCode PostgresStatementSetOptionBytes(struct AdbcStatement* statement,
const char* key, const uint8_t* value,
size_t length, struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->SetOptionBytes(key, value, length, error);
}
AdbcStatusCode PostgresStatementSetOptionDouble(struct AdbcStatement* statement,
const char* key, double value,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->SetOptionDouble(key, value, error);
}
AdbcStatusCode PostgresStatementSetOptionInt(struct AdbcStatement* statement,
const char* key, int64_t value,
struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->SetOptionInt(key, value, error);
}
AdbcStatusCode PostgresStatementSetSqlQuery(struct AdbcStatement* statement,
const char* query, struct AdbcError* error) {
if (!statement->private_data) return ADBC_STATUS_INVALID_STATE;
auto* ptr =
reinterpret_cast<std::shared_ptr<PostgresStatement>*>(statement->private_data);
return (*ptr)->SetSqlQuery(query, error);
}
} // namespace
#if !defined(ADBC_NO_COMMON_ENTRYPOINTS)
AdbcStatusCode AdbcStatementBind(struct AdbcStatement* statement,
struct ArrowArray* values, struct ArrowSchema* schema,
struct AdbcError* error) {
return PostgresStatementBind(statement, values, schema, error);
}
AdbcStatusCode AdbcStatementBindStream(struct AdbcStatement* statement,
struct ArrowArrayStream* stream,
struct AdbcError* error) {
return PostgresStatementBindStream(statement, stream, error);
}
AdbcStatusCode AdbcStatementCancel(struct AdbcStatement* statement,
struct AdbcError* error) {
return PostgresStatementCancel(statement, error);
}
AdbcStatusCode AdbcStatementExecutePartitions(struct AdbcStatement* statement,
ArrowSchema* schema,
struct AdbcPartitions* partitions,
int64_t* rows_affected,
struct AdbcError* error) {
return PostgresStatementExecutePartitions(statement, schema, partitions, rows_affected,
error);
}
AdbcStatusCode AdbcStatementExecuteQuery(struct AdbcStatement* statement,
struct ArrowArrayStream* output,
int64_t* rows_affected,
struct AdbcError* error) {
return PostgresStatementExecuteQuery(statement, output, rows_affected, error);
}
AdbcStatusCode AdbcStatementExecuteSchema(struct AdbcStatement* statement,
ArrowSchema* schema, struct AdbcError* error) {
return PostgresStatementExecuteSchema(statement, schema, error);
}
AdbcStatusCode AdbcStatementGetOption(struct AdbcStatement* statement, const char* key,
char* value, size_t* length,
struct AdbcError* error) {
return PostgresStatementGetOption(statement, key, value, length, error);
}
AdbcStatusCode AdbcStatementGetOptionBytes(struct AdbcStatement* statement,
const char* key, uint8_t* value,
size_t* length, struct AdbcError* error) {
return PostgresStatementGetOptionBytes(statement, key, value, length, error);
}
AdbcStatusCode AdbcStatementGetOptionInt(struct AdbcStatement* statement, const char* key,
int64_t* value, struct AdbcError* error) {
return PostgresStatementGetOptionInt(statement, key, value, error);
}
AdbcStatusCode AdbcStatementGetOptionDouble(struct AdbcStatement* statement,
const char* key, double* value,
struct AdbcError* error) {
return PostgresStatementGetOptionDouble(statement, key, value, error);
}
AdbcStatusCode AdbcStatementGetParameterSchema(struct AdbcStatement* statement,
struct ArrowSchema* schema,
struct AdbcError* error) {
return PostgresStatementGetParameterSchema(statement, schema, error);
}
AdbcStatusCode AdbcStatementNew(struct AdbcConnection* connection,
struct AdbcStatement* statement,
struct AdbcError* error) {
return PostgresStatementNew(connection, statement, error);
}
AdbcStatusCode AdbcStatementPrepare(struct AdbcStatement* statement,
struct AdbcError* error) {
return PostgresStatementPrepare(statement, error);
}
AdbcStatusCode AdbcStatementRelease(struct AdbcStatement* statement,
struct AdbcError* error) {
return PostgresStatementRelease(statement, error);
}
AdbcStatusCode AdbcStatementSetOption(struct AdbcStatement* statement, const char* key,
const char* value, struct AdbcError* error) {
return PostgresStatementSetOption(statement, key, value, error);
}
AdbcStatusCode AdbcStatementSetOptionBytes(struct AdbcStatement* statement,
const char* key, const uint8_t* value,
size_t length, struct AdbcError* error) {
return PostgresStatementSetOptionBytes(statement, key, value, length, error);
}
AdbcStatusCode AdbcStatementSetOptionInt(struct AdbcStatement* statement, const char* key,
int64_t value, struct AdbcError* error) {
return PostgresStatementSetOptionInt(statement, key, value, error);
}
AdbcStatusCode AdbcStatementSetOptionDouble(struct AdbcStatement* statement,
const char* key, double value,
struct AdbcError* error) {
return PostgresStatementSetOptionDouble(statement, key, value, error);
}
AdbcStatusCode AdbcStatementSetSqlQuery(struct AdbcStatement* statement,
const char* query, struct AdbcError* error) {
return PostgresStatementSetSqlQuery(statement, query, error);
}
#endif // ADBC_NO_COMMON_ENTRYPOINTS
extern "C" {
ADBC_EXPORT
AdbcStatusCode AdbcDriverPostgresqlInit(int version, void* raw_driver,
struct AdbcError* error) {
if (version != ADBC_VERSION_1_0_0 && version != ADBC_VERSION_1_1_0) {
return ADBC_STATUS_NOT_IMPLEMENTED;
}
if (!raw_driver) return ADBC_STATUS_INVALID_ARGUMENT;
auto* driver = reinterpret_cast<struct AdbcDriver*>(raw_driver);
if (version >= ADBC_VERSION_1_1_0) {
std::memset(driver, 0, ADBC_DRIVER_1_1_0_SIZE);
driver->ErrorGetDetailCount = PostgresErrorGetDetailCount;
driver->ErrorGetDetail = PostgresErrorGetDetail;
driver->ErrorFromArrayStream = PostgresErrorFromArrayStream;
driver->DatabaseGetOption = PostgresDatabaseGetOption;
driver->DatabaseGetOptionBytes = PostgresDatabaseGetOptionBytes;
driver->DatabaseGetOptionDouble = PostgresDatabaseGetOptionDouble;
driver->DatabaseGetOptionInt = PostgresDatabaseGetOptionInt;
driver->DatabaseSetOptionBytes = PostgresDatabaseSetOptionBytes;
driver->DatabaseSetOptionDouble = PostgresDatabaseSetOptionDouble;
driver->DatabaseSetOptionInt = PostgresDatabaseSetOptionInt;
driver->ConnectionCancel = PostgresConnectionCancel;
driver->ConnectionGetOption = PostgresConnectionGetOption;
driver->ConnectionGetOptionBytes = PostgresConnectionGetOptionBytes;
driver->ConnectionGetOptionDouble = PostgresConnectionGetOptionDouble;
driver->ConnectionGetOptionInt = PostgresConnectionGetOptionInt;
driver->ConnectionGetStatistics = PostgresConnectionGetStatistics;
driver->ConnectionGetStatisticNames = PostgresConnectionGetStatisticNames;
driver->ConnectionSetOptionBytes = PostgresConnectionSetOptionBytes;
driver->ConnectionSetOptionDouble = PostgresConnectionSetOptionDouble;
driver->ConnectionSetOptionInt = PostgresConnectionSetOptionInt;
driver->StatementCancel = PostgresStatementCancel;
driver->StatementExecuteSchema = PostgresStatementExecuteSchema;
driver->StatementGetOption = PostgresStatementGetOption;
driver->StatementGetOptionBytes = PostgresStatementGetOptionBytes;
driver->StatementGetOptionDouble = PostgresStatementGetOptionDouble;
driver->StatementGetOptionInt = PostgresStatementGetOptionInt;
driver->StatementSetOptionBytes = PostgresStatementSetOptionBytes;
driver->StatementSetOptionDouble = PostgresStatementSetOptionDouble;
driver->StatementSetOptionInt = PostgresStatementSetOptionInt;
} else {
std::memset(driver, 0, ADBC_DRIVER_1_0_0_SIZE);
}
driver->DatabaseInit = PostgresDatabaseInit;
driver->DatabaseNew = PostgresDatabaseNew;
driver->DatabaseRelease = PostgresDatabaseRelease;
driver->DatabaseSetOption = PostgresDatabaseSetOption;
driver->ConnectionCommit = PostgresConnectionCommit;
driver->ConnectionGetInfo = PostgresConnectionGetInfo;
driver->ConnectionGetObjects = PostgresConnectionGetObjects;
driver->ConnectionGetTableSchema = PostgresConnectionGetTableSchema;
driver->ConnectionGetTableTypes = PostgresConnectionGetTableTypes;
driver->ConnectionInit = PostgresConnectionInit;
driver->ConnectionNew = PostgresConnectionNew;
driver->ConnectionReadPartition = PostgresConnectionReadPartition;
driver->ConnectionRelease = PostgresConnectionRelease;
driver->ConnectionRollback = PostgresConnectionRollback;
driver->ConnectionSetOption = PostgresConnectionSetOption;
driver->StatementBind = PostgresStatementBind;
driver->StatementBindStream = PostgresStatementBindStream;
driver->StatementExecutePartitions = PostgresStatementExecutePartitions;
driver->StatementExecuteQuery = PostgresStatementExecuteQuery;
driver->StatementGetParameterSchema = PostgresStatementGetParameterSchema;
driver->StatementNew = PostgresStatementNew;
driver->StatementPrepare = PostgresStatementPrepare;
driver->StatementRelease = PostgresStatementRelease;
driver->StatementSetOption = PostgresStatementSetOption;
driver->StatementSetSqlQuery = PostgresStatementSetSqlQuery;
return ADBC_STATUS_OK;
}
#if !defined(ADBC_NO_COMMON_ENTRYPOINTS)
ADBC_EXPORT
AdbcStatusCode AdbcDriverInit(int version, void* raw_driver, struct AdbcError* error) {
return AdbcDriverPostgresqlInit(version, raw_driver, error);
}
#endif // ADBC_NO_COMMON_ENTRYPOINTS
}