SQLiteDataProxy.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package com.baoshi.swms.sqlite;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.util.Log;
  6. import java.util.List;
  7. import java.util.concurrent.atomic.AtomicInteger;
  8. /**
  9. * SQL proxy
  10. *
  11. * @author Zhendong Zhou
  12. * @date 2021/12/27 13:40
  13. */
  14. public class SQLiteDataProxy implements ISQLiteOperate{
  15. private java.util.concurrent.Semaphore semaphoreTransaction = new java.util.concurrent.Semaphore(1);
  16. private ThreadLocal<Boolean> isQuery = new ThreadLocal<>();
  17. private AtomicInteger mOpenCounter = new AtomicInteger();
  18. private SQLiteDatabase db;
  19. private Cursor cursor;
  20. private SQLiteDataProxy() {
  21. }
  22. private static SQLiteDataProxy proxy;
  23. private static TaskTable helper;
  24. public static SQLiteDataProxy getSQLiteProxy(Context context) {
  25. helper = TaskTable.getInstance(context);
  26. if (proxy == null) {
  27. synchronized (SQLiteDataProxy.class) {
  28. if (proxy == null) {
  29. proxy = new SQLiteDataProxy();
  30. }
  31. }
  32. }
  33. return proxy;
  34. }
  35. public SQLiteDatabase getSQLiteDataBase() {
  36. if (mOpenCounter.incrementAndGet() == 1 || db == null) {
  37. db = helper.getWritableDatabase();
  38. }
  39. return db;
  40. }
  41. private void closeSQLiteDatabase(){
  42. if(mOpenCounter.decrementAndGet() == 0){
  43. db.close();
  44. }
  45. }
  46. @Override
  47. public boolean execSQL(String sql) {
  48. boolean result = true;
  49. db = getSQLiteDataBase();
  50. try {
  51. db.execSQL(sql);
  52. } catch (Exception e) {
  53. Log.e("SQLERROR", "In SQLDA:" + e.getMessage() + sql);
  54. result = false;
  55. } finally {
  56. closeSQLiteDatabase();
  57. }
  58. return result;
  59. }
  60. @Override
  61. public boolean execSQLList(List<String> sqlList) {
  62. boolean result = true;
  63. db = getSQLiteDataBase();
  64. String currentSqlString = "";
  65. try {
  66. semaphoreTransaction.acquire();
  67. db.beginTransaction();
  68. for (String sql : sqlList) {
  69. currentSqlString = sql;
  70. db.execSQL(sql);
  71. }
  72. db.setTransactionSuccessful();
  73. result = true;
  74. } catch (Exception e) {
  75. result = false;
  76. Log.e("SQLERROR", "IN SQLDA: " + e.getMessage() + currentSqlString);
  77. } finally {
  78. db.endTransaction();
  79. semaphoreTransaction.release();
  80. closeSQLiteDatabase();
  81. }
  82. return result;
  83. }
  84. @Override
  85. public boolean execSQLs(List<String[]> sqlList) {
  86. boolean result = true;
  87. db = getSQLiteDataBase();
  88. String currentSql = "";
  89. try {
  90. semaphoreTransaction.acquire();
  91. db.beginTransaction();
  92. for (String[] arr : sqlList) {
  93. currentSql = arr[0];
  94. Cursor curCount = db.rawQuery(arr[0], null);
  95. curCount.moveToFirst();
  96. int count = curCount.getInt(0);
  97. curCount.close();
  98. if (count == 0) {
  99. if (arr[1] != null && arr[1].length() > 0) {
  100. currentSql = arr[1];
  101. db.execSQL(arr[1]);
  102. }
  103. } else {
  104. if (arr.length > 2 && arr[2] != null && arr[2].length() > 0) {
  105. currentSql = arr[2];
  106. db.execSQL(arr[2]);
  107. }
  108. }
  109. }
  110. db.setTransactionSuccessful();
  111. result = true;
  112. } catch (Exception e) {
  113. Log.e("SQLERROR", "IN SQLDA: " + currentSql + e.getMessage());
  114. result = false;
  115. } finally {
  116. db.endTransaction();
  117. semaphoreTransaction.release();
  118. closeSQLiteDatabase();
  119. }
  120. return result;
  121. }
  122. @Override
  123. public boolean execSQLIgnoreError(List<String> sqlList) {
  124. db = getSQLiteDataBase();
  125. try {
  126. semaphoreTransaction.acquire();
  127. } catch (InterruptedException e) {
  128. e.printStackTrace();
  129. }
  130. db.beginTransaction();
  131. for (String sql : sqlList) {
  132. try {
  133. db.execSQL(sql);
  134. } catch (Exception e) {
  135. Log.e("SQLERROR", "IN SQLDA: " + sql + e.getMessage());
  136. }
  137. }
  138. db.setTransactionSuccessful();
  139. db.endTransaction();
  140. semaphoreTransaction.release();
  141. closeSQLiteDatabase();
  142. return true;
  143. }
  144. @Override
  145. public Cursor query(String sql) {
  146. return query(sql, null);
  147. }
  148. @Override
  149. public Cursor query(String sql, String[] params) {
  150. isQuery.set(true);//设置为true,表示正在查询</span>
  151. db = getSQLiteDataBase();
  152. cursor = db.rawQuery(sql, params);
  153. return cursor;
  154. }
  155. /*如果调用query方法,抛异常时要调用此方法</span>
  156. */
  157. @Override
  158. public void closeWhileError(){
  159. if (cursor != null) {
  160. cursor.close();
  161. }
  162. if(isQuery.get()){//没有执行完毕,异常后需要去关闭数据库
  163. closeSQLiteDataBase();
  164. }
  165. }
  166. @Override
  167. public void close() {
  168. if (cursor != null) {
  169. cursor.close();
  170. }
  171. closeSQLiteDatabase();
  172. }
  173. private void closeSQLiteDataBase(){
  174. if(mOpenCounter.decrementAndGet() == 0){
  175. db.close();
  176. isQuery.set(false);//设置为false,表示执行完毕</span>
  177. Log.i("DataBaseState","DB------Closed");
  178. }
  179. }
  180. }