Commit b1c029f6 by ning

新增数据库操作

parent 147aa704
...@@ -151,7 +151,7 @@ export default abstract class BaseTable<T> implements ITable { ...@@ -151,7 +151,7 @@ export default abstract class BaseTable<T> implements ITable {
if (resultSet.goToFirstRow()) { if (resultSet.goToFirstRow()) {
do { do {
Logger.info(this, 'queryAll rowIndex=' + resultSet.rowIndex) Logger.info(this, 'queryAll rowIndex=' + resultSet.rowIndex)
items.push(this.createItem(resultSet)) items.push(this.toBean(resultSet))
} while (resultSet.goToNextRow()) } while (resultSet.goToNextRow())
} }
Logger.e(this, 'queryAll items=' + JSON.stringify(items)) Logger.e(this, 'queryAll items=' + JSON.stringify(items))
...@@ -178,5 +178,8 @@ export default abstract class BaseTable<T> implements ITable { ...@@ -178,5 +178,8 @@ export default abstract class BaseTable<T> implements ITable {
*/ */
abstract getEntityId(item: T): ValueType; abstract getEntityId(item: T): ValueType;
abstract createItem(cursor: rdb.ResultSet): T; /**
* 结果集转对象
*/
abstract toBean(cursor: rdb.ResultSet): T;
} }
\ No newline at end of file
import { Globals } from '../utils/Globals';
import BaseTable from './BaseTable';
import IDatabase from './IDatabase';
export class DatabaseImpl implements IDatabase {
private readonly dbName
private readonly key
constructor(dbName: string) {
this.dbName = dbName
this.key = "jp_wms_table_map_" + this.dbName
}
getTable<T extends BaseTable<any>>(tableClass: new (dbName: any, tableName: any) => T): T {
return Globals.getOrCreate(this.dbName + '_' + tableClass.name, () => {
let tableName = Reflect.getMetadata('TableName', tableClass)
if (!tableName) {
throw new Error('table name is empty')
}
return new tableClass(this.dbName, tableName);
});
}
}
\ No newline at end of file
import relationalStore from '@ohos.data.relationalStore';
import { Bggl } from '../../entity/Bggl';
import BaseTable, { ValueType } from '../BaseTable';
import { Table } from '../decorator/Decorators';
/**
* 包裹管理的数据层
*/
@Table({ db: 'db_wms_app', name: 'TAB_BZGL_KNZY_APP_BGGL' })
export class BgglDao extends BaseTable<Bggl> {
toBean(cursor: relationalStore.ResultSet): Bggl {
throw new Error('Method not implemented.');
}
getEntityId(item: any): ValueType {
throw new Error('Method not implemented.');
}
getColumnId(): string {
throw new Error('Method not implemented.');
}
getTableColumns(): string[] {
throw new Error('Method not implemented.');
}
getCreateTableSql(): string {
const bggl_sql = `create table if not exists TAB_BZGL_KNZY_APP_BGGL(
"GUID" TEXT NOT NULL PRIMARY KEY,
"DDH" TEXT,
"XS" INTEGER,
"PC" INTEGER,
"FWDW" TEXT,
"BGZT" TEXT,
"SLR" TEXT,
"SLDWDM" TEXT,
"SJH" INTEGER,
"JSSJ" TEXT,
"CKSJ" TEXT,
"SFDC" TEXT,
"SFDF" TEXT,
"KFDM" TEXT,
"HWH" TEXT,
"QJM" TEXT,
"DFJE" INTEGER
)`;
return bggl_sql;
}
}
\ No newline at end of file
import relationalStore from '@ohos.data.relationalStore';
import { DwInfo } from '../../entity/DwInfo';
import BaseTable, { ValueType } from '../BaseTable';
import { Table } from '../decorator/Decorators';
/**
* 包裹管理的数据层
*/
@Table({ db: 'db_wms_app', name: 'TAB_BZGL_KNZY_APP_BGDW' })
export class DwInfoDao extends BaseTable<DwInfo> {
getEntityId(item: DwInfo): ValueType {
return item.guid;
}
toBean(cursor: relationalStore.ResultSet): DwInfo {
throw new Error('Method not implemented.');
}
getColumnId(): string {
return 'GUID';
}
getTableColumns(): string[] {
return ['GUID', 'DWDM', 'DWMC', 'DWDH', 'DWXZ', 'JGBDH', 'JGBDH', 'JGBFH', 'JGBJC'];
}
getCreateTableSql(): string {
const dw_sql =
`create table if not exists TAB_BZGL_KNZY_APP_BGDW(
"GUID" TEXT NOT NULL PRIMARY KEY,
"DWDM" TEXT,
"DWMC" TEXT,
"DWDH" TEXT,
"DWJC" TEXT,
"DWXZ" TEXT,
"JGBDH" TEXT,
"JGBFH" TEXT,
"JGBJC" TEXT
)`;
return dw_sql;
}
}
\ No newline at end of file
/**
* 管理单位的数据层
*/
import relationalStore from '@ohos.data.relationalStore'; import relationalStore from '@ohos.data.relationalStore';
import { Gldw } from '../../entity/Gldw'; import { Gldw } from '../../entity/Gldw';
import BaseTable, { ValueType } from '../BaseTable'; import BaseTable, { ValueType } from '../BaseTable';
import { Table } from '../decorator/Decorators'; import { Table } from '../decorator/Decorators';
/**
* 管理单位的数据层
*/
@Table({ db: 'db_wms_app', name: 'TAB_BZGL_KNZY_APP_BGDDW' }) @Table({ db: 'db_wms_app', name: 'TAB_BZGL_KNZY_APP_BGDDW' })
export class GldwDao extends BaseTable<Gldw> { export class GldwDao extends BaseTable<Gldw> {
createItem(cursor: relationalStore.ResultSet): Gldw {
throw new Error('Method not implemented.'); toBean(cursor: relationalStore.ResultSet): Gldw {
let info: Gldw = {
guid: cursor.getString(cursor.getColumnIndex('GUID')),
dwfh: cursor.getString(cursor.getColumnIndex('DWFH')),
dwdm: cursor.getString(cursor.getColumnIndex('DWDM')),
dwxz: cursor.getString(cursor.getColumnIndex('DWXZ'))
}
return info;
} }
getEntityId(item: Gldw): ValueType { getEntityId(item: Gldw): ValueType {
throw new Error('Method not implemented.'); return item.guid;
} }
getColumnId(): string { getColumnId(): string {
throw new Error('Method not implemented.'); return "guid";
} }
/**
* GUID
* DWFH 单位代码
* DWDM 单位名称
* DWXZ 单位性质
*/
getTableColumns(): string[] { getTableColumns(): string[] {
throw new Error('Method not implemented.'); return ['GUID', 'DWFH', 'DWDM', 'DWXZ'];
} }
getCreateTableSql(): string { getCreateTableSql(): string {
throw new Error('Method not implemented.'); const gldw_sql =
`create table if not exists TAB_BZGL_KNZY_APP_BGDDW(
"GUID" TEXT NOT NULL PRIMARY KEY,
"DWFH" TEXT,
"DWDM" TEXT,
"DWXZ" TEXT
)`;
return gldw_sql;
} }
} }
\ No newline at end of file
/**
* 管理单位信息
*/
export interface Bggl {
guid?: string,
dwfh: string,
dwdm: string,
dwxz: string
}
\ No newline at end of file
/**
* 管理单位信息
*/
export interface DwInfo {
guid?: string,
dwdm: string,
dwmc: string,
dwdh: string,
dwjc: string,
dwxz: string,
jgbdh: string,
jgbfh: string,
jgbgc: string
}
\ No newline at end of file
import HashMap from '@ohos.util.HashMap';
import { Logger } from './Logger';
export class Globals {
/**
* 根据字符串类型的key获取全局保存的对象,如果不存在则新创建一个
* @param key 对象的键
* @param buildFunc 对象创建的函数
*/
static get<T>(key: string, buildFunc: () => T): T {
let value = globalThis[key]
if (!value) {
value = buildFunc()
globalThis[key] = value
}
return value;
}
/**
* 根据key获取全局保存的对象,如果不存在则新创建一个
* @param key 保存对象的key
* @param buildFunc 对象创建函数
*/
static getOrCreate<T>(key: any, buildFunc: () => T): T {
let globalMap = this.getGlobalMap()
let value = globalMap.get(key)
Logger.info(this, 'getOrCreate key=' + key + ' value=' + value)
if (!value) {
value = buildFunc()
globalMap.set(key, value)
}
return value
}
/**
* 用于保存全局对象的map
* 可以保存any类型的key和value
* 默认全职的键:jp_global_map
*/
static getGlobalMap(): HashMap<any, any> {
return Globals.get<HashMap<any, any>>('jp_global_map', () => {
return new HashMap();
})
}
/**
* 根据key移除全局对象
* @param key 保存对象的key
*/
static remove<T>(key: any): T {
return this.getGlobalMap().remove(key);
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论