Skip to content

Commit

Permalink
Merge pull request #928 from lanzagar/sqlschema
Browse files Browse the repository at this point in the history
owsql: Option to specify schema
  • Loading branch information
kernc committed Dec 16, 2015
2 parents a9ef22f + 803af54 commit 88ed7b8
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions Orange/widgets/data/owsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class OWSql(widget.OWWidget):
host = Setting(None)
port = Setting(None)
database = Setting(None)
schema = Setting(None)
username = Setting(None)
password = Setting(None)
table = Setting(None)
Expand All @@ -61,22 +62,28 @@ def __init__(self):
box = gui.widgetBox(vbox)
self.servertext = QtGui.QLineEdit(box)
self.servertext.setPlaceholderText('Server')
self.servertext.setToolTip('Server')
if self.host:
self.servertext.setText(self.host if not self.port else
'{}:{}'.format(self.host, self.port))
box.layout().addWidget(self.servertext)
self.databasetext = QtGui.QLineEdit(box)
self.databasetext.setPlaceholderText('Database')
self.databasetext.setPlaceholderText('Database[/Schema]')
self.databasetext.setToolTip('Database or optionally Database/Schema')
if self.database:
self.databasetext.setText(self.database)
self.databasetext.setText(
self.database if not self.schema else
'{}/{}'.format(self.database, self.schema))
box.layout().addWidget(self.databasetext)
self.usernametext = QtGui.QLineEdit(box)
self.usernametext.setPlaceholderText('Username')
self.usernametext.setToolTip('Username')
if self.username:
self.usernametext.setText(self.username)
box.layout().addWidget(self.usernametext)
self.passwordtext = QtGui.QLineEdit(box)
self.passwordtext.setPlaceholderText('Password')
self.passwordtext.setToolTip('Password')
self.passwordtext.setEchoMode(QtGui.QLineEdit.Password)
if self.password:
self.passwordtext.setText(self.password)
Expand All @@ -88,6 +95,7 @@ def __init__(self):
minimumContentsLength=35,
sizeAdjustPolicy=QtGui.QComboBox.AdjustToMinimumContentsLength
)
self.tablecombo.setToolTip('table')
tables.layout().addWidget(self.tablecombo)
self.tablecombo.activated[int].connect(self.select_table)
self.connectbutton = gui.button(
Expand Down Expand Up @@ -143,7 +151,7 @@ def connect(self):
hostport = self.servertext.text().split(':')
self.host = hostport[0]
self.port = hostport[1] if len(hostport) == 2 else None
self.database = self.databasetext.text()
self.database, _, self.schema = self.databasetext.text().partition('/')
self.username = self.usernametext.text() or None
self.password = self.passwordtext.text() or None
try:
Expand Down Expand Up @@ -173,6 +181,10 @@ def refresh_tables(self):
return

cur = self._connection.cursor()
if self.schema:
schema_clause = "AND n.nspname = '{}'".format(self.schema)
else:
schema_clause = "AND pg_catalog.pg_table_is_visible(c.oid)"
cur.execute("""SELECT --n.nspname as "Schema",
c.relname AS "Name"
FROM pg_catalog.pg_class c
Expand All @@ -181,9 +193,9 @@ def refresh_tables(self):
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
{}
AND NOT c.relname LIKE '\\_\\_%'
ORDER BY 1;""")
ORDER BY 1;""".format(schema_clause))

self.tablecombo.addItem("Select a table")
for i, (table_name,) in enumerate(cur.fetchall()):
Expand Down

0 comments on commit 88ed7b8

Please sign in to comment.