129 |
|
|
130 |
class PostGISTable: |
class PostGISTable: |
131 |
|
|
132 |
"""A Table in a PostGIS database""" |
"""A Table in a PostGIS database |
133 |
|
|
134 |
|
A PostgreSQL table may contain columns with types not (yet) |
135 |
|
supported by Thuban. Instances of this class ignore those columns |
136 |
|
and pretend they don't exist, i.e. they won't show up in the column |
137 |
|
descriptions returned by Columns() and other methods. |
138 |
|
""" |
139 |
|
|
140 |
def __init__(self, db, tablename): |
def __init__(self, db, tablename): |
141 |
"""Initialize the PostGISTable. |
"""Initialize the PostGISTable. |
158 |
for i in range(len(description)): |
for i in range(len(description)): |
159 |
for pgtyp, tabletyp in type_map: |
for pgtyp, tabletyp in type_map: |
160 |
if pgtyp == description[i][1]: |
if pgtyp == description[i][1]: |
161 |
col = PostGISColumn(description[i][0], tabletyp, i) |
col = PostGISColumn(description[i][0], tabletyp, |
162 |
|
len(self.columns)) |
163 |
break |
break |
164 |
else: |
else: |
165 |
if description[i][1] == self.db.geometry_type: |
if description[i][1] == self.db.geometry_type: |
216 |
cursor.execute("SELECT count(*) FROM %s" % self.tablename) |
cursor.execute("SELECT count(*) FROM %s" % self.tablename) |
217 |
return cursor.fetchone()[0] |
return cursor.fetchone()[0] |
218 |
|
|
219 |
def ReadRowAsDict(self, row): |
def RowIdToOrdinal(self, gid): |
220 |
|
"""Return the row ordinal given its id""" |
221 |
|
cursor = self.db.cursor() |
222 |
|
cursor.execute("SELECT count(*) FROM %s WHERE gid < %d;" |
223 |
|
% (self.tablename, gid)) |
224 |
|
return cursor.fetchone()[0] |
225 |
|
|
226 |
|
def RowOrdinalToId(self, num): |
227 |
|
"""Return the rowid for given its ordinal""" |
228 |
|
cursor = self.db.cursor() |
229 |
|
cursor.execute("SELECT gid FROM %s LIMIT 1 OFFSET %d;" |
230 |
|
% (self.tablename, num)) |
231 |
|
return cursor.fetchone()[0] |
232 |
|
|
233 |
|
def ReadRowAsDict(self, row, row_is_ordinal = 0): |
234 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
235 |
cursor.execute(self.query_stmt + " LIMIT 1 OFFSET %d" % row) |
if row_is_ordinal: |
236 |
|
stmt = self.query_stmt + " LIMIT 1 OFFSET %d" % row |
237 |
|
else: |
238 |
|
stmt = self.query_stmt + " WHERE gid = %d" % row |
239 |
|
cursor.execute(stmt) |
240 |
result = {} |
result = {} |
241 |
for col, value in zip(self.columns, cursor.fetchone()): |
for col, value in zip(self.columns, cursor.fetchone()): |
242 |
result[col.name] = value |
result[col.name] = value |
243 |
return result |
return result |
244 |
|
|
245 |
def ReadValue(self, row, col): |
def ReadValue(self, row, col, row_is_ordinal = 0): |
246 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
247 |
cursor.execute("SELECT %s FROM %s LIMIT 1 OFFSET %d" % |
if row_is_ordinal: |
248 |
(self.column_map[col].name, self.tablename, row)) |
stmt = ("SELECT %s FROM %s LIMIT 1 OFFSET %d" % |
249 |
|
(self.column_map[col].name, self.tablename, row)) |
250 |
|
else: |
251 |
|
stmt = ("SELECT %s FROM %s WHERE gid = %d" % |
252 |
|
(self.column_map[col].name, self.tablename, row)) |
253 |
|
cursor.execute(stmt) |
254 |
return cursor.fetchone()[0] |
return cursor.fetchone()[0] |
255 |
|
|
256 |
def ValueRange(self, col): |
def ValueRange(self, col): |