This file is indexed.

/usr/lib/python3/dist-packages/postgresql/api.py is in python3-postgresql 1.1.0-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
##
# .api - ABCs for database interface elements
##
"""
Application Programmer Interfaces for PostgreSQL.

``postgresql.api`` is a collection of Python APIs for the PostgreSQL DBMS. It
is designed to take full advantage of PostgreSQL's features to provide the
Python programmer with substantial convenience.

This module is used to define "PG-API". It creates a set of ABCs
that makes up the basic interfaces used to work with a PostgreSQL server.
"""
import collections
import abc

from .python.element import Element

__all__ = [
	'Message',
	'Statement',
	'Chunks',
	'Cursor',
	'Connector',
	'Category',
	'Database',
	'TypeIO',
	'Connection',
	'Transaction',
	'Settings',
	'StoredProcedure',
	'Driver',
	'Installation',
	'Cluster',
]

class Message(Element):
	"""
	A message emitted by PostgreSQL.
	A message being a NOTICE, WARNING, INFO, etc.
	"""
	_e_label = 'MESSAGE'

	severities = (
		'DEBUG',
		'INFO',
		'NOTICE',
		'WARNING',
		'ERROR',
		'FATAL',
		'PANIC',
	)
	sources = (
		'SERVER',
		'CLIENT',
	)

	@property
	@abc.abstractmethod
	def source(self) -> str:
		"""
		Where the message originated from. Normally, 'SERVER', but sometimes
		'CLIENT'.
		"""

	@property
	@abc.abstractmethod
	def code(self) -> str:
		"""
		The SQL state code of the message.
		"""

	@property
	@abc.abstractmethod
	def message(self) -> str:
		"""
		The primary message string.
		"""

	@property
	@abc.abstractmethod
	def details(self) -> dict:
		"""
		The additional details given with the message. Common keys *should* be the
		following:

		 * 'severity'
		 * 'context'
		 * 'detail'
		 * 'hint'
		 * 'file'
		 * 'line'
		 * 'function'
		 * 'position'
		 * 'internal_position'
		 * 'internal_query'
		"""

	@abc.abstractmethod
	def isconsistent(self, other) -> bool:
		"""
		Whether the fields of the `other` Message object is consistent with the
		fields of `self`.

		This *must* return the result of the comparison of code, source, message,
		and details.

		This method is provided as the alternative to overriding equality;
		often, pointer equality is the desirable means for comparison, but
		equality of the fields is also necessary.
		"""

class Result(Element):
	"""
	A result is an object managing the results of a prepared statement.

	These objects represent a binding of parameters to a given statement object.

	For results that were constructed on the server and a reference passed back
	to the client, statement and parameters may be None.
	"""
	_e_label = 'RESULT'
	_e_factors = ('statement', 'parameters', 'cursor_id')

	@abc.abstractmethod
	def close(self) -> None:
		"""
		Close the Result handle.
		"""

	@property
	@abc.abstractmethod
	def cursor_id(self) -> str:
		"""
		The cursor's identifier.
		"""

	@property
	@abc.abstractmethod
	def sql_column_types(self) -> [str]:
		"""
		The type of the columns produced by the cursor.

		A sequence of `str` objects stating the SQL type name::

			['INTEGER', 'CHARACTER VARYING', 'INTERVAL']
		"""

	@property
	@abc.abstractmethod
	def pg_column_types(self) -> [int]:
		"""
		The type Oids of the columns produced by the cursor.

		A sequence of `int` objects stating the SQL type name::

			[27, 28]
		"""

	@property
	@abc.abstractmethod
	def column_names(self) -> [str]:
		"""
		The attribute names of the columns produced by the cursor.

		A sequence of `str` objects stating the column name::

			['column1', 'column2', 'emp_name']
		"""

	@property
	@abc.abstractmethod
	def column_types(self) -> [str]:
		"""
		The Python types of the columns produced by the cursor.

		A sequence of type objects::

			[<class 'int'>, <class 'str'>]
		"""

	@property
	@abc.abstractmethod
	def parameters(self) -> (tuple, None):
		"""
		The parameters bound to the cursor. `None`, if unknown and an empty tuple
		`()`, if no parameters were given.

		These should be the *original* parameters given to the invoked statement.

		This should only be `None` when the cursor is created from an identifier,
		`postgresql.api.Database.cursor_from_id`.
		"""

	@property
	@abc.abstractmethod
	def statement(self) -> ("Statement", None):
		"""
		The query object used to create the cursor. `None`, if unknown.

		This should only be `None` when the cursor is created from an identifier,
		`postgresql.api.Database.cursor_from_id`.
		"""

