7 Module implementing the SQL Browser widget. |
7 Module implementing the SQL Browser widget. |
8 """ |
8 """ |
9 |
9 |
10 from PyQt4.QtCore import pyqtSignal, QVariant, Qt, pyqtSlot |
10 from PyQt4.QtCore import pyqtSignal, QVariant, Qt, pyqtSlot |
11 from PyQt4.QtGui import QWidget, QStandardItemModel, QDialog, QAbstractItemView |
11 from PyQt4.QtGui import QWidget, QStandardItemModel, QDialog, QAbstractItemView |
12 from PyQt4.QtSql import QSqlDatabase, QSqlError, QSqlTableModel, QSqlQueryModel, QSqlQuery |
12 from PyQt4.QtSql import QSqlDatabase, QSqlError, QSqlTableModel, \ |
|
13 QSqlQueryModel, QSqlQuery |
13 |
14 |
14 from E5Gui import E5MessageBox |
15 from E5Gui import E5MessageBox |
15 |
16 |
16 from .Ui_SqlBrowserWidget import Ui_SqlBrowserWidget |
17 from .Ui_SqlBrowserWidget import Ui_SqlBrowserWidget |
17 |
18 |
39 self.table.addAction(self.deleteRowAction) |
40 self.table.addAction(self.deleteRowAction) |
40 |
41 |
41 if len(QSqlDatabase.drivers()) == 0: |
42 if len(QSqlDatabase.drivers()) == 0: |
42 E5MessageBox.information(self, |
43 E5MessageBox.information(self, |
43 self.trUtf8("No database drivers found"), |
44 self.trUtf8("No database drivers found"), |
44 self.trUtf8("""This tool requires at least one Qt database driver. """ |
45 self.trUtf8( |
45 """Please check the Qt documentation how to build the """ |
46 """This tool requires at least one Qt database driver. """ |
46 """Qt SQL plugins.""")) |
47 """Please check the Qt documentation how to build the """ |
47 |
48 """Qt SQL plugins.""")) |
48 self.connections.tableActivated.connect(self.on_connections_tableActivated) |
49 |
49 self.connections.schemaRequested.connect(self.on_connections_schemaRequested) |
50 self.connections.tableActivated.connect( |
|
51 self.on_connections_tableActivated) |
|
52 self.connections.schemaRequested.connect( |
|
53 self.on_connections_schemaRequested) |
50 self.connections.cleared.connect(self.on_connections_cleared) |
54 self.connections.cleared.connect(self.on_connections_cleared) |
51 |
55 |
52 self.statusMessage.emit(self.trUtf8("Ready")) |
56 self.statusMessage.emit(self.trUtf8("Ready")) |
53 |
57 |
54 @pyqtSlot() |
58 @pyqtSlot() |
123 @return SQL error object (QSqlError) |
127 @return SQL error object (QSqlError) |
124 """ |
128 """ |
125 err = QSqlError() |
129 err = QSqlError() |
126 |
130 |
127 self.__class__.cCount += 1 |
131 self.__class__.cCount += 1 |
128 db = QSqlDatabase.addDatabase(driver.upper(), |
132 db = QSqlDatabase.addDatabase( |
129 "Browser{0:d}".format(self.__class__.cCount)) |
133 driver.upper(), "Browser{0:d}".format(self.__class__.cCount)) |
130 db.setDatabaseName(dbName) |
134 db.setDatabaseName(dbName) |
131 db.setHostName(host) |
135 db.setHostName(host) |
132 db.setPort(port) |
136 db.setPort(port) |
133 if not db.open(user, password): |
137 if not db.open(user, password): |
134 err = db.lastError() |
138 err = db.lastError() |
135 db = QSqlDatabase() |
139 db = QSqlDatabase() |
136 QSqlDatabase.removeDatabase("Browser{0:d}".format(self.__class__.cCount)) |
140 QSqlDatabase.removeDatabase( |
|
141 "Browser{0:d}".format(self.__class__.cCount)) |
137 |
142 |
138 self.connections.refresh() |
143 self.connections.refresh() |
139 |
144 |
140 return err |
145 return err |
141 |
146 |
145 """ |
150 """ |
146 from .SqlConnectionDialog import SqlConnectionDialog |
151 from .SqlConnectionDialog import SqlConnectionDialog |
147 dlg = SqlConnectionDialog(self) |
152 dlg = SqlConnectionDialog(self) |
148 if dlg.exec_() == QDialog.Accepted: |
153 if dlg.exec_() == QDialog.Accepted: |
149 driver, dbName, user, password, host, port = dlg.getData() |
154 driver, dbName, user, password, host, port = dlg.getData() |
150 err = self.addConnection(driver, dbName, user, password, host, port) |
155 err = self.addConnection( |
|
156 driver, dbName, user, password, host, port) |
151 |
157 |
152 if err.type() != QSqlError.NoError: |
158 if err.type() != QSqlError.NoError: |
153 E5MessageBox.warning(self, |
159 E5MessageBox.warning(self, |
154 self.trUtf8("Unable to open database"), |
160 self.trUtf8("Unable to open database"), |
155 self.trUtf8("""An error occurred while opening the connection.""")) |
161 self.trUtf8( |
|
162 """An error occurred while opening the connection.""")) |
156 |
163 |
157 def showTable(self, table): |
164 def showTable(self, table): |
158 """ |
165 """ |
159 Public slot to show the contents of a table. |
166 Public slot to show the contents of a table. |
160 |
167 |
170 self.table.setEditTriggers( |
177 self.table.setEditTriggers( |
171 QAbstractItemView.DoubleClicked | QAbstractItemView.EditKeyPressed) |
178 QAbstractItemView.DoubleClicked | QAbstractItemView.EditKeyPressed) |
172 |
179 |
173 self.table.resizeColumnsToContents() |
180 self.table.resizeColumnsToContents() |
174 |
181 |
175 self.table.selectionModel().currentRowChanged.connect(self.updateActions) |
182 self.table.selectionModel().currentRowChanged.connect( |
|
183 self.updateActions) |
176 |
184 |
177 self.updateActions() |
185 self.updateActions() |
178 |
186 |
179 def showSchema(self, table): |
187 def showSchema(self, table): |
180 """ |
188 """ |
201 model.setData(model.index(i, 0), fld.name()) |
209 model.setData(model.index(i, 0), fld.name()) |
202 if fld.typeID() == -1: |
210 if fld.typeID() == -1: |
203 model.setData(model.index(i, 1), |
211 model.setData(model.index(i, 1), |
204 QVariant.typeToName(fld.type())) |
212 QVariant.typeToName(fld.type())) |
205 else: |
213 else: |
206 model.setData(model.index(i, 1), "{0} ({1})".format( |
214 model.setData( |
207 QVariant.typeToName(fld.type()), |
215 model.index(i, 1), "{0} ({1})".format( |
208 fld.typeID())) |
216 QVariant.typeToName(fld.type()), fld.typeID())) |
209 if fld.length() < 0: |
217 if fld.length() < 0: |
210 model.setData(model.index(i, 2), "?") |
218 model.setData(model.index(i, 2), "?") |
211 else: |
219 else: |
212 model.setData(model.index(i, 2), fld.length()) |
220 model.setData(model.index(i, 2), fld.length()) |
213 if fld.precision() < 0: |
221 if fld.precision() < 0: |
280 def executeQuery(self): |
288 def executeQuery(self): |
281 """ |
289 """ |
282 Public slot to execute the entered query. |
290 Public slot to execute the entered query. |
283 """ |
291 """ |
284 model = QSqlQueryModel(self.table) |
292 model = QSqlQueryModel(self.table) |
285 model.setQuery( |
293 model.setQuery(QSqlQuery( |
286 QSqlQuery(self.sqlEdit.toPlainText(), self.connections.currentDatabase())) |
294 self.sqlEdit.toPlainText(), self.connections.currentDatabase())) |
287 self.table.setModel(model) |
295 self.table.setModel(model) |
288 |
296 |
289 if model.lastError().type() != QSqlError.NoError: |
297 if model.lastError().type() != QSqlError.NoError: |
290 self.statusMessage.emit(model.lastError().text()) |
298 self.statusMessage.emit(model.lastError().text()) |
291 elif model.query().isSelect(): |
299 elif model.query().isSelect(): |