You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
7.0 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. // Asbtract base class for SqlDB classes
  16. Ext.data.SqlDB = function(config){
  17. Ext.apply(this, config);
  18. Ext.data.SqlDB.superclass.constructor.call(this);
  19. this.addEvents({
  20. open : true,
  21. close: true
  22. });
  23. };
  24. Ext.extend(Ext.data.SqlDB, Ext.util.Observable, {
  25. maxResults: 10000,
  26. openState : false,
  27. // abstract methods
  28. open : function(file, cb, scope){
  29. },
  30. close : function(){
  31. },
  32. exec : function(sql, cb, scope){
  33. },
  34. execBy : function(sql, args, cb, scope){
  35. },
  36. query : function(sql, cb, scope){
  37. },
  38. queryBy : function(sql, args, cb, scope){
  39. },
  40. // protected/inherited method
  41. isOpen : function(){
  42. return this.openState;
  43. },
  44. getTable : function(name, keyName){
  45. return new Ext.data.SqlDB.Table(this, name, keyName);
  46. },
  47. createTable : function(o){
  48. var tableName = o.name,
  49. keyName = o.key,
  50. fs = o.fields,
  51. cb = o.callback,
  52. scope = o.scope,
  53. buf = [],
  54. types = Ext.data.Types;
  55. if(!(fs instanceof Array)){ // Ext fields collection
  56. fs = fs.items;
  57. }
  58. for(var i = 0, len = fs.length; i < len; i++){
  59. var f = fs[i],
  60. s = f.name;
  61. switch(f.type){
  62. case types.INT:
  63. case types.BOOL:
  64. s += ' INTEGER';
  65. break;
  66. case types.FLOAT:
  67. s += ' REAL';
  68. break;
  69. default:
  70. s += ' TEXT';
  71. }
  72. if(f.allowNull === false || f.name == keyName){
  73. s += ' NOT NULL';
  74. }
  75. if(f.name == keyName){
  76. s += ' PRIMARY KEY';
  77. }
  78. if(f.unique === true){
  79. s += ' UNIQUE';
  80. }
  81. buf[buf.length] = s;
  82. }
  83. var sql = ['CREATE TABLE IF NOT EXISTS ', tableName, ' (', buf.join(','), ')'].join('');
  84. this.exec(sql, cb, scope);
  85. }
  86. });
  87. Ext.data.SqlDB.getInstance = function(db, config){
  88. if(window.htmlControl){ // air
  89. return new Ext.data.AirDB(config);
  90. } else { // gears
  91. return new Ext.data.GearsDB(config);
  92. }
  93. };
  94. Ext.data.SqlDB.Table = function(conn, name, keyName){
  95. this.conn = conn;
  96. this.name = name;
  97. this.keyName = keyName;
  98. };
  99. Ext.data.SqlDB.Table.prototype = {
  100. update : function(o, cb, scope){
  101. var clause = this.keyName + " = :" + this.keyName;
  102. this.updateBy(o, clause, {}, cb, scope);
  103. },
  104. updateBy : function(o, clause, args, cb, scope){
  105. var sql = "UPDATE " + this.name + " set ";
  106. var fs = [], a = [];
  107. for(var key in o){
  108. if(o.hasOwnProperty(key)){
  109. fs[fs.length] = key + ' = ?';
  110. a[a.length] = o[key];
  111. }
  112. }
  113. for(var key in args){
  114. if(args.hasOwnProperty(key)){
  115. a[a.length] = args[key];
  116. }
  117. }
  118. sql = [sql, fs.join(','), ' WHERE ', clause].join('');
  119. this.conn.execBy(sql, a, cb, scope);
  120. },
  121. insert : function(o, cb, scope){
  122. var sql = "INSERT into " + this.name + " ";
  123. var fs = [], vs = [], a = [];
  124. for(var key in o){
  125. if(o.hasOwnProperty(key)){
  126. fs[fs.length] = key;
  127. vs[vs.length] = '?';
  128. a[a.length] = o[key];
  129. }
  130. }
  131. sql = [sql, '(', fs.join(','), ') VALUES (', vs.join(','), ')'].join('');
  132. this.conn.execBy(sql, a, cb, scope);
  133. },
  134. select : function(clause, cb, scope){
  135. this.selectBy(clause, null, cb, scope);
  136. },
  137. selectBy : function(clause, args, cb, scope){
  138. var sql = "select * from " + this.name;
  139. if(clause){
  140. sql += ' ' + clause;
  141. }
  142. args = args || {};
  143. this.conn.queryBy(sql, args, cb, scope);
  144. },
  145. remove : function(clause, cb, scope){
  146. this.deleteBy(clause, null, cb, scope);
  147. },
  148. removeBy : function(clause, args, cb, scope){
  149. var sql = "delete from " + this.name;
  150. if(clause){
  151. sql += ' where ' + clause;
  152. }
  153. args = args || {};
  154. this.conn.execBy(sql, args, cb, scope);
  155. }
  156. };
  157. Ext.data.SqlDB.Proxy = function(conn, table, keyName, store){
  158. Ext.data.SqlDB.Proxy.superclass.constructor.call(this);
  159. this.conn = conn;
  160. this.table = this.conn.getTable(table, keyName);
  161. this.store = store;
  162. this.store.on('add', this.onAdd, this);
  163. this.store.on('update', this.onUpdate, this);
  164. this.store.on('remove', this.onRemove, this);
  165. };
  166. Ext.extend(Ext.data.SqlDB.Proxy, Ext.data.DataProxy, {
  167. load : function(params, reader, callback, scope, arg){
  168. if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
  169. this.conn.on('open', function(){
  170. this.load(params, reader, callback, scope, arg);
  171. }, this, {single:true});
  172. return;
  173. };
  174. if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
  175. var clause = params.where || '';
  176. var args = params.args || [];
  177. var group = params.groupBy;
  178. var sort = params.sort;
  179. var dir = params.dir;
  180. if(group || sort){
  181. clause += ' ORDER BY ';
  182. if(group && group != sort){
  183. clause += group + ' ASC, ';
  184. }
  185. clause += sort + ' ' + (dir || 'ASC');
  186. }
  187. this.table.selectBy(clause, args,
  188. this.onLoad.createDelegate(this, [{callback:callback, scope:scope, arg:arg, reader: reader}], 0));
  189. }else{
  190. callback.call(scope||this, null, arg, false);
  191. }
  192. },
  193. onLoad : function(trans, rs, e, stmt){
  194. if(rs === false){
  195. this.fireEvent("loadexception", this, null, trans.arg, e);
  196. trans.callback.call(trans.scope||window, null, trans.arg, false);
  197. return;
  198. }
  199. var result = trans.reader.readRecords(rs);
  200. this.fireEvent("load", this, rs, trans.arg);
  201. trans.callback.call(trans.scope||window, result, trans.arg, true);
  202. },
  203. processData : function(o){
  204. var fs = this.store.fields,
  205. r = {},
  206. types = Ext.data.Types;
  207. for(var key in o){
  208. var f = fs.key(key), v = o[key];
  209. if(f){
  210. if(f.type == types.DATE){
  211. r[key] = v ? v.format('Y-m-d H:i:s') : '';
  212. }else if(f.type == types.BOOL){
  213. r[key] = v ? 1 : 0;
  214. }else{
  215. r[key] = v;
  216. }
  217. }
  218. }
  219. return r;
  220. },
  221. onUpdate : function(ds, record){
  222. var changes = record.getChanges();
  223. var kn = this.table.keyName;
  224. this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
  225. record.commit(true);
  226. },
  227. onAdd : function(ds, records, index){
  228. for(var i = 0, len = records.length; i < len; i++){
  229. this.table.insert(this.processData(records[i].data));
  230. }
  231. },
  232. onRemove : function(ds, record, index){
  233. var kn = this.table.keyName;
  234. this.table.removeBy(kn + ' = ?', [record.data[kn]]);
  235. }
  236. });