class Chunks(
	Result,
	collections.Iterator,
	collections.Iterable,
):
	pass

class Cursor(
	Result,
	collections.Iterator,
	collections.Iterable,
):
	"""
	A `Cursor` object is an interface to a sequence of tuples(rows). A result
	set. Cursors publish a file-like interface for reading tuples from a cursor
	declared on the database.

	`Cursor` objects are created by invoking the `Statement.declare`
	method or by opening a cursor using an identifier via the
	`Database.cursor_from_id` method.
	"""
	_e_label = 'CURSOR'

	_seek_whence_map = {
		0 : 'ABSOLUTE',
		1 : 'RELATIVE',
		2 : 'FROM_END',
		3 : 'FORWARD',
		4 : 'BACKWARD'
	}
	_direction_map = {
		True : 'FORWARD',
		False : 'BACKWARD',
	}

	@abc.abstractmethod
	def clone(self) -> "Cursor":
		"""
		Create a new cursor using the same factors as `self`.
		"""

	def __iter__(self):
		return self

	@property
	@abc.abstractmethod
	def direction(self) -> bool:
		"""
		The default `direction` argument for read().

		When `True` reads are FORWARD.
		When `False` reads are BACKWARD.

		Cursor operation option.
		"""

	@abc.abstractmethod
	def read(self,
		quantity : "Number of rows to read" = None,
		direction : "Direction to fetch in, defaults to `self.direction`" = None,
	) -> ["Row"]:
		"""
		Read, fetch, the specified number of rows and return them in a list.
		If quantity is `None`, all records will be fetched.

		`direction` can be used to override the default configured direction.

		This alters the cursor's position.

		Read does not directly correlate to FETCH. If zero is given as the
		quantity, an empty sequence *must* be returned.
		"""

	@abc.abstractmethod
	def __next__(self) -> "Row":
		"""
		Get the next tuple in the cursor.
		Advances the cursor position by one.
		"""

	@abc.abstractmethod
	def seek(self, offset, whence = 'ABSOLUTE'):
		"""
		Set the cursor's position to the given offset with respect to the
		whence parameter and the configured direction.

		Whence values:

		 ``0`` or ``"ABSOLUTE"``
		  Absolute.
		 ``1`` or ``"RELATIVE"``
		  Relative.
		 ``2`` or ``"FROM_END"``
		  Absolute from end.
		 ``3`` or ``"FORWARD"``
		  Relative forward.
		 ``4`` or ``"BACKWARD"``
		  Relative backward.

		Direction effects whence. If direction is BACKWARD, ABSOLUTE positioning
		will effectively be FROM_END, RELATIVE's position will be negated, and
		FROM_END will effectively be ABSOLUTE.
		"""

