def __init__( self, connection_params, table_or_sql, backend=None, type_hints=None, inspect_values=False): """ Create a new proxy for sql table. To create a new SqlTable, specify the connection parameters for psycopg2 and the name of the table/sql query used to fetch the data. table = SqlTable('database_name', 'table_name') table = SqlTable('database_name', 'SELECT * FROM table') For complex configurations, dictionary of connection parameters can be used instead of the database name. For documentation about connection parameters, see: http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS Data domain is inferred from the columns of the table/query. The (very quick) default setting is to treat all numeric columns as continuous variables and everything else as strings and placed among meta attributes. If inspect_values parameter is set to True, all column values are inspected and int/string columns with less than 21 values are intepreted as discrete features. Domains can be constructed by the caller and passed in type_hints parameter. Variables from the domain are used for the columns with the matching names; for columns without the matching name in the domain, types are inferred as described above. """ if isinstance(connection_params, str): connection_params = dict(database=connection_params) if backend is None: for backend in Backend.available_backends(): try: self.backend = backend(connection_params) break except BackendError as ex: print(ex) else: raise ValueError("No backend could connect to server") else: self.backend = backend(connection_params) if table_or_sql is not None: if isinstance(table_or_sql, TableDesc): table = table_or_sql.sql elif "SELECT" in table_or_sql: table = "(%s) as my_table" % table_or_sql.strip("; ") else: table = self.backend.quote_identifier(table_or_sql) self.table_name = table self.domain = self.get_domain(type_hints, inspect_values) self.name = table
def __init__( self, connection_params, table_or_sql, backend=None, type_hints=None, inspect_values=False): """ Create a new proxy for sql table. To create a new SqlTable, specify the connection parameters for psycopg2 and the name of the table/sql query used to fetch the data. table = SqlTable('database_name', 'table_name') table = SqlTable('database_name', 'SELECT * FROM table') For complex configurations, dictionary of connection parameters can be used instead of the database name. For documentation about connection parameters, see: http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS Data domain is inferred from the columns of the table/query. The (very quick) default setting is to treat all numeric columns as continuous variables and everything else as strings and placed among meta attributes. If inspect_values parameter is set to True, all column values are inspected and int/string columns with less than 21 values are intepreted as discrete features. Domains can be constructed by the caller and passed in type_hints parameter. Variables from the domain are used for the columns with the matching names; for columns without the matching name in the domain, types are inferred as described above. """ if isinstance(connection_params, str): connection_params = dict(database=connection_params) if backend is None: for backend in Backend.available_backends(): try: self.backend = backend(connection_params) break except BackendError as ex: print(ex) else: raise ValueError("No backend could connect to server") else: self.backend = backend(connection_params) if table_or_sql is not None: if isinstance(table_or_sql, TableDesc): table = table_or_sql.sql elif "select" in table_or_sql.lower(): table = "(%s) as my_table" % table_or_sql.strip("; ") else: table = self.backend.quote_identifier(table_or_sql) self.table_name = table self.domain = self.get_domain(type_hints, inspect_values) self.name = table
def _add_backend_controls(self): box = self.serverbox self.backends = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if self.backends: self.backendcombo.setModel(self.backends) else: self.Error.no_backends() box.setEnabled(False) box.layout().insertWidget(0, self.backendcombo)
def _add_backend_controls(self): box = self.serverbox self.backends = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if self.backends: self.backendcombo.setModel(self.backends) names = [backend.display_name for backend in self.backends] if self.selected_backend and self.selected_backend in names: self.backendcombo.setCurrentText(self.selected_backend) else: self.Error.no_backends() box.setEnabled(False) self.backendcombo.currentTextChanged.connect(self.__backend_changed) box.layout().insertWidget(0, self.backendcombo)
def __init__(self): super().__init__() self.backend = None self.data_desc_table = None self.database_desc = None vbox = gui.vBox(self.controlArea, "Server", addSpace=True) box = gui.vBox(vbox) self.backendmodel = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if len(self.backendmodel): self.backendcombo.setModel(self.backendmodel) else: self.Error.no_backends() box.setEnabled(False) box.layout().addWidget(self.backendcombo) self.servertext = QLineEdit(box) self.servertext.setPlaceholderText('Server') self.servertext.setToolTip('Server') self.servertext.editingFinished.connect(self._load_credentials) 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 = QLineEdit(box) self.databasetext.setPlaceholderText('Database[/Schema]') self.databasetext.setToolTip('Database or optionally Database/Schema') if self.database: self.databasetext.setText( self.database if not self.schema else '{}/{}'. format(self.database, self.schema)) box.layout().addWidget(self.databasetext) self.usernametext = QLineEdit(box) self.usernametext.setPlaceholderText('Username') self.usernametext.setToolTip('Username') box.layout().addWidget(self.usernametext) self.passwordtext = QLineEdit(box) self.passwordtext.setPlaceholderText('Password') self.passwordtext.setToolTip('Password') self.passwordtext.setEchoMode(QLineEdit.Password) box.layout().addWidget(self.passwordtext) self._load_credentials() tables = gui.hBox(box) self.tablemodel = TableModel() self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength) self.tablecombo.setModel(self.tablemodel) self.tablecombo.setToolTip('table') tables.layout().addWidget(self.tablecombo) self.tablecombo.activated[int].connect(self.select_table) self.connectbutton = gui.button(tables, self, '↻', callback=self.connect) self.connectbutton.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) tables.layout().addWidget(self.connectbutton) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, 'materialize', 'Materialize to table ') cb.setToolTip('Save results of the query in a table') le = gui.lineEdit(mt, self, 'materialize_table_name') le.setToolTip('Save results of the query in a table') self.executebtn = gui.button(self.custom_sql, self, 'Execute', callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox(box, self, "guess_values", "Auto-discover discrete variables", callback=self.open_table) gui.checkBox(box, self, "download", "Download data to local memory", callback=self.open_table) gui.rubber(self.buttonsArea) QTimer.singleShot(0, self.connect)
def __init__(self): super().__init__() self.backend = None self.data_desc_table = None self.database_desc = None vbox = gui.vBox(self.controlArea, "Server", addSpace=True) box = gui.vBox(vbox) self.backends = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if len(self.backends): self.backendcombo.setModel(self.backends) else: self.Error.no_backends() box.setEnabled(False) box.layout().addWidget(self.backendcombo) self.servertext = QLineEdit(box) self.servertext.setPlaceholderText('Server') self.servertext.setToolTip('Server') self.servertext.editingFinished.connect(self._load_credentials) 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 = QLineEdit(box) self.databasetext.setPlaceholderText('Database[/Schema]') self.databasetext.setToolTip('Database or optionally Database/Schema') if self.database: self.databasetext.setText( self.database if not self.schema else '{}/{}'.format(self.database, self.schema)) box.layout().addWidget(self.databasetext) self.usernametext = QLineEdit(box) self.usernametext.setPlaceholderText('Username') self.usernametext.setToolTip('Username') box.layout().addWidget(self.usernametext) self.passwordtext = QLineEdit(box) self.passwordtext.setPlaceholderText('Password') self.passwordtext.setToolTip('Password') self.passwordtext.setEchoMode(QLineEdit.Password) box.layout().addWidget(self.passwordtext) self._load_credentials() self.tables = TableModel() tables = gui.hBox(box) self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength ) self.tablecombo.setModel(self.tables) self.tablecombo.setToolTip('table') tables.layout().addWidget(self.tablecombo) self.connect() index = self.tablecombo.findText(str(self.table)) if index != -1: self.tablecombo.setCurrentIndex(index) # set up the callback to select_table in case of selection change self.tablecombo.activated[int].connect(self.select_table) self.connectbutton = gui.button( tables, self, '↻', callback=self.connect) self.connectbutton.setSizePolicy( QSizePolicy.Fixed, QSizePolicy.Fixed) tables.layout().addWidget(self.connectbutton) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, 'materialize', 'Materialize to table ') cb.setToolTip('Save results of the query in a table') le = gui.lineEdit(mt, self, 'materialize_table_name') le.setToolTip('Save results of the query in a table') self.executebtn = gui.button( self.custom_sql, self, 'Execute', callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox(box, self, "guess_values", "Auto-discover categorical variables", callback=self.open_table) gui.checkBox(box, self, "download", "Download data to local memory", callback=self.open_table) gui.rubber(self.buttonsArea) QTimer.singleShot(0, self.select_table)
def __init__(self): super().__init__() self.backend = None self.data_desc_table = None self.database_desc = None vbox = gui.vBox(self.controlArea, "Server", addSpace=True) box = gui.vBox(vbox) self.backends = BackendModel(Backend.available_backends()) self.backendcombo = QComboBox(box) if len(self.backends): self.backendcombo.setModel(self.backends) else: self.Error.no_backends() box.setEnabled(False) box.layout().addWidget(self.backendcombo) self.servertext = QLineEdit(box) self.servertext.setPlaceholderText("Server") self.servertext.setToolTip("Server") self.servertext.editingFinished.connect(self._load_credentials) 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 = QLineEdit(box) self.databasetext.setPlaceholderText("Database[/Schema]") self.databasetext.setToolTip("Database or optionally Database/Schema") if self.database: self.databasetext.setText( self.database if not self.schema else "{}/{}". format(self.database, self.schema)) box.layout().addWidget(self.databasetext) self.usernametext = QLineEdit(box) self.usernametext.setPlaceholderText("Username") self.usernametext.setToolTip("Username") box.layout().addWidget(self.usernametext) self.passwordtext = QLineEdit(box) self.passwordtext.setPlaceholderText("Password") self.passwordtext.setToolTip("Password") self.passwordtext.setEchoMode(QLineEdit.Password) box.layout().addWidget(self.passwordtext) self._load_credentials() self.tables = TableModel() tables = gui.hBox(box) self.tablecombo = QComboBox( minimumContentsLength=35, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength, ) self.tablecombo.setModel(self.tables) self.tablecombo.setToolTip("table") tables.layout().addWidget(self.tablecombo) self.connect() index = self.tablecombo.findText(str(self.table)) if index != -1: self.tablecombo.setCurrentIndex(index) # set up the callback to select_table in case of selection change self.tablecombo.activated[int].connect(self.select_table) self.connectbutton = gui.button(tables, self, "↻", callback=self.connect) self.connectbutton.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) tables.layout().addWidget(self.connectbutton) self.custom_sql = gui.vBox(box) self.custom_sql.setVisible(False) self.sqltext = QTextEdit(self.custom_sql) self.sqltext.setPlainText(self.sql) self.custom_sql.layout().addWidget(self.sqltext) mt = gui.hBox(self.custom_sql) cb = gui.checkBox(mt, self, "materialize", "Materialize to table ") cb.setToolTip("Save results of the query in a table") le = gui.lineEdit(mt, self, "materialize_table_name") le.setToolTip("Save results of the query in a table") self.executebtn = gui.button(self.custom_sql, self, "Execute", callback=self.open_table) box.layout().addWidget(self.custom_sql) gui.checkBox( box, self, "guess_values", "Auto-discover categorical variables", callback=self.open_table, ) gui.checkBox( box, self, "download", "Download data to local memory", callback=self.open_table, ) gui.rubber(self.buttonsArea) QTimer.singleShot(0, self.select_table)