|
|
@@ -7,6 +7,9 @@ import 'package:path/path.dart';
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
|
+// 为桌面平台添加 ffi 支持
|
|
|
+import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
|
+
|
|
|
class SqliteManager {
|
|
|
// 单例模式
|
|
|
static final SqliteManager _instance = SqliteManager._internal();
|
|
|
@@ -28,6 +31,14 @@ class SqliteManager {
|
|
|
/// [dbName] 数据库名称
|
|
|
/// [version] 数据库版本(默认1)
|
|
|
Future<void> init({required String dbName, int version = 1}) async {
|
|
|
+ // 在桌面平台上初始化 ffi 数据库工厂
|
|
|
+ if (Platform.isWindows || Platform.isMacOS || Platform.isLinux) {
|
|
|
+ // 初始化 FFI 数据库工厂
|
|
|
+ sqfliteFfiInit();
|
|
|
+ // 设置数据库工厂为 FFI 工厂
|
|
|
+ databaseFactory = databaseFactoryFfi;
|
|
|
+ }
|
|
|
+
|
|
|
_dbName = dbName;
|
|
|
_dbVersion = version;
|
|
|
if (TableConfig.defaultConfigs().isEmpty) {
|
|
|
@@ -43,30 +54,56 @@ class SqliteManager {
|
|
|
if (_dbName == null) {
|
|
|
throw Exception('请先调用 init 方法初始化数据库配置');
|
|
|
}
|
|
|
- // 获取数据库路径
|
|
|
- // Directory documentsDir = await getApplicationDocumentsDirectory();
|
|
|
- // String dbPath = join(documentsDir.path, _dbName!);
|
|
|
|
|
|
- // 1. 获取应用文档根目录
|
|
|
- Directory? documentsDir = await getExternalStorageDirectory();
|
|
|
- // logger.d('应用文档根目录1:${documentsDir?.path}');
|
|
|
- documentsDir ??= await getApplicationDocumentsDirectory();
|
|
|
- // logger.d('应用文档根目录2:${documentsDir.path}');
|
|
|
- // 2. 拼接自定义的 db 子文件夹路径
|
|
|
- String dbFolderPath = join(documentsDir.path, 'db');
|
|
|
- // 3. 检查并创建 db 文件夹(不存在则创建)
|
|
|
- Directory dbFolder = Directory(dbFolderPath);
|
|
|
- if (!await dbFolder.exists()) {
|
|
|
- await dbFolder.create(recursive: true); // recursive: true 支持多级文件夹创建
|
|
|
- logger.d('已创建 db 文件夹:$dbFolderPath');
|
|
|
- }
|
|
|
-
|
|
|
- // 4. 拼接最终的数据库文件路径(db文件夹 + 数据库名)
|
|
|
+ String dbFolderPath = await _dbFolderPath();
|
|
|
String dbPath = join(dbFolderPath, _dbName!);
|
|
|
logger.d('数据库文件路径:$dbPath');
|
|
|
return dbPath;
|
|
|
}
|
|
|
|
|
|
+ Future<String> _dbFolderPath() async {
|
|
|
+ String dbFolderPath = "";
|
|
|
+ if (Platform.isAndroid) {
|
|
|
+ // 1. 获取应用文档根目录
|
|
|
+ Directory? documentsDir = await getExternalStorageDirectory();
|
|
|
+ // logger.d('应用文档根目录1:${documentsDir?.path}');
|
|
|
+ documentsDir ??= await getApplicationDocumentsDirectory();
|
|
|
+ // logger.d('应用文档根目录2:${documentsDir.path}');
|
|
|
+ // 2. 拼接自定义的 db 子文件夹路径
|
|
|
+ dbFolderPath = join(documentsDir.path, 'db');
|
|
|
+ // 3. 检查并创建 db 文件夹(不存在则创建)
|
|
|
+ Directory dbFolder = Directory(dbFolderPath);
|
|
|
+ if (!await dbFolder.exists()) {
|
|
|
+ await dbFolder.create(recursive: true); // recursive: true 支持多级文件夹创建
|
|
|
+ logger.d('已创建 db 文件夹:$dbFolderPath');
|
|
|
+ }
|
|
|
+ } else if (Platform.isIOS) {
|
|
|
+ } else if (Platform.isMacOS || Platform.isLinux) {
|
|
|
+ // macOS 和 Linux 使用文档目录
|
|
|
+ Directory documentsDir = await getApplicationDocumentsDirectory();
|
|
|
+ dbFolderPath = join(documentsDir.path, 'db');
|
|
|
+ // 检查并创建 db 文件夹(不存在则创建)
|
|
|
+ Directory dbFolder = Directory(dbFolderPath);
|
|
|
+ if (!await dbFolder.exists()) {
|
|
|
+ await dbFolder.create(recursive: true);
|
|
|
+ logger.d('已创建 db 文件夹:$dbFolderPath');
|
|
|
+ }
|
|
|
+ } else if (Platform.isWindows) {
|
|
|
+ // Windows 使用应用运行目录下的Data目录
|
|
|
+ dbFolderPath = join(Directory.current.path, 'Data');
|
|
|
+ // 检查并创建 Data 文件夹(不存在则创建)
|
|
|
+ Directory dbFolder = Directory(dbFolderPath);
|
|
|
+ if (!await dbFolder.exists()) {
|
|
|
+ await dbFolder.create(recursive: true);
|
|
|
+ logger.d('已创建 Data 文件夹:$dbFolderPath');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw Exception('不支持的操作系统');
|
|
|
+ }
|
|
|
+
|
|
|
+ return dbFolderPath;
|
|
|
+ }
|
|
|
+
|
|
|
/// 内部方法:获取数据库连接(自动创建/升级)
|
|
|
Future<Database> _getDatabase() async {
|
|
|
if (_dbPath.isEmpty) {
|
|
|
@@ -334,10 +371,27 @@ class SqliteManager {
|
|
|
throw Exception('请先调用 init 方法初始化数据库配置');
|
|
|
}
|
|
|
await closeDb();
|
|
|
- Directory documentsDir = await getApplicationDocumentsDirectory();
|
|
|
- String dbPath = join(documentsDir.path, _dbName!);
|
|
|
- await deleteDatabase(dbPath);
|
|
|
- logger.d('数据库文件已删除: $dbPath');
|
|
|
+
|
|
|
+ // // 使用正确的平台相关路径删除数据库
|
|
|
+ // String dbPath;
|
|
|
+ // if (Platform.isAndroid) {
|
|
|
+ // Directory? documentsDir = await getExternalStorageDirectory();
|
|
|
+ // documentsDir ??= await getApplicationDocumentsDirectory();
|
|
|
+ // dbPath = join(documentsDir.path, 'db', _dbName!);
|
|
|
+ // } else if (Platform.isIOS) {
|
|
|
+ // Directory documentsDir = await getApplicationDocumentsDirectory();
|
|
|
+ // dbPath = join(documentsDir.path, _dbName!);
|
|
|
+ // } else if (Platform.isMacOS || Platform.isLinux) {
|
|
|
+ // Directory documentsDir = await getApplicationDocumentsDirectory();
|
|
|
+ // dbPath = join(documentsDir.path, 'db', _dbName!);
|
|
|
+ // } else if (Platform.isWindows) {
|
|
|
+ // dbPath = join(Directory.current.path, 'Data', _dbName!);
|
|
|
+ // } else {
|
|
|
+ // throw Exception('不支持的操作系统');
|
|
|
+ // }
|
|
|
+
|
|
|
+ await databaseFactory.deleteDatabase(_dbPath);
|
|
|
+ logger.d('数据库文件已删除: $_dbPath');
|
|
|
}
|
|
|
|
|
|
/// 清空表数据
|