class Execution(metaclass = abc.ABCMeta):
	"""
	The abstract class of execution methods.
	"""

	@abc.abstractmethod
	def __call__(self, *parameters : "Positional Parameters") -> ["Row"]:
		"""
		Execute the prepared statement with the given arguments as parameters.

		Usage:

		>>> p=db.prepare("SELECT column FROM ttable WHERE key = $1")
		>>> p('identifier')
		[...]
		"""

	@abc.abstractmethod
	def column(self, *parameters) -> collections.Iterable:
		"""
		Return an iterator producing the values of first column of the
		rows produced by the cursor created from the statement bound with the
		given parameters.

		Column iterators are never scrollable.

		Supporting cursors will be WITH HOLD when outside of a transaction to
		allow cross-transaction access.

		`column` is designed for the situations involving large data sets.

		Each iteration returns a single value.

		column expressed in sibling terms::

			return map(operator.itemgetter(0), self.rows(*parameters))
		"""

	@abc.abstractmethod
	def chunks(self, *parameters) -> collections.Iterable:
		"""
		Return an iterator producing sequences of rows produced by the cursor
		created from the statement bound with the given parameters.

		Chunking iterators are *never* scrollable.

		Supporting cursors will be WITH HOLD when outside of a transaction.

		`chunks` is designed for moving large data sets efficiently.

		Each iteration returns sequences of rows *normally* of length(seq) ==
		chunksize. If chunksize is unspecified, a default, positive integer will
		be filled in. The rows contained in the sequences are only required to
		support the basic `collections.Sequence` interfaces; simple and quick
		sequence types should be used.
		"""

	@abc.abstractmethod
	def rows(self, *parameters) -> collections.Iterable:
		"""
		Return an iterator producing rows produced by the cursor
		created from the statement bound with the given parameters.

		Row iterators are never scrollable.

		Supporting cursors will be WITH HOLD when outside of a transaction to
		allow cross-transaction access.

		`rows` is designed for the situations involving large data sets.

		Each iteration returns a single row. Arguably, best implemented::

			return itertools.chain.from_iterable(self.chunks(*parameters))
		"""

	@abc.abstractmethod
	def column(self, *parameters) -> collections.Iterable:
		"""
		Return an iterator producing the values of the first column in
		the cursor created from the statement bound with the given parameters.

		Column iterators are never scrollable.

		Supporting cursors will be WITH HOLD when outside of a transaction to
		allow cross-transaction access.

		`column` is designed for the situations involving large data sets.

		Each iteration returns a single value. `column` is equivalent to::

			return map(operator.itemgetter(0), self.rows(*parameters))
		"""

	@abc.abstractmethod
	def declare(self, *parameters) -> Cursor:
		"""
		Return a scrollable cursor with hold using the statement bound with the
		given parameters.
		"""

	@abc.abstractmethod
	def first(self, *parameters) -> "'First' object that is returned by the query":
		"""
		Execute the prepared statement with the given arguments as parameters.
		If the statement returns rows with multiple columns, return the first
		row. If the statement returns rows with a single column, return the
		first column in the first row. If the query does not return rows at all,
		return the count or `None` if no count exists in the completion message.

		Usage:

		>>> db.prepare("SELECT * FROM ttable WHERE key = $1").first("somekey")
		('somekey', 'somevalue')
		>>> db.prepare("SELECT 'foo'").first()
		'foo'
		>>> db.prepare("INSERT INTO atable (col) VALUES (1)").first()
		1
		"""

	@abc.abstractmethod
	def load_rows(self,
		iterable : "A iterable of tuples to execute the statement with"
	):
		"""
		Given an iterable, `iterable`, feed the produced parameters to the
		query. This is a bulk-loading interface for parameterized queries.

		Effectively, it is equivalent to:

			>>> q = db.prepare(sql)
			>>> for i in iterable:
			...  q(*i)

		Its purpose is to allow the implementation to take advantage of the
		knowledge that a series of parameters are to be loaded so that the
		operation can be optimized.
		"""

	@abc.abstractmethod
	def load_chunks(self,
		iterable : "A iterable of chunks of tuples to execute the statement with"
	):
		"""
		Given an iterable, `iterable`, feed the produced parameters of the chunks
		produced by the iterable to the query. This is a bulk-loading interface
		for parameterized queries.

		Effectively, it is equivalent to:

			>>> ps = db.prepare(...)
			>>> for c in iterable:
			...  for i in c:
			...   q(*i)

		Its purpose is to allow the implementation to take advantage of the
		knowledge that a series of chunks of parameters are to be loaded so
		that the operation can be optimized.
		"""

class Statement(
	Element,
	collections.Callable,
	collections.Iterable,
):
	"""
	Instances of `Statement` are returned by the `prepare` method of
	`Database` instances.

	A Statement is an Iterable as well as Callable.

	The Iterable interface is supported for queries that take no arguments at
	all. It allows the syntax::

		>>> for x in db.prepare('select * FROM table'):
		...  pass
	"""
	_e_label = 'STATEMENT'
	_e_factors = ('database', 'statement_id', 'string',)

	@property
	@abc.abstractmethod
	def statement_id(self) -> str:
		"""
		The statment's identifier.
		"""

	@property
	@abc.abstractmethod
	def string(self) -> object:
		"""
		The SQL string of the prepared statement.

		`None` if not available. This can happen in cases where a statement is
		prepared on the server and a reference to the statement is sent to the
		client which subsequently uses the statement via the `Database`'s
		`statement` constructor.
		"""

	@property
	@abc.abstractmethod
	def sql_parameter_types(self) -> [str]:
		"""
		The type of the parameters required by the statement.

		A sequence of `str` objects stating the SQL type name::

			['INTEGER', 'VARCHAR', 'INTERVAL']
		"""

	@property
	@abc.abstractmethod
	def sql_column_types(self) -> [str]:
		"""
		The type of the columns produced by the statement.

		A sequence of `str` objects stating the SQL type name::

			['INTEGER', 'VARCHAR', 'INTERVAL']
		"""

	@property
	@abc.abstractmethod
	def pg_parameter_types(self) -> [int]:
		"""
		The type Oids of the parameters required by the statement.

		A sequence of `int` objects stating the PostgreSQL type Oid::

			[27, 28]
		"""

	@property
	@abc.abstractmethod
	def pg_column_types(self) -> [int]:
		"""
		The type Oids of the columns produced by the statement.

		A sequence of `int` objects stating the SQL type name::

			[27, 28]
		"""

	@property
	@abc.abstractmethod
	def column_names(self) -> [str]:
		"""
		The attribute names of the columns produced by the statement.

		A sequence of `str` objects stating the column name::

			['column1', 'column2', 'emp_name']
		"""

	@property
	@abc.abstractmethod
	def column_types(self) -> [type]:
		"""
		The Python types of the columns produced by the statement.

		A sequence of type objects::

			[<class 'int'>, <class 'str'>]
		"""

	@property
	@abc.abstractmethod
	def parameter_types(self) -> [type]:
		"""
		The Python types expected of parameters given to the statement.

		A sequence of type objects::

			[<class 'int'>, <class 'str'>]
		"""

	@abc.abstractmethod
	def clone(self) -> "Statement":
		"""
		Create a new statement object using the same factors as `self`.

		When used for refreshing plans, the new clone should replace references to
		the original.
		"""

	@abc.abstractmethod
	def close(self) -> None:
		"""
		Close the prepared statement releasing resources associated with it.
		"""
