def __init__(self, db, sql, parent=None): self.db = db.connector t = QTime() t.start() c = self.db._execute(None, unicode(sql)) self._secs = t.elapsed() / 1000.0 del t self._affectedRows = 0 data = [] header = self.db._get_cursor_columns(c) if header is None: header = [] try: if len(header) > 0: data = self.db._fetchall(c) self._affectedRows = c.rowcount except DbError: # nothing to fetch! data = [] header = [] BaseTableModel.__init__(self, header, data, parent) # commit before closing the cursor to make sure that the changes are stored self.db._commit() c.close() del c
def __init__(self, db, sql, parent=None): # create a virtual layer with non-geometry results q = QUrl.toPercentEncoding(sql) t = QTime() t.start() tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() p = QgsVectorLayer( "%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "vv", "virtual") self._secs = t.elapsed() / 1000.0 if not p.isValid(): data = [] header = [] raise DbError(p.dataProvider().error().summary(), sql) else: header = [f.name() for f in p.fields()] has_geometry = False if p.geometryType() != QGis.WKBNoGeometry: gn = getQueryGeometryName(tmp) if gn: has_geometry = True header += [gn] data = [] for f in p.getFeatures(): a = f.attributes() if has_geometry: if f.geometry(): a += [f.geometry().exportToWkt()] else: a += [None] data += [a] self._secs = 0 self._affectedRows = len(data) BaseTableModel.__init__(self, header, data, parent)
def __init__(self, db, sql, parent=None): # create a virtual layer with non-geometry results q = QUrl.toPercentEncoding(sql) t = QTime() t.start() tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() p = QgsVectorLayer("%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "vv", "virtual") self._secs = t.elapsed() / 1000.0 if not p.isValid(): data = [] header = [] raise DbError(p.dataProvider().error().summary(), sql) else: header = [f.name() for f in p.fields()] has_geometry = False if p.geometryType() != QGis.WKBNoGeometry: gn = getQueryGeometryName(tmp) if gn: has_geometry = True header += [gn] data = [] for f in p.getFeatures(): a = f.attributes() if has_geometry: if f.geometry(): a += [f.geometry().exportToWkt()] else: a += [None] data += [a] self._secs = 0 self._affectedRows = len(data) BaseTableModel.__init__(self, header, data, parent)