|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from PyQt5.QtWidgets import (QApplication, QMainWindow, |
| 4 | + QTableWidget, QTableWidgetItem, QMenu, QAction, QDialog, |
| 5 | + QInputDialog, QTableView, QHeaderView, QLineEdit, QLabel, QVBoxLayout) |
| 6 | +from PyQt5.QtGui import QIcon, QStandardItemModel |
| 7 | + |
| 8 | +import sys |
| 9 | +import re |
| 10 | +import mysql.connector |
| 11 | + |
| 12 | +from PyQt5.QtCore import Qt, QSortFilterProxyModel, QModelIndex, QRegExp |
| 13 | + |
| 14 | +headers = ["Word", "Meaning", ""] |
| 15 | + |
| 16 | + |
| 17 | +NUMBER_OF_COLUMNS = 2 |
| 18 | +NUMBER_OF_ROWS = 3 |
| 19 | + |
| 20 | + |
| 21 | +class TableView(QTableView): |
| 22 | + def __init__(self, title, rows, columns): |
| 23 | + QTableView.__init__(self) |
| 24 | + |
| 25 | + self.proxyModel = SortFilterProxyModel() |
| 26 | + # This property holds whether the proxy model is dynamically sorted and filtered whenever the contents of the source model change |
| 27 | + self.proxyModel.setDynamicSortFilter(True) |
| 28 | + self.model = QStandardItemModel( |
| 29 | + rows, columns, self) |
| 30 | + self.model.setHeaderData(0, Qt.Horizontal, "Word") |
| 31 | + self.model.setHeaderData(1, Qt.Horizontal, "Meaning") |
| 32 | + self.setWindowTitle(title) |
| 33 | + self.setSampleData() |
| 34 | + self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) |
| 35 | + self.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents) |
| 36 | + self.setAlternatingRowColors(True) |
| 37 | + self.setSortingEnabled(True) |
| 38 | + self.verticalHeader().hide() |
| 39 | + self.setSelectionMode(QTableView.SingleSelection) |
| 40 | + self.setSelectionBehavior(QTableView.SelectRows) |
| 41 | + self.clicked.connect(self.getItem) |
| 42 | + self.filterSTring = "" |
| 43 | + |
| 44 | + def setSampleData(self): |
| 45 | + self.model.setData(self.model.index( |
| 46 | + 0, 0, QModelIndex()), "Abundance", Qt.DisplayRole) |
| 47 | + self.model.setData(self.model.index( |
| 48 | + 0, 1, QModelIndex()), "A very large quantity of something", Qt.DisplayRole) |
| 49 | + self.model.setData(self.model.index( |
| 50 | + 1, 0, QModelIndex()), "Belonging", Qt.DisplayRole) |
| 51 | + self.model.setData(self.model.index( |
| 52 | + 1, 1, QModelIndex()), "An affinity for a place or situation.", Qt.DisplayRole) |
| 53 | + self.model.setData(self.model.index( |
| 54 | + 2, 0, QModelIndex()), "Candor", Qt.DisplayRole) |
| 55 | + self.model.setData(self.model.index( |
| 56 | + 2, 1, QModelIndex()), "The quality of being open and honest in expression; frankness", Qt.DisplayRole) |
| 57 | + self.proxyModel.setSourceModel(self.model) |
| 58 | + self.setModel(self.proxyModel) |
| 59 | + |
| 60 | + def setFilterString(self, string): |
| 61 | + self.filterString = "^" + string |
| 62 | + |
| 63 | + def getItem(self, index): |
| 64 | + |
| 65 | + mapped_index = self.proxyModel.mapToSource(index) |
| 66 | + item = self.model.itemFromIndex(mapped_index) |
| 67 | + print(item.text() + " ") |
| 68 | + item = self.model.data(mapped_index) |
| 69 | + row = mapped_index.row() |
| 70 | + column = mapped_index.column() |
| 71 | + data = mapped_index.data() |
| 72 | + item = self.model.itemFromIndex(mapped_index) |
| 73 | + print(item.text() + " " + str(row) + " " + str(column) + " " + data) |
| 74 | + |
| 75 | + def filterRegExpChanged(self): |
| 76 | + |
| 77 | + # can be one of QRegExp.RegExp2, QRegExp.WildCard, QRegExp.RegExp2 etc, |
| 78 | + # see https://doc.qt.io/qt-5/qregexp.html#PatternSyntax-enum |
| 79 | + syntax = QRegExp.RegExp |
| 80 | + caseSensitivity = Qt.CaseInsensitive |
| 81 | + regExp = QRegExp(self.filterString, |
| 82 | + caseSensitivity, syntax) |
| 83 | + # This property holds the QRegExp used to filter the contents of the source model |
| 84 | + self.proxyModel.setFilterRegExp(regExp) |
| 85 | + |
| 86 | +class SortFilterProxyModel(QSortFilterProxyModel): |
| 87 | + def __init__(self): |
| 88 | + super().__init__() |
| 89 | + |
| 90 | + def filterAcceptsRow(self, sourceRow, sourceParent): |
| 91 | + result = False |
| 92 | + if self.filterKeyColumn() == 0: # only interested in the first column |
| 93 | + |
| 94 | + index = self.sourceModel().index(sourceRow, 0, sourceParent) |
| 95 | + data = self.sourceModel().data(index) |
| 96 | + # we could additionally filter here on the data |
| 97 | + return True |
| 98 | + |
| 99 | + # Otherwise ignore |
| 100 | + return super(SortFilterProxyModel, self).filterAcceptsRow(sourceRow, sourceParent) |
| 101 | + |
| 102 | + |
| 103 | +class WordSelector(QDialog): |
| 104 | + def __init__(self, parent=None): |
| 105 | + QDialog.__init__(self, parent) |
| 106 | + self.parent = parent |
| 107 | + self.lastStart = 0 |
| 108 | + self.initUI() |
| 109 | + |
| 110 | + def initUI(self): |
| 111 | + |
| 112 | + self.table = TableView("Words", |
| 113 | + NUMBER_OF_ROWS, NUMBER_OF_COLUMNS) |
| 114 | + layout = QVBoxLayout() |
| 115 | + self.filterLabel = QLabel(" Filter") |
| 116 | + layout.addWidget(self.filterLabel) |
| 117 | + self.filter = QLineEdit(self) |
| 118 | + self.filter.setStyleSheet( |
| 119 | + "background-color: #FFFFFF; padding:1px 1px 1px 1px") |
| 120 | + self.filter.setFixedWidth(120) |
| 121 | + self.filter.textChanged.connect(self.findInTable) |
| 122 | + layout.addWidget(self.filter) |
| 123 | + layout.addWidget(self.table) |
| 124 | + self.setGeometry(300, 300, 500, 300) |
| 125 | + self.setWindowTitle("Find and Replace") |
| 126 | + self.setLayout(layout) |
| 127 | + |
| 128 | + |
| 129 | + def findInTable(self, text): |
| 130 | + for row in range(NUMBER_OF_ROWS): |
| 131 | + row_text = self.table.proxyModel.data(self.table.proxyModel.index(row, 0), 0) |
| 132 | + if row_text: |
| 133 | + if row_text.lower().startswith(text.lower()): |
| 134 | + print(self.table.proxyModel.data(self.table.proxyModel.index(row, 0), 0)) |
| 135 | + self.table.showRow(row) |
| 136 | + else: |
| 137 | + self.table.hideRow(row) |
| 138 | + |
| 139 | +class MySQLManager: |
| 140 | + def __init__(self, username, password, host, database): |
| 141 | + self.username = username |
| 142 | + self.password = password |
| 143 | + self.host = host |
| 144 | + self.database = database |
| 145 | + self.conn = None |
| 146 | + |
| 147 | + def connect_to_mysql(self): |
| 148 | + try: |
| 149 | + # Connect to the MySQL database |
| 150 | + self.conn = mysql.connector.connect( |
| 151 | + user=self.username, |
| 152 | + password=self.password, |
| 153 | + host=self.host, |
| 154 | + database=self.database |
| 155 | + ) |
| 156 | + if self.conn.is_connected(): |
| 157 | + print("Connected to MySQL database") |
| 158 | + return self.conn |
| 159 | + except mysql.connector.Error as e: |
| 160 | + print(f"Error connecting to MySQL database: {e}") |
| 161 | + return None |
| 162 | + |
| 163 | + def close_connection(self): |
| 164 | + if self.conn: |
| 165 | + self.conn.close() |
| 166 | + print("Connection closed") |
| 167 | + |
| 168 | + def get_table_schema(self, table_name): |
| 169 | + try: |
| 170 | + cursor = self.conn.cursor() |
| 171 | + cursor.execute(f"DESCRIBE {table_name}") |
| 172 | + schema = cursor.fetchall() |
| 173 | + cursor.close() |
| 174 | + return schema |
| 175 | + except mysql.connector.Error as e: |
| 176 | + print(f"Error getting table schema: {e}") |
| 177 | + return [] |
| 178 | + |
| 179 | + def convert_field(self, data_type): |
| 180 | + # Regular expression to match 'tinyint(n)' pattern |
| 181 | + pattern = r'tinyint\((\d+)\)' |
| 182 | + match = re.search(pattern, data_type) |
| 183 | + |
| 184 | + if match: |
| 185 | + size = match.group(1) |
| 186 | + return 'int', size |
| 187 | + |
| 188 | + # Regular expression to match 'int(n)' pattern |
| 189 | + pattern = r'int\((\d+)\)' |
| 190 | + match = re.search(pattern, data_type) |
| 191 | + |
| 192 | + if match: |
| 193 | + size = match.group(1) |
| 194 | + return 'int', size |
| 195 | + |
| 196 | + # Regular expression to match 'int(n)' pattern |
| 197 | + pattern = r'varchar\((\d+)\)' |
| 198 | + match = re.search(pattern, data_type) |
| 199 | + |
| 200 | + if match: |
| 201 | + size = match.group(1) |
| 202 | + return 'char', size |
| 203 | + else: |
| 204 | + return data_type, None |
| 205 | + |
| 206 | + def extract_enum_values(self, enum_string): |
| 207 | + pattern = r"'(.*?)'" |
| 208 | + enum_values = re.findall(pattern, enum_string) |
| 209 | + return enum_values |
| 210 | + |
| 211 | + def get_scheme_details(self, tables): |
| 212 | + cursor = self.conn.cursor() |
| 213 | + d = {} |
| 214 | + for table_name in tables: |
| 215 | + schema = self.get_table_schema(table_name) |
| 216 | + if schema: |
| 217 | + d[table_name] = {} |
| 218 | + for field in schema: |
| 219 | + converted_type, size = self.convert_field(field[1]) |
| 220 | + if converted_type.startswith('enum'): |
| 221 | + d[table_name]['type'] = 'enum' |
| 222 | + d[table_name]['size'] = None |
| 223 | + d[table_name]['list'] = self.extract_enum_values(converted_type) |
| 224 | + else: |
| 225 | + d[table_name]['type'] = converted_type |
| 226 | + d[table_name]['size'] = size |
| 227 | + else: |
| 228 | + print(f"No schema found for table '{table_name}'") |
| 229 | + cursor.close() |
| 230 | + return d |
| 231 | + |
| 232 | + |
| 233 | +def main(args): |
| 234 | + username = 'owhite' |
| 235 | + password = 'TaritRagi83' |
| 236 | + host = 'mysql-devel.igs.umaryland.edu' |
| 237 | + database = 'nemo_assets_devel' |
| 238 | + |
| 239 | + tables = [ |
| 240 | + "program", "project", "project_assoc_project", "cohort", |
| 241 | + "project_assoc_lab", "project_attributes", "grant_info", |
| 242 | + "project_has_contributor", "lab" |
| 243 | + ] |
| 244 | + |
| 245 | + manager = MySQLManager(username, password, host, database) |
| 246 | + manager.connect_to_mysql() |
| 247 | + |
| 248 | + if manager.conn: |
| 249 | + d = manager.get_scheme_details(tables) |
| 250 | + print(d) |
| 251 | + manager.close_connection() |
| 252 | + |
| 253 | + app = QApplication(args) |
| 254 | + |
| 255 | + wordSelector = WordSelector() |
| 256 | + wordSelector.show() |
| 257 | + sys.exit(app.exec_()) |
| 258 | + |
| 259 | +if __name__ == "__main__": |
| 260 | + main(sys.argv) |
0 commit comments