32 |
return psycopg.__version__ |
return psycopg.__version__ |
33 |
|
|
34 |
if psycopg is not None: |
if psycopg is not None: |
35 |
|
# type_map maps psycopg type objects. It's a list of pairs since |
36 |
|
# the psycopg type objects are unhashable. |
37 |
type_map = [(psycopg.STRING, table.FIELDTYPE_STRING), |
type_map = [(psycopg.STRING, table.FIELDTYPE_STRING), |
38 |
(psycopg.INTEGER, table.FIELDTYPE_INT), |
(psycopg.INTEGER, table.FIELDTYPE_INT), |
39 |
|
(psycopg.ROWID, table.FIELDTYPE_INT), |
40 |
(psycopg.FLOAT, table.FIELDTYPE_DOUBLE)] |
(psycopg.FLOAT, table.FIELDTYPE_DOUBLE)] |
41 |
|
|
42 |
|
# _raw_type_map maps the postgresql type constants to Thuban type |
43 |
|
# constants. This is very low level and postgresql specific and |
44 |
|
# should be used only when necessary. |
45 |
|
_raw_type_map = {} |
46 |
|
def _fill_raw_type_map(): |
47 |
|
for psycopg_type, thuban_type in type_map: |
48 |
|
for value in psycopg_type.values: |
49 |
|
_raw_type_map[value] = thuban_type |
50 |
|
_fill_raw_type_map() |
51 |
|
|
52 |
|
|
53 |
def quote_identifier(ident): |
def quote_identifier(ident): |
54 |
"""Return a quoted version of the identifier ident. |
"""Return a quoted version of the identifier ident. |
148 |
|
|
149 |
def GeometryTables(self): |
def GeometryTables(self): |
150 |
"""Return a list with the names of all tables with a geometry column""" |
"""Return a list with the names of all tables with a geometry column""" |
151 |
|
|
152 |
|
# The query is basically taken from the psql v. 7.2.1. When |
153 |
|
# started with -E it prints the queries used for internal |
154 |
|
# commands such as \d, which does mostly what we need here. |
155 |
cursor = self.connection.cursor() |
cursor = self.connection.cursor() |
156 |
cursor.execute("SELECT f_table_name FROM geometry_columns;") |
cursor.execute("SELECT c.relname FROM pg_class c" |
157 |
|
" WHERE c.relkind IN ('r', 'v')" |
158 |
|
# Omit the system tables |
159 |
|
" AND c.relname !~ '^pg_'" |
160 |
|
# Omit the special PostGIS tables |
161 |
|
" AND c.relname NOT IN ('geometry_columns'," |
162 |
|
" 'spatial_ref_sys')" |
163 |
|
" AND %d in (SELECT a.atttypid FROM pg_attribute a" |
164 |
|
" WHERE a.attrelid = c.oid)" |
165 |
|
" ORDER BY c.relname;", (self.geometry_type,)) |
166 |
result = [row[0] for row in cursor.fetchall()] |
result = [row[0] for row in cursor.fetchall()] |
167 |
self.connection.commit() |
self.connection.commit() |
168 |
return result |
return result |
169 |
|
|
170 |
|
def table_columns(self, tablename): |
171 |
|
"""Experimental: return information about the columns of a table |
172 |
|
|
173 |
|
Return value is a list of (name, type) pairs where name is the |
174 |
|
name of the column and type either one of the field type columns |
175 |
|
or the string 'geometry' indicating a geometry column. |
176 |
|
|
177 |
|
The intended use of this method is for table selection dialogs |
178 |
|
which need to determine which columns are usable as id or |
179 |
|
geometry columns respectively. Suitable id columns will have |
180 |
|
type FIELDTYPE_INT and geometry columns will have 'geometry'. |
181 |
|
""" |
182 |
|
result = [] |
183 |
|
cursor = self.connection.cursor() |
184 |
|
|
185 |
|
# This query is taken basically from the \d command of psql |
186 |
|
# 7.2.1 |
187 |
|
cursor.execute("SELECT a.attname, a.atttypid, a.attnum" |
188 |
|
" FROM pg_class c, pg_attribute a" |
189 |
|
" WHERE c.relname = %s AND a.attrelid = c.oid" |
190 |
|
" ORDER BY a.attnum;", (tablename,)) |
191 |
|
|
192 |
|
for row in cursor.fetchall(): |
193 |
|
col_name, col_type, col_attnum = row |
194 |
|
col = None |
195 |
|
if col_attnum < 1: |
196 |
|
# It's a system column. Only the OID is interesting |
197 |
|
# here |
198 |
|
if col_name == "oid": |
199 |
|
col = (col_name, _raw_type_map[col_type]) |
200 |
|
else: |
201 |
|
# If it's an integer |
202 |
|
thuban_type = _raw_type_map.get(col_type) |
203 |
|
if thuban_type is not None: |
204 |
|
col = (col_name, thuban_type) |
205 |
|
elif row[1] == self.geometry_type: |
206 |
|
col = (col_name, "geometry") |
207 |
|
if col is not None: |
208 |
|
result.append(col) |
209 |
|
|
210 |
|
return result |
211 |
|
|
212 |
def cursor(self): |
def cursor(self): |
213 |
"""Return a DB API 2.0 cursor for the database""" |
"""Return a DB API 2.0 cursor for the database""" |
214 |
return self.connection.cursor() |
return self.connection.cursor() |
243 |
descriptions returned by Columns() and other methods. |
descriptions returned by Columns() and other methods. |
244 |
""" |
""" |
245 |
|
|
246 |
def __init__(self, db, tablename): |
def __init__(self, db, tablename, id_column = None): |
247 |
"""Initialize the PostGISTable. |
"""Initialize the PostGISTable. |
248 |
|
|
249 |
The db parameter should be an instance of PostGISConnection and |
The db parameter should be an instance of PostGISConnection and |
250 |
tablename the name of a table in the database represented by db. |
tablename the name of a table in the database represented by db. |
251 |
|
|
252 |
|
The id_column parameter should be the name of a column in the |
253 |
|
table that can be used to identify rows. The column must have |
254 |
|
the type integer and be unique and not null. |
255 |
|
|
256 |
|
For backwards compatibility reasons, the id_column parameter is |
257 |
|
optional. If not given the table must have a column called |
258 |
|
'gid' which is used as the id_column. New code should always |
259 |
|
provide this parameter. |
260 |
""" |
""" |
261 |
self.db = db |
self.db = db |
262 |
self.tablename = tablename |
self.tablename = tablename |
263 |
# Tablename quoted for use in SQL statements. |
# Tablename quoted for use in SQL statements. |
264 |
self.quoted_tablename = quote_identifier(tablename) |
self.quoted_tablename = quote_identifier(tablename) |
265 |
|
|
266 |
|
if not id_column: |
267 |
|
id_column = "gid" |
268 |
|
self.id_column = id_column |
269 |
|
# id column name quoted for use in SQL statements. |
270 |
|
self.quoted_id_column = quote_identifier(id_column) |
271 |
|
|
272 |
# Map column names and indices to column objects. |
# Map column names and indices to column objects. |
273 |
self.column_map = {} |
self.column_map = {} |
274 |
|
|
319 |
"""Return the dbconnection used by the table""" |
"""Return the dbconnection used by the table""" |
320 |
return self.db |
return self.db |
321 |
|
|
322 |
|
def IDColumn(self): |
323 |
|
"""Return the column description object for the id column. |
324 |
|
|
325 |
|
If the oid column was used as the id column, the return value is |
326 |
|
not one of the regular column objects that would be returned by |
327 |
|
e.g. the Column() method, but it still has meaningful name |
328 |
|
attribute. |
329 |
|
""" |
330 |
|
if self.id_column == "oid": |
331 |
|
return PostGISColumn(self.id_column, table.FIELDTYPE_INT, None) |
332 |
|
return self.column_map[self.id_column] |
333 |
|
|
334 |
def TableName(self): |
def TableName(self): |
335 |
"""Return the name of the table in the database""" |
"""Return the name of the table in the database""" |
336 |
return self.tablename |
return self.tablename |
367 |
def RowIdToOrdinal(self, gid): |
def RowIdToOrdinal(self, gid): |
368 |
"""Return the row ordinal given its id""" |
"""Return the row ordinal given its id""" |
369 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
370 |
cursor.execute("SELECT count(*) FROM %s WHERE gid < %d;" |
cursor.execute("SELECT count(*) FROM %s WHERE %s < %d;" |
371 |
% (self.quoted_tablename, gid)) |
% (self.quoted_tablename, self.quoted_id_column, gid)) |
372 |
return cursor.fetchone()[0] |
return cursor.fetchone()[0] |
373 |
|
|
374 |
def RowOrdinalToId(self, num): |
def RowOrdinalToId(self, num): |
375 |
"""Return the rowid for given its ordinal""" |
"""Return the rowid for given its ordinal""" |
376 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
377 |
cursor.execute("SELECT gid FROM %s LIMIT 1 OFFSET %d;" |
cursor.execute("SELECT %s FROM %s LIMIT 1 OFFSET %d;" |
378 |
% (self.quoted_tablename, num)) |
% (self.quoted_id_column, self.quoted_tablename, num)) |
379 |
return cursor.fetchone()[0] |
return cursor.fetchone()[0] |
380 |
|
|
381 |
def ReadRowAsDict(self, row, row_is_ordinal = 0): |
def ReadRowAsDict(self, row, row_is_ordinal = 0): |
383 |
if row_is_ordinal: |
if row_is_ordinal: |
384 |
stmt = self.query_stmt + " LIMIT 1 OFFSET %d" % row |
stmt = self.query_stmt + " LIMIT 1 OFFSET %d" % row |
385 |
else: |
else: |
386 |
stmt = self.query_stmt + " WHERE gid = %d" % row |
stmt = self.query_stmt + " WHERE %s = %d" % (self.quoted_id_column, |
387 |
|
row) |
388 |
cursor.execute(stmt) |
cursor.execute(stmt) |
389 |
result = {} |
result = {} |
390 |
for col, value in zip(self.columns, cursor.fetchone()): |
for col, value in zip(self.columns, cursor.fetchone()): |
398 |
(self.column_map[col].quoted_name, self.quoted_tablename, |
(self.column_map[col].quoted_name, self.quoted_tablename, |
399 |
row)) |
row)) |
400 |
else: |
else: |
401 |
stmt = ("SELECT %s FROM %s WHERE gid = %d" % |
stmt = ("SELECT %s FROM %s WHERE %s = %d" % |
402 |
(self.column_map[col].quoted_name, self.quoted_tablename, |
(self.column_map[col].quoted_name, self.quoted_tablename, |
403 |
row)) |
self.quoted_id_column, row)) |
404 |
cursor.execute(stmt) |
cursor.execute(stmt) |
405 |
return cursor.fetchone()[0] |
return cursor.fetchone()[0] |
406 |
|
|
432 |
right_template = "%s" |
right_template = "%s" |
433 |
params = (right,) |
params = (right,) |
434 |
|
|
435 |
query = "SELECT gid FROM %s WHERE %s %s %s ORDER BY gid;" \ |
query = "SELECT %s FROM %s WHERE %s %s %s ORDER BY %s;" \ |
436 |
% (self.quoted_tablename, left.quoted_name, comparison, |
% (self.quoted_id_column, self.quoted_tablename, |
437 |
right_template) |
left.quoted_name, comparison, right_template, |
438 |
|
self.quoted_id_column) |
439 |
|
|
440 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
441 |
cursor.execute(query, params) |
cursor.execute(query, params) |
486 |
|
|
487 |
"""Shapestore interface to a table in a PostGIS database""" |
"""Shapestore interface to a table in a PostGIS database""" |
488 |
|
|
489 |
def __init__(self, db, tablename, geometry_column = None): |
def __init__(self, db, tablename, id_column = "gid", |
490 |
|
geometry_column = None): |
491 |
"""Initialize the PostGISShapeStore. |
"""Initialize the PostGISShapeStore. |
492 |
|
|
493 |
The db parameter should be an instance of PostGISConnection and |
The db parameter should be an instance of PostGISConnection and |
494 |
tablename the name of a table in the database represented by db. |
tablename the name of a table in the database represented by db. |
495 |
|
|
496 |
|
The id_column parameter should be the name of a column in the |
497 |
|
table that can be used to identify rows. The column must have |
498 |
|
the type integer and be unique and not null. |
499 |
|
|
500 |
The geometry_column paramter, if given, should be the name of |
The geometry_column paramter, if given, should be the name of |
501 |
the geometry column to use. If the name given is not a geometry |
the geometry column to use. If the name given is not a geometry |
502 |
column, raise a ValueError. |
column, raise a ValueError. |
507 |
""" |
""" |
508 |
self.geometry_column = geometry_column |
self.geometry_column = geometry_column |
509 |
self.geometry_column_was_given = geometry_column is not None |
self.geometry_column_was_given = geometry_column is not None |
510 |
PostGISTable.__init__(self, db, tablename) |
PostGISTable.__init__(self, db, tablename, id_column) |
511 |
|
|
512 |
# For convenience, we have a quoted version of the geometry |
# For convenience, we have a quoted version of the geometry |
513 |
# column in self.quoted_geo_col |
# column in self.quoted_geo_col |
525 |
def _create_col_from_description(self, index, description): |
def _create_col_from_description(self, index, description): |
526 |
"""Extend the inherited method to find geometry columns |
"""Extend the inherited method to find geometry columns |
527 |
|
|
528 |
If the column indicated by the paramters is a geometry column, |
If the column indicated by the parameters is a geometry column, |
529 |
record its name in self.geometry_column and a quoted version in |
record its name in self.geometry_column and a quoted version in |
530 |
self.quoted_geo_col. In any case return the return value of the |
self.quoted_geo_col. In any case return the return value of the |
531 |
inherited method. |
inherited method. |
562 |
""" |
""" |
563 |
return None |
return None |
564 |
|
|
565 |
|
def GeometryColumn(self): |
566 |
|
"""Return the column description object for the geometry column |
567 |
|
|
568 |
|
There's currently no FIELDTYPE constant for this column, so the |
569 |
|
return value is not a regular column object that could also be |
570 |
|
returned from e.g. the Column() method. Only the name attribute |
571 |
|
of the return value is meaningful at the moment. |
572 |
|
""" |
573 |
|
return PostGISColumn(self.geometry_column, None, None) |
574 |
|
|
575 |
def ShapeType(self): |
def ShapeType(self): |
576 |
"""Return the type of the shapes in the shapestore.""" |
"""Return the type of the shapes in the shapestore.""" |
577 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
617 |
|
|
618 |
def Shape(self, shapeid): |
def Shape(self, shapeid): |
619 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
620 |
cursor.execute("SELECT AsText(%s) FROM %s WHERE gid=%d" |
cursor.execute("SELECT AsText(%s) FROM %s WHERE %s=%d" |
621 |
% (self.quoted_geo_col, self.quoted_tablename, shapeid)) |
% (self.quoted_geo_col, self.quoted_tablename, |
622 |
|
self.quoted_id_column, shapeid)) |
623 |
wkt = cursor.fetchone()[0] |
wkt = cursor.fetchone()[0] |
624 |
cursor.close() |
cursor.close() |
625 |
return PostGISShape(shapeid, wkt) |
return PostGISShape(shapeid, wkt) |
626 |
|
|
627 |
def AllShapes(self): |
def AllShapes(self): |
628 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
629 |
cursor.execute("SELECT gid, AsText(%s) FROM %s ORDER BY gid" |
cursor.execute("SELECT %s, AsText(%s) FROM %s ORDER BY %s" |
630 |
% (self.quoted_geo_col, self.quoted_tablename)) |
% (self.quoted_id_column, self.quoted_geo_col, |
631 |
|
self.quoted_tablename, self.quoted_id_column)) |
632 |
while 1: |
while 1: |
633 |
result = cursor.fetchone() |
result = cursor.fetchone() |
634 |
if result is None: |
if result is None: |
644 |
% (left, bottom, left, top, right, top, right, bottom, |
% (left, bottom, left, top, right, top, right, bottom, |
645 |
left, bottom)) |
left, bottom)) |
646 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
647 |
cursor.execute("SELECT gid, AsText(%s) FROM %s" |
cursor.execute("SELECT %(gid)s, AsText(%(geom)s) FROM %(table)s" |
648 |
" WHERE %s && GeometryFromText('%s', %d) ORDER BY gid" |
" WHERE %(geom)s && GeometryFromText('%(box)s', %(srid)d)" |
649 |
% (self.quoted_geo_col, self.quoted_tablename, |
" ORDER BY %(gid)s" |
650 |
self.quoted_geo_col, geom, self.srid)) |
% {"table": self.quoted_tablename, |
651 |
|
"geom": self.quoted_geo_col, |
652 |
|
"gid": self.quoted_id_column, |
653 |
|
"box": geom, |
654 |
|
"srid": self.srid}) |
655 |
while 1: |
while 1: |
656 |
result = cursor.fetchone() |
result = cursor.fetchone() |
657 |
if result is None: |
if result is None: |