Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ TuneDbParamPlugin#setJournalSizeLimit

> 推荐开启自动升级,之后只需要修改版本号即可,无需手写 Migration

> 自动升级默认不会生成删除列(drop column)的 sql,如需在自动升级时删除实体中已移除的列,配置 autoMigrateDropColumn: true 开启

若仍想手动升级,可以通过配置 migration 进行升级

```
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{
"name": "default",
"signingConfig": "default",
"targetSdkVersion": "6.1.1(24)",
"compatibleSdkVersion": "5.0.1(13)",
"runtimeOS": "HarmonyOS",
"buildOption": {
Expand Down
5 changes: 4 additions & 1 deletion rdbstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ TuneDbParamPlugin#setJournalSizeLimit

> 推荐开启自动升级,之后只需要修改版本号即可,无需手写 Migration

> 自动升级默认不会生成删除列(drop column)的 sql,如需在自动升级时删除实体中已移除的列,配置 autoMigrateDropColumn: true 开启

通过配置 migration 进行升级

```
Expand All @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions rdbstore/src/main/ets/dao/RdbDao.ets
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,20 @@ export class RdbDao<Entity extends object> {
return indexes
}

/**
* 根据谓词查询匹配的数据行数
*/
public async count(predicates: relationalStore.RdbPredicates): Promise<number> {
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
}

/**
* 查询数据库中列数量
*/
Expand Down
4 changes: 4 additions & 0 deletions rdbstore/src/main/ets/database/RdbConfig.ets
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export interface RdbConfig {
* @returns
*/
autoMigrate: boolean
/**
* 自动升级时是否允许生成删除列(drop column)的 sql,默认关闭;开启后,实体中已移除的列会自动生成 DROP COLUMN 语句
*/
autoMigrateDropColumn?: boolean
/**
* 从某个版本开始废弃低版本数据库,重新创建
* 比如配置:5,会删除数据库版本为 1 2 3 4 这些版本数据库
Expand Down
3 changes: 2 additions & 1 deletion rdbstore/src/main/ets/database/RdbOpenHelper.ets
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 10 additions & 4 deletions rdbstore/src/main/ets/database/auto/DiffUtil.ets
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiffResult<Object>> = new Array
const newTables = Array.from(DbStructInfo.getInstance().tableMap.values())
const newTableName = new Set(newTables.map((item) => item.tableName))
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -132,15 +134,19 @@ export namespace DiffUtil {

/**
* 填充所有涉及列相关自动升级
* @param allowDropColumn 是否允许生成删除列(drop column)的 sql,默认关闭
*/
function fillColumnMigration(migration: Migration, tableName: string, result: Array<DiffResult<Property>>) {
function fillColumnMigration(migration: Migration, tableName: string, result: Array<DiffResult<Property>>,
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;
}
})
Expand Down
9 changes: 9 additions & 0 deletions rdbstore/src/main/ets/query/Query.ets
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export class Query<Entity extends object> {
return this.dao.queryPredicate(this.predicates, this._predicateDesc)
}

/**
* 只获取匹配的数据行数,不获取内容
* @returns
*/
count(): Promise<number> {
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
Expand Down