Execution.register(Statement)
PreparedStatement = Statement

class StoredProcedure(
	Element,
	collections.Callable,
):
	"""
	A function stored on the database.
	"""
	_e_label = 'FUNCTION'
	_e_factors = ('database',)

	@abc.abstractmethod
	def __call__(self, *args, **kw) -> (object, Cursor, collections.Iterable):
		"""
		Execute the procedure with the given arguments. If keyword arguments are
		passed they must be mapped to the argument whose name matches the key.
		If any positional arguments are given, they must fill in gaps created by
		the stated keyword arguments. If too few or too many arguments are
		given, a TypeError must be raised. If a keyword argument is passed where
		the procedure does not have a corresponding argument name, then,
		likewise, a TypeError must be raised.

		In the case where the `StoredProcedure` references a set returning
		function(SRF), the result *must* be an iterable. SRFs that return single
		columns *must* return an iterable of that column; not row data. If the
		SRF returns a composite(OUT parameters), it *should* return a `Cursor`.
		"""

##
# Arguably, it would be wiser to isolate blocks, and savepoints, but the utility
# of the separation is not significant. It's really
# more interesting as a formality that the user may explicitly state the
# type of the transaction. However, this capability is not completely absent
# from the current interface as the configuration parameters, or lack thereof,
# help imply the expectations.
class Transaction(Element):
	"""
	A `Tranaction` is an element that represents a transaction in the session.
	Once created, it's ready to be started, and subsequently committed or
	rolled back.

	Read-only transaction:

		>>> with db.xact(mode = 'read only'):
		...  ...

	Read committed isolation:

		>>> with db.xact(isolation = 'READ COMMITTED'):
		...  ...

	Savepoints are created if inside a transaction block:

		>>> with db.xact():
		...  with db.xact():
		...   ...
	"""
	_e_label = 'XACT'
	_e_factors = ('database',)

	@property
	@abc.abstractmethod
	def mode(self) -> (None, str):
		"""
		The mode of the transaction block:

			START TRANSACTION [ISOLATION] <mode>;

		The `mode` property is a string and will be directly interpolated into the
		START TRANSACTION statement.
		"""

	@property
	@abc.abstractmethod
	def isolation(self) -> (None, str):
		"""
		The isolation level of the transaction block:

			START TRANSACTION <isolation> [MODE];

		The `isolation` property is a string and will be directly interpolated into
		the START TRANSACTION statement.
		"""

	@abc.abstractmethod
	def start(self) -> None:
		"""
		Start the transaction.

		If the database is in a transaction block, the transaction should be
		configured as a savepoint. If any transaction block configuration was
		applied to the transaction, raise a `postgresql.exceptions.OperationError`.

		If the database is not in a transaction block, start one using the
		configuration where:

		`self.isolation` specifies the ``ISOLATION LEVEL``. Normally, ``READ
		COMMITTED``, ``SERIALIZABLE``, or ``READ UNCOMMITTED``.

		`self.mode` specifies the mode of the transaction. Normally, ``READ
		ONLY`` or ``READ WRITE``.

		If the transaction is already open, do nothing.

		If the transaction has been committed or aborted, raise an
		`postgresql.exceptions.OperationError`.
		"""
	begin = start

	@abc.abstractmethod
	def commit(self) -> None:
		"""
		Commit the transaction.

		If the transaction is a block, issue a COMMIT statement.

		If the transaction was started inside a transaction block, it should be
		identified as a savepoint, and the savepoint should be released.

		If the transaction has already been committed, do nothing.
		"""

	@abc.abstractmethod
	def rollback(self) -> None:
		"""
		Abort the transaction.

		If the transaction is a savepoint, ROLLBACK TO the savepoint identifier.

		If the transaction is a transaction block, issue an ABORT.

		If the transaction has already been aborted, do nothing.
		"""
	abort = rollback

	@abc.abstractmethod
	def __enter__(self):
		"""
		Run the `start` method and return self.
		"""

	@abc.abstractmethod
	def __exit__(self, typ, obj, tb):
		"""
		If an exception is indicated by the parameters, run the transaction's
		`rollback` method iff the database is still available(not closed), and
		return a `False` value.

		If an exception is not indicated, but the database's transaction state is
		in error, run the transaction's `rollback` method and raise a
		`postgresql.exceptions.InFailedTransactionError`. If the database is
		unavailable, the `rollback` method should cause a
		`postgresql.exceptions.ConnectionDoesNotExistError` exception to occur.

		Otherwise, run the transaction's `commit` method.

		When the `commit` is ultimately unsuccessful or not ran at all, the purpose
		of __exit__ is to resolve the error state of the database iff the
		database is available(not closed) so that more commands can be after the
		block's exit.
		"""

