Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use lazy evaluation for query result descriptions
  • Loading branch information
strk committed Jan 14, 2020
1 parent 1ee8034 commit b4f4c33
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions python/plugins/db_manager/db_plugins/postgis/connector.py
Expand Up @@ -69,7 +69,6 @@ def __init__(self, connection, sql=None):
self.result = None
self.cursor = 0
self.closed = False
self.description = None
if (self.sql != None):
self._execute()

Expand Down Expand Up @@ -102,30 +101,38 @@ def _execute(self, sql=None):
self._debug("execute returned " + str(len(self.result)) + " rows")
self.cursor = 0

self.description = []

uri = QgsDataSourceUri(self.connection.uri())

# TODO: make this part provider-agnostic
uri.setTable('(SELECT row_number() OVER () AS __rid__, * FROM (' + self.sql + ') as foo)')
uri.setKeyColumn('__rid__')
# 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
str, # type_code
10, # display_size
10, # internal_size
0, # precision
None, # scale
True # null_ok
])
self._debug("execute returned " + str(len(self.description)) + " cols")
self._description = None # reset description

@property
def description(self):

if self._description is None:

uri = QgsDataSourceUri(self.connection.uri())

# TODO: make this part provider-agnostic
uri.setTable('(SELECT row_number() OVER () AS __rid__, * FROM (' + self.sql + ') as foo)')
uri.setKeyColumn('__rid__')
# 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()
self._description = []
for i in range(1, len(fields)): # skip first field (__rid__)
f = fields[i]
self._description.append([
f.name(), # name
str, # type_code
10, # display_size
10, # internal_size
0, # precision
None, # scale
True # null_ok
])
self._debug("get_description returned " + str(len(self._description)) + " cols")

return self._description

def fetchone(self):
self._execute()
Expand Down

0 comments on commit b4f4c33

Please sign in to comment.