index.uts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import Cursor from 'android.database.Cursor';
  2. import SQLiteDatabase from 'android.database.sqlite.SQLiteDatabase';
  3. import SQLiteOpenHelper from 'android.database.sqlite.SQLiteOpenHelper';
  4. import Environment from 'android.os.Environment';
  5. import { createSQLiteContextOptions, executeSqlOptions, selectSqlOptions, executeSqlOptionsResult, selectSqlOptionsResult, CreateSQLiteContext, transactionOptions, transactionResult, ICreateSQLiteContextError } from '../interface.uts';
  6. //import { createSQLiteContextOptions, executeSqlOptions, selectSqlOptions, transactionOptions } from '../interface.uts';
  7. import { createSQLiteContextFailImpl } from '../unierror.uts';
  8. class SQLiteContext extends SQLiteOpenHelper {
  9. private dbName: string | null;
  10. constructor(name: string) {
  11. console.log(UTSAndroid.getResourcePath("/static/db/"+name))
  12. super(UTSAndroid.getAppContext(), UTSAndroid.getResourcePath("/static/db/"+name), null, 1);
  13. this.dbName = name;
  14. }
  15. override onCreate(db: SQLiteDatabase):void {
  16. }
  17. override onUpgrade(db: SQLiteDatabase, oldVersion:Int, newVersion:Int):void {
  18. }
  19. public executeSql(options: executeSqlOptions) {
  20. const database: SQLiteDatabase = this.getWritableDatabase();
  21. const SqlArray = options.sql.split(';');
  22. let result: executeSqlOptionsResult = {
  23. data: [] as boolean[],
  24. errMsg: 'ok'
  25. }
  26. try {
  27. for (let i = 0; i < SqlArray.length; i++) {
  28. if (SqlArray[i].length > 0) {
  29. const sql = SqlArray[i].replace(/^\s+/, '');
  30. try {
  31. database.execSQL(sql);
  32. result.data.push(true);
  33. } catch(e:Error) {
  34. result.data.push(false);
  35. }
  36. }
  37. }
  38. options.success?.(result);
  39. } catch (e) {
  40. const err = new createSQLiteContextFailImpl(1000002);
  41. result.errMsg = err.errMsg;
  42. options.fail?.(result);
  43. }
  44. options.complete?.(result);
  45. return result;
  46. }
  47. public selectSql(options: selectSqlOptions) {
  48. const database: SQLiteDatabase = this.getReadableDatabase();
  49. const SqlArray = options.sql.split(';');
  50. let result: selectSqlOptionsResult = {
  51. data: [] as UTSJSONObject[],
  52. errMsg: 'ok',
  53. }
  54. try {
  55. for (let i = 0; i < SqlArray.length; i++) {
  56. if (SqlArray[i].length > 0) {
  57. const sql = SqlArray[i].replace(/^\s+/, '');
  58. try {
  59. const cursor: Cursor = database.rawQuery(sql, null);
  60. //获取查询结果的字符串并push到result.data中
  61. if (cursor.moveToFirst()) {
  62. do {
  63. const row = cursor.getColumnCount();
  64. const utsJsonObject = {} as UTSJSONObject;
  65. for (let j:Int = 0; j < row; j++) {
  66. //rowArray.push(cursor.getString(j)!=null? cursor.getString(j):'');
  67. //let obj = {cursor.getColumnName(j):cursor.getString(j)!=null? cursor.getString(j):''}
  68. //rowArray.push(obj)
  69. utsJsonObject.set(cursor.getColumnName(j).toString(), cursor.getString(j)!=null? cursor.getString(j):'')
  70. }
  71. result.data.push(utsJsonObject);
  72. } while (cursor.moveToNext());
  73. }
  74. cursor.close();
  75. } catch(e:Error) {
  76. console.log(e)
  77. result.data.push({});
  78. }
  79. }
  80. }
  81. options.success?.(result);
  82. } catch (e) {
  83. const err = new createSQLiteContextFailImpl(1000003);
  84. result.errMsg = err.errMsg;
  85. options.fail?.(result);
  86. }
  87. options.complete?.(result);
  88. return result;
  89. }
  90. /*
  91. public close(){
  92. const database: SQLiteDatabase = this.getReadableDatabase();
  93. database.close();
  94. } */
  95. public transaction(options: transactionOptions) {
  96. const database: SQLiteDatabase = this.getReadableDatabase();
  97. const transaction = options.operation;
  98. let result: transactionResult = {
  99. errMsg: 'transaction:ok',
  100. }
  101. try {
  102. if (transaction == 'begin') {
  103. //开启事务
  104. database.execSQL('BEGIN TRANSACTION');
  105. } else if (transaction == 'commit') {
  106. //提交事务
  107. database.execSQL('COMMIT');
  108. } else if (transaction == 'rollback') {
  109. //回滚事务
  110. database.execSQL('ROLLBACK');
  111. }
  112. options.success?.(result);
  113. } catch (e) {
  114. let errCode = 1000008;
  115. if (transaction == 'begin') {
  116. errCode = 1000004;
  117. } else if (transaction == 'commit') {
  118. errCode = 1000005;
  119. } else if (transaction == 'rollback') {
  120. errCode = 1000006;
  121. }
  122. const err = new createSQLiteContextFailImpl(errCode);
  123. const errInfo = {errMsg:err.errMsg } as transactionResult ;
  124. options.fail?.(errInfo);
  125. }
  126. let ret = {
  127. errMsg: result.errMsg as string
  128. } as UTSJSONObject
  129. options.complete?.(result);
  130. return ret;
  131. }
  132. }
  133. export const createSQLiteContext = function (dbName: string): SQLiteContext {
  134. const name = dbName + '.db';
  135. return new SQLiteContext(name);
  136. }