-/*!
- * Ext JS Library 3.0.0
- * Copyright(c) 2006-2009 Ext JS, LLC
- * licensing@extjs.com
- * http://www.extjs.com/license
- */
-\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
-});
\ No newline at end of file