Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Automatically create required SQL extensions #1395

Merged
merged 2 commits into from
Jul 1, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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