class Settings(
	Element,
	collections.MutableMapping
):
	"""
	A mapping interface to the session's settings. This provides a direct
	interface to ``SHOW`` or ``SET`` commands. Identifiers and values need
	not be quoted specially as the implementation must do that work for the
	user.
	"""
	_e_label = 'SETTINGS'

	@abc.abstractmethod
	def __getitem__(self, key):
		"""
		Return the setting corresponding to the given key. The result should be
		consistent with what the ``SHOW`` command returns. If the key does not
		exist, raise a KeyError.
		"""

	@abc.abstractmethod
	def __setitem__(self, key, value):
		"""
		Set the setting with the given key to the given value. The action should
		be consistent with the effect of the ``SET`` command.
		"""

	@abc.abstractmethod
	def __call__(self, **kw):
		"""
		Create a context manager applying the given settings on __enter__ and
		restoring the old values on __exit__.

		>>> with db.settings(search_path = 'local,public'):
		...  ...
		"""

	@abc.abstractmethod
	def get(self, key, default = None):
		"""
		Get the setting with the corresponding key. If the setting does not
		exist, return the `default`.
		"""

	@abc.abstractmethod
	def getset(self, keys):
		"""
		Return a dictionary containing the key-value pairs of the requested
		settings. If *any* of the keys do not exist, a `KeyError` must be raised
		with the set of keys that did not exist.
		"""

	@abc.abstractmethod
	def update(self, mapping):
		"""
		For each key-value pair, incur the effect of the `__setitem__` method.
		"""

	@abc.abstractmethod
	def keys(self):
		"""
		Return an iterator to all of the settings' keys.
		"""

	@abc.abstractmethod
	def values(self):
		"""
		Return an iterator to all of the settings' values.
		"""

	@abc.abstractmethod
	def items(self):
		"""
		Return an iterator to all of the setting value pairs.
		"""

