Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[extjs.git] / examples / tasks / db / ext-db.js
index 7b3f09b..0c8985e 100644 (file)
-/*\r
- * Ext JS Library 2.2.1\r
- * Copyright(c) 2006-2009, Ext JS, LLC.\r
- * licensing@extjs.com\r
- * \r
- * http://extjs.com/license\r
- */\r
-\r
-\r
-// Asbtract base class for SqlDB classes\r
-Ext.data.SqlDB = function(config){\r
-       Ext.apply(this, config);\r
-       Ext.data.SqlDB.superclass.constructor.call(this);\r
-\r
-       this.addEvents({\r
-               open : true,\r
-               close: true\r
-       });\r
-};\r
-\r
-Ext.extend(Ext.data.SqlDB, Ext.util.Observable, {\r
-       maxResults: 10000,\r
-       openState : false,\r
-\r
-    // abstract methods\r
-    open : function(file, cb, scope){\r
-       },\r
-\r
-       close : function(){\r
-       },\r
-\r
-    exec : function(sql, cb, scope){\r
-       },\r
-\r
-       execBy : function(sql, args, cb, scope){\r
-       },\r
-\r
-       query : function(sql, cb, scope){\r
-       },\r
-\r
-       queryBy : function(sql, args, cb, scope){\r
-       },\r
-\r
-    // protected/inherited method\r
-    isOpen : function(){\r
-               return this.openState;\r
-       },\r
-\r
-       getTable : function(name, keyName){\r
-               return new Ext.data.SqlDB.Table(this, name, keyName);\r
-       },\r
-\r
-       createTable : function(o){\r
-               var tableName = o.name;\r
-               var keyName = o.key;\r
-               var fs = o.fields;\r
-               var cb = o.callback;\r
-               var scope = o.scope;\r
-               if(!(fs instanceof Array)){ // Ext fields collection\r
-                       fs = fs.items;\r
-               }\r
-               var buf = [];\r
-               for(var i = 0, len = fs.length; i < len; i++){\r
-                       var f = fs[i], s = f.name;\r
-                       switch(f.type){\r
-                   case "int":\r
-                   case "bool":\r
-                   case "boolean":\r
-                       s += ' INTEGER';\r
-                       break;\r
-                   case "float":\r
-                       s += ' REAL';\r
-                       break;\r
-                   default:\r
-                       s += ' TEXT';\r
-               }\r
-               if(f.allowNull === false || f.name == keyName){\r
-                       s += ' NOT NULL';\r
-               }\r
-               if(f.name == keyName){\r
-                       s += ' PRIMARY KEY';\r
-               }\r
-               if(f.unique === true){\r
-                       s += ' UNIQUE';\r
-               }\r
-\r
-               buf[buf.length] = s;\r
-           }\r
-           var sql = ['CREATE TABLE IF NOT EXISTS ', tableName, ' (', buf.join(','), ')'].join('');\r
-        this.exec(sql, cb, scope);\r
-       }\r
-});\r
-\r
-\r
-Ext.data.SqlDB.getInstance = function(db, config){\r
-    if(window.htmlControl){ // air\r
-        return new Ext.data.AirDB(config);\r
-    } else { // gears\r
-        return new Ext.data.GearsDB(config);\r
-    }\r
-};\r
-\r
-\r
-Ext.data.SqlDB.Table = function(conn, name, keyName){\r
-       this.conn = conn;\r
-       this.name = name;\r
-       this.keyName = keyName;\r
-};\r
-\r
-Ext.data.SqlDB.Table.prototype = {\r
-       update : function(o, cb, scope){\r
-               var clause = this.keyName + " = :" + this.keyName;\r
-               this.updateBy(o, clause, {}, cb, scope);\r
-       },\r
-\r
-       updateBy : function(o, clause, args, cb, scope){\r
-               var sql = "UPDATE " + this.name + " set ";\r
-               var fs = [], a = [];\r
-               for(var key in o){\r
-                       if(o.hasOwnProperty(key)){\r
-                               fs[fs.length] = key + ' = ?';\r
-                               a[a.length] = o[key];\r
-                       }\r
-               }\r
-               for(var key in args){\r
-                       if(args.hasOwnProperty(key)){\r
-                               a[a.length] = args[key];\r
-                       }\r
-               }\r
-               sql = [sql, fs.join(','), ' WHERE ', clause].join('');\r
-               this.conn.execBy(sql, a, cb, scope);\r
-       },\r
-\r
-       insert : function(o, cb, scope){\r
-               var sql = "INSERT into " + this.name + " ";\r
-               var fs = [], vs = [], a = [];\r
-               for(var key in o){\r
-                       if(o.hasOwnProperty(key)){\r
-                               fs[fs.length] = key;\r
-                               vs[vs.length] = '?';\r
-                               a[a.length] = o[key];\r
-                       }\r
-               }\r
-               sql = [sql, '(', fs.join(','), ') VALUES (', vs.join(','), ')'].join('');\r
-        this.conn.execBy(sql, a, cb, scope);\r
-    },\r
-\r
-       select : function(clause, cb, scope){\r
-               this.selectBy(clause, null, cb, scope);\r
-       },\r
-\r
-       selectBy : function(clause, args, cb, scope){\r
-               var sql = "select * from " + this.name;\r
-               if(clause){\r
-                       sql += ' ' + clause;\r
-               }\r
-               args = args || {};\r
-               this.conn.queryBy(sql, args, cb, scope);\r
-       },\r
-\r
-       remove : function(clause, cb, scope){\r
-               this.deleteBy(clause, null, cb, scope);\r
-       },\r
-\r
-       removeBy : function(clause, args, cb, scope){\r
-               var sql = "delete from " + this.name;\r
-               if(clause){\r
-                       sql += ' where ' + clause;\r
-               }\r
-               args = args || {};\r
-               this.conn.execBy(sql, args, cb, scope);\r
-       }\r
-};\r
-\r
-Ext.data.SqlDB.Proxy = function(conn, table, keyName, store){\r
-    Ext.data.SqlDB.Proxy.superclass.constructor.call(this);\r
-    this.conn = conn;\r
-    this.table = this.conn.getTable(table, keyName);\r
-    this.store = store;\r
-\r
-    this.store.on('add', this.onAdd, this);\r
-    this.store.on('update', this.onUpdate, this);\r
-    this.store.on('remove', this.onRemove, this);\r
-};\r
-\r
-Ext.extend(Ext.data.SqlDB.Proxy, Ext.data.DataProxy, {\r
-    load : function(params, reader, callback, scope, arg){\r
-       if(!this.conn.isOpen()){ // assume that the connection is in the process of opening\r
-               this.conn.on('open', function(){\r
-                       this.load(params, reader, callback, scope, arg);\r
-               }, this, {single:true});\r
-               return;\r
-       };\r
-       if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){\r
-                       var clause = params.where || '';\r
-                       var args = params.args || [];\r
-                       var group = params.groupBy;\r
-                       var sort = params.sort;\r
-                       var dir = params.dir;\r
-\r
-                       if(group || sort){\r
-                               clause += ' ORDER BY ';\r
-                               if(group && group != sort){\r
-                                       clause += group + ' ASC, ';\r
-                               }\r
-                               clause += sort + ' ' + (dir || 'ASC');\r
-                       }\r
-\r
-                       this.table.selectBy(clause, args,\r
-                                       this.onLoad.createDelegate(this, [{callback:callback, scope:scope, arg:arg, reader: reader}], 0));\r
-        }else{\r
-            callback.call(scope||this, null, arg, false);\r
-        }\r
-    },\r
-\r
-    onLoad : function(trans, rs, e, stmt){\r
-        if(rs === false){\r
-               this.fireEvent("loadexception", this, null, trans.arg, e);\r
-            trans.callback.call(trans.scope||window, null, trans.arg, false);\r
-            return;\r
-       }\r
-       var result = trans.reader.readRecords(rs);\r
-        this.fireEvent("load", this, rs, trans.arg);\r
-        trans.callback.call(trans.scope||window, result, trans.arg, true);\r
-    },\r
-\r
-    processData : function(o){\r
-       var fs = this.store.fields;\r
-       var r = {};\r
-       for(var key in o){\r
-               var f = fs.key(key), v = o[key];\r
-                       if(f){\r
-                               if(f.type == 'date'){\r
-                                       r[key] = v ? v.format('Y-m-d H:i:s') : '';\r
-                               }else if(f.type == 'boolean'){\r
-                                       r[key] = v ? 1 : 0;\r
-                               }else{\r
-                                       r[key] = v;\r
-                               }\r
-                       }\r
-               }\r
-               return r;\r
-    },\r
-\r
-    onUpdate : function(ds, record){\r
-       var changes = record.getChanges();\r
-       var kn = this.table.keyName;\r
-       this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);\r
-       record.commit(true);\r
-    },\r
-\r
-    onAdd : function(ds, records, index){\r
-       for(var i = 0, len = records.length; i < len; i++){\r
-               this.table.insert(this.processData(records[i].data));\r
-       }\r
-    },\r
-\r
-    onRemove : function(ds, record, index){\r
-        var kn = this.table.keyName;\r
-       this.table.removeBy(kn + ' = ?', [record.data[kn]]);\r
-    }\r
+/*!
+ * Ext JS Library 3.3.1
+ * Copyright(c) 2006-2010 Sencha Inc.
+ * licensing@sencha.com
+ * http://www.sencha.com/license
+ */
+
+// Asbtract base class for SqlDB classes
+Ext.data.SqlDB = function(config){
+       Ext.apply(this, config);
+       Ext.data.SqlDB.superclass.constructor.call(this);
+
+       this.addEvents({
+               open : true,
+               close: true
+       });
+};
+
+Ext.extend(Ext.data.SqlDB, Ext.util.Observable, {
+       maxResults: 10000,
+       openState : false,
+
+    // abstract methods
+    open : function(file, cb, scope){
+       },
+
+       close : function(){
+       },
+
+    exec : function(sql, cb, scope){
+       },
+
+       execBy : function(sql, args, cb, scope){
+       },
+
+       query : function(sql, cb, scope){
+       },
+
+       queryBy : function(sql, args, cb, scope){
+       },
+
+    // protected/inherited method
+    isOpen : function(){
+               return this.openState;
+       },
+
+       getTable : function(name, keyName){
+               return new Ext.data.SqlDB.Table(this, name, keyName);
+       },
+
+       createTable : function(o){
+               var tableName = o.name,
+                   keyName = o.key,
+                   fs = o.fields,
+                   cb = o.callback,
+                   scope = o.scope,
+            buf = [],
+            types = Ext.data.Types;
+            
+               if(!(fs instanceof Array)){ // Ext fields collection
+                       fs = fs.items;
+               }
+               for(var i = 0, len = fs.length; i < len; i++){
+                       var f = fs[i], 
+                s = f.name;
+                       switch(f.type){
+                   case types.INT:
+                   case types.BOOL:
+                       s += ' INTEGER';
+                       break;
+                   case types.FLOAT:
+                       s += ' REAL';
+                       break;
+                   default:
+                       s += ' TEXT';
+               }
+               if(f.allowNull === false || f.name == keyName){
+                       s += ' NOT NULL';
+               }
+               if(f.name == keyName){
+                       s += ' PRIMARY KEY';
+               }
+               if(f.unique === true){
+                       s += ' UNIQUE';
+               }
+
+               buf[buf.length] = s;
+           }
+           var sql = ['CREATE TABLE IF NOT EXISTS ', tableName, ' (', buf.join(','), ')'].join('');
+        this.exec(sql, cb, scope);
+       }
+});
+
+
+Ext.data.SqlDB.getInstance = function(db, config){
+    if(window.htmlControl){ // air
+        return new Ext.data.AirDB(config);
+    } else { // gears
+        return new Ext.data.GearsDB(config);
+    }
+};
+
+
+Ext.data.SqlDB.Table = function(conn, name, keyName){
+       this.conn = conn;
+       this.name = name;
+       this.keyName = keyName;
+};
+
+Ext.data.SqlDB.Table.prototype = {
+       update : function(o, cb, scope){
+               var clause = this.keyName + " = :" + this.keyName;
+               this.updateBy(o, clause, {}, cb, scope);
+       },
+
+       updateBy : function(o, clause, args, cb, scope){
+               var sql = "UPDATE " + this.name + " set ";
+               var fs = [], a = [];
+               for(var key in o){
+                       if(o.hasOwnProperty(key)){
+                               fs[fs.length] = key + ' = ?';
+                               a[a.length] = o[key];
+                       }
+               }
+               for(var key in args){
+                       if(args.hasOwnProperty(key)){
+                               a[a.length] = args[key];
+                       }
+               }
+               sql = [sql, fs.join(','), ' WHERE ', clause].join('');
+               this.conn.execBy(sql, a, cb, scope);
+       },
+
+       insert : function(o, cb, scope){
+               var sql = "INSERT into " + this.name + " ";
+               var fs = [], vs = [], a = [];
+               for(var key in o){
+                       if(o.hasOwnProperty(key)){
+                               fs[fs.length] = key;
+                               vs[vs.length] = '?';
+                               a[a.length] = o[key];
+                       }
+               }
+               sql = [sql, '(', fs.join(','), ') VALUES (', vs.join(','), ')'].join('');
+        this.conn.execBy(sql, a, cb, scope);
+    },
+
+       select : function(clause, cb, scope){
+               this.selectBy(clause, null, cb, scope);
+       },
+
+       selectBy : function(clause, args, cb, scope){
+               var sql = "select * from " + this.name;
+               if(clause){
+                       sql += ' ' + clause;
+               }
+               args = args || {};
+               this.conn.queryBy(sql, args, cb, scope);
+       },
+
+       remove : function(clause, cb, scope){
+               this.deleteBy(clause, null, cb, scope);
+       },
+
+       removeBy : function(clause, args, cb, scope){
+               var sql = "delete from " + this.name;
+               if(clause){
+                       sql += ' where ' + clause;
+               }
+               args = args || {};
+               this.conn.execBy(sql, args, cb, scope);
+       }
+};
+
+Ext.data.SqlDB.Proxy = function(conn, table, keyName, store){
+    Ext.data.SqlDB.Proxy.superclass.constructor.call(this);
+    this.conn = conn;
+    this.table = this.conn.getTable(table, keyName);
+    this.store = store;
+
+    this.store.on('add', this.onAdd, this);
+    this.store.on('update', this.onUpdate, this);
+    this.store.on('remove', this.onRemove, this);
+};
+
+Ext.extend(Ext.data.SqlDB.Proxy, Ext.data.DataProxy, {
+    load : function(params, reader, callback, scope, arg){
+       if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
+               this.conn.on('open', function(){
+                       this.load(params, reader, callback, scope, arg);
+               }, this, {single:true});
+               return;
+       };
+       if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
+                       var clause = params.where || '';
+                       var args = params.args || [];
+                       var group = params.groupBy;
+                       var sort = params.sort;
+                       var dir = params.dir;
+
+                       if(group || sort){
+                               clause += ' ORDER BY ';
+                               if(group && group != sort){
+                                       clause += group + ' ASC, ';
+                               }
+                               clause += sort + ' ' + (dir || 'ASC');
+                       }
+
+                       this.table.selectBy(clause, args,
+                                       this.onLoad.createDelegate(this, [{callback:callback, scope:scope, arg:arg, reader: reader}], 0));
+        }else{
+            callback.call(scope||this, null, arg, false);
+        }
+    },
+
+    onLoad : function(trans, rs, e, stmt){
+        if(rs === false){
+               this.fireEvent("loadexception", this, null, trans.arg, e);
+            trans.callback.call(trans.scope||window, null, trans.arg, false);
+            return;
+       }
+       var result = trans.reader.readRecords(rs);
+        this.fireEvent("load", this, rs, trans.arg);
+        trans.callback.call(trans.scope||window, result, trans.arg, true);
+    },
+
+    processData : function(o){
+       var fs = this.store.fields,
+           r = {},
+            types = Ext.data.Types;
+       for(var key in o){
+               var f = fs.key(key), v = o[key];
+                       if(f){
+                               if(f.type == types.DATE){
+                                       r[key] = v ? v.format('Y-m-d H:i:s') : '';
+                               }else if(f.type == types.BOOL){
+                                       r[key] = v ? 1 : 0;
+                               }else{
+                                       r[key] = v;
+                               }
+                       }
+               }
+               return r;
+    },
+
+    onUpdate : function(ds, record){
+       var changes = record.getChanges();
+       var kn = this.table.keyName;
+       this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
+       record.commit(true);
+    },
+
+    onAdd : function(ds, records, index){
+       for(var i = 0, len = records.length; i < len; i++){
+               this.table.insert(this.processData(records[i].data));
+       }
+    },
+
+    onRemove : function(ds, record, index){
+        var kn = this.table.keyName;
+       this.table.removeBy(kn + ' = ?', [record.data[kn]]);
+    }
 });
\ No newline at end of file