Skip to content

Commit

Permalink
throw error when database type is not match with driver
Browse files Browse the repository at this point in the history
  • Loading branch information
necessarylion committed Jan 2, 2024
1 parent ac3acb3 commit ab782e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
15 changes: 13 additions & 2 deletions packages/dox-query-builder/lib/src/main.dart
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions packages/dox-query-builder/test/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ Future<void> initQueryBuilder() async {
}

Future<dynamic> 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;
}
}

0 comments on commit ab782e4

Please sign in to comment.