diff --git a/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/Database.ets b/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/Database.ets index 475f49c94cbb7cc0f9304a8d721c24c9c4faa6ff..3943bb3836fb89e3fd9cafc9ba16bbb5e46d74d2 100644 --- a/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/Database.ets +++ b/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/Database.ets @@ -22,9 +22,12 @@ import { Constant } from './Constant'; import { BusinessError } from '@ohos.base'; import { MethodResult } from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel'; import MethodCall from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodCall'; +import Log from '@ohos/flutter_ohos/src/main/ets/util/Log'; import { MethodCallOperation } from './operation/MethodCallOperation'; import { BatchOperation } from './operation/BatchOperation'; +type HandleCallback = (x: Object) => void; + export class Database { public context: common.Context; public path: string; @@ -56,19 +59,24 @@ export class Database { operation.error(Constant.SQLITE_ERROR, message + '' + code, ''); } - private static async executeOrError(operation: Operation, db: relationalStore.RdbStore): Promise { + private static async executeOrError(operation: Operation, db: relationalStore.RdbStore, callback?: HandleCallback): Promise { let command: SqlCommand = operation.getSqlCommand(); - let operationInTransaction: Boolean = operation.getInTransactionChange(); + let operationInTransaction: boolean = operation.getInTransactionChange(); let sqlArguments: ValueType[] | undefined = operation.getArgument(Constant.PARAM_SQL_ARGUMENTS); let sql: string = command.getSql(); + let promise: ValueType; try{ if(sqlArguments == undefined || sqlArguments.length < 1) { - let promise = await db.executeSql(sql); + promise = await db.execute(sql) as ValueType; } else { - let promise = await db.executeSql(sql, sqlArguments); + promise = await db.execute(sql, sqlArguments) as ValueType; } + Log.i("Tag", "返回来的值:" + promise); Database.enterOrLeaveInTransaction(operationInTransaction); + if (callback != undefined) { + callback(promise) + } return true; } catch(err) { Database.handleException(operation, err); @@ -80,26 +88,37 @@ export class Database { public static async execute(operation: Operation, db: relationalStore.RdbStore): Promise { let command: SqlCommand = operation.getSqlCommand(); let sql: string = command.getSql(); + let executeSuccess: boolean = false; + let inTransactionChange: boolean = operation.getInTransactionChange(); + let enteringTransaction: boolean = (inTransactionChange == true && operation.hasNullTransactionId()); + if(enteringTransaction) { + Database.currentTransactionId = ++ (Database.lastTransactionId); + } + if(sql == 'BEGIN IMMEDIATE') { db.beginTransaction(); - operation.success(null); + Database.enterOrLeaveInTransaction(inTransactionChange); + executeSuccess = true; + Database.handleExecute(executeSuccess, enteringTransaction, operation, inTransactionChange); return; } else if(sql == 'COMMIT') { db.commit(); - operation.success(null); + Database.enterOrLeaveInTransaction(inTransactionChange); + executeSuccess = true; + Database.handleExecute(executeSuccess, enteringTransaction, operation, inTransactionChange); return; } else if (sql == 'BEGIN EXCLUSIVE') { - operation.success(null); + Database.enterOrLeaveInTransaction(inTransactionChange); + executeSuccess = true; + Database.handleExecute(executeSuccess, enteringTransaction, operation, inTransactionChange); return; } - let inTransactionChange: Boolean = operation.getInTransactionChange(); - let enteringTransaction: boolean = (inTransactionChange == true && operation.hasNullTransactionId()); - if(enteringTransaction) { - Database.currentTransactionId = ++ (Database.lastTransactionId); - } + executeSuccess = await Database.executeOrError(operation, db); + Database.handleExecute(executeSuccess, enteringTransaction, operation, inTransactionChange); + } - let executeSuccess: boolean = await Database.executeOrError(operation, db); + private static handleExecute(executeSuccess: boolean, enteringTransaction: boolean, operation: Operation, inTransactionChange: Boolean) { if(!executeSuccess) { // Revert if needed if(enteringTransaction) { @@ -137,7 +156,9 @@ export class Database { } private static async doInsert(operation: Operation, db: relationalStore.RdbStore): Promise { - let executeSuccess: boolean = await Database.executeOrError(operation, db); + let executeSuccess: boolean = await Database.executeOrError(operation, db, (rowId) => { + operation.success(rowId); + }); if(!executeSuccess) { return false; } @@ -147,7 +168,6 @@ export class Database { operation.success(null); return true; } - operation.success(1); return true; } @@ -157,7 +177,9 @@ export class Database { } private static async doUpdate(operation: Operation, db: relationalStore.RdbStore): Promise { - let executeSuccess: boolean = await Database.executeOrError(operation, db); + let executeSuccess: boolean = await Database.executeOrError(operation, db, (rows) => { + operation.success(rows); + }); if(!executeSuccess) { return false; } @@ -167,7 +189,6 @@ export class Database { operation.success(null); return true; } - operation.success(1); return true; } diff --git a/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/operation/BaseReadOperation.ets b/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/operation/BaseReadOperation.ets index ac82656750a82bb5ba50af03c6ac37de2bdf2312..aaa44e7e58f35c6fb65184b4de23e268f2b3b773 100644 --- a/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/operation/BaseReadOperation.ets +++ b/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/operation/BaseReadOperation.ets @@ -40,7 +40,7 @@ export abstract class BaseReadOperation implements Operation { return new SqlCommand(this.getSql(), this.getSqlArguments()); } - public getInTransactionChange(): Boolean { + public getInTransactionChange(): boolean { return this.getBoolean(Constant.PARAM_IN_TRANSACTION_CHANGE); } @@ -60,9 +60,9 @@ export abstract class BaseReadOperation implements Operation { return false; } - private getBoolean(key: string): Boolean { + private getBoolean(key: string): boolean { let value: object = this.getArgument(key); - if(value instanceof Boolean) { + if(typeof value == 'boolean') { return value; } return false; diff --git a/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/operation/Operation.ets b/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/operation/Operation.ets index b6b01f47349a38d4a38741c969dea8a2bfc28126..a64b8158852fc201c8ec01071725c3ebe734c63d 100644 --- a/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/operation/Operation.ets +++ b/sqflite/ohos/sqflite/src/main/ets/io/flutter/plugins/sqflite/operation/Operation.ets @@ -31,7 +31,7 @@ export interface Operation extends OperationResult { getContinueOnError(): boolean; // Only for execute command, true when entering a transaction, false when exiting - getInTransactionChange(): Boolean; + getInTransactionChange(): boolean; /** * transaction id if any, only for within a transaction