class Database(Element):
	"""
	The interface to an individual database. `Connection` objects inherit from
	this
	"""
	_e_label = 'DATABASE'

	@property
	@abc.abstractmethod
	def backend_id(self) -> (int, None):
		"""
		The backend's process identifier.
		"""

	@property
	@abc.abstractmethod
	def version_info(self) -> tuple:
		"""
		A version tuple of the database software similar Python's `sys.version_info`.

		>>> db.version_info
		(8, 1, 3, '', 0)
		"""

	@property
	@abc.abstractmethod
	def client_address(self) -> (str, None):
		"""
		The client address that the server sees. This is obtainable by querying
		the ``pg_catalog.pg_stat_activity`` relation.

		`None` if unavailable.
		"""

	@property
	@abc.abstractmethod
	def client_port(self) -> (int, None):
		"""
		The client port that the server sees. This is obtainable by querying
		the ``pg_catalog.pg_stat_activity`` relation.

		`None` if unavailable.
		"""

	@property
	@abc.abstractmethod
	def xact(self,
		isolation : "ISOLATION LEVEL to use with the transaction" = None,
		mode : "Mode of the transaction, READ ONLY or READ WRITE" = None,
	) -> Transaction:
		"""
		Create a `Transaction` object using the given keyword arguments as its
		configuration.
		"""

	@property
	@abc.abstractmethod
	def settings(self) -> Settings:
		"""
		A `Settings` instance bound to the `Database`.
		"""

	@abc.abstractmethod
	def do(language, source) -> None:
		"""
		Execute a DO statement using the given language and source.
		Always returns `None`.

		Likely to be a function of Connection.execute.
		"""

	@abc.abstractmethod
	def execute(sql) -> None:
		"""
		Execute an arbitrary block of SQL. Always returns `None` and raise
		an exception on error.
		"""

	@abc.abstractmethod
	def prepare(self, sql : str) -> Statement:
		"""
		Create a new `Statement` instance bound to the connection
		using the given SQL.

		>>> s = db.prepare("SELECT 1")
		>>> c = s()
		>>> c.next()
		(1,)
		"""

	@abc.abstractmethod
	def statement_from_id(self,
		statement_id : "The statement's identification string.",
	) -> Statement:
		"""
		Create a `Statement` object that was already prepared on the
		server. The distinction between this and a regular query is that it
		must be explicitly closed if it is no longer desired, and it is
		instantiated using the statement identifier as opposed to the SQL
		statement itself.
		"""

	@abc.abstractmethod
	def cursor_from_id(self,
		cursor_id : "The cursor's identification string."
	) -> Cursor:
		"""
		Create a `Cursor` object from the given `cursor_id` that was already
		declared on the server.

		`Cursor` objects created this way must *not* be closed when the object
		is garbage collected. Rather, the user must explicitly close it for
		the server resources to be released. This is in contrast to `Cursor`
		objects that are created by invoking a `Statement` or a SRF
		`StoredProcedure`.
		"""

	@abc.abstractmethod
	def proc(self,
		procedure_id : \
			"The procedure identifier; a valid ``regprocedure`` or Oid."
	) -> StoredProcedure:
		"""
		Create a `StoredProcedure` instance using the given identifier.

		The `proc_id` given can be either an ``Oid``, or a ``regprocedure``
		that identifies the stored procedure to create the interface for.

		>>> p = db.proc('version()')
		>>> p()
		'PostgreSQL 8.3.0'
		>>> qstr = "select oid from pg_proc where proname = 'generate_series'"
		>>> db.prepare(qstr).first()
		1069
		>>> generate_series = db.proc(1069)
		>>> list(generate_series(1,5))
		[1, 2, 3, 4, 5]
		"""

	@abc.abstractmethod
	def reset(self) -> None:
		"""
		Reset the connection into it's original state.

		Issues a ``RESET ALL`` to the database. If the database supports
		removing temporary tables created in the session, then remove them.
		Reapply initial configuration settings such as path.

		The purpose behind this method is to provide a soft-reconnect method
		that re-initializes the connection into its original state. One
		obvious use of this would be in a connection pool where the connection
		is being recycled.
		"""

	@abc.abstractmethod
	def notify(self, *channels, **channel_and_payload) -> int:
		"""
		NOTIFY the channels with the given payload.

		Equivalent to issuing "NOTIFY <channel>" or "NOTIFY <channel>, <payload>"
		for each item in `channels` and `channel_and_payload`. All NOTIFYs issued
		*must* occur in the same transaction.

		The items in `channels` can either be a string or a tuple. If a string,
		no payload is given, but if an item is a `builtins.tuple`, the second item
		will be given as the payload. `channels` offers a means to issue NOTIFYs
		in guaranteed order.

		The items in `channel_and_payload` are all payloaded NOTIFYs where the
		keys are the channels and the values are the payloads. Order is undefined.
		"""

	@abc.abstractmethod
	def listen(self, *channels) -> None:
		"""
		Start listening to the given channels.

		Equivalent to issuing "LISTEN <x>" for x in channels.
		"""

	@abc.abstractmethod
	def unlisten(self, *channels) -> None:
		"""
		Stop listening to the given channels.

		Equivalent to issuing "UNLISTEN <x>" for x in channels.
		"""

	@abc.abstractmethod
	def listening_channels(self) -> ["channel name", ...]:
		"""
		Return an *iterator* to all the channels currently being listened to.
		"""

	@abc.abstractmethod
	def iternotifies(self, timeout = None) -> collections.Iterator:
		"""
		Return an iterator to the notifications received by the connection. The
		iterator *must* produce triples in the form ``(channel, payload, pid)``.

		If timeout is not `None`, `None` *must* be emitted at the specified
		timeout interval. If the timeout is zero, all the pending notifications
		*must* be yielded by the iterator and then `StopIteration` *must* be
		raised.

		If the connection is closed for any reason, the iterator *must* silently
		stop by raising `StopIteration`. Further error control is then the
		responsibility of the user.
		"""

