Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
J
jump_hm_warehouse
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Members
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
毛勇泽
jump_hm_warehouse
Commits
743822e7
Commit
743822e7
authored
Jan 27, 2024
by
ning
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
数据迁移
parent
5b4940dd
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
117 行增加
和
0 行删除
+117
-0
Migration.ets
common/src/main/ets/dbflow/Migration.ets
+117
-0
没有找到文件。
common/src/main/ets/dbflow/Migration.ets
0 → 100644
View file @
743822e7
import rdb from '@ohos.data.relationalStore'
import { Logger } from '../utils/Logger'
/**
* 数据迁移类
*/
export class Migration {
/**
* 数据库名称
*/
private dbName: string = "";
/**
* 数据库表名
*/
private tableName: string = "";
/**
* 数据库版本(默认:1)
*/
private dbVersion: number = 1;
/**
* 对表进行修改的Alter语句(ALTER TABLE 语句用于在已有的表中添加、修改或删除列)
*/
private alters: string = "";
/**
* @param dbName 数据库名称
* @param tableName 表名称
* @param dbVersion 数据库版本
*/
constructor(dbName: string, tableName: string, dbVersion: number) {
this.tableName = tableName;
this.dbName = dbName;
this.dbVersion = dbVersion;
}
/**
* 新增列操作
*/
addColumn(columnName: string, columnType: string): any {
if (this.alters == "") {
this.alters = "alter table " + this.tableName + " add " + columnName + " " + columnType;
}
return this;
}
/**
* 修改列操作(需要修改的列名以及列数据类型名称,返回当前类实例)
*/
updateColumn(columnName: string, columnType: string): any {
if (this.alters == "") {
this.alters = "alter table " + this.tableName + " alter column " + columnName + " " + columnType;
}
return this;
}
/**
* 删除列操作(需要删除的列名,返回当前类实例)
*/
deleteColumn(columnName: string): any {
if (this.alters == "") {
this.alters = "alter table " + this.tableName + " drop column " + columnName;
}
return this;
}
/**
* 列操作
*/
Alter(alters: string): any {
if (this.alters != "") {
this.alters = ""
}
this.alters = alters;
return this;
}
/**
* 执行操作并升级数据库
* @param context
* @param encrypt
*/
public execute(context: any, encrypt: boolean): any {
let that = this;
rdb.getRdbStore(context, {
name: that.dbName,
securityLevel: rdb.SecurityLevel.S1,
encrypt: encrypt
}, (err, rdbStore) => {
if (err) {
Logger.info(this, '[数据迁移失败]|错误信息:' + err)
} else {
rdbStore.executeSql(that.alters, null, () => {
Logger.info(this, '数据迁移完成')
})
}
})
return this;
}
/**
* 异步执行操作并升级数据库
* @param context
* @param encrypt
*/
public async executeAsync(context: any, encrypt: boolean): Promise<any> {
let rdbStore: rdb.RdbStore = await rdb.getRdbStore(context, {
name: this.dbName,
securityLevel: rdb.SecurityLevel.S1,
encrypt: encrypt
})
await rdbStore.executeSql(this.alters, null);
Logger.info(this, '数据迁移完成')
return this;
}
}
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论