index.uts 4.0 KB

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