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