Skip to content

Commit

Permalink
Merge pull request #1395 from lanzagar/create_ext
Browse files Browse the repository at this point in the history
[ENH] Automatically create required SQL extensions
  • Loading branch information
astaric authored Jul 1, 2016
2 parents 9e4e5dd + 6c93cd4 commit ecc474d
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Orange/widgets/data/owsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@


MAX_DL_LIMIT = 1000000
EXTENSIONS = ('tsm_system_time', 'quantile')


class OWSql(widget.OWWidget):
Expand Down Expand Up @@ -132,20 +133,20 @@ def __init__(self):

def error(self, id=0, text=""):
super().error(id, text)
err_style = 'QLineEdit {border: 2px solid red;}'
if 'server' in text or 'host' in text:
self.servertext.setStyleSheet('QLineEdit {border: 2px solid red;}')
self.servertext.setStyleSheet(err_style)
else:
self.servertext.setStyleSheet('')
if 'role' in text:
self.usernametext.setStyleSheet('QLineEdit {border: 2px solid red;}')
self.usernametext.setStyleSheet(err_style)
else:
self.usernametext.setStyleSheet('')
if 'database' in text:
self.databasetext.setStyleSheet('QLineEdit {border: 2px solid red;}')
self.databasetext.setStyleSheet(err_style)
else:
self.databasetext.setStyleSheet('')


def connect(self):
hostport = self.servertext.text().split(':')
self.host = hostport[0]
Expand Down Expand Up @@ -173,9 +174,9 @@ def connect(self):
self.database_desc = self.data_desc_table = None
self.tablecombo.clear()


def refresh_tables(self):
self.tablecombo.clear()
self.error(1)
if self._connection is None:
self.data_desc_table = None
return
Expand Down Expand Up @@ -215,7 +216,25 @@ def select_table(self):
self.database_desc["Table"] = "(None)"
self.table = None

def create_extensions(self):
missing = []
for ext in EXTENSIONS:
try:
cur = self._connection.cursor()
cur.execute("CREATE EXTENSION IF NOT EXISTS " + ext)
except psycopg2.OperationalError:
missing.append(ext)
finally:
self._connection.commit()
if missing:
self.error(1, 'Database missing extension{}: {}'.format(
's' if len(missing) > 1 else '',
', '.join(missing)))
else:
self.error(1)

def open_table(self):
self.create_extensions()
table = self.get_table()
self.data_desc_table = table
self.send("Data", table)
Expand Down

0 comments on commit ecc474d

Please sign in to comment.