diff --git a/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart b/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart index 7635688..4983ac3 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_connexion.dart @@ -1,6 +1,5 @@ import 'dart:convert'; import 'dart:io'; - import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; @@ -9,19 +8,26 @@ import 'package:postgresql2/constants.dart'; import 'package:postgresql2/pool.dart'; import 'package:postgresql2/postgresql.dart'; + class DatabaseConnexion{ - final String filePath = 'assets/logs.txt'; + // Path to the file containing the database identifiers + static final String filePath = 'assets/logs.txt'; + - String? _psqlUser; - String? _psqlPswd; - String? _psqlHost; - String? _psqlDataBase; + // Database identifiers + static String? _psqlUser; + static String? _psqlPswd; + static String? _psqlHost; + static String? _psqlDataBase; - Future _loadLogs() async{ + // Read the database connection identifiers in a file + static Future _loadLogs() async{ try{ final _loadedData = await rootBundle.loadString(filePath); + print('appel de -readLogs'); + final _logs = LineSplitter.split(_loadedData).toList(); _psqlUser = _logs[0]; @@ -35,9 +41,12 @@ class DatabaseConnexion{ } + //Initialise connexion to the database - Future initConnexion() async{ - await _loadLogs(); + static Future initConnexion() async{ + if(_psqlHost == null || _psqlPswd == null || _psqlUser == null || _psqlDataBase == null){ + await _loadLogs(); + } var uri = 'postgres://$_psqlUser:$_psqlPswd@$_psqlHost:5442/$_psqlDataBase'; diff --git a/Sources/dafl_project_flutter/lib/persistence/database_loader.dart b/Sources/dafl_project_flutter/lib/persistence/database_loader.dart index a632976..94416d4 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_loader.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_loader.dart @@ -5,12 +5,10 @@ import '../model/user.dart'; import 'database_connexion.dart'; class DatabaseLoader extends Loader{ - DatabaseConnexion dbConnexion = DatabaseConnexion(); - @override Future load(String? username, String? password) async { - final connection = await dbConnexion.initConnexion(); + final connection = await DatabaseConnexion.initConnexion(); connection.query('select * from utilisateur where username = @username AND password = @password', {'username': username, @@ -18,4 +16,5 @@ class DatabaseLoader extends Loader{ .then((result) { print(result); }); } + } diff --git a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart index 53d70fe..e900980 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_saver.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_saver.dart @@ -9,11 +9,12 @@ class DatabaseSaver extends Saver{ @override void save(User userToSave) async{ - final connection = await dbConnexion.initConnexion(); + final connection = await DatabaseConnexion.initConnexion(); connection.execute('insert into utilisateur (username, password) values (@username, @password)', { 'id' : '', 'username': userToSave.usernameDafl, 'password' : userToSave.passwDafl}).then((_) {}); } + } \ No newline at end of file diff --git a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart index 6148e8a..5bf2d40 100644 --- a/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart +++ b/Sources/dafl_project_flutter/lib/persistence/database_searcher.dart @@ -3,14 +3,12 @@ import 'package:dafl_project_flutter/persistence/database_connexion.dart'; import 'searcher.dart'; class DatabaseSearcher extends Searcher{ - DatabaseConnexion dbConnexion = DatabaseConnexion(); - Future searchUser(String? username, String? password) async { return true; } @override Future searchByUsername(String? username) async{ - final connection = await dbConnexion.initConnexion(); + final connection = await DatabaseConnexion.initConnexion(); connection.query('select * from utilisateur where username = $username').toList().then((rows) { if(rows.isEmpty){ diff --git a/Sources/dafl_project_flutter/test/testFileRead.dart b/Sources/dafl_project_flutter/test/testFileRead.dart index d5b2113..5904fdd 100644 --- a/Sources/dafl_project_flutter/test/testFileRead.dart +++ b/Sources/dafl_project_flutter/test/testFileRead.dart @@ -10,6 +10,6 @@ Future main() async { DatabaseConnexion d = DatabaseConnexion(); - d.initConnexion(); + DatabaseConnexion.initConnexion(); } \ No newline at end of file