179 lines
5.0 KiB
Plaintext
179 lines
5.0 KiB
Plaintext
import Cursor from 'android.database.Cursor';
|
|
import SQLiteDatabase from 'android.database.sqlite.SQLiteDatabase';
|
|
import SQLiteOpenHelper from 'android.database.sqlite.SQLiteOpenHelper';
|
|
|
|
import { createSQLiteContextOptions, executeSqlOptions, selectSqlOptions, executeSqlOptionsResult, selectSqlOptionsResult, transactionOptions,transactionResult } from '../interface.uts';
|
|
|
|
class SQLiteContext extends SQLiteOpenHelper {
|
|
private databaseName: string | null;
|
|
|
|
constructor(name: string) {
|
|
super(UTSAndroid.getAppContext()!, name, null, 1);
|
|
this.databaseName = name;
|
|
}
|
|
|
|
public executeSql(options: executeSqlOptions): void {
|
|
const database: SQLiteDatabase = this.getReadableDatabase();
|
|
const SqlArray = options.sql.split(';');
|
|
let result: executeSqlOptionsResult = {
|
|
data: [] as boolean[],
|
|
errMsg: 'executeSql:ok',
|
|
errCode: 0,
|
|
errSubject: '',
|
|
cause: null
|
|
}
|
|
try {
|
|
for (let i = 0; i < SqlArray.length; i++) {
|
|
if (SqlArray[i].length > 0) {
|
|
const sql = SqlArray[i].replace(/^\s+/, '');
|
|
try {
|
|
database.execSQL(sql);
|
|
result.data.push(true);
|
|
} catch (err) {
|
|
console.error('database.execSQL 出错:', err, 'SQL:', sql);
|
|
result.data.push(false);
|
|
// 立即调用 fail 并返回
|
|
result.errMsg = 'executeSql:fail';
|
|
result.errCode = 1000002;
|
|
result.cause = err;
|
|
options.fail?.(result);
|
|
options.complete?.(result);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
options.success?.(result);
|
|
} catch (e) {
|
|
console.error('executeSql 外层出错:', e);
|
|
const data = result.data;
|
|
result = {
|
|
errCode: 1000002,
|
|
errMsg: 'executeSql:fail',
|
|
errSubject: '',
|
|
cause: e,
|
|
data: data
|
|
};
|
|
options.fail?.(result);
|
|
}
|
|
options.complete?.(result);
|
|
}
|
|
|
|
public selectSql(options: selectSqlOptions): void {
|
|
const database: SQLiteDatabase = this.getReadableDatabase();
|
|
const SqlArray = options.sql.split(';');
|
|
let result: selectSqlOptionsResult = {
|
|
data: [] as string[][],
|
|
errMsg: 'selectSql:ok',
|
|
errCode: 0,
|
|
errSubject: '',
|
|
cause: null
|
|
}
|
|
try {
|
|
for (let i = 0; i < SqlArray.length; i++) {
|
|
if (SqlArray[i].length > 0) {
|
|
const sql = SqlArray[i].replace(/^\s+/, '');
|
|
try {
|
|
const cursor: Cursor = database.rawQuery(sql, null);
|
|
//获取查询结果的字符串并push到result.data中
|
|
if (cursor.moveToFirst()) {
|
|
do {
|
|
const row = cursor.getColumnCount();
|
|
const rowArray = [] as string[];
|
|
for (let j = 0; j < row; j++) {
|
|
rowArray.push(cursor.getString(j.toInt()));
|
|
}
|
|
result.data.push(rowArray);
|
|
} while (cursor.moveToNext());
|
|
}
|
|
cursor.close();
|
|
} catch {
|
|
result.data.push([] as string[]);
|
|
}
|
|
}
|
|
}
|
|
options.success?.(result);
|
|
} catch (e) {
|
|
const data = result.data;
|
|
result = {
|
|
errCode: 1000003,
|
|
errMsg: 'selectSql:fail',
|
|
errSubject: '',
|
|
cause: e,
|
|
data: data
|
|
};
|
|
options.fail?.(result);
|
|
}
|
|
options.complete?.(result);
|
|
}
|
|
|
|
public transaction(options: transactionOptions): void {
|
|
const database: SQLiteDatabase = this.getReadableDatabase();
|
|
const transaction = options.transaction;
|
|
let result: transactionResult = {
|
|
errMsg: 'transaction:ok',
|
|
errCode: 0,
|
|
errSubject: '',
|
|
cause: null
|
|
};
|
|
try {
|
|
if (transaction == 'begin') {
|
|
database.execSQL('BEGIN TRANSACTION');
|
|
} else if (transaction == 'commit') {
|
|
database.execSQL('COMMIT');
|
|
} else if (transaction == 'rollback') {
|
|
database.execSQL('ROLLBACK');
|
|
}
|
|
options.success?.(result);
|
|
} catch (e) {
|
|
let errCode = 1000008;
|
|
if (transaction == 'begin') {
|
|
errCode = 1000004;
|
|
} else if (transaction == 'commit') {
|
|
errCode = 1000005;
|
|
} else if (transaction == 'rollback') {
|
|
errCode = 1000006;
|
|
}
|
|
result = {
|
|
errCode: errCode,
|
|
errMsg: 'transaction:fail',
|
|
errSubject: '',
|
|
cause: e
|
|
};
|
|
options.fail?.(result);
|
|
}
|
|
options.complete?.(result);
|
|
}
|
|
|
|
public override onCreate(db: SQLiteDatabase): void {
|
|
// 可选:初始化表结构
|
|
}
|
|
|
|
public override onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int): void {
|
|
// 可选:数据库升级逻辑
|
|
}
|
|
}
|
|
|
|
export const createSQLiteContext = function (options: createSQLiteContextOptions) {
|
|
const name = options.name + '.db';
|
|
return new SQLiteContext(name); // 必须返回对象
|
|
}
|
|
|
|
|
|
|
|
export type executeSqlOptionsResultType = {
|
|
errCode: number;
|
|
errSubject?: string;
|
|
cause?: any;
|
|
errMsg: string;
|
|
date?: boolean[];
|
|
};
|
|
|
|
export type selectSqlOptionsResultType = {
|
|
errCode: number;
|
|
errSubject?: string;
|
|
cause?: any;
|
|
errMsg: string;
|
|
date?: string[];
|
|
};
|
|
|