From 915a115899f31be04b186e0b01bbf3e09be66d01 Mon Sep 17 00:00:00 2001 From: michael Date: Thu, 7 May 2026 09:35:19 +0800 Subject: [PATCH 1/2] select count --- rdbstore/src/main/ets/dao/RdbDao.ets | 14 ++++++++++++++ rdbstore/src/main/ets/query/Query.ets | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/rdbstore/src/main/ets/dao/RdbDao.ets b/rdbstore/src/main/ets/dao/RdbDao.ets index e345a7d..427f1b1 100644 --- a/rdbstore/src/main/ets/dao/RdbDao.ets +++ b/rdbstore/src/main/ets/dao/RdbDao.ets @@ -517,6 +517,20 @@ export class RdbDao { return indexes } + /** + * 根据谓词查询匹配的数据行数 + */ + public async count(predicates: relationalStore.RdbPredicates): Promise { + const db = await this.database.getRdbImpl() + const result = await db.query(predicates, ['count(*)'], 'count', this.config.tableName) + let count = 0 + if (result.goToNextRow()) { + count = result.getRow()['count(*)'] as number + } + result.close() + return count + } + /** * 查询数据库中列数量 */ diff --git a/rdbstore/src/main/ets/query/Query.ets b/rdbstore/src/main/ets/query/Query.ets index 2a7a6a8..2cef2b3 100644 --- a/rdbstore/src/main/ets/query/Query.ets +++ b/rdbstore/src/main/ets/query/Query.ets @@ -38,6 +38,15 @@ export class Query { return this.dao.queryPredicate(this.predicates, this._predicateDesc) } + /** + * 只获取匹配的数据行数,不获取内容 + * @returns + */ + count(): Promise { + this.dao.database.extension.log?.d(this.tag, `query count: table: ${this.dao.config.tableName} do query predicates: ${this._predicateDesc}`) + return this.dao.count(this.predicates) + } + /** * 谓词描述 * @returns From b18440a8c4d611b9924fb53d5d2edc231662f736 Mon Sep 17 00:00:00 2001 From: michael1995 Date: Thu, 20 Aug 2026 10:53:06 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=B8=8D=E4=BC=9A=E7=94=9F=E6=88=90=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- build-profile.json5 | 1 + rdbstore/README.md | 5 ++++- rdbstore/src/main/ets/database/RdbConfig.ets | 4 ++++ rdbstore/src/main/ets/database/RdbOpenHelper.ets | 3 ++- rdbstore/src/main/ets/database/auto/DiffUtil.ets | 14 ++++++++++---- 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6d40a36..9ebdfb0 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,8 @@ TuneDbParamPlugin#setJournalSizeLimit > 推荐开启自动升级,之后只需要修改版本号即可,无需手写 Migration +> 自动升级默认不会生成删除列(drop column)的 sql,如需在自动升级时删除实体中已移除的列,配置 autoMigrateDropColumn: true 开启 + 若仍想手动升级,可以通过配置 migration 进行升级 ``` @@ -212,7 +214,8 @@ entities: [SampleEntity], migrations: [migration1_2, migration2_4], encrypt: false, securityLevel: relationalStore.SecurityLevel.S1, -autoMigrate: false +autoMigrate: false, +autoMigrateDropColumn: false }).setLogger(new DBLog()) .setEvent(new DBEventSender()) .build() diff --git a/build-profile.json5 b/build-profile.json5 index e410e50..5f532f5 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -5,6 +5,7 @@ { "name": "default", "signingConfig": "default", + "targetSdkVersion": "6.1.1(24)", "compatibleSdkVersion": "5.0.1(13)", "runtimeOS": "HarmonyOS", "buildOption": { diff --git a/rdbstore/README.md b/rdbstore/README.md index 184d4c1..596e9bd 100644 --- a/rdbstore/README.md +++ b/rdbstore/README.md @@ -202,6 +202,8 @@ TuneDbParamPlugin#setJournalSizeLimit > 推荐开启自动升级,之后只需要修改版本号即可,无需手写 Migration +> 自动升级默认不会生成删除列(drop column)的 sql,如需在自动升级时删除实体中已移除的列,配置 autoMigrateDropColumn: true 开启 + 通过配置 migration 进行升级 ``` @@ -215,7 +217,8 @@ entities: [SampleEntity], migrations: [migration1_2, migration2_4], encrypt: false, securityLevel: relationalStore.SecurityLevel.S1, -autoMigrate: false +autoMigrate: false, +autoMigrateDropColumn: false }).setLogger(new DBLog()) .setEvent(new DBEventSender()) .build() diff --git a/rdbstore/src/main/ets/database/RdbConfig.ets b/rdbstore/src/main/ets/database/RdbConfig.ets index cfce7cd..f21042e 100644 --- a/rdbstore/src/main/ets/database/RdbConfig.ets +++ b/rdbstore/src/main/ets/database/RdbConfig.ets @@ -51,6 +51,10 @@ export interface RdbConfig { * @returns */ autoMigrate: boolean + /** + * 自动升级时是否允许生成删除列(drop column)的 sql,默认关闭;开启后,实体中已移除的列会自动生成 DROP COLUMN 语句 + */ + autoMigrateDropColumn?: boolean /** * 从某个版本开始废弃低版本数据库,重新创建 * 比如配置:5,会删除数据库版本为 1 2 3 4 这些版本数据库 diff --git a/rdbstore/src/main/ets/database/RdbOpenHelper.ets b/rdbstore/src/main/ets/database/RdbOpenHelper.ets index 3d556fb..25b7a75 100644 --- a/rdbstore/src/main/ets/database/RdbOpenHelper.ets +++ b/rdbstore/src/main/ets/database/RdbOpenHelper.ets @@ -64,7 +64,8 @@ export class RdbOpenHelper { const autoMigration = new Migration(rdbStore.version, this.database.config.version) // 计算且填充自动升级 migration - await DiffUtil.fillAutoUpgradeMigration(autoMigration, rdbStore) + await DiffUtil.fillAutoUpgradeMigration(autoMigration, rdbStore, + this.database.config.autoMigrateDropColumn ?? false) this.database.config.migrations.push(autoMigration) } diff --git a/rdbstore/src/main/ets/database/auto/DiffUtil.ets b/rdbstore/src/main/ets/database/auto/DiffUtil.ets index a1df9e7..fcdd27d 100644 --- a/rdbstore/src/main/ets/database/auto/DiffUtil.ets +++ b/rdbstore/src/main/ets/database/auto/DiffUtil.ets @@ -38,8 +38,10 @@ export namespace DiffUtil { * 列增加、列删除 * @param migration * @param database + * @param allowDropColumn 是否允许生成删除列(drop column)的 sql,默认关闭 */ - export async function fillAutoUpgradeMigration(migration: Migration, database: relationalStore.RdbStore) { + export async function fillAutoUpgradeMigration(migration: Migration, database: relationalStore.RdbStore, + allowDropColumn: boolean = false) { const diffResult: Array> = new Array const newTables = Array.from(DbStructInfo.getInstance().tableMap.values()) const newTableName = new Set(newTables.map((item) => item.tableName)) @@ -70,7 +72,7 @@ export namespace DiffUtil { const propDiff = calculateDiff(DiffType.COLUMN, props, DbStructInfo.getInstance().getPropsByName(table.tableName)) diffResult.push(...propDiff) - fillColumnMigration(migration, table.tableName, propDiff) + fillColumnMigration(migration, table.tableName, propDiff, allowDropColumn) } } @@ -132,15 +134,19 @@ export namespace DiffUtil { /** * 填充所有涉及列相关自动升级 + * @param allowDropColumn 是否允许生成删除列(drop column)的 sql,默认关闭 */ - function fillColumnMigration(migration: Migration, tableName: string, result: Array>) { + function fillColumnMigration(migration: Migration, tableName: string, result: Array>, + allowDropColumn: boolean = false) { result.forEach(item => { switch (item.op) { case DiffOps.ADD: migration.addColumn(tableName, item.data.columnName, item.data.type) break; case DiffOps.DELETE: - migration.deleteColumn(tableName, item.data.columnName) + if (allowDropColumn) { + migration.deleteColumn(tableName, item.data.columnName) + } break; } })