Web Interface For Lite Argus Control modules
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.

262 lines
6.4 KiB

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