diff --git a/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/Database.ets b/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/Database.ets index db9fb2d93245a4d95e47e864cd5085be788f5683..60986acfe033bd02bc1d7d36e2d6b7a037d1cfc3 100644 --- a/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/Database.ets +++ b/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/Database.ets @@ -152,8 +152,8 @@ export class Database { } /// insert - public static insert(operation: Operation, db: relationalStore.RdbStore): void { - Database.doInsert(operation, db); + public static async insert(operation: Operation, db: relationalStore.RdbStore): Promise { + await Database.doInsert(operation, db); } private static async doInsert(operation: Operation, db: relationalStore.RdbStore): Promise { @@ -173,14 +173,15 @@ export class Database { } /// update - public static update(operation: Operation, db: relationalStore.RdbStore): void { - Database.doUpdate(operation, db); + public static async update(operation: Operation, db: relationalStore.RdbStore): Promise { + await Database.doUpdate(operation, db); } private static async doUpdate(operation: Operation, db: relationalStore.RdbStore): Promise { let executeSuccess: boolean = await Database.executeOrError(operation, db, (rows) => { operation.success(rows); }); + if(!executeSuccess) { return false; } @@ -194,8 +195,8 @@ export class Database { } /// query - public static query(operation: Operation, db: relationalStore.RdbStore): void { - Database.doQuery(operation, db); + public static async query(operation: Operation, db: relationalStore.RdbStore): Promise { + await Database.doQuery(operation, db); } private static async doQuery(operation: Operation, db: relationalStore.RdbStore): Promise { diff --git a/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/Messages.ets b/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/Messages.ets index 4668bdf0f48de8d8aa7673afbf7f8f215feb1c56..7ceb88e1d43324451ad730235d20f7c190c9e5bf 100644 --- a/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/Messages.ets +++ b/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/Messages.ets @@ -13,11 +13,13 @@ * limitations under the License. */ -import Log from '@ohos/flutter_ohos/src/main/ets/util/Log'; -import BasicMessageChannel from '@ohos/flutter_ohos/src/main/ets/plugin/common/BasicMessageChannel'; -import { BinaryMessenger } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BinaryMessenger'; -import MessageCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/MessageCodec'; -import StandardMessageCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/StandardMessageCodec'; +import { + BasicMessageChannel, + BinaryMessenger, + Log, + MessageCodec, + StandardMessageCodec, +} from '@ohos/flutter_ohos' import { SqlHelper } from './sql_helper'; @@ -85,7 +87,7 @@ export abstract class SqfliteApi { abstract databaseExists(dataBaseName: string): Promise; static getCodec(): MessageCodec { - return new StandardMessageCodec(); + return StandardMessageCodec.INSTANCE; } static setup(binaryMessenger: BinaryMessenger, api: SqfliteApi) { diff --git a/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/SqflitePlugin.ets b/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/SqflitePlugin.ets index 207e323ff4af4670edbfd50e463982ad9577d869..a20e636bb60c9e27b16c6a8c933ec65658b4e5b1 100644 --- a/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/SqflitePlugin.ets +++ b/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/SqflitePlugin.ets @@ -14,100 +14,76 @@ */ import common from '@ohos.app.ability.common'; -import AbilityAware from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/ability/AbilityAware'; -import { - AbilityPluginBinding -} from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/ability/AbilityPluginBinding'; -import { - FlutterPlugin, - FlutterPluginBinding -} from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/FlutterPlugin'; -import MethodChannel, { - MethodCallHandler, - MethodResult -} from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel'; -import Log from '@ohos/flutter_ohos/src/main/ets/util/Log'; -import { BinaryMessenger } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BinaryMessenger'; -import { SqfliteApi } from './Messages'; -import { SqfLiteHelper } from './sqflite_helper'; import relationalStore from '@ohos.data.relationalStore'; import { ValueType } from '@ohos.data.ValuesBucket'; -import { SqlHelper } from './sql_helper'; +import { + AbilityAware, + AbilityPluginBinding, + BackgroundMethodChannel, + BinaryMessenger, + FlutterPlugin, + FlutterPluginBinding, + Log, + MethodCall, + MethodResult, + SendableMethodCallHandler, + SendableStandardMethodCodec, + TaskQueueOptions +} from '@ohos/flutter_ohos'; + import { Constant } from './OhosConstant'; -import MethodCall from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodCall'; import { Database } from './Database'; import { MethodCallOperation } from './operation/MethodCallOperation'; +import { SqfliteApi } from './Messages'; +import { SqfLiteHelper } from './sqflite_helper'; +import { SqlHelper } from './sql_helper'; const TAG: string = "SqflitePlugin"; -export default class SqflitePlugin extends SqfliteApi implements FlutterPlugin, AbilityAware, MethodCallHandler { - private pluginBinding: FlutterPluginBinding | null = null; - private abilityPluginBinding: AbilityPluginBinding | null = null; - private context: common.Context | null = null; - private channel: MethodChannel | null = null; - - constructor(context?: common.Context) { - super(); - if (context) { - this.context = context; - } - } - - getUniqueClassName(): string { - return "SqflitePlugin"; - } - - onAttachedToEngine(binding: FlutterPluginBinding): void { - this.pluginBinding = binding; - this.channel = new MethodChannel(binding.getBinaryMessenger(), Constant.PLUGIN_KEY); - this.channel.setMethodCallHandler(this); - } - - onDetachedFromEngine(binding: FlutterPluginBinding): void { - this.pluginBinding = null; - } +@Sendable +class SqfLiteMethodCallHandler implements SendableMethodCallHandler { - onMethodCall(call: MethodCall, result: MethodResult): void { - Log.d(TAG, '-----onMethodCall step in ---' + call.method) + async onMethodCall(call: MethodCall, result: MethodResult, ...args: Object[]): Promise { + Log.d('SqflitePlugin', '-----onMethodCall step in ---' + call.method) switch (call.method) { case Constant.METHOD_OPEN_DATABASE: - this.handleOpenDatabaseCall(call, result); + await this.handleOpenDatabaseCall(args[0] as common.Context, call, result); break; case Constant.METHOD_GET_DATABASES_PATH: - this.handleGetDatabasesPathCall(call, result); + this.handleGetDatabasesPathCall(args[0] as common.Context, call, result); break; case Constant.METHOD_DATABASE_EXISTS: - this.handleDatabaseExistsCall(call, result); + await this.handleDatabaseExistsCall(call, result); break; case Constant.METHOD_EXECUTE: - this.handleExecuteCall(call, result); + await this.handleExecuteCall(call, result); break; case Constant.METHOD_INSERT: - this.handleInsertCall(call, result); + await this.handleInsertCall(call, result); break; case Constant.METHOD_QUERY: - this.handleQueryCall(call, result); + await this.handleQueryCall(call, result); break; case Constant.METHOD_UPDATE: - this.handleUpdateCall(call, result); + await this.handleUpdateCall(call, result); break; case Constant.METHOD_BATCH: - this.handleBatchCall(call, result); + await this.handleBatchCall(call, result); break; case Constant.METHOD_OPTIONS: - this.handleOptionsCall(call, result); + await this.handleOptionsCall(call, result); break; case Constant.METHOD_DEBUG_MODE: - this.handleDebugModeCall(call, result); + await this.handleDebugModeCall(call, result); break; case Constant.METHOD_GET_PLATFORM_VERSION: - this.handleGetPlatformVersionCall(call, result); + await this.handleGetPlatformVersionCall(call, result); break; case Constant.METHOD_CLOSE_DATABASE: - this.handleCloseDatabaseCall(call, result); + await this.handleCloseDatabaseCall(call, result); break; case Constant.METHOD_DELETE_DATABASE: - this.handleDeleteDatabaseCall(call, result); + await this.handleDeleteDatabaseCall(args[0] as common.Context, call, result); break; default: result.notImplemented(); @@ -115,75 +91,44 @@ export default class SqflitePlugin extends SqfliteApi implements FlutterPlugin, } } - onAttachedToAbility(binding: AbilityPluginBinding): void { - Log.i(TAG, "flutter-sqflite-onAttachedToAbility"); - this.setup(this.pluginBinding!.getBinaryMessenger(), this.pluginBinding!.getApplicationContext()); - this.abilityPluginBinding = binding; - this.channel?.setMethodCallHandler(this); - } - - onDetachedFromAbility(): void { - } - - static registerWith(): void { - } - - setup(messenger: BinaryMessenger, context: common.Context) { - try { - SqfliteApi.setup(messenger, this); - } catch (err) { - Log.e(TAG, "Received exception while setting up SqflitePlugin", err); - } - this.context = context; - } - - async options(data: Map): Promise { - data.forEach((value, key) => { - Log.i(TAG, 'options收到数据: ' + key + ' -> ' + value); - }) - } - - getPlatformVersion(): string { - Log.i(TAG, "HarmonyOS 通信正常"); - return "HarmonyOS 通信正常"; - } - //----------begin - ///打开数据库 - async handleOpenDatabaseCall(call: MethodCall, result: MethodResult): Promise { + /// 打开数据库 + async handleOpenDatabaseCall(context: common.Context, call: MethodCall, result: MethodResult): Promise { let daPath: string = call.argument(Constant.PARAM_PATH); - await SqfLiteHelper.getDataBase(this.context!, daPath, result); + await SqfLiteHelper.getDataBase(context, daPath, result); } - ///获取数据库路径 - handleGetDatabasesPathCall(call: MethodCall, result: MethodResult) { - let appContext = this.context!.getApplicationContext(); + /// 获取数据库路径 + handleGetDatabasesPathCall(context: common.Context, call: MethodCall, result: MethodResult): void { + let appContext = context.getApplicationContext(); let path: string = appContext.databaseDir; result.success(path); } - ///检查数据库是否存在 + /// 检查数据库是否存在 async handleDatabaseExistsCall(call: MethodCall, result: MethodResult): Promise { let dbPath: string = call.argument(Constant.PARAM_PATH); let isExist: boolean = await SqfLiteHelper.checkDatabaseExisted(dbPath); result.success(isExist); } - ///执行Sql + /// 执行Sql async handleExecuteCall(call: MethodCall, result: MethodResult): Promise { - let rdbStore = SqfLiteHelper.getDatabaseOrError(call, result); + let rdbStore = SqfLiteHelper.getDatabaseOrError(call); if (rdbStore == null) { + result.error(Constant.SQLITE_ERROR, 'Failed to get database', ''); return; } - let operation: MethodCallOperation = new MethodCallOperation(call, result); - Database.execute(operation, rdbStore); + let operation: MethodCallOperation = new MethodCallOperation(call, result); + await Database.execute(operation, rdbStore); } - ///插入数据 + /// 插入数据 async handleInsertCall(call: MethodCall, result: MethodResult): Promise { - let rdbStore = SqfLiteHelper.getDatabaseOrError(call, result); + let rdbStore = SqfLiteHelper.getDatabaseOrError(call); if (rdbStore == null) { + result.error(Constant.SQLITE_ERROR, 'Failed to get database', ''); return; } @@ -199,14 +144,14 @@ export default class SqflitePlugin extends SqfliteApi implements FlutterPlugin, } let operation: MethodCallOperation = new MethodCallOperation(call, result); - Database.insert(operation, rdbStore); + await Database.insert(operation, rdbStore); } - ///查询数据 + /// 查询数据 async handleQueryCall(call: MethodCall, result: MethodResult): Promise { - Log.i(TAG, "begin query data"); - let rdbStore = SqfLiteHelper.getDatabaseOrError(call, result); + let rdbStore = SqfLiteHelper.getDatabaseOrError(call); if (rdbStore == null) { + result.error(Constant.SQLITE_ERROR, 'Failed to get database', ''); return; } @@ -222,13 +167,14 @@ export default class SqflitePlugin extends SqfliteApi implements FlutterPlugin, } let operation: MethodCallOperation = new MethodCallOperation(call, result); - Database.query(operation, rdbStore); + await Database.query(operation, rdbStore); } - ///更新(包括删除)数据 + /// 更新(包括删除)数据 async handleUpdateCall(call: MethodCall, result: MethodResult): Promise { - let rdbStore = SqfLiteHelper.getDatabaseOrError(call, result); + let rdbStore = SqfLiteHelper.getDatabaseOrError(call); if (rdbStore == null) { + result.error(Constant.SQLITE_ERROR, 'Failed to get database', ''); return; } @@ -244,55 +190,121 @@ export default class SqflitePlugin extends SqfliteApi implements FlutterPlugin, } let operation: MethodCallOperation = new MethodCallOperation(call, result); - Database.update(operation, rdbStore); + await Database.update(operation, rdbStore); } - ///batch操作 + /// batch操作 async handleBatchCall(call: MethodCall, result: MethodResult): Promise { - let rdbStore = SqfLiteHelper.getDatabaseOrError(call, result); + let rdbStore = SqfLiteHelper.getDatabaseOrError(call); if (rdbStore == null) { + result.error(Constant.SQLITE_ERROR, 'Failed to get database', ''); return; } - Database.batch(call, result, rdbStore); + await Database.batch(call, result, rdbStore); } - ///Options操作,设置LogLevel,仅限开发,已废弃 + /// Options操作,设置LogLevel,仅限开发,已废弃 async handleOptionsCall(call: MethodCall, result: MethodResult): Promise { SqfLiteHelper.options(call, result); } - ///设置debug模式,已废弃 + /// 设置debug模式,已废弃 async handleDebugModeCall(call: MethodCall, result: MethodResult): Promise { SqfLiteHelper.debugMode(call, result); } - ///获取设备信息 + /// 获取设备信息 async handleGetPlatformVersionCall(call: MethodCall, result: MethodResult): Promise { SqfLiteHelper.getPlatformVersion(call, result); } - ///关闭数据库 + /// 关闭数据库 async handleCloseDatabaseCall(call: MethodCall, result: MethodResult): Promise { - let rdbStore = SqfLiteHelper.getDatabaseOrError(call, result); + let rdbStore = SqfLiteHelper.getDatabaseOrError(call); if (rdbStore == null) { + result.error(Constant.SQLITE_ERROR, 'Failed to get database', ''); return; } SqfLiteHelper.closeDataBase(call, result); - } - ///删除数据库 - async handleDeleteDatabaseCall(call: MethodCall, result: MethodResult): Promise { + /// 删除数据库 + async handleDeleteDatabaseCall(context: common.Context, call: MethodCall, result: MethodResult): Promise { let dataBaseName: string = call.argument(Constant.PARAM_PATH); - let res = await SqfLiteHelper.deleteDataBase(this.context!, dataBaseName); + let res = await SqfLiteHelper.deleteDataBase(context, dataBaseName); if (res) { result.success(null); - }else { + } else { result.error('-1', 'Delete database faile', ''); } } - //----------end +} + +export default class SqflitePlugin extends SqfliteApi implements FlutterPlugin, AbilityAware { + private pluginBinding: FlutterPluginBinding | null = null; + private abilityPluginBinding: AbilityPluginBinding | null = null; + private context: common.Context | null = null; + private channel: BackgroundMethodChannel | null = null; + + constructor(context?: common.Context) { + super(); + if (context) { + this.context = context; + } + } + + getUniqueClassName(): string { + return "SqflitePlugin"; + } + + onAttachedToEngine(binding: FlutterPluginBinding): void { + this.pluginBinding = binding; + } + + onDetachedFromEngine(binding: FlutterPluginBinding): void { + this.pluginBinding = null; + } + + onAttachedToAbility(binding: AbilityPluginBinding): void { + Log.i(TAG, "flutter-sqflite-onAttachedToAbility"); + this.setup(this.pluginBinding!.getBinaryMessenger(), this.pluginBinding!.getApplicationContext()); + this.abilityPluginBinding = binding; + + let option: TaskQueueOptions = new TaskQueueOptions().setSingleThreadMode(true); + this.channel = new BackgroundMethodChannel( + this.pluginBinding!.getBinaryMessenger(), + Constant.PLUGIN_KEY, + SendableStandardMethodCodec.INSTANCE, + this.pluginBinding!.getBinaryMessenger().makeBackgroundTaskQueue(option), + this.context! + ); + this.channel!.setMethodCallHandler(new SqfLiteMethodCallHandler()); + } + + onDetachedFromAbility(): void { + this.channel?.setMethodCallHandler(null); + } + + setup(messenger: BinaryMessenger, context: common.Context) { + try { + SqfliteApi.setup(messenger, this); + } catch (err) { + Log.e(TAG, "Received exception while setting up SqflitePlugin", err); + } + this.context = context; + } + + async options(data: Map): Promise { + data.forEach((value, key) => { + Log.i(TAG, 'options收到数据: ' + key + ' -> ' + value); + }) + } + + getPlatformVersion(): string { + Log.i(TAG, "HarmonyOS 通信正常"); + return "HarmonyOS 通信正常"; + } /// 获取数据库的路径 getDatabasesPath(): string { diff --git a/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/sqflite_helper.ets b/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/sqflite_helper.ets index dc13a0ce44b954ed03321cfe29dafa901ddb3fa5..a2c1926c90c84362ab6eaa4a636f1de85e416555 100644 --- a/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/sqflite_helper.ets +++ b/sqflite/ohos/src/main/ets/io/flutter/plugins/sqflite/sqflite_helper.ets @@ -187,27 +187,19 @@ export class SqfLiteHelper { if (SqfLiteHelper.dataBaseList.has(dataBaseName)) { return SqfLiteHelper.dataBaseList.get(dataBaseName) as relationalStore.RdbStore | null; } - Log.i(TAG, '没有加载这个数据库', '%{public}s'); + Log.i(TAG, '没有加载这个数据库', '%{public}s', dataBaseName); return null; } /// 从已加载的列表里获取一个数据库 - public static getDatabaseOrError(call: MethodCall, result: MethodResult): relationalStore.RdbStore | null { + public static getDatabaseOrError(call: MethodCall): relationalStore.RdbStore | null { let dataBaseId: number = call.argument("id"); let databaseName: string | undefined = SqfLiteHelper.dBIdList.get(dataBaseId); if (typeof databaseName == undefined) { - result.error('sqlite_error', "database_closed" + "" + dataBaseId, null); return null; } - let rdbStore = SqfLiteHelper.getOnlyLocal(databaseName as string); - if (rdbStore != null) { - return rdbStore; - } else { - result.error('sqlite_error', "database_closed" + "" + dataBaseId, null); - return null; - } - Log.i(TAG, '没有加载这个数据库', '%{public{}s'); - return null; + + return SqfLiteHelper.getOnlyLocal(databaseName as string); } /// 关闭一个数据库 @@ -257,7 +249,7 @@ export class SqfLiteHelper { } } - Log.i(TAG, '~~custom db Dir: ' + customDir + ' system version:' + deviceInfo.osFullName, ''); + Log.i(TAG, '~~custom db Dir: ' + customDir + ', system version:' + deviceInfo.osFullName, ''); // 从系统中获取 let config: relationalStore.StoreConfig; @@ -306,7 +298,6 @@ export class SqfLiteHelper { return result; } } - } public static async insert(call: MethodCall, result: MethodResult, dataBase: relationalStore.RdbStore): Promise { @@ -634,21 +625,21 @@ export class SqfLiteHelper { } /// Options操作,设置LogLevel,仅限开发,已被废弃 - public static async options(call: MethodCall, result: MethodResult): Promise { + public static options(call: MethodCall, result: MethodResult): void { let logLevel: number | undefined = call.argument(Constant.PARAM_LOG_LEVEL); Log.i(TAG, "handle options:" + logLevel); result.success(null); } /// 设置debug模式,查看SQL查询,已弃用 - public static async debugMode(call: MethodCall, result: MethodResult): Promise { + public static debugMode(call: MethodCall, result: MethodResult): void { let on: boolean = call.args; Log.i(TAG, "debugMode:" + on); result.success(null); } /// 获取设备版本信息 - public static async getPlatformVersion(call: MethodCall, result: MethodResult): Promise { + public static getPlatformVersion(call: MethodCall, result: MethodResult): void { result.success('ohos' + deviceInfo.osFullName); }