From ab782e4cbe1ede9c8903878b652454519b12f15c Mon Sep 17 00:00:00 2001 From: necessarylion Date: Tue, 2 Jan 2024 22:26:38 +0630 Subject: [PATCH] throw error when database type is not match with driver --- packages/dox-query-builder/lib/src/main.dart | 15 +++++++++++++-- packages/dox-query-builder/test/connection.dart | 12 ++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/dox-query-builder/lib/src/main.dart b/packages/dox-query-builder/lib/src/main.dart index c17d9d6..f3d3620 100644 --- a/packages/dox-query-builder/lib/src/main.dart +++ b/packages/dox-query-builder/lib/src/main.dart @@ -1,5 +1,7 @@ import 'package:dox_query_builder/dox_query_builder.dart'; import 'package:dox_query_builder/src/drivers/mysql_driver.dart'; +import 'package:mysql1/mysql1.dart'; +import 'package:postgres/postgres.dart'; class SqlQueryBuilder { static final SqlQueryBuilder _singleton = SqlQueryBuilder._internal(); @@ -33,10 +35,19 @@ class SqlQueryBuilder { SqlQueryBuilder sql = SqlQueryBuilder(); if (driver == Driver.postgres) { + if (database is! Connection) { + throw Exception( + 'Invalid database connection. It must be postgres `Connection` type'); + } sql.dbDriver = PostgresDriver(conn: database); - } - if (driver == Driver.mysql) { + } else if (driver == Driver.mysql) { + if (database is! MySqlConnection) { + throw Exception( + 'Invalid database connection. It must be `MySqlConnection` type'); + } sql.dbDriver = MysqlDriver(conn: database); + } else { + throw Exception('Invalid driver or not supported'); } sql.debug = debug; diff --git a/packages/dox-query-builder/test/connection.dart b/packages/dox-query-builder/test/connection.dart index 1cc333f..d6db65f 100644 --- a/packages/dox-query-builder/test/connection.dart +++ b/packages/dox-query-builder/test/connection.dart @@ -13,17 +13,17 @@ Future initQueryBuilder() async { } Future poolConnection() { - if (Platform.environment['DRIVER'] == 'postgres') { - return postgresConnection(); - } else { + if (Platform.environment['DRIVER'] == 'mysql') { return mysqlConnection(); + } else { + return postgresConnection(); } } Driver getDriver() { - if (Platform.environment['DRIVER'] == 'postgres') { - return Driver.postgres; - } else { + if (Platform.environment['DRIVER'] == 'mysql') { return Driver.mysql; + } else { + return Driver.postgres; } }