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 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
|
"""
/***************************************************************************
Name : DB Manager
Description : Database manager plugin for QGIS
Date : May 23, 2011
copyright : (C) 2011 by Giuseppe Sucameli
email : brush.tyler@gmail.com
The content of this file is based on
- PG_Manager by Martin Dobias <wonder.sk@gmail.com> (GPLv2 license)
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from functools import cmp_to_key
from qgis.PyQt.QtCore import (
QRegularExpression,
QFile,
QVariant,
QDateTime,
QTime,
QDate,
Qt,
)
from qgis.core import (
Qgis,
QgsCoordinateReferenceSystem,
QgsVectorLayer,
QgsDataSourceUri,
QgsProviderRegistry,
QgsProviderConnectionException,
QgsFeedback,
)
from ..connector import DBConnector
from ..plugin import DbError, Table
import os
import re
def classFactory():
return PostGisDBConnector
class CursorAdapter:
def _debug(self, msg):
pass
# print("XXX CursorAdapter[" + hex(id(self)) + "]: " + msg)
def __init__(self, connection, sql=None, feedback=None):
self._debug("Created with sql: " + str(sql))
self.connection = connection
self.sql = sql
self.result = None
self.cursor = 0
self.feedback = feedback
self.closed = False
if self.sql is not None:
self._execute()
def _toStrResultSet(self, res):
newres = []
for rec in res:
newrec = []
for col in rec:
if type(col) == type(QVariant(None)): # noqa
if str(col) == "NULL":
col = None
else:
col = str(col) # force to string
if (
isinstance(col, QDateTime)
or isinstance(col, QDate)
or isinstance(col, QTime)
):
col = col.toString(Qt.DateFormat.ISODate)
newrec.append(col)
newres.append(newrec)
return newres
def _execute(self, sql=None):
if (sql is None or self.sql == sql) and self.result is not None:
return
if sql is not None:
self.sql = sql
if self.sql is None:
return
self._debug("execute called with sql " + self.sql)
try:
result = self.connection.execSql(self.sql, feedback=self.feedback)
self._description = [] # reset description
self.result = self._toStrResultSet(result.rows())
for c in result.columns():
self._description.append(
[
c, # name
"", # type_code
-1, # display_size
-1, # internal_size
-1, # precision
None, # scale
True, # null_ok
]
)
except QgsProviderConnectionException as e:
self._description = None
raise DbError(e, self.sql)
self._debug("execute returned " + str(len(self.result)) + " rows")
self.cursor = 0
@property
def description(self):
"""Returns columns description, it should be already set by _execute"""
if self._description is None:
self._description = []
if re.match("^SHOW", self.sql.strip().upper()):
try:
count = len(self.connection.executeSql(self.sql)[0])
except QgsProviderConnectionException:
count = 1
for i in range(count):
self._description.append(
[
"", # name
"", # type_code
-1, # display_size
-1, # internal_size
-1, # precision
None, # scale
True, # null_ok
]
)
else:
uri = QgsDataSourceUri(self.connection.uri())
# TODO: make this part provider-agnostic
sql = (
self.sql
if self.sql.upper().find(" LIMIT ") >= 0
else self.sql + " LIMIT 1 "
)
uri.setTable(
"(SELECT row_number() OVER () AS __rid__, * FROM ("
+ sql
+ ") as foo)"
)
uri.setKeyColumn("__rid__")
uri.setParam("checkPrimaryKeyUnicity", "0")
# TODO: fetch provider name from connection (QgsAbstractConnectionProvider)
# TODO: re-use the VectorLayer for fetching rows in batch mode
vl = QgsVectorLayer(uri.uri(False), "dbmanager_cursor", "postgres")
fields = vl.fields()
for i in range(1, len(fields)): # skip first field (__rid__)
f = fields[i]
self._description.append(
[
f.name(), # name
f.type(), # type_code
f.length(), # display_size
f.length(), # internal_size
f.precision(), # precision
None, # scale
True, # null_ok
]
)
self._debug(
"get_description returned " + str(len(self._description)) + " cols"
)
return self._description
def fetchone(self):
self._execute()
if len(self.result) - self.cursor:
res = self.result[self.cursor]
self.cursor += 1
return res
return None
def fetchmany(self, size):
self._execute()
if self.result is None:
self._debug(
"fetchmany: none result after _execute (self.sql is "
+ str(self.sql)
+ ", returning []"
)
return []
leftover = len(self.result) - self.cursor
self._debug(
"fetchmany: cursor: "
+ str(self.cursor)
+ " leftover: "
+ str(leftover)
+ " requested: "
+ str(size)
)
if leftover < 1:
return []
if size > leftover:
size = leftover
stop = self.cursor + size
res = self.result[self.cursor : stop]
self.cursor = stop
self._debug(
"fetchmany: new cursor: "
+ str(self.cursor)
+ " reslen: "
+ str(len(self.result))
)
return res
def fetchall(self):
self._execute()
res = self.result[self.cursor :]
self.cursor = len(self.result)
return res
def scroll(self, pos, mode="relative"):
self._execute()
if pos < 0:
self._debug("scroll pos is negative: " + str(pos))
if mode == "relative":
self.cursor = self.cursor + pos
elif mode == "absolute":
self.cursor = pos
def close(self):
self.result = None
self.closed = True
class PostGisDBConnector(DBConnector):
def __init__(self, uri, connection):
"""Creates a new PostgreSQL connector
:param uri: data source URI
:type uri: QgsDataSourceUri
:param connection: the plugin parent instance
:type connection: PostGisDBPlugin
"""
DBConnector.__init__(self, uri)
username = uri.username() or os.environ.get("PGUSER")
# Do not get db and user names from the env if service is used
if not uri.service():
if username is None:
username = os.environ.get("USER")
self.dbname = uri.database() or os.environ.get("PGDATABASE") or username
uri.setDatabase(self.dbname)
# self.connName = connName
# self.user = uri.username() or os.environ.get('USER')
# self.passwd = uri.password()
self.host = uri.host()
md = QgsProviderRegistry.instance().providerMetadata(connection.providerName())
# QgsAbstractDatabaseProviderConnection instance
self.core_connection = md.findConnection(connection.connectionName())
if self.core_connection is None:
self.core_connection = md.createConnection(uri.uri(), {})
c = self._execute(None, "SELECT current_user,current_database()")
self.user, self.dbname = self._fetchone(c)
self._close_cursor(c)
self._checkSpatial()
self._checkRaster()
self._checkGeometryColumnsTable()
self._checkRasterColumnsTable()
self.feedback = None
def _connectionInfo(self):
return str(self.uri().connectionInfo(True))
def _clearSslTempCertsIfAny(self, connectionInfo):
# remove certs (if any) of the connectionInfo
expandedUri = QgsDataSourceUri(connectionInfo)
def removeCert(certFile):
certFile = certFile.replace("'", "")
file = QFile(certFile)
# set permission to allow removing on Win.
# On linux and Mac if file is set with QFile::>ReadUser
# does not create problem removing certs
if not file.setPermissions(QFile.Permission.WriteOwner):
raise Exception(
f"Cannot change permissions on {file.fileName()}: error code: {file.error()}"
)
if not file.remove():
raise Exception(
f"Cannot remove {file.fileName()}: error code: {file.error()}"
)
sslCertFile = expandedUri.param("sslcert")
if sslCertFile:
removeCert(sslCertFile)
sslKeyFile = expandedUri.param("sslkey")
if sslKeyFile:
removeCert(sslKeyFile)
sslCAFile = expandedUri.param("sslrootcert")
if sslCAFile:
removeCert(sslCAFile)
def _checkSpatial(self):
"""check whether postgis_version is present in catalog"""
c = self._execute(
None, "SELECT COUNT(*) FROM pg_proc WHERE proname = 'postgis_version'"
)
self.has_spatial = self._fetchone(c)[0] > 0
self._close_cursor(c)
return self.has_spatial
def _checkRaster(self):
"""check whether postgis_version is present in catalog"""
c = self._execute(
None,
"SELECT COUNT(*) FROM pg_proc WHERE proname = 'postgis_raster_lib_version'",
)
self.has_raster = self._fetchone(c)[0] > 0
self._close_cursor(c)
return self.has_raster
def _checkGeometryColumnsTable(self):
c = self._execute(
None,
"SELECT relkind = 'v' OR relkind = 'm' FROM pg_class WHERE relname = 'geometry_columns' AND relkind IN ('v', 'r', 'm', 'p')",
)
res = self._fetchone(c)
self._close_cursor(c)
self.has_geometry_columns = res is not None and len(res) != 0
if not self.has_geometry_columns:
self.has_geometry_columns_access = self.is_geometry_columns_view = False
else:
self.is_geometry_columns_view = res[0]
# find out whether has privileges to access geometry_columns table
priv = self.getTablePrivileges("geometry_columns")
self.has_geometry_columns_access = priv[0]
return self.has_geometry_columns
def _checkRasterColumnsTable(self):
c = self._execute(
None,
"SELECT relkind = 'v' OR relkind = 'm' FROM pg_class WHERE relname = 'raster_columns' AND relkind IN ('v', 'r', 'm', 'p')",
)
res = self._fetchone(c)
self._close_cursor(c)
self.has_raster_columns = res is not None and len(res) != 0
if not self.has_raster_columns:
self.has_raster_columns_access = self.is_raster_columns_view = False
else:
self.is_raster_columns_view = res[0]
# find out whether has privileges to access geometry_columns table
self.has_raster_columns_access = self.getTablePrivileges("raster_columns")[
0
]
return self.has_raster_columns
def cancel(self):
if self.connection:
self.connection.cancel()
if self.core_connection:
self.feedback.cancel()
def getInfo(self):
c = self._execute(None, "SELECT version()")
res = self._fetchone(c)
self._close_cursor(c)
return res
def getPsqlVersion(self):
regex = r"^PostgreSQL\s([0-9]{1,2})"
match = re.match(regex, self.getInfo()[0])
if match:
return int(match.group(1))
raise DbError(f"Unknown PostgreSQL version: {self.getInfo()[0]}")
def getSpatialInfo(self):
"""returns tuple about PostGIS support:
- lib version
- geos version
- proj version
- installed scripts version
- released scripts version
"""
if not self.has_spatial:
return
try:
c = self._execute(
None,
"SELECT postgis_lib_version(), postgis_geos_version(), postgis_proj_version(), postgis_scripts_installed(), postgis_scripts_released()",
)
except DbError:
return
res = self._fetchone(c)
self._close_cursor(c)
return res
def hasSpatialSupport(self):
return self.has_spatial
def hasRasterSupport(self):
return self.has_raster
def hasCustomQuerySupport(self):
return Qgis.QGIS_VERSION[0:3] >= "1.5"
def hasTableColumnEditingSupport(self):
return True
def hasCreateSpatialViewSupport(self):
return True
def fieldTypes(self):
return [
"integer",
"bigint",
"smallint", # integers
"serial",
"bigserial", # auto-incrementing ints
"real",
"double precision",
"numeric", # floats
"varchar",
"varchar(255)",
"char(20)",
"text", # strings
"date",
"time",
"timestamp", # date/time
"boolean", # bool
]
def getDatabasePrivileges(self):
"""db privileges: (can create schemas, can create temp. tables)"""
sql = "SELECT has_database_privilege(current_database(), 'CREATE'), has_database_privilege(current_database(), 'TEMP')"
c = self._execute(None, sql)
res = self._fetchone(c)
self._close_cursor(c)
return res
def getSchemaPrivileges(self, schema):
"""schema privileges: (can create new objects, can access objects in schema)"""
schema = "current_schema()" if schema is None else self.quoteString(schema)
sql = "SELECT has_schema_privilege({s}, 'CREATE'), has_schema_privilege({s}, 'USAGE')".format(
s=schema
)
c = self._execute(None, sql)
res = self._fetchone(c)
self._close_cursor(c)
return res
def getTablePrivileges(self, table):
"""table privileges: (select, insert, update, delete)"""
schema, tablename = self.getSchemaTableName(table)
schema_priv = self.getSchemaPrivileges(schema)
if not schema_priv[1]:
return
t = self.quoteId(table)
sql = """SELECT has_table_privilege({t}, 'SELECT'), has_table_privilege({t}, 'INSERT'),
has_table_privilege({t}, 'UPDATE'), has_table_privilege({t}, 'DELETE')""".format(
t=self.quoteString(t)
)
c = self._execute(None, sql)
res = self._fetchone(c)
self._close_cursor(c)
return res
def getSchemas(self):
"""get list of schemas in tuples: (oid, name, owner, perms)"""
sql = "SELECT oid, nspname, pg_get_userbyid(nspowner), nspacl, pg_catalog.obj_description(oid) FROM pg_namespace WHERE nspname !~ '^pg_' AND nspname != 'information_schema' ORDER BY nspname"
c = self._execute(None, sql)
res = self._fetchall(c)
self._close_cursor(c)
return res
def getTables(self, schema=None, add_sys_tables=False):
"""get list of tables"""
tablenames = []
items = []
sys_tables = [
"spatial_ref_sys",
"geography_columns",
"geometry_columns",
"raster_columns",
"raster_overviews",
]
try:
vectors = self.getVectorTables(schema)
for tbl in vectors:
if (
not add_sys_tables
and tbl[1] in sys_tables
and tbl[2] in ["", "public"]
):
continue
tablenames.append((tbl[2], tbl[1]))
items.append(tbl)
except DbError:
pass
try:
rasters = self.getRasterTables(schema)
for tbl in rasters:
if (
not add_sys_tables
and tbl[1] in sys_tables
and tbl[2] in ["", "public"]
):
continue
tablenames.append((tbl[2], tbl[1]))
items.append(tbl)
except DbError:
pass
sys_tables = [
"spatial_ref_sys",
"geography_columns",
"geometry_columns",
"raster_columns",
"raster_overviews",
]
if schema:
schema_where = " AND nspname = %s " % self.quoteString(schema)
else:
schema_where = (
" AND (nspname != 'information_schema' AND nspname !~ 'pg_') "
)
# get all tables and views
sql = (
"""SELECT
cla.relname, nsp.nspname, cla.relkind,
pg_get_userbyid(relowner), reltuples, relpages,
pg_catalog.obj_description(cla.oid)
FROM pg_class AS cla
JOIN pg_namespace AS nsp ON nsp.oid = cla.relnamespace
WHERE cla.relkind IN ('v', 'r', 'm', 'p') """
+ schema_where
+ """
ORDER BY nsp.nspname, cla.relname"""
)
c = self._execute(None, sql)
for tbl in self._fetchall(c):
if tablenames.count((tbl[1], tbl[0])) <= 0:
item = list(tbl)
item.insert(0, Table.TableType)
items.append(item)
self._close_cursor(c)
return sorted(items, key=cmp_to_key(lambda x, y: (x[1] > y[1]) - (x[1] < y[1])))
def getVectorTables(self, schema=None):
"""get list of table with a geometry column
it returns:
name (table name)
namespace (schema)
type = 'view' (is a view?)
owner
tuples
pages
geometry_column:
f_geometry_column (or pg_attribute.attname, the geometry column name)
type (or pg_attribute.atttypid::regtype, the geometry column type name)
coord_dimension
srid
"""
if not self.has_spatial:
return []
if schema:
schema_where = " AND nspname = %s " % self.quoteString(schema)
else:
schema_where = (
" AND (nspname != 'information_schema' AND nspname !~ 'pg_') "
)
geometry_column_from = ""
geometry_fields_select = """att.attname,
textin(regtypeout(att.atttypid::regtype)),
NULL, NULL"""
if self.has_geometry_columns and self.has_geometry_columns_access:
geometry_column_from = """LEFT OUTER JOIN geometry_columns AS geo ON
cla.relname = geo.f_table_name AND nsp.nspname = f_table_schema AND
lower(att.attname) = lower(f_geometry_column)"""
geometry_fields_select = """CASE WHEN geo.f_geometry_column IS NOT NULL THEN geo.f_geometry_column ELSE att.attname END,
CASE WHEN geo.type IS NOT NULL THEN geo.type ELSE textin(regtypeout(att.atttypid::regtype)) END,
geo.coord_dimension, geo.srid"""
# discovery of all tables and whether they contain a geometry column
sql = (
"""SELECT
cla.relname, nsp.nspname, cla.relkind,
pg_get_userbyid(relowner), cla.reltuples, cla.relpages,
pg_catalog.obj_description(cla.oid),
"""
+ geometry_fields_select
+ """
FROM pg_class AS cla
JOIN pg_namespace AS nsp ON
nsp.oid = cla.relnamespace
JOIN pg_attribute AS att ON
att.attrelid = cla.oid AND
att.atttypid = 'geometry'::regtype OR
att.atttypid IN (SELECT oid FROM pg_type WHERE typbasetype='geometry'::regtype )
"""
+ geometry_column_from
+ """
WHERE cla.relkind IN ('v', 'r', 'm', 'p') """
+ schema_where
+ """
ORDER BY nsp.nspname, cla.relname, att.attname"""
)
items = []
c = self._execute(None, sql)
for i, tbl in enumerate(self._fetchall(c)):
item = list(tbl)
item.insert(0, Table.VectorType)
items.append(item)
self._close_cursor(c)
return items
def getRasterTables(self, schema=None):
"""get list of table with a raster column
it returns:
name (table name)
namespace (schema)
type = 'view' (is a view?)
owner
tuples
pages
raster_column:
r_raster_column (or pg_attribute.attname, the raster column name)
pixel type
block size
internal or external
srid
"""
if not self.has_spatial:
return []
if not self.has_raster:
return []
if schema:
schema_where = " AND nspname = %s " % self.quoteString(schema)
else:
schema_where = (
" AND (nspname != 'information_schema' AND nspname !~ 'pg_') "
)
raster_column_from = ""
raster_fields_select = """att.attname, NULL, NULL, NULL, NULL, NULL"""
if self.has_raster_columns and self.has_raster_columns_access:
raster_column_from = """LEFT OUTER JOIN raster_columns AS rast ON
cla.relname = rast.r_table_name AND nsp.nspname = r_table_schema AND
lower(att.attname) = lower(r_raster_column)"""
raster_fields_select = """CASE WHEN rast.r_raster_column IS NOT NULL THEN rast.r_raster_column ELSE att.attname END,
rast.pixel_types,
rast.scale_x,
rast.scale_y,
rast.out_db,
rast.srid"""
# discovery of all tables and whether they contain a raster column
sql = (
"""SELECT
cla.relname, nsp.nspname, cla.relkind,
pg_get_userbyid(relowner), cla.reltuples, cla.relpages,
pg_catalog.obj_description(cla.oid),
"""
+ raster_fields_select
+ """
FROM pg_class AS cla
JOIN pg_namespace AS nsp ON
nsp.oid = cla.relnamespace
JOIN pg_attribute AS att ON
att.attrelid = cla.oid AND
att.atttypid = 'raster'::regtype OR
att.atttypid IN (SELECT oid FROM pg_type WHERE typbasetype='raster'::regtype )
"""
+ raster_column_from
+ """
WHERE cla.relkind IN ('v', 'r', 'm', 'p') """
+ schema_where
+ """
ORDER BY nsp.nspname, cla.relname, att.attname"""
)
items = []
c = self._execute(None, sql)
for i, tbl in enumerate(self._fetchall(c)):
item = list(tbl)
item.insert(0, Table.RasterType)
items.append(item)
self._close_cursor(c)
return items
def getTableRowCount(self, table):
c = self._execute(None, "SELECT COUNT(*) FROM %s" % self.quoteId(table))
res = self._fetchone(c)[0]
self._close_cursor(c)
return res
def getTableFields(self, table):
"""return list of columns in table"""
schema, tablename = self.getSchemaTableName(table)
schema_where = (
" AND nspname=%s " % self.quoteString(schema) if schema is not None else ""
)
version_number = self.getPsqlVersion()
ad_col_name = "adsrc" if version_number < 12 else "adbin"
sql = """SELECT a.attnum AS ordinal_position,
a.attname AS column_name,
t.typname AS data_type,
a.attlen AS char_max_len,
a.atttypmod AS modifier,
a.attnotnull AS notnull,
a.atthasdef AS hasdefault,
adef.{} AS default_value,
pg_catalog.format_type(a.atttypid,a.atttypmod) AS formatted_type
FROM pg_class c
JOIN pg_attribute a ON a.attrelid = c.oid
JOIN pg_type t ON a.atttypid = t.oid
JOIN pg_namespace nsp ON c.relnamespace = nsp.oid
LEFT JOIN pg_attrdef adef ON adef.adrelid = a.attrelid AND adef.adnum = a.attnum
WHERE
a.attnum > 0 AND c.relname={} {}
ORDER BY a.attnum""".format(
ad_col_name, self.quoteString(tablename), schema_where
)
c = self._execute(None, sql)
res = self._fetchall(c)
self._close_cursor(c)
return res
def getTableIndexes(self, table):
"""get info about table's indexes. ignore primary key constraint index, they get listed in constraints"""
schema, tablename = self.getSchemaTableName(table)
schema_where = (
" AND nspname=%s " % self.quoteString(schema) if schema is not None else ""
)
sql = """SELECT idxcls.relname, indkey, indisunique = 't'
FROM pg_index JOIN pg_class ON pg_index.indrelid=pg_class.oid
JOIN pg_class AS idxcls ON pg_index.indexrelid=idxcls.oid
JOIN pg_namespace nsp ON pg_class.relnamespace = nsp.oid
WHERE pg_class.relname={} {}
AND indisprimary != 't' """.format(
self.quoteString(tablename), schema_where
)
c = self._execute(None, sql)
res = self._fetchall(c)
self._close_cursor(c)
return res
def getTableConstraints(self, table):
schema, tablename = self.getSchemaTableName(table)
schema_where = (
" AND nspname=%s " % self.quoteString(schema) if schema is not None else ""
)
version_number = self.getPsqlVersion()
con_col_name = "consrc" if version_number < 12 else "conbin"
# In the query below, we exclude rows where pg_constraint.contype whose values are equal to 't'
# because 't' describes a CONSTRAINT TRIGGER, which is not really a constraint in the traditional
# sense, but a special type of trigger, and an extension to the SQL standard.
sql = """SELECT c.conname, c.contype, c.condeferrable, c.condeferred, array_to_string(c.conkey, ' '), c.{},
t2.relname, c.confupdtype, c.confdeltype, c.confmatchtype, array_to_string(c.confkey, ' ') FROM pg_constraint c
LEFT JOIN pg_class t ON c.conrelid = t.oid
LEFT JOIN pg_class t2 ON c.confrelid = t2.oid
JOIN pg_namespace nsp ON t.relnamespace = nsp.oid
WHERE c.contype <> 't' AND t.relname = {} {} """.format(
con_col_name, self.quoteString(tablename), schema_where
)
c = self._execute(None, sql)
res = self._fetchall(c)
self._close_cursor(c)
return res
def getTableTriggers(self, table):
schema, tablename = self.getSchemaTableName(table)
schema_where = (
" AND nspname=%s " % self.quoteString(schema) if schema is not None else ""
)
sql = """SELECT tgname, proname, tgtype, tgenabled NOT IN ('f', 'D') FROM pg_trigger trig
LEFT JOIN pg_class t ON trig.tgrelid = t.oid
LEFT JOIN pg_proc p ON trig.tgfoid = p.oid
JOIN pg_namespace nsp ON t.relnamespace = nsp.oid
WHERE t.relname = {} {} """.format(
self.quoteString(tablename), schema_where
)
c = self._execute(None, sql)
res = self._fetchall(c)
self._close_cursor(c)
return res
def enableAllTableTriggers(self, enable, table):
"""enable or disable all triggers on table"""
self.enableTableTrigger(None, enable, table)
def enableTableTrigger(self, trigger, enable, table):
"""enable or disable one trigger on table"""
trigger = self.quoteId(trigger) if trigger is not None else "ALL"
sql = "ALTER TABLE {} {} TRIGGER {}".format(
self.quoteId(table), "ENABLE" if enable else "DISABLE", trigger
)
self._execute_and_commit(sql)
def deleteTableTrigger(self, trigger, table):
"""Deletes trigger on table"""
sql = f"DROP TRIGGER {self.quoteId(trigger)} ON {self.quoteId(table)}"
self._execute_and_commit(sql)
def getTableRules(self, table):
schema, tablename = self.getSchemaTableName(table)
schema_where = (
" AND schemaname=%s " % self.quoteString(schema)
if schema is not None
else ""
)
sql = """SELECT rulename, definition FROM pg_rules
WHERE tablename={} {} """.format(
self.quoteString(tablename), schema_where
)
c = self._execute(None, sql)
res = self._fetchall(c)
self._close_cursor(c)
return res
def deleteTableRule(self, rule, table):
"""Deletes rule on table"""
sql = f"DROP RULE {self.quoteId(rule)} ON {self.quoteId(table)}"
self._execute_and_commit(sql)
def getTableExtent(self, table, geom):
"""find out table extent"""
subquery = "SELECT st_extent({}) AS extent FROM {}".format(
self.quoteId(geom), self.quoteId(table)
)
sql = (
"SELECT st_xmin(extent), st_ymin(extent), st_xmax(extent), st_ymax(extent) FROM (%s) AS subquery"
% subquery
)
c = self._execute(None, sql)
res = self._fetchone(c)
self._close_cursor(c)
return res
def getTableEstimatedExtent(self, table, geom):
"""find out estimated extent (from the statistics)"""
if self.isRasterTable(table):
return
schema, tablename = self.getSchemaTableName(table)
schema_part = "%s," % self.quoteString(schema) if schema is not None else ""
pgis_versions = self.getSpatialInfo()[0].split(".")
pgis_major_version = int(pgis_versions[0])
pgis_minor_version = int(pgis_versions[1])
pgis_old = False
if pgis_major_version < 2:
pgis_old = True
elif pgis_major_version == 2 and pgis_minor_version < 1:
pgis_old = True
subquery = "SELECT {}({}{},{}) AS extent".format(
"st_estimated_extent" if pgis_old else "st_estimatedextent",
schema_part,
self.quoteString(tablename),
self.quoteString(geom),
)
sql = (
"""SELECT st_xmin(extent), st_ymin(extent), st_xmax(extent), st_ymax(extent) FROM (%s) AS subquery """
% subquery
)
try:
c = self._execute(None, sql)
except DbError: # No statistics for the current table
return
res = self._fetchone(c)
self._close_cursor(c)
return res
def getViewDefinition(self, view):
"""returns definition of the view"""
schema, tablename = self.getSchemaTableName(view)
schema_where = (
" AND nspname=%s " % self.quoteString(schema) if schema is not None else ""
)
sql = """SELECT pg_get_viewdef(c.oid) FROM pg_class c
JOIN pg_namespace nsp ON c.relnamespace = nsp.oid
WHERE relname={} {} AND (relkind='v' OR relkind='m') """.format(
self.quoteString(tablename), schema_where
)
c = self._execute(None, sql)
res = self._fetchone(c)
self._close_cursor(c)
return res[0] if res is not None else None
def getCrs(self, srid):
if not self.has_spatial:
return QgsCoordinateReferenceSystem()
try:
c = self._execute(
None, "SELECT proj4text FROM spatial_ref_sys WHERE srid = '%d'" % srid
)
except DbError:
return QgsCoordinateReferenceSystem()
res = self._fetchone(c)
self._close_cursor(c)
if res is None:
return QgsCoordinateReferenceSystem()
proj4text = res[0]
crs = QgsCoordinateReferenceSystem.fromProj(proj4text)
return crs
def getSpatialRefInfo(self, srid):
if not self.has_spatial:
return
try:
c = self._execute(
None, "SELECT srtext FROM spatial_ref_sys WHERE srid = '%d'" % srid
)
except DbError:
return
sr = self._fetchone(c)
self._close_cursor(c)
if sr is None:
return
srtext = sr[0]
# try to extract just SR name (should be quoted in double quotes)
regex = QRegularExpression('"([^"]+)"')
match = regex.match(srtext)
if match.hasMatch():
srtext = match.captured(1)
return srtext
def isVectorTable(self, table):
if self.has_geometry_columns and self.has_geometry_columns_access:
schema, tablename = self.getSchemaTableName(table)
sql = "SELECT count(*) FROM geometry_columns WHERE f_table_schema = {} AND f_table_name = {}".format(
self.quoteString(schema), self.quoteString(tablename)
)
c = self._execute(None, sql)
res = self._fetchone(c)
self._close_cursor(c)
return res is not None and res[0] > 0
return False
def isRasterTable(self, table):
if self.has_raster_columns and self.has_raster_columns_access:
schema, tablename = self.getSchemaTableName(table)
sql = "SELECT count(*) FROM raster_columns WHERE r_table_schema = {} AND r_table_name = {}".format(
self.quoteString(schema), self.quoteString(tablename)
)
c = self._execute(None, sql)
res = self._fetchone(c)
self._close_cursor(c)
return res is not None and res[0] > 0
return False
def createTable(self, table, field_defs, pkey):
"""Creates ordinary table
'fields' is array containing field definitions
'pkey' is the primary key name
"""
if len(field_defs) == 0:
return False
sql = "CREATE TABLE %s (" % self.quoteId(table)
sql += ", ".join(field_defs)
if pkey is not None and pkey != "":
sql += ", PRIMARY KEY (%s)" % self.quoteId(pkey)
sql += ")"
self._execute_and_commit(sql)
return True
def deleteTable(self, table):
"""Deletes table and its reference in either geometry_columns or raster_columns"""
schema, tablename = self.getSchemaTableName(table)
schema_part = "%s, " % self.quoteString(schema) if schema is not None else ""
if self.isVectorTable(table):
sql = "SELECT DropGeometryTable({}{})".format(
schema_part, self.quoteString(tablename)
)
elif self.isRasterTable(table):
# Fix #8521: delete raster table and references from raster_columns table
sql = "DROP TABLE %s" % self.quoteId(table)
else:
sql = "DROP TABLE %s" % self.quoteId(table)
self._execute_and_commit(sql)
def emptyTable(self, table):
"""Deletes all rows from table"""
sql = "TRUNCATE %s" % self.quoteId(table)
self._execute_and_commit(sql)
def renameTable(self, table, new_table):
"""Renames a table in database"""
schema, tablename = self.getSchemaTableName(table)
if new_table == tablename:
return
sql = "ALTER TABLE {} RENAME TO {}".format(
self.quoteId(table), self.quoteId(new_table)
)
self._executeSql(sql)
# update geometry_columns if PostGIS is enabled
if self.has_geometry_columns and not self.is_geometry_columns_view:
schema_where = (
" AND f_table_schema=%s " % self.quoteString(schema)
if schema is not None
else ""
)
sql = "UPDATE geometry_columns SET f_table_name={} WHERE f_table_name={} {}".format(
self.quoteString(new_table), self.quoteString(tablename), schema_where
)
self._executeSql(sql)
def renameSchema(self, schema, new_schema):
try:
self.core_connection.renameSchema(schema, new_schema)
return True
except QgsProviderConnectionException:
return False
def commentTable(self, schema, tablename, comment=None):
if comment is None:
self._execute(None, f'COMMENT ON TABLE "{schema}"."{tablename}" IS NULL;')
else:
self._execute(
None,
f'COMMENT ON TABLE "{schema}"."{tablename}" IS $escape${comment}$escape$;',
)
def getComment(self, tablename, field):
"""Returns the comment for a field"""
# SQL Query checking if a comment exists for the field
sql_cpt = "Select count(*) from pg_description pd, pg_class pc, pg_attribute pa where relname = '{}' and attname = '{}' and pa.attrelid = pc.oid and pd.objoid = pc.oid and pd.objsubid = pa.attnum".format(
tablename, field
)
# SQL Query that return the comment of the field
sql = "Select pd.description from pg_description pd, pg_class pc, pg_attribute pa where relname = '{}' and attname = '{}' and pa.attrelid = pc.oid and pd.objoid = pc.oid and pd.objsubid = pa.attnum".format(
tablename, field
)
c = self._execute(None, sql_cpt) # Execute Check query
res = self._fetchone(c)[0] # Store result
if res == 1:
# When a comment exists
c = self._execute(None, sql) # Execute query
res = self._fetchone(c)[0] # Store result
self._close_cursor(c) # Close cursor
return res # Return comment
else:
return ""
def moveTableToSchema(self, table, new_schema):
schema, tablename = self.getSchemaTableName(table)
if new_schema == schema:
return
c = self._get_cursor()
sql = "ALTER TABLE {} SET SCHEMA {}".format(
self.quoteId(table), self.quoteId(new_schema)
)
self._execute(c, sql)
# update geometry_columns if PostGIS is enabled
if self.has_geometry_columns and not self.is_geometry_columns_view:
schema, tablename = self.getSchemaTableName(table)
schema_where = (
" AND f_table_schema=%s " % self.quoteString(schema)
if schema is not None
else ""
)
sql = "UPDATE geometry_columns SET f_table_schema={} WHERE f_table_name={} {}".format(
self.quoteString(new_schema), self.quoteString(tablename), schema_where
)
self._execute(c, sql)
self._commit()
def moveTable(self, table, new_table, new_schema=None):
schema, tablename = self.getSchemaTableName(table)
if new_schema == schema and new_table == tablename:
return
if new_schema == schema:
return self.renameTable(table, new_table)
if new_table == table:
return self.moveTableToSchema(table, new_schema)
c = self._get_cursor()
t = "__new_table__"
sql = "ALTER TABLE {} RENAME TO {}".format(
self.quoteId(table), self.quoteId(t)
)
self._execute(c, sql)
sql = "ALTER TABLE {} SET SCHEMA {}".format(
self.quoteId((schema, t)), self.quoteId(new_schema)
)
self._execute(c, sql)
sql = "ALTER TABLE {} RENAME TO {}".format(
self.quoteId((new_schema, t)), self.quoteId(table)
)
self._execute(c, sql)
# update geometry_columns if PostGIS is enabled
if self.has_geometry_columns and not self.is_geometry_columns_view:
schema, tablename = self.getSchemaTableName(table)
schema_where = (
" f_table_schema=%s AND " % self.quoteString(schema)
if schema is not None
else ""
)
schema_part = (
" f_table_schema=%s, " % self.quoteString(new_schema)
if schema is not None
else ""
)
sql = "UPDATE geometry_columns SET {} f_table_name={} WHERE {} f_table_name={}".format(
schema_part,
self.quoteString(new_table),
schema_where,
self.quoteString(tablename),
)
self._execute(c, sql)
self._commit()
def createView(self, view, query):
view_name_parts = view.split(".")
if len(view_name_parts) > 2:
# Raise an error when more than one period is used.
raise DbError(
"Invalid view name: Please use the format 'schema.viewname', or enter only the viewname for the public schema."
)
elif len(view_name_parts) == 2: # To allow view creation into specified schema
schema, view_name = view_name_parts
sql = "CREATE VIEW {} AS {}".format(
self.quoteId([schema, view_name]), query
)
else: # No specific schema specified
sql = f"CREATE VIEW {self.quoteId(view)} AS {query}"
self._execute_and_commit(sql)
def createSpatialView(self, view, query):
self.createView(view, query)
def deleteView(self, view, isMaterialized=False):
sql = "DROP {} VIEW {}".format(
"MATERIALIZED" if isMaterialized else "", self.quoteId(view)
)
self._execute_and_commit(sql)
def renameView(self, view, new_name):
"""Renames view in database"""
self.renameTable(view, new_name)
def createSchema(self, schema):
"""Creates a new empty schema in database"""
sql = "CREATE SCHEMA %s" % self.quoteId(schema)
self._execute_and_commit(sql)
def deleteSchema(self, schema):
"""Drops (empty) schema from database"""
sql = "DROP SCHEMA %s" % self.quoteId(schema)
self._execute_and_commit(sql)
def renamesSchema(self, schema, new_schema):
"""Renames a schema in database"""
sql = "ALTER SCHEMA {} RENAME TO {}".format(
self.quoteId(schema), self.quoteId(new_schema)
)
self._execute_and_commit(sql)
def runVacuum(self):
"""Runs vacuum on the db"""
self._execute_and_commit("VACUUM")
def runVacuumAnalyze(self, table):
"""Runs vacuum analyze on a table"""
sql = "VACUUM ANALYZE %s" % self.quoteId(table)
self._execute(None, sql)
self._commit()
def runRefreshMaterializedView(self, table):
"""Runs refresh materialized view on a table"""
sql = "REFRESH MATERIALIZED VIEW %s" % self.quoteId(table)
self._execute(None, sql)
self._commit()
def addTableColumn(self, table, field_def):
"""Adds a column to table"""
sql = f"ALTER TABLE {self.quoteId(table)} ADD {field_def}"
self._execute_and_commit(sql)
def deleteTableColumn(self, table, column):
"""Deletes column from a table"""
if self.isGeometryColumn(table, column):
# use PostGIS function to delete geometry column correctly
schema, tablename = self.getSchemaTableName(table)
schema_part = "%s, " % self.quoteString(schema) if schema else ""
sql = "SELECT DropGeometryColumn({}{}, {})".format(
schema_part, self.quoteString(tablename), self.quoteString(column)
)
else:
sql = "ALTER TABLE {} DROP {}".format(
self.quoteId(table), self.quoteId(column)
)
self._execute_and_commit(sql)
def updateTableColumn(
self,
table,
column,
new_name=None,
data_type=None,
not_null=None,
default=None,
comment=None,
test=None,
):
if (
new_name is None
and data_type is None
and not_null is None
and default is None
and comment is None
):
return
c = self._get_cursor()
# update column definition
col_actions = []
if data_type is not None:
col_actions.append("TYPE %s" % data_type)
if not_null is not None:
col_actions.append("SET NOT NULL" if not_null else "DROP NOT NULL")
if default is not None:
if default and default != "":
col_actions.append("SET DEFAULT %s" % default)
else:
col_actions.append("DROP DEFAULT")
if len(col_actions) > 0:
sql = "ALTER TABLE %s" % self.quoteId(table)
alter_col_str = "ALTER %s" % self.quoteId(column)
for a in col_actions:
sql += f" {alter_col_str} {a},"
self._execute(c, sql[:-1])
# Renames the column
if new_name is not None and new_name != column:
sql = "ALTER TABLE {} RENAME {} TO {}".format(
self.quoteId(table), self.quoteId(column), self.quoteId(new_name)
)
self._execute(c, sql)
# update geometry_columns if PostGIS is enabled
if self.has_geometry_columns and not self.is_geometry_columns_view:
schema, tablename = self.getSchemaTableName(table)
schema_where = (
" f_table_schema=%s AND " % self.quoteString(schema)
if schema is not None
else ""
)
sql = "UPDATE geometry_columns SET f_geometry_column={} WHERE {} f_table_name={} AND f_geometry_column={}".format(
self.quoteString(new_name),
schema_where,
self.quoteString(tablename),
self.quoteString(column),
)
self._execute(c, sql)
# comment the column
if comment is not None:
schema, tablename = self.getSchemaTableName(table)
column_name = (
new_name if new_name is not None and new_name != column else column
)
sql = "COMMENT ON COLUMN {}.{}.{} IS '{}'".format(
schema, tablename, column_name, comment
)
self._execute(c, sql)
self._commit()
def renamesTableColumn(self, table, column, new_name):
"""Renames column in a table"""
return self.updateTableColumn(table, column, new_name)
def setTableColumnType(self, table, column, data_type):
"""Changes column type"""
return self.updateTableColumn(table, column, None, data_type)
def setTableColumnNull(self, table, column, is_null):
"""Changes whether column can contain null values"""
return self.updateTableColumn(table, column, None, None, not is_null)
def setTableColumnDefault(self, table, column, default):
"""Changes column's default value.
If default=None or an empty string drop default value"""
return self.updateTableColumn(table, column, None, None, None, default)
def isGeometryColumn(self, table, column):
schema, tablename = self.getSchemaTableName(table)
schema_where = (
" f_table_schema=%s AND " % self.quoteString(schema)
if schema is not None
else ""
)
sql = "SELECT count(*) > 0 FROM geometry_columns WHERE {} f_table_name={} AND f_geometry_column={}".format(
schema_where, self.quoteString(tablename), self.quoteString(column)
)
c = self._execute(None, sql)
res = self._fetchone(c)[0] == "t"
self._close_cursor(c)
return res
def addGeometryColumn(
self, table, geom_column="geom", geom_type="POINT", srid=-1, dim=2
):
schema, tablename = self.getSchemaTableName(table)
schema_part = "%s, " % self.quoteString(schema) if schema else ""
sql = "SELECT AddGeometryColumn(%s%s, %s, %d, %s, %d)" % (
schema_part,
self.quoteString(tablename),
self.quoteString(geom_column),
srid,
self.quoteString(geom_type),
dim,
)
self._execute_and_commit(sql)
def deleteGeometryColumn(self, table, geom_column):
return self.deleteTableColumn(table, geom_column)
def addTableUniqueConstraint(self, table, column):
"""Adds a unique constraint to a table"""
sql = "ALTER TABLE {} ADD UNIQUE ({})".format(
self.quoteId(table), self.quoteId(column)
)
self._execute_and_commit(sql)
def deleteTableConstraint(self, table, constraint):
"""Deletes constraint in a table"""
sql = "ALTER TABLE {} DROP CONSTRAINT {}".format(
self.quoteId(table), self.quoteId(constraint)
)
self._execute_and_commit(sql)
def addTablePrimaryKey(self, table, column):
"""Adds a primery key (with one column) to a table"""
sql = "ALTER TABLE {} ADD PRIMARY KEY ({})".format(
self.quoteId(table), self.quoteId(column)
)
self._execute_and_commit(sql)
def createTableIndex(self, table, name, column):
"""Creates index on one column using default options"""
sql = "CREATE INDEX {} ON {} ({})".format(
self.quoteId(name), self.quoteId(table), self.quoteId(column)
)
self._execute_and_commit(sql)
def deleteTableIndex(self, table, name):
schema, tablename = self.getSchemaTableName(table)
sql = "DROP INDEX %s" % self.quoteId((schema, name))
self._execute_and_commit(sql)
def createSpatialIndex(self, table, geom_column="geom"):
schema, tablename = self.getSchemaTableName(table)
idx_name = self.quoteId(f"sidx_{tablename}_{geom_column}")
sql = "CREATE INDEX {} ON {} USING GIST({})".format(
idx_name, self.quoteId(table), self.quoteId(geom_column)
)
self._execute_and_commit(sql)
def deleteSpatialIndex(self, table, geom_column="geom"):
schema, tablename = self.getSchemaTableName(table)
idx_name = self.quoteId(f"sidx_{tablename}_{geom_column}")
return self.deleteTableIndex(table, idx_name)
def _execute(self, cursor, sql):
if cursor is not None:
cursor._execute(sql)
return cursor
self.feedback = QgsFeedback()
return CursorAdapter(self.core_connection, sql, feedback=self.feedback)
def _executeSql(self, sql):
return self.core_connection.executeSql(sql)
def _get_cursor(self, name=None):
# if name is not None:
# print("XXX _get_cursor called with a Name: " + name)
return CursorAdapter(self.core_connection, name)
def _commit(self):
pass
# moved into the parent class: DbConnector._rollback()
def _rollback(self):
pass
# moved into the parent class: DbConnector._get_cursor_columns()
# def _get_cursor_columns(self, c):
# pass
def getSqlDictionary(self):
from .sql_dictionary import getSqlDictionary
sql_dict = getSqlDictionary()
# get schemas, tables and field names
sql = """SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_' AND nspname != 'information_schema'
UNION SELECT relname FROM pg_class WHERE relkind IN ('v', 'r', 'm', 'p')
UNION SELECT attname FROM pg_attribute WHERE attnum > 0"""
c = self._execute(None, sql)
items = [row[0] for row in self._fetchall(c)]
self._close_cursor(c)
sql_dict["identifier"] = items
return sql_dict
def getQueryBuilderDictionary(self):
from .sql_dictionary import getQueryBuilderDictionary
return getQueryBuilderDictionary()
|