def edit_update_ui(self): enable = bool(self.db) self.edit_refresh_action.setEnabled(enable) for action in (self.edit_replace_star_action, self.edit_copy_action, self.edit_cut_action, self.edit_paste_action): action.setEnabled(False) widget = qApp.focusWidget() if isinstance(widget, QLineEdit): hasText = widget.selectionLength() > 0 self.edit_copy_action.setEnabled(hasText) self.edit_cut_action.setEnabled(hasText) clipboard = qApp.clipboard() self.edit_paste_action.setEnabled(bool(clipboard.text())) elif isinstance(widget, (QPlainTextEdit, QTextEdit)): if enable: if sql := widget.toPlainText().strip(): sql = Sql.uncommented(sql) if re.match(r'\s*SELECT(:?\s+(:?ALL|DISTINCT))?\s+\*', sql, re.IGNORECASE | re.DOTALL): self.edit_replace_star_action.setEnabled(True) text_cursor = widget.textCursor() enable = text_cursor.hasSelection() self.edit_copy_action.setEnabled(enable) self.edit_cut_action.setEnabled(enable) self.edit_paste_action.setEnabled(widget.canPaste())
def columnCount(self, parent=QModelIndex()): try: try: return Sql.field_count_from_select(self.select) except Sql.Error: return len(self.db.field_names_for_select(self.select)) except apsw.SQLError as err: self.sql_error.emit(str(err)) return 0
def refresh(self): if not self.is_select: self.statusLabel.setText('<font color=red>Only SELECT ' 'statements are supported here</font>') else: select = Sql.uncommented(self.sqlEdit.toPlainText()) if re.match(r'\s*SELECT(:?\s+(:?ALL|DISTINCT))?\s+\*', select, re.IGNORECASE | re.DOTALL): try: names = ', '.join([ Sql.quoted(name) for name in self.db.field_names_for_select(select) ]) select = select.replace('*', names, 1) except apsw.SQLError as err: self.on_sql_error(str(err)) return self.tableModel.refresh(select) self.update_status(select)
def make_widgets(self, select): self.sqlEdit = SQLEdit.SQLEdit(select) self.sqlEdit.setTabChangesFocus(True) self.tableModel = TableModel.TableModel(self.db, Sql.uncommented(select)) self.tableView = QTableView() self.tableView.setModel(self.tableModel) self.statusLabel = QLabel() self.statusLabel.setTextFormat(Qt.RichText) self.update_status(select)
def headerData(self, section, orientation, role): if role != Qt.DisplayRole: return if orientation == Qt.Horizontal: try: try: names = Sql.field_names_from_select(self.select) except Sql.Error: names = self.db.field_names_for_select(self.select) return names[section] except apsw.SQLError as err: self.sql_error.emit(str(err)) return f'{section + 1:,}'
def edit_replace_star(self): widget = qApp.focusWidget() if widget is not None and isinstance(widget, (QPlainTextEdit, QTextEdit)): select = widget.toPlainText() try: names = ', '.join([ Sql.quoted(name) for name in self.db.field_names_for_select(select) ]) with EditBlock(widget) as cursor: cursor.select(QTextCursor.Document) text = cursor.selectedText() cursor.insertText( re.sub(r'(SELECT(:?\s+(:?ALL|DISTINCT))?\s)\s*\*', lambda match: match.group(1).upper() + names, text, flags=re.IGNORECASE | re.DOTALL)) except apsw.SQLError as err: while widget is not None and not isinstance( widget, QMdiSubWindow): widget = widget.parent() if widget is not None: widget.widget().on_sql_error(str(err))
def is_select(self): return Sql.is_select(self.sql)