SQL.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. class SQL{
  2. constructor(plus){
  3. // 数据库名称一个软件只需要一个库这里写死
  4. this.name = 'sqlite_mine_sql';
  5. this.plus = plus;
  6. // 存储路径
  7. this.path = `_doc/sqlite_mine.db`;
  8. }
  9. initDatabase(){
  10. return new Promise((resolve , reject) => {
  11. const that = this;
  12. let sql = `SELECT count(*) FROM sqlite_master WHERE type='table' AND name= database`;
  13. that.plus.sqlite.executeSql({
  14. name: that.name,
  15. sql,
  16. success(data){
  17. if(data === 0){
  18. that.plus.sqlite.executeSql({
  19. name: that.name,
  20. sql: `create table if not exists database("value" TEXT,"time" CHAR(100),"chat_id" CHAR(50))`,
  21. success(data){
  22. resolve(data);
  23. }
  24. });
  25. }else{
  26. resolve();
  27. }
  28. console.log('initDatabase success: '+JSON.stringify(data));
  29. },
  30. fail(e){
  31. that.plus.sqlite.executeSql({
  32. name: that.name,
  33. sql: `create table if not exists database("value" TEXT,"time" CHAR(100),"chat_id" CHAR(50))`,
  34. success(data){
  35. resolve(data);
  36. }
  37. });
  38. // reject();
  39. console.log('initDatabase failed: '+JSON.stringify(e));
  40. }
  41. });
  42. })
  43. }
  44. // 连接数据库
  45. openDB(){
  46. return new Promise((resolve , reject)=>{
  47. if(this.isOpen()){
  48. return resolve();
  49. }
  50. this.plus.sqlite.openDatabase({
  51. name: this.name,
  52. path: this.path,
  53. success(data){
  54. console.log(`openDB success`,data)
  55. resolve(data);
  56. },
  57. fail(e){
  58. console.log('openDB failed: '+JSON.stringify(e));
  59. reject(e);
  60. }
  61. });
  62. });
  63. }
  64. // 判断数据库是否打开
  65. isOpen(){
  66. return this.plus.sqlite.isOpenDatabase({
  67. name: this.name,
  68. path: this.path
  69. });
  70. }
  71. // 关闭数据库
  72. closeDB(){
  73. this.plus.sqlite.closeDatabase({
  74. name: this.name,
  75. success(e){
  76. console.log('closeDatabase success!');
  77. },
  78. fail(e){
  79. console.log('closeDatabase failed: '+JSON.stringify(e));
  80. }
  81. });
  82. }
  83. // 执行sql语句
  84. async handleSQL(handleType, options, isClose){
  85. await this.openDB();
  86. await this.initDatabase();
  87. switch(handleType){
  88. case 'insert':
  89. return this.insertSQL(options, isClose);
  90. case 'delete':
  91. return this.deleteSQL(options, isClose);
  92. case 'select':
  93. return this.selectSQL(options, isClose);
  94. default:
  95. throw new Error('没有这个操作!!!');
  96. }
  97. }
  98. // 插入sql语句
  99. insertSQL(options, isClose) {
  100. console.log('insert');
  101. const that = this;
  102. return new Promise((resolve , reject)=>{
  103. const sql = `insert into database values(${options.value},${options.time},${options.chat_id})`;
  104. that.plus.sqlite.executeSql({
  105. name: that.name,
  106. sql,
  107. success(data){
  108. console.log('insertSQL success: '+JSON.stringify(data));
  109. isClose && that.closeDB();
  110. resolve(data);
  111. },
  112. fail(e){
  113. console.log('insertSQL failed: '+JSON.stringify(e));
  114. isClose && that.closeDB();
  115. reject(e);
  116. }
  117. });
  118. });
  119. }
  120. // 删除sql语句
  121. deleteSQL(options, isClose){
  122. return new Promise((resolve , reject)=>{
  123. const that = that || this;
  124. const sql = options.time ? `delete from database where chat_id = ${options.chat_id}where time > ${options.time}`:`delete from database where chat_id = ${options.chat_id}`;
  125. that.plus.sqlite.executeSql({
  126. name: that.name,
  127. sql,
  128. success(data){
  129. console.log('deleteSQL success: '+JSON.stringify(data));
  130. isClose && that.closeDB();
  131. resolve(data);
  132. },
  133. fail(e){
  134. console.log('deleteSQL failed: '+JSON.stringify(e));
  135. isClose && that.closeDB();
  136. reject(e);
  137. }
  138. });
  139. });
  140. }
  141. // 查询sql语句
  142. selectSQL(options, isClose){
  143. return new Promise((resolve , reject)=>{
  144. const that = that || this;
  145. const sql = `select * from database where chat_id = ${options.chat_id}`;
  146. that.plus.sqlite.selectSql({
  147. name: that.name,
  148. sql,
  149. success(data){
  150. console.log('selectSQL success: '+JSON.stringify(data));
  151. if(options.hasOwnProperty('pageNum') && options.hasOwnProperty('pageSize')){
  152. let {pageNum,pageSize} = options;
  153. let total = data.length;
  154. let page = Math.ceil(total/pageSize);
  155. let req = {
  156. pageNum,
  157. pageSize,
  158. list:[],
  159. total
  160. };
  161. let startIndex = (pageNum - 1) * pageSize;
  162. if(pageNum<page){
  163. req.list = data.slice(startIndex,startIndex+pageSize);
  164. return resolve(req);
  165. }
  166. }else{
  167. resolve(data);
  168. }
  169. isClose && that.closeDB();
  170. },
  171. fail(e){
  172. console.log('selectSQL failed: '+JSON.stringify(e));
  173. isClose && that.closeDB();
  174. reject(e);
  175. }
  176. });
  177. });
  178. }
  179. }
  180. export default new SQL(plus);