class TypeIO(Element):
	_e_label = 'TYPIO'

	def _e_metas(self):
		return ()

class SocketFactory(object):
	@property
	@abc.abstractmethod
	def fatal_exception(self) -> Exception:
		"""
		The exception that is raised by sockets that indicate a fatal error.

		The exception can be a base exception as the `fatal_error_message` will
		indicate if that particular exception is actually fatal.
		"""

	@property
	@abc.abstractmethod
	def timeout_exception(self) -> Exception:
		"""
		The exception raised by the socket when an operation could not be
		completed due to a configured time constraint.
		"""

	@property
	@abc.abstractmethod
	def tryagain_exception(self) -> Exception:
		"""
		The exception raised by the socket when an operation was interrupted, but
		should be tried again.
		"""

	@property
	@abc.abstractmethod
	def tryagain(self, err : Exception) -> bool:
		"""
		Whether or not `err` suggests the operation should be tried again.
		"""

	@abc.abstractmethod
	def fatal_exception_message(self, err : Exception) -> (str, None):
		"""
		A function returning a string describing the failure, this string will be
		given to the `postgresql.exceptions.ConnectionFailure` instance that will
		subsequently be raised by the `Connection` object.

		Returns `None` when `err` is not actually fatal.
		"""

	@abc.abstractmethod
	def socket_secure(self, socket : "socket object") -> "secured socket":
		"""
		Return a reference to the secured socket using the given parameters.

		If securing the socket for the connector is impossible, the user should
		never be able to instantiate the connector with parameters requesting
		security.
		"""

	@abc.abstractmethod
	def socket_factory_sequence(self) -> [collections.Callable]:
		"""
		Return a sequence of `SocketCreator`s that `Connection` objects will use to
		create the socket object.
		"""

class Category(Element):
	"""
	A category is an object that initializes the subject connection for a
	specific purpose.

	Arguably, a runtime class for use with connections.
	"""
	_e_label = 'CATEGORY'
	_e_factors = ()

	@abc.abstractmethod
	def __call__(self, connection):
		"""
		Initialize the given connection in order to conform to the category.
		"""

class Connector(Element):
	"""
	A connector is an object providing the necessary information to establish a
	connection. This includes credentials, database settings, and many times
	addressing information.
	"""
	_e_label = 'CONNECTOR'
	_e_factors = ('driver', 'category')

	def __call__(self, *args, **kw):
		"""
		Create and connect. Arguments will be given to the `Connection` instance's
		`connect` method.
		"""
		return self.driver.connection(self, *args, **kw)

	def __init__(self,
		user : "required keyword specifying the user name(str)" = None,
		password : str = None,
		database : str = None,
		settings : (dict, [(str,str)]) = None,
		category : Category = None,
	):
		if user is None:
			# sure, it's a "required" keyword, makes for better documentation
			raise TypeError("'user' is a required keyword")
		self.user = user
		self.password = password
		self.database = database
		self.settings = settings
		self.category = category
		if category is not None and not isinstance(category, Category):
			raise TypeError("'category' must a be `None` or `postgresql.api.Category`")

class Connection(Database):
	"""
	The interface to a connection to a PostgreSQL database. This is a
	`Database` interface with the additional connection management tools that
	are particular to using a remote database.
	"""
	_e_label = 'CONNECTION'
	_e_factors = ('connector',)

	@property
	@abc.abstractmethod
	def connector(self) -> Connector:
		"""
		The :py:class:`Connector` instance facilitating the `Connection` object's
		communication and initialization.
		"""

	@property
	@abc.abstractmethod
	def query(self) -> Execution:
		"""
		The :py:class:`Execution` instance providing a one-shot query interface::

			connection.query.<method>(sql, *parameters) == connection.prepare(sql).<method>(*parameters)
		"""

	@property
	@abc.abstractmethod
	def closed(self) -> bool:
		"""
		`True` if the `Connection` is closed, `False` if the `Connection` is
		open.

		>>> db.closed
		True
		"""

	@abc.abstractmethod
	def clone(self) -> "Connection":
		"""
		Create another connection using the same factors as `self`. The returned
		object should be open and ready for use.
		"""

	@abc.abstractmethod
	def connect(self) -> None:
		"""
		Establish the connection to the server and initialize the category.

		Does nothing if the connection is already established.
		"""
		cat = self.connector.category
		if cat is not None:
			cat(self)

	@abc.abstractmethod
	def close(self) -> None:
		"""
		Close the connection.

		Does nothing if the connection is already closed.
		"""

	@abc.abstractmethod
	def __enter__(self):
		"""
		Establish the connection and return self.
		"""

	@abc.abstractmethod
	def __exit__(self, typ, obj, tb):
		"""
		Closes the connection and returns `False` when an exception is passed in,
		`True` when `None`.
		"""

class Driver(Element):
	"""
	The `Driver` element provides the `Connector` and other information
	pertaining to the implementation of the driver. Information about what the
	driver supports is available in instances.
	"""
	_e_label = "DRIVER"
	_e_factors = ()

	@abc.abstractmethod
	def connect(**kw):
		"""
		Create a connection using the given parameters for the Connector.
		"""

class Installation(Element):
	"""
	Interface to a PostgreSQL installation. Instances would provide various
	information about an installation of PostgreSQL accessible by the Python
	"""
	_e_label = "INSTALLATION"
	_e_factors = ()

	@property
	@abc.abstractmethod
	def version(self):
		"""
		A version string consistent with what `SELECT version()` would output.
		"""

	@property
	@abc.abstractmethod
	def version_info(self):
		"""
		A tuple specifying the version in a form similar to Python's
		sys.version_info. (8, 3, 3, 'final', 0)

		See `postgresql.versionstring`.
		"""

	@property
	@abc.abstractmethod
	def type(self):
		"""
		The "type" of PostgreSQL. Normally, the first component of the string
		returned by pg_config.
		"""

	@property
	@abc.abstractmethod
	def ssl(self) -> bool:
		"""
		Whether the installation supports SSL.
		"""

class Cluster(Element):
	"""
	Interface to a PostgreSQL cluster--a data directory. An implementation of
	this provides a means to control a server.
	"""
	_e_label = 'CLUSTER'
	_e_factors = ('installation', 'data_directory')

	@property
	@abc.abstractmethod
	def installation(self) -> Installation:
		"""
		The installation used by the cluster.
		"""

	@property
	@abc.abstractmethod
	def data_directory(self) -> str:
		"""
		The path to the data directory of the cluster.
		"""

	@abc.abstractmethod
	def init(self,
		initdb : "path to the initdb to use" = None,
		user : "name of the cluster's superuser" = None,
		password : "superuser's password" = None,
		encoding : "the encoding to use for the cluster" = None,
		locale : "the locale to use for the cluster" = None,
		collate : "the collation to use for the cluster" = None,
		ctype : "the ctype to use for the cluster" = None,
		monetary : "the monetary to use for the cluster" = None,
		numeric : "the numeric to use for the cluster" = None,
		time : "the time to use for the cluster" = None,
		text_search_config : "default text search configuration" = None,
		xlogdir : "location for the transaction log directory" = None,
	):
		"""
		Create the cluster at the `data_directory` associated with the Cluster
		instance.
		"""

	@abc.abstractmethod
	def drop(self):
		"""
		Kill the server and completely remove the data directory.
		"""

	@abc.abstractmethod
	def start(self):
		"""
		Start the cluster.
		"""

	@abc.abstractmethod
	def stop(self):
		"""
		Signal the server to shutdown.
		"""

	@abc.abstractmethod
	def kill(self):
		"""
		Kill the server.
		"""

	@abc.abstractmethod
	def restart(self):
		"""
		Restart the cluster.
		"""

	@abc.abstractmethod
	def wait_until_started(self,
		timeout : "maximum time to wait" = 10
	):
		"""
		After the start() method is ran, the database may not be ready for use.
		This method provides a mechanism to block until the cluster is ready for
		use.

		If the `timeout` is reached, the method *must* throw a
		`postgresql.exceptions.ClusterTimeoutError`.
		"""

	@abc.abstractmethod
	def wait_until_stopped(self,
		timeout : "maximum time to wait" = 10
	):
		"""
		After the stop() method is ran, the database may still be running.
		This method provides a mechanism to block until the cluster is completely
		shutdown.

		If the `timeout` is reached, the method *must* throw a
		`postgresql.exceptions.ClusterTimeoutError`.
		"""

	@property
	@abc.abstractmethod
	def settings(self):
		"""
		A `Settings` interface to the ``postgresql.conf`` file associated with the
		cluster.
		"""

	@abc.abstractmethod
	def __enter__(self):
		"""
		Start the cluster if it's not already running, and wait for it to be
		readied.
		"""

	@abc.abstractmethod
	def __exit__(self, exc, val, tb):
		"""
		Stop the cluster and wait for it to shutdown *iff* it was started by the
		corresponding enter.
		"""

__docformat__ = 'reStructuredText'
if __name__ == '__main__':
	help(__package__ + '.api')
##
# vim: ts=3:sw=3:noet: