-
Notifications
You must be signed in to change notification settings - Fork 1
/
pq.odin
1562 lines (1359 loc) · 78 KB
/
pq.odin
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
package pq
import "core:c/libc"
foreign import pq "system:pq"
// An opaque handle to a connection.
Conn :: distinct rawptr
// An opaque handle to a result.
//
// The result structure encapsulates the result returned by the server. libpq application
// programmers should be careful to maintain the result abstraction. Use the accessor functions
// to get at the contents. Avoid directly referencing the fields because they are subject to change.
Result :: distinct rawptr
// An opaque handle to a cancel object.
Cancel :: distinct rawptr
OID :: distinct u32
INVALID_OID: OID: 0
Encoding :: distinct i32
ENCODING_ERROR: Encoding: -1
SSL_Key_Pass_Hook :: #type proc "c" (buf: [^]byte, size: i32, conn: Conn) -> i32
// Called when a notice is retrieved from the server or generated internally by libpq.
// It is passed a `Result` in the state of `Non_Fatal_Error`. (This allows the receiver to extract
// individual fields using `result_error_field`, or obtain a complete preformatted message using
// `result_error_message` or `result_verbose_error_message`.) The same user pointer passed to
// `set_notice_receiver` is passed.
//
// The default notice receiver extracts the message using `result_error_message` and passes it on
// to the notice processor.
Notice_Receiver :: #type proc "c" (user: rawptr, res: Result)
// Responsible for handling a notice or warning message given in text form. It is passed the string
// text of the message (including a trailing newline), plus a user pointer from `set_notice_processor`.
//
// The default notice processor prints to stderr.
Notice_Processor :: #type proc "c" (user: rawptr, message: cstring)
Event_Proc :: #type proc "c" (evt_id: Event_ID, evt_info: rawptr, pass_through: rawptr) -> b32
Polling_Status :: enum i32 {
Failed,
Reading,
Writing,
Ok,
}
Connection_Status :: enum i32 {
Ok,
Bad,
// Below are only possible in nonblocking mode:
// Waiting for connection to be made.
Started,
// Connection OK; waiting to send.
Made,
// Waiting for a response from the server.
Awaiting_Response,
// Received authentication; waiting for backend start-up to finish.
Auth_OK,
// Negotiating environment-driven parameter settings.
Set_Env,
// Negotiating SSL encryption.
SSL_Startup,
// Internal state; connect() needed.
Needed,
// Checking if connection is able to handle write transactions.
Check_Writable,
// Consuming any remaining response messages on connection.
Consume,
// Negotiating GSSAPI.
GSS_Startup,
// Checking target server properties.
Check_Target,
// Checking if server is in standby mode.
Check_Standby,
}
Ping :: enum i32 {
// The server is running and appears to be accepting connections.
OK,
// The server is running but is in a state that disallows connections (startup, shutdown, or crash recovery).
Reject,
// The server could not be contacted. This might indicate that the server is not running,
// or that there is something wrong with the given connection parameters (for example, wrong port number),
// or that there is a network connectivity problem (for example, a firewall blocking the connection request).
No_Response,
// No attempt was made to contact the server, because the supplied parameters were obviously incorrect or there
// was some client-side problem (for example, out of memory).
No_Attempt,
}
Transaction_Status :: enum i32 {
Idle,
// Command in progress.
Active,
// Idle, within transaction block.
In_Trans,
// Idle, within failed transaction.
In_Error,
// Cannot determine status.
Unknown,
}
Pipeline_Status :: enum i32 {
// The pipeline mode is off.
Off,
// The pipeline mode is on.
On,
// The pipeline mode is on but is currently in an error state.
Aborted,
}
Exec_Status :: enum i32 {
// The string sent to the server was empty.
Empty_Query,
// Successful completion of a command returning no data.
Command_OK,
// Successful completion of a command returning data (such as `SELECT` or `SHOW`).
Tuples_OK,
// Copy Out (from server) data transfer started.
Copy_Out,
// Copy In (to server) data transfer started.
Copy_In,
// The server's response was not understood.
Bad_Response,
// A nonfatal error (a notice or warning) occurred.
//
// A result of `Non_Fatal_Error` will never be returned directly by `exec` or other query execution functions;
// results of this kind are instead passed to the notice processor.
Non_Fatal_Error,
// A fatal error occurred.
Fatal_Error,
// Copy in/out (to and from server) data transfer started. This is currently only for streaming replication.
Copy_Both,
// The `Result` contains a single result tuple from the current command.
// Only occurs in single row mode.
Single_Tuple,
// The `Result` represents a synchronization point in pipeline mode, requested by `pipeline_sync`.
// This status occurs only when pipeline mode has been selected.
Pipeline_Sync,
// The `Result` represents a pipeline that has received an error from the server.
// `get_result` must be called repeatedly, and each time it will return this status code until the end
// of the current pipeline, at which point it will return `Pipeline_Sync` and normal processing can resume.
Pipeline_Aborted,
}
Field_Code :: enum i32 {
// The severity; the field contents are `ERROR`, `FATAL` or `PANIC` (in an error message), or
// `WARNING`, `NOTICE`, `DEBUG`, `INFO`, or `LOG` (in a notice message), or a localized translation
// of one of these. Always present.
Severity = 'S',
// Severity, without localization.
Severity_Non_Localized = 'V',
// The code identifies the type of error that has occurred; it can be used by front-end applications
// to perform specific operations (such as error handling) in response to a particular database error.
// For a list of possible codes, see [[Appendix A; https://www.postgresql.org/docs/16/errcodes-appendix.html]].
// This field is not localizable, and is always present.
SQL_State = 'C',
// The primary human-readable error message (typically one line). Always present.
Message_Primary = 'M',
// An optional secondary error message carrying more detail about the problem. Might run to multiple lines.
Message_Detail = 'D',
// An optional suggestion what to do about the problem. This is intended to differ from detail in that it offers advice
// (potentially inappropriate) rather than hard facts. Might run to multiple lines.
Message_Hint = 'H',
// A string containing a decimal integer indicating an error cursor position as an index into the original statement string.
// The first character has index 1, and positions are measured in characters, not bytes.
Statement_Position = 'P',
// This is defined the same as `Statement_Position` but it is used when the cursor position refers to an internally
// generated command rather than the one submitted by the client. The `Internal_Query` field will always appear when this field appears.
Internal_Position = 'p',
// The text of failed internally-generated command. This could be, for example, a SQL query issued by a PL/pgSQL function.
Internal_Query = 'q',
// An indication of the context in which the error occurred. Presently this includes a call stack traceback of active
// procedural language functions and internally-generated queries. The trace is one entry per line, most recent first.
Context = 'W',
Schema_Name = 's',
Table_Name = 't',
Column_Name = 'c',
Data_Type_Name = 'd',
Constraint_Name = 'n',
// The file name of the source-code location where the error was reported.
Source_File = 'F',
// The line number of the source-code location where the error was reporte.
Source_Line = 'L',
// The name of the source-code function reporting the error.
Source_Function = 'R',
}
Format :: enum i32 {
Text = 0,
Binary = 1,
}
Flush_Result :: enum i32 {
Failure = -1,
Success = 0,
Would_Block = 1,
}
Nonblocking_Result :: enum i32 {
Failure = -1,
Success = 0,
}
Put_Copy_Result :: enum i32 {
// An error occurred, use `error_message` to retrieve details.
Failure = -1,
// Not queued because of full buffers (this will only happen in nonblocking mode).
Full_Buffers = 0,
// Data was queued.
Queued = 1,
}
Get_Copy_Result :: enum i32 {
// -2 is returned when an error occurred, consult `error_message` for the reason.
Failure = -2,
// -1 is returned when the `COPY` is done, call `get_result` to obtain the final result.
Done = -1,
// Zero is returned for in progress `COPY`'s when `async` is set to true.
In_Progress = 0,
// NOTE: Any other value is the amount of bytes that have been retrieved, to be cast to an int.
}
Set_Encoding_Result :: enum i32 {
Failure = -1,
Success = 0,
}
Verbosity :: enum i32 {
// Severity, primary text, and position, normally on a single line.
Terse,
// Terse + detail, hint, or context, might span multiple lines.
Default,
// All available fields.
Verbose,
// Severity and SQL_State error code if one is available (if not, the output is like .Terse mode).
SQL_State,
}
Context_Visibility :: enum i32 {
// Context field is never in messages.
Never,
// Context field is only in error messages.
Errors,
// Context field is in all messages.
Always,
}
Trace_Flag :: enum i32 {
// Don't include the timestamp.
Suppress_Timestamp,
// Redact some fields, such as object OIDs so the output is more convenient for testing frameworks.
Regress_Mode,
}
Trace_Flags :: bit_set[Trace_Flag; i32]
Result_Copy_Flag :: enum i32 {
// Copy the source result's attributes (column definitions).
Attrs,
// Copy the source result's tuples (implies copying the attributes).
Tuples,
// Copy the source result's events. (But any instance data associated with the source is not copied.)
Events,
// Copy the source result's notify hooks.
Notice_Hooks,
}
Result_Copy_Flags :: bit_set[Result_Copy_Flag; i32]
Res_Att_Desc :: struct {
// Column name.
name: cstring,
// Source table, if known.
table_id: OID,
// Source column, if known,
column_id: i32,
// Format code for value.
format: Format,
type_id: OID,
// Type size.
type_len: i32,
// type-specific modifier info.
atttypmod: i32,
}
Conninfo_Option :: struct {
// The keyword of the option.
keyword: cstring,
// Fallback environment variable name.
env_var: cstring,
// Fallback compiled in default name.
compiled: cstring,
// Option's current value, or nil.
val: cstring,
// Label for field in connect dialog.
label: cstring,
// Indicates how to display this field in a connect dialog. Values are:
// "" Display entered value as is
// "*" Password field - hide value
// "D" Debug option - don't show by default
disp_char: cstring,
// Field size in characters for dialog.
disp_size: i32,
}
pqbool :: i8
Print_Opt :: struct {
// Print output field headings and row count.
header: pqbool,
// Fill align the fields.
align: pqbool,
// Old brain dead format.
standard: pqbool,
// Output HTML tables.
html3: pqbool,
// Expand tables.
expanded: pqbool,
// Use pager for output if needed.
pager: pqbool,
// Field separator.
field_sep: cstring,
// Attributes for HTML table element.
table_opt: cstring,
// HTML table caption.
caption: cstring,
// null-terminated array of replacement field names.
field_names: [^]cstring,
}
Notify :: struct {
// Notification channel name.
relname: cstring,
// Process ID of notifying server process.
be_pid: i32,
// Notification payload string.
extra: cstring,
// Private to libpq, do not use.
_next: ^Notify,
}
Event_ID :: enum i32 {
// The register event occurs when `register_event_proc` is called. It is the ideal time to
// initialize any `instance_data` an event procedure may need. Only one register event will
// be fired per event handler connection. If the event procedure fails (returns 0), the registration is cancelled.
// If the event procedure returns false the registration is aborted.
// The `evt_info` should be cast to `Event_Register`.
Register,
// The connection reset event is fired on completion of `reset` or `reset_poll`. In both cases
// the event is only fired if the reset was successful. The return value of the event procedure
// is ignored in PostgreSQL 15 and later. With earlier versions, however, it's important to return
// success or the connection will be aborted.
// The `evt_info` should be cast to `Event_Conn_Reset`.
Conn_Reset,
// The connection destroy event is fired in response to `finish`. It is the event procedure's
// responsibility to properly clean up its event data as libpq has no ability to manage this memory.
// Failure to clean up will lead to memory leaks.
// This event is fired before `finish` does any other cleanup. The return value is ignored.
// The `evt_info` should be cast to `Event_Conn_Destroy`.
Conn_Destroy,
// The result creation event is fired in response to any query execution that generates a result,
// including `get_result`. This event will only be fired after the result has been created successfully.
// If the event procedure returns false, that event procedure will be ignore for the remaining lifetime of the result.
// The `evt_info` should be cast to `Event_Result_Create`.
Result_Create,
// The result copy event is fired in response to `copy_result`. This event will only be fired after the copy is complete.
// Only event procedures that have successfully handled the `Result_Create` or `Result_Copy` event for the
// source result will receive `Result_Copy` events.
// If the event procedure returns false, that event procedure will be ignored for the remaining lifetime of the new result.
// The `evt_info` should be cast to `Event_Result_Copy`.
Result_Copy,
// The result destroy event is fired in response to `clear`. It is the event procedure's responsibility
// to properly clean up its event data as libpq has no ability to manage this memory. Failure to clean up will lead to memory leaks.
// The event is fired just before `clear` performs any other cleanup. The return value is ignored.
Result_Destroy,
}
Event_Register :: struct {
conn: Conn,
}
Event_Conn_Reset :: struct {
conn: Conn,
}
Event_Conn_Destroy :: struct {
conn: Conn,
}
Event_Result_Create :: struct {
conn: Conn,
result: Result,
}
Event_Result_Copy :: struct {
src: Result,
dest: Result,
}
Event_Result_Destroy :: struct {
result: Result,
}
@(link_prefix="PQ")
foreign pq {
/*----- [[Database Connection Control Functions; https://www.postgresql.org/docs/16/libpq-connect.html]] -----*/
// Makes a new connection to the database server.
//
// This function opens a new database connection using the parameters taken from two nil-terminated arrays.
// The first, `keywords`, is defined as an array of strings, each one being a key word.
// The second, `values`, gives the value for each key word.
//
// When `expand_dbname` is `true`, the `dbname` key word value is allowed to be recognized as a `conninfo` string. See below for details.
//
// The passed arrays can be empty to use all default parameters, or can contain one or more parameter settings. They should be matched in length.
// Processing will stop with the last non-nil element of the `keywords` array.
//
// [[More info; https://www.postgresql.org/docs/16/libpq-connect.html#LIBPQ-PQCONNECTDBPARAMS]]
@(link_name="PQconnectdbParams")
connectdb_params :: proc(keywords: [^]cstring, values: [^]cstring, expand_dbname: b32) -> Conn ---
// Makes a new connection to the database server.
//
// This function opens a new database connection using the parameters taken from the string `conninfo`.
//
// The passed string can be empty to use all default parameters, or it can contain one or more parameter settings
// separated by whitespace. Each parameter setting is in the form `keyword = value`. Spaces around the equal sign are optional.
// To write an empty value, or a value containing spaces, surround it in single quotes, e.g., `keyword = 'a value`.
// Single quotes and backslashes withing the value must be escaped with a backslash, i.e., `\'` and `\\`.
connectdb :: proc(conninfo: cstring) -> Conn ---
// Makes a new connection to the database server.
//
// This is the predecessor of `connectdb` with a fixed set of parameters. It has the same functionality except
// that the missing parameters will always take on default values. write `nil` or an empty string for any one of the fixed
// parameters that is to be defaulted.
//
// If the `dbName` contains an = sign, it is taken as a `conninfo` string in exactly the same way as if it had been
// passed to `connectdb`, and the remaining parameters are then applied as above.
@(link_name="PQsetdbLogin")
setdb_login :: proc(
host: cstring = nil,
port: cstring = nil,
options: cstring = nil,
tty: cstring = nil,
dbname: cstring = nil,
login: cstring = nil,
pwd: cstring = nil,
) -> Conn ---
// Makes a connection to the database server in a nonblocking manner.
//
// [[More info; https://www.postgresql.org/docs/16/libpq-connect.html#LIBPQ-PQCONNECTSTARTPARAMS]]
@(link_name="PQconnectStartParams")
connect_start_params :: proc(keywords: [^]cstring, values: [^]cstring, expand_dbname: b32) -> Conn ---
// Makes a connection to the database server in a nonblocking manner.
//
// [[More info; https://www.postgresql.org/docs/16/libpq-connect.html#LIBPQ-PQCONNECTSTARTPARAMS]]
@(link_name="PQconnectStart")
connect_start :: proc(conninfo: cstring) -> Conn ---
// Poll the connection status after one of the 2 functions above succeeds.
//
// [[More info; https://www.postgresql.org/docs/16/libpq-connect.html#LIBPQ-PQCONNECTSTARTPARAMS]]
@(link_name="PQconnectPoll")
connect_poll :: proc(conn: Conn) -> Polling_Status ---
// Returns the default connection options.
//
// NOTE: After processing the options array, free it by passing it to `conninfoFree`. If that is not done,
// a small amount of memory is leaked for each call to `conndefaults`.
//
// [[More info; https://www.postgresql.org/docs/16/libpq-connect.html#LIBPQ-PQCONNDEFAULTS]]
@(link_name="PQconndefaults")
conn_defaults :: proc() -> [^]Conninfo_Option ---
// Returns parsed connection options from the provided connection string.
//
// No defaults are inserted.
//
// If `errmsg` is not nil, then `errmsg^` is set to nil on success, else to an allocated error string
// explaining the problem. (It is also possible for `errmsg^` to be nil and the result to be nil, this indicates out-of-memory.).
//
// NOTE: After processing the options array , free it by passing it to `conninfoFree`. If that is not done,
// some memory is leaked for each callto `conninfoParse`. Conversely, if an error occurs and `errmsg` is not nil,
// be sure to free the error string using `freemem`.
//
// [[More info; https://www.postgresql.org/docs/16/libpq-connect.html#LIBPQ-PQCONNINFOPARSE]]
@(link_name="PQconninfoParse")
conninfo_parse :: proc(conninfo: cstring, errmsg: ^cstring = nil) -> [^]Conninfo_Option ---
// Closes the connection to the server. Also frees memory used by the `Conn` object.
//
// Note that even if the server connection attempt fails (as indicated by `status`), the application
// should call `finish` to free the memory used by the `Conn` object. The `conn` pointer must not be used
// again after `finish` has been called.
finish :: proc(conn: Conn) ---
// Resets the communication channel to the server.
//
// This function will close the connection to the server and attempt to reestablish a new connection to the same server,
// using all the same parameters previously used. This might be useful for error recovery if a working connection is lost.
reset :: proc(conn: Conn) ---
// Resets the communication channel to the server, in a nonblocking manner.
//
// [[More info; https://www.postgresql.org/docs/16/libpq-connect.html#LIBPQ-PQRESETSTART]]
@(link_name="PQresetStart")
reset_start :: proc(conn: Conn) -> b32 ---
// If `resetStart` returned true, poll the status using this function.
@(link_name="PQresetPoll")
reset_poll :: proc(conn: Conn) -> Polling_Status ---
// Reports the status of the server. It accepts connection parameters identical to those of `connectdbParams`, described above.
// It is not, however, necessary to supply correct user name, password, or database name values to obtain the server status.
@(link_name="PQpingParams")
ping_params :: proc(keywords: [^]cstring, values: [^]cstring, expand_dbname: b32) -> Ping ---
// Reports the status of the server. It accepts a connection parameter identical to those of `connectdb` described above.
// It is not, however, necessary to supply correct user name, password, or database name value to obtain the server status.
@(link_name="PQping")
ping :: proc(conninfo: cstring) -> Ping ---
// [[More info; https://www.postgresql.org/docs/16/libpq-connect.html#LIBPQ-PQSETSSLKEYPASSHOOK-OPENSSL]]
@(link_name="PQsetSSLKeyPassHook_OpenSSL")
set_ssl_key_pass_hook :: proc(hook: SSL_Key_Pass_Hook) ---
// [[More info; https://www.postgresql.org/docs/16/libpq-connect.html#LIBPQ-PQGETSSLKEYPASSHOOK-OPENSSL]]
@(link_name="PQgetSSLKeyPassHook_OpenSSL")
ssl_key_pass_hook :: proc() -> SSL_Key_Pass_Hook ---
/*----- [[Connection State Functions; https://www.postgresql.org/docs/16/libpq-status.html]] -----*/
// Returns the database name of the connection.
db :: proc(conn: Conn) -> cstring ---
// Returns the user name of the connection.
user :: proc(conn: Conn) -> cstring ---
// Returns the password of the connection.
pass :: proc(conn: Conn) -> cstring ---
// Returns the host of the connection.
host :: proc(conn: Conn) -> cstring ---
// Returns the server IP address of the active connection.
// This can be the address that a host name resolved to, or an IP address provided through
// the hostaddr parameter.
hostaddr :: proc(conn: Conn) -> cstring ---
// Returns the port of the connection.
port :: proc(conn: Conn) -> cstring ---
// Returns the command-line options passed in the connection request.
options :: proc(conn: Conn) -> cstring ---
// Returns the status of the connection.
//
// [[More Info; https://www.postgresql.org/docs/16/libpq-status.html#LIBPQ-PQSTATUS]]
status :: proc(conn: Conn) -> Connection_Status ---
// Returns the current in-transaction status of the server.
//
// WARN: PQtransactionStatus will give incorrect results when using a PostgreSQL 7.3 server that has the parameter autocommit set to off. The server-side autocommit feature has been deprecated and does not exist in later server versions.
@(link_name="PQtransactionStatus")
transaction_status :: proc(conn: Conn) -> Transaction_Status ---
// Looks up a current parameter setting of the server.
//
// [[More Info; https://www.postgresql.org/docs/16/libpq-status.html#LIBPQ-PQPARAMETERSTATUS]]
@(link_name="PQparamterStatus")
parameter_status :: proc(conn: Conn, param: cstring) -> cstring ---
// Interrogates the frontend/backend protocol being used.
//
// [[More Info; https://www.postgresql.org/docs/16/libpq-status.html#LIBPQ-PQPROTOCOLVERSION]]
@(link_name="PQprotocolVersion")
protocol_version :: proc(conn: Conn) -> i32 ---
// Returns an integer representing the backend version.
//
// [[More Info; https://www.postgresql.org/docs/16/libpq-status.html#LIBPQ-SERVERVERSION]]
@(link_name="PQserverVersion")
server_version :: proc(conn: Conn) -> i32 ---
// Returns the error message most recently generated by an operation on the connection.
//
// The returned string does not need to be freed, note that the string is overwritten between
// function calls so you can't keep it around after calling another function.
@(link_name="PQerrorMessage")
error_message :: proc(conn: Conn) -> cstring ---
// Obtains the file descriptor number of the connection socket to the server.
// A valid descriptor will be greater than or equal to 0; a result of -1 indicates that no
// server connection is currently open. (This will not change during normal operation, but could change
// during connection setup or reset.)
socket :: proc(conn: Conn) -> i32 ---
// Returns the process ID (PID) of the backend process handling this connection.
//
// The backend PID is useful for debugging purposes and for comparison to `NOTIFY` messages (which include the PID of the notifying backend process).
// Note that the PID belongs to the process executing on the database server host, not the local host.
@(link_name="PQbackendPID")
backend_pid :: proc(conn: Conn) -> i32 ---
// Returns true if the connection authentication method required a password, but none was available.
@(link_name="PQconnectionNeedsPassword")
connection_needs_password :: proc(conn: Conn) -> b32 ---
@(link_name="PQconnectionUsedPassword")
// Returns true if the connection authentication method used a password.
connection_used_password :: proc(conn: Conn) -> b32 ---
// Returns true if the connection authentication method used GSSAPI.
@(link_name="PQconnectionUsedGSSAPI")
connection_used_gssapi :: proc(conn: Conn) -> b32 ---
// Returns true if the connection uses SSL.
@(link_name="PQsslInUse")
ssl_in_use :: proc(conn: Conn) -> b32 ---
// Returns SSL-related information about the connection.
//
// [[The list of available attributes; https://www.postgresql.org/docs/16/libpq-status.html#LIBPQ-PQSSLATTRIBUTE]]
@(link_name="PQsslAttribute")
ssl_attribute :: proc(conn: Conn, attribute_name: cstring) -> cstring ---
// Returns an array of SSL attribute names that can be used in `ssl_attribute`.
@(link_name="PQsslAttributeNames")
ssl_attribute_names :: proc(conn: Conn) -> [^]cstring ---
// Returns a pointer to an SSL-implementation-specific object describing the connection.
//
// The available names depend on the SSL implementation, for OpenSSL there is one struct available under
// "OpenSSL".
@(link_name="PQsslStruct")
ssl_struct :: proc(conn: Conn, name: cstring) -> rawptr ---
/*----- [[Command Execution Functions; https://www.postgresql.org/docs/16/libpq-exec.html]] -----*/
// Submits a command to the server and waits for the result.
//
// Returns a Result pointer or nil (when out of memory or inability to send to server).
// The `result_status` function should be called to check the return value for any errors.
// The `error_message` function can be used to get more information about the errors.
//
// The command string can include multiple SQL commands (seperated by semicolons).
// Multiple queries sent in a single `exec` call are processed in a single transaction, unless there are
// explicit `BEGIN/COMMIT` commands included in the query string to divide it into multiple transactions.
// Note however that the returned `Result` struct describes only the result of the last command executed.
// Should one of the commands fail, processing of the string stops with it and the returned `Result`
// describes the error condition.
exec :: proc(conn: Conn, command: cstring) -> Result ---
// Submits a command to the server and waits for the result, with the ability to pass parameters separately from the SQL command text.
//
// `exec_params` is like `exec`, but offers additional functionality; parameter values can be specified separately from the command string proper,
// and query results can be requested in either text or binary format. `exec_params` is supported only in protocol 3.0 and later connections;
// It will fail when using protocol 2.0.
//
// The primary advantage over `exec` is that parameter values can be separated from the command string, thus avoiding
// the need for tedious and error-prone quoting and escaping.
//
// NOTE: Unlike `exec` it allows at most one SQL command in the given string. (There can be semicolons in it,
// but not more than one nonempty command.) This is a limitation of the underlying protocol, but has some usefulness
// as an extra defense against SQL-injection attacks.
//
// INFO: Specifying parameter types via OIDs is tedious, particularly if you prefer not to hard-wire particular
// OID values into your program. However, you can avoid doing so even in cases where the server by itself cannot
// determine the type of the parameter, or chooses a different type than you want. In the SQL command text, attach an
// explicit cast to the parameter symbol to show what data type you will send. For example:
// `SELECT * FROM mytable WHERE x = $1::bigint;`
// This forces the parameter `$1` to be treated as `bigint`, whereas by default it would be assigned the same type as
// `x`. Forcing the parameter type decision, either this way or by specifying a numeric type OID, is strongly recommended when
// sending parameters values in binary format, because binary format has less redundancy than text format and so there is
// less chance that the server will detect a type mismatch mistake for you.
//
// Inputs:
// - conn: The connection object to send the command through.
//
// - command: The SQL command string to be executed. If parameters are used, they are referred to in the comand string as `$1`, `$2`, etc.
//
// - n_params: The number of parameters supplied; it is the length of the arrays `param_types`, `param_values`, `param_lengths`, and `param_formats`.
//
// - param_types: Specifies, by OID, the data types to be assigned to the parameter symbols. If `param_types` is `nil`, or any particular element in the array is zero,
// the server infers the data type for the parameter symbol in the same way it would do for an untyped literal string.
//
// - param_values: Specifies the actual values of the parameters. A nil pointer in this array means the corresponding parameter is nil;
// otherwise the pointer points to a zero-terminated text string (for text formats) or binary data in the format expected by the server (for binary format).
//
// - param_lengths: Specifies the actual data lengths of binary-format parameters. It is ignored for nil parameters and text-format parameters.
// The array pointer can be nil when there are no binary parameters.
//
// - param_formats: Specifies whether parameters are text (put a zero in the array entry for the corresponding parameter) or binary (put a one in the array entry for the corresponding parameter).
// If the array pointer is nil then all parameters are presumed to be text strings.
//
// - result_format: Specify zero to obtain results in text format, or one to obtain results in binary format.
// (There is not currently a provision to obtain different result columns in different formats, although that is possible in the underlying protocol.)
@(link_name="PQexecParams")
exec_params :: proc(
conn: Conn,
command: cstring,
n_params: i32,
param_types: [^]OID,
param_values: [^][^]byte,
param_lengths: [^]i32,
param_formats: [^]Format,
result_format: Format,
) -> Result ---
// Submits a request to create a prepared statement with the given parameters, and waits for completion.
//
// Creates a prepared statement for later execution with `exec_prepared`. This feature allows commands that will be used
// repeatedly to be parsed and planned just once, rather than each time they are executed. This is supported only in protocol 3.0 and later;
// it will fail when using protocol 2.0.
//
// The function creates a prepared statement named `stmt_name` from the query string, which must contain a single SQL command.
// `stmt_name` can be `""` to create an unnamed statement, in which case any pre-existing unnamed statement is automatically replaced;
// otherwise it is an error if the statement name is already defined in the current session. If any parameters are used, they are referred to in the
// query as `$1`, `$2`, etc. `n_params` is the number of parameters for which types are pre-specified in `param_types`.
// (The pointer can be nil when `n_params` is 0.) `param_types` specifies, by OID` the data types to be assigned to the parameter symbols.
// If `param_types` is nil, or any particular element in the array is 0, the server assigns a data type to the parameter symbol
// in the same way it would do for an untyped literal string. Also, the query can use parameter symbols with numbers higher than
// `n_params`; data types will be inferred for these symbols as well. (See `describe_prepared` for a means to find out what data types were inferred.)
//
// As with `exec`, the result is nil when the request was not able to be sent at all. Use `error_message` for more info on errors.
//
// Prepared statements for use with `exec_prepared` can also be created by executing SQL PREPARE statements. Also, although there is no libpq function
// for deleting a prepared statement, the SQL DEALLOCATE statement can be used for that purpose.
prepare :: proc(conn: Conn, stmt_name: cstring, query: cstring, n_params: i32, param_types: [^]OID) -> Result ---
// Sends a request to execute a prepared statement with given parameters and waits for the result.
//
// `exec_prepared` is like `exec_params`, but the command to be executed is specified by naming a previously-prepared statement,
// instead of giving a query string. This feature allows commands that will be used repeatedly to be parsed and planned just once,
// rather than each time they are executed. The statement must have been prepared previously in the current session.
// This is supported in protocol 3.0 and later connections; it will fail when using protocol version 2.0.
//
// The parameters are identical to `exec_params`, except that the name of the prepared statement is given instead of a query string,
// and the `param_types` parameter is not present (it is not needed since the prepared statement's parameter types were determined when it was created).
@(link_name="PQexecPrepared")
exec_prepared :: proc(
conn: Conn,
stmt_name: cstring,
n_params: i32,
param_values: [^][^]byte,
param_lengths: [^]i32,
param_formats: [^]Format,
result_format: Format,
) -> Result ---
// Submits a request to obtain information about the specified prepared statement, and waits for completion.
//
// This allows an application to obtain information about a previously prepared statement. This is supported in protocol 3.0 and later;
// it will fail when using protocol 2.0.
//
// `stmt_name` can be empty or nil to reference the unnamed statement, otherwise it must be the name of an existing prepared statement.
// On success, result status OK will be returned. The functions `n_params` and `param_types` can be applied to this result to obtain info
// about the parameters of the prepared statement, and the function `n_fields`, `f_name`, `f_type` etc provide info about the result columns.
@(link_name="PQdescribePrepared")
describe_prepared :: proc(conn: Conn, stmt_name: cstring) -> Result ---
// Submits a request to obtain information about the specified portal, and waits for completion.
//
// This allows an application to obtain info about a previously created portal. (libpq does not provide any
// direct access to portals, but you can use this function to inspect the properties of a cursor created with a
// `DECLARE CURSOR` SQL statement. This is only supported in protocol 3.0 and later; it will fail when using protocol 2.0.
//
// `portal_name` can be empty or nil to reference the unnamed portal, otherwise it must be the name of an existing portal.
// On success, result status OK will be returned. The functions `n_fields`, `f_name`, `f_type` etc can be applied to the result to
// obtain info about the result columns.
@(link_name="PQdescribePortal")
describe_portal :: proc(conn: Conn, portal_name: cstring) -> Result ---
// Returns the result status of the command.
//
// If the result status is `Tuples_OK`, then the functions described below can be used to retrieve
// the rows returned by the query. Note that a `SELECT` command that happens to retrieve zero rows
// still shows `Tuples_OK`, `Command_OK` is for commands that can never return rows (`INSERT`, `UPDATE`, etc.).
// A response of `Empty_Query` might indicate a bug in the client software.
//
// A result of `Non_Fatal_Error` will never be returned directly by `exec` or other query execution functions;
// results of this kind are instead passed to the notice processor.
@(link_name="PQresultStatus")
result_status :: proc(res: Result) -> Exec_Status ---
// Converts the enumerated type returned by `result_status` into a string constant describing the status code.
// The caller should not free the result.
@(link_name="PQresStatus")
res_status :: proc(status: Exec_Status) -> cstring ---
// Returns the error message associated with the command, or an empty string if there was no error.
//
// If there was an error, the returned string will include a trailing newline. The caller should NOT free
// the result directly. It will be freed when the associated `Result` handle is passed to `clear`.
//
// Immediately following `exec` or `get_result`, `error_message` (on the connection) will return the same
// string as this function. However, a `Result` will retain its error message until destroyed, whereas the connection's error message
// will change when subsequent operations are done. Use this function when you want to know the status associated with
// a particular `Result`; use `error_message` when you want to know the status from the latest operation on the connection.
@(link_name="PQresultErrorMessage")
result_error_message :: proc(res: Result) -> cstring ---
// Returns a reformatted error message associated with a `Result` object.
//
// NOTE: The caller must free the returned string.
//
// [[More info; https://www.postgresql.org/docs/16/libpq-exec.html#LIBPQ-PQRESULTVERBOSEERRORMESSAGE]]
@(link_name="PQresultVerboseErrorMessage")
result_verbose_error_message :: proc(res: Result) -> cstring ---
// Returns an individual field of an error report.
//
// `field_code` is an error field identifier. nil is returned if the `Result` is not an errro or warning result, or
// does not include the specified field. Field values will normally not include a trailing newline. The caller should not free the result.
// It will be freed when the associated `Result` handle is passed to `clear`.
@(link_name="PQresultErrorField")
result_error_field :: proc(res: Result, field_code: Field_Code) -> cstring ---
// Frees the storage associated with a `Result`. Every command result should be freed via `clear` when it is no longer needed.
//
// You can keep a `Result` object around for as long as you need it; it does not go away when you issue a new command,
// nor even if you close the connection. To get rid of it, you must call `clear`. Failure to do this will result in memory leaks in your application.
clear :: proc(res: Result) ---
/*----- [[Retrieving Query Result Information; https://www.postgresql.org/docs/16/libpq-exec.html#LIBPQ-EXEC-SELECT-INFO]] -----*/
// Returns the number of rows (tuples) in the query result.
// Because it returns an integer result, large result sets might overflow the return value.
@(link_name="PQntuples")
n_tuples :: proc(res: Result) -> i32 ---
// Returns the number of columns (fields) in each row of the query result.
@(link_name="PQnfields")
n_fields :: proc(res: Result) -> i32 ---
// Returns the column name associated with the given column number. Column numbers start at 0.
// The caller should not free the result directly. It will be freed when the associated `Result` is
// passed to `clear`.
//
// nil is returned if the column number is out of range.
@(link_name="PQfname")
f_name :: proc(res: Result, column_number: i32) -> cstring ---
// Returns the column number associated with the given column name.
//
// -1 is returned if the given name does not match any column.
//
// The given name is treated like an identifier in an SQL command, it is downcased unless
// double-qouted.
@(link_name="PQfnumber")
f_number :: proc(res: Result, column_name: cstring) -> i32 ---
// Returns an OID of the table from which the given column was fetched. Column numbers start at 0.
//
// `INVALID_OID` is returned if the column number is out of range, or if the specified column is
// not a simple reference to a table column, or when using pre-3.0 protocol. You can query the system
// table `pg_class` to determine exactly which table is referenced.
@(link_name="PQftable")
f_table :: proc(res: Result, column_number: i32) -> OID ---
// Returns the column number (within its table) of the column making up the specified query result column.
// Query-result column numbers start at 0, but table columns have nonzero numbers.
//
// Zero is returned if the column number is out of range, or if the specified column is not a simple reference to a
// table column, or when using pre-3.0 protocol.
@(link_name="PQftablecol")
f_tablecol :: proc(res: Result, column_number: i32) -> i32 ---
// Returns the format code indicating the format of the given column. Column numbers start at 0.
@(link_name="PQfformat")
f_format :: proc(res: Result, column_number: i32) -> Format ---
// Returns the data type associated with the given column number. The integer returned is the internal OID number of the type.
// Column numbers start at 0.
//
// You can query the system table `pg_type` to obtain the names and properties of the various data types.
// The OIDs of the built-in data types are defined in the `src/include/catalog/pg_type.h` header.
@(link_name="PQftype")
f_type :: proc(res: Result, column_number: i32) -> OID ---
// Returns the type modifier of the column associated with the given column number. Column numbers start at 0.
//
// The interpretation of modifier values is type-specific; they typically indicate precision or size limits.
// The value -1 is used to indicate "no information available". Most data types do not use modifiers, in which case
// the value is always -1.
@(link_name="PQfmod")
f_mod :: proc(res: Result, column_number: i32) -> i32 ---
// Returns the size in bytes of the column associated with the given column number. Column numbers start at 0.
//
// This returns the space allocated for this column in a database row, in other words the size of the server's internal representation
// of the data type. (Accordingly, it is not really very useful to clients.) A negative value indicates the data type is variable-length.
@(link_name="PQfsize")
f_size :: proc(res: Result, column_number: i32) -> i32 ---
// Returns a single field value of one row of a `Result`. Row and column numbers start at 0.
// The caller should not free the result directly. It will be freed when the associated `Result` handle is passed to `clear`.
//
// For data in text format, the value returned is a null-terminated character string representation of the field value.
// For data in binary format, the value is in the binary representation determined by the data type's `typsend` and `typreceive` functions.
// (The value is actually followed by a zero byte in this case too, but that is not ordinarily useful, since the value is likely to contain embedded nulls.)
//
// An empty string is returned if the field value is nil, See `get_is_null` to distinguish null values from empty-string values.
//
// The pointer returned by `get_value` points to storage that is part of the `Result` structure. One should not modify the data it points
// to, and one must explicitly copy the data into other storage if it is to be used past the lifetime of the `Result` itself.
@(link_name="PQgetvalue")
get_value :: proc(res: Result, row_number: i32, column_number: i32) -> [^]byte ---
// Tests the field for a null value. Row and column numbers start at 0.
@(link_name="PQgetisnull")
get_is_null :: proc(res: Result, row_number: i32, column_number: i32) -> b32 ---
// Returns the actual length of the field value in bytes. Row and column numbers start at 0.
//
// This is the actual data length for the particular data value, the size of the objects pointed to
// by `get_value`. For text data format this is the same as `strlen()`. For binary format this is essential
// information. Note that one should not rely on `f_size` to obtain the actual data length.
@(link_name="PQgetlength")
get_length :: proc(res: Result, row_number: i32, column_number: i32) -> i32 ---
// Returns the number of parameters of a prepared statement.
@(link_name="PQnparams")
n_params :: proc(res: Result) -> i32 ---
// Returns the data type of the indicated statement parameter. Parameter numbers start at 0.
@(link_name="PQparamtype")
param_type :: proc(res: Result, param_number: i32) -> OID ---
// Prints out all the rows and, optionally, the column names to the specified output stream.
//
// NOTE: All data is assumed to be in text format.
print :: proc(fout: ^libc.FILE, res: Result, po: ^Print_Opt) ---
/*----- [[Retrieving Other Result Information; https://www.postgresql.org/docs/16/libpq-exec.html#LIBPQ-EXEC-NONSELECT]] -----*/
// Returns the command status tag from the SQL command that generated the `Result`.
//
// Commonly this is just the name of the command, but it might include additional data such as the number of rows processed.
// The caller should not free the results directly. It will be freed when the associated `Result` handle is passed to `clear`.
@(link_name="PQcmdStatus")
cmd_status :: proc(res: Result) -> cstring ---
// Returns the number of rows affected by the SQL command.
//
// This function returns a string containing the number of rows affected by the SQL statement that generated the `Result`.
// This function can only be used following the execution of a `SELECT`, `CREATE TABLE AS`, `INSERT`, `UPDATE`,
// `DELETE`, `MOVE`, `FETCH` or `COPY` statement, or an `EXECUTE` of a prepared query that contains an `INSERT`, `UPDATE` or `DELETE` statement.
// If the command that generated the `Result` was anything else, an empty string is returned.
//
// The caller should not free the return value directly. It will be freed when the associated `Result` handle is passed to `clear`.
@(link_name="PQcmdTuples")
cmd_tuples :: proc(res: Result) -> cstring ---
// Returns the OID of the inserted row, if the SQL command was an `INSERT` that inserted exactly one row into a table
// that has OIDs, or a `EXECUTE` of a prepared query containing a suitable `INSERT` statement. Otherwise, this function returns `INVALID_OID`.
// This function will also return `INVALID_OID` if the table affected by the `INSERT` statement does not contain OIDs.
@(link_name="PQoidValue")
oid_value :: proc(res: Result) -> OID ---
/*----- [[Escaping Strings for Inclusion in SQL Commands; https://www.postgresql.org/docs/16/libpq-exec.html#LIBPQ-EXEC-ESCAPE-STRING]] -----*/
// Escapes a string for use within an SQL command. This is useful when inserting data values as literal constants in SQL commands.
// Certain characters (such as qoutes or backslashes) must be escaped to prevent them from being interpreted specially by the SQL parser.
//
// This function allocates and the result should be freed with `free_mem`.
//
// A terminating zero byte is not required and should not be counted in the length parameter.
//
// The return value has special characters escaped and contains a zero byte.
// The single quotes that must surround PostgreSQL string literals are included in the result string.
//
// On error, this returns nil and the `Conn` will have an error set.
//
// Note that it is not necessary nor correct to do escaping when a data value is passed as a separate parameter in `exec_params` or its sibling routines.
@(link_name="PQescapeLiteral")
escape_literal :: proc(conn: Conn, str: cstring, length: uint) -> cstring ---
// Escapes a string for use as an SQL identifier, such as a table, column, or function name.
// This is useful when a user-supplied identifier might contain special characters that would otherwise not be interpreted as part of the identifier
// by the SQL parser, or when the identifier might contain upper case characters whose case should be preserved.
//
// This function allocates and the result should be freed with `free_mem`.
//
// A terminating zero byte is not required and should not be counted in the length parameter.
//