Separate the database details into a class that can provide different connections using the same connection information. That paves the way for sub-processes that require their own connection.
Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all
Unified · Split
| @@ -284,6 +284,42 @@ class MainWindow(QMainWindow): · 283 unmodified lines | ||
| 284 | 284 | |
| 285 | 285 | self.setCentralWidget(self.view) |
| 286 | 286 | |
| 287 | +# Database reference | |
| 288 | + | |
| 289 | +class DBRef(): | |
| 290 | + | |
| 291 | + def __init__(self, is_sqlite3, dbname): | |
| 292 | + self.is_sqlite3 = is_sqlite3 | |
| 293 | + self.dbname = dbname | |
| 294 | + | |
| 295 | + def Open(self, connection_name): | |
| 296 | + dbname = self.dbname | |
| 297 | + if self.is_sqlite3: | |
| 298 | + db = QSqlDatabase.addDatabase("QSQLITE", connection_name) | |
| 299 | + else: | |
| 300 | + db = QSqlDatabase.addDatabase("QPSQL", connection_name) | |
| 301 | + opts = dbname.split() | |
| 302 | + for opt in opts: | |
| 303 | + if "=" in opt: | |
| 304 | + opt = opt.split("=") | |
| 305 | + if opt[0] == "hostname": | |
| 306 | + db.setHostName(opt[1]) | |
| 307 | + elif opt[0] == "port": | |
| 308 | + db.setPort(int(opt[1])) | |
| 309 | + elif opt[0] == "username": | |
| 310 | + db.setUserName(opt[1]) | |
| 311 | + elif opt[0] == "password": | |
| 312 | + db.setPassword(opt[1]) | |
| 313 | + elif opt[0] == "dbname": | |
| 314 | + dbname = opt[1] | |
| 315 | + else: | |
| 316 | + dbname = opt | |
| 317 | + | |
| 318 | + db.setDatabaseName(dbname) | |
| 319 | + if not db.open(): | |
| 320 | + raise Exception("Failed to open database " + dbname + " error: " + db.lastError().text()) | |
| 321 | + return db, dbname | |
| 322 | + | |
| 287 | 323 | # Main |
| 288 | 324 | |
| 289 | 325 | def Main(): |
| @@ -302,31 +338,8 @@ def Main(): · 12 unmodified lines | ||
| 302 | 338 | except: |
| 303 | 339 | pass |
| 304 | 340 | |
| 305 | − if is_sqlite3: | |
| 306 | − db = QSqlDatabase.addDatabase('QSQLITE') | |
| 307 | − else: | |
| 308 | − db = QSqlDatabase.addDatabase('QPSQL') | |
| 309 | − opts = dbname.split() | |
| 310 | − for opt in opts: | |
| 311 | − if '=' in opt: | |
| 312 | − opt = opt.split('=') | |
| 313 | − if opt[0] == 'hostname': | |
| 314 | − db.setHostName(opt[1]) | |
| 315 | − elif opt[0] == 'port': | |
| 316 | − db.setPort(int(opt[1])) | |
| 317 | − elif opt[0] == 'username': | |
| 318 | − db.setUserName(opt[1]) | |
| 319 | − elif opt[0] == 'password': | |
| 320 | − db.setPassword(opt[1]) | |
| 321 | − elif opt[0] == 'dbname': | |
| 322 | − dbname = opt[1] | |
| 323 | − else: | |
| 324 | − dbname = opt | |
| 325 | − | |
| 326 | − db.setDatabaseName(dbname) | |
| 327 | − if not db.open(): | |
| 328 | − raise Exception("Failed to open database " + dbname + " error: " + db.lastError().text()) | |
| 329 | − | |
| 341 | + dbref = DBRef(is_sqlite3, dbname) | |
| 342 | + db, dbname = dbref.Open("main") | |
| 330 | 343 | app = QApplication(sys.argv) |
| 331 | 344 | window = MainWindow(db, dbname) |
| 332 | 345 | window.show() |