3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js">
\r
9 // Asbtract base class for SqlDB classes
\r
10 Ext.data.SqlDB = function(config){
\r
11 Ext.apply(this, config);
\r
12 Ext.data.SqlDB.superclass.constructor.call(this);
\r
20 Ext.extend(Ext.data.SqlDB, Ext.util.Observable, {
\r
25 open : function(file, cb, scope){
\r
31 exec : function(sql, cb, scope){
\r
34 execBy : function(sql, args, cb, scope){
\r
37 query : function(sql, cb, scope){
\r
40 queryBy : function(sql, args, cb, scope){
\r
43 // protected/inherited method
\r
44 isOpen : function(){
\r
45 return this.openState;
\r
48 getTable : function(name, keyName){
\r
49 return new Ext.data.SqlDB.Table(this, name, keyName);
\r
52 createTable : function(o){
\r
53 var tableName = o.name;
\r
54 var keyName = o.key;
\r
56 var cb = o.callback;
\r
57 var scope = o.scope;
\r
58 if(!(fs instanceof Array)){ // Ext fields collection
\r
62 for(var i = 0, len = fs.length; i < len; i++){
\r
63 var f = fs[i], s = f.name;
\r
76 if(f.allowNull === false || f.name == keyName){
\r
79 if(f.name == keyName){
\r
80 s += ' PRIMARY KEY';
\r
82 if(f.unique === true){
\r
86 buf[buf.length] = s;
\r
88 var sql = ['CREATE TABLE IF NOT EXISTS ', tableName, ' (', buf.join(','), ')'].join('');
\r
89 this.exec(sql, cb, scope);
\r
94 Ext.data.SqlDB.getInstance = function(db, config){
\r
95 if(window.htmlControl){ // air
\r
96 return new Ext.data.AirDB(config);
\r
98 return new Ext.data.GearsDB(config);
\r
103 Ext.data.SqlDB.Table = function(conn, name, keyName){
\r
106 this.keyName = keyName;
\r
109 Ext.data.SqlDB.Table.prototype = {
\r
110 update : function(o, cb, scope){
\r
111 var clause = this.keyName + " = :" + this.keyName;
\r
112 this.updateBy(o, clause, {}, cb, scope);
\r
115 updateBy : function(o, clause, args, cb, scope){
\r
116 var sql = "UPDATE " + this.name + " set ";
\r
117 var fs = [], a = [];
\r
119 if(o.hasOwnProperty(key)){
\r
120 fs[fs.length] = key + ' = ?';
\r
121 a[a.length] = o[key];
\r
124 for(var key in args){
\r
125 if(args.hasOwnProperty(key)){
\r
126 a[a.length] = args[key];
\r
129 sql = [sql, fs.join(','), ' WHERE ', clause].join('');
\r
130 this.conn.execBy(sql, a, cb, scope);
\r
133 insert : function(o, cb, scope){
\r
134 var sql = "INSERT into " + this.name + " ";
\r
135 var fs = [], vs = [], a = [];
\r
137 if(o.hasOwnProperty(key)){
\r
138 fs[fs.length] = key;
\r
139 vs[vs.length] = '?';
\r
140 a[a.length] = o[key];
\r
143 sql = [sql, '(', fs.join(','), ') VALUES (', vs.join(','), ')'].join('');
\r
144 this.conn.execBy(sql, a, cb, scope);
\r
147 select : function(clause, cb, scope){
\r
148 this.selectBy(clause, null, cb, scope);
\r
151 selectBy : function(clause, args, cb, scope){
\r
152 var sql = "select * from " + this.name;
\r
154 sql += ' ' + clause;
\r
157 this.conn.queryBy(sql, args, cb, scope);
\r
160 remove : function(clause, cb, scope){
\r
161 this.deleteBy(clause, null, cb, scope);
\r
164 removeBy : function(clause, args, cb, scope){
\r
165 var sql = "delete from " + this.name;
\r
167 sql += ' where ' + clause;
\r
170 this.conn.execBy(sql, args, cb, scope);
\r
174 Ext.data.SqlDB.Proxy = function(conn, table, keyName, store){
\r
175 Ext.data.SqlDB.Proxy.superclass.constructor.call(this);
\r
177 this.table = this.conn.getTable(table, keyName);
\r
178 this.store = store;
\r
180 this.store.on('add', this.onAdd, this);
\r
181 this.store.on('update', this.onUpdate, this);
\r
182 this.store.on('remove', this.onRemove, this);
\r
185 Ext.extend(Ext.data.SqlDB.Proxy, Ext.data.DataProxy, {
\r
186 load : function(params, reader, callback, scope, arg){
\r
187 if(!this.conn.isOpen()){ // assume that the connection is in the process of opening
\r
188 this.conn.on('open', function(){
\r
189 this.load(params, reader, callback, scope, arg);
\r
190 }, this, {single:true});
\r
193 if(this.fireEvent("beforeload", this, params, reader, callback, scope, arg) !== false){
\r
194 var clause = params.where || '';
\r
195 var args = params.args || [];
\r
196 var group = params.groupBy;
\r
197 var sort = params.sort;
\r
198 var dir = params.dir;
\r
201 clause += ' ORDER BY ';
\r
202 if(group && group != sort){
\r
203 clause += group + ' ASC, ';
\r
205 clause += sort + ' ' + (dir || 'ASC');
\r
208 this.table.selectBy(clause, args,
\r
209 this.onLoad.createDelegate(this, [{callback:callback, scope:scope, arg:arg, reader: reader}], 0));
\r
211 callback.call(scope||this, null, arg, false);
\r
215 onLoad : function(trans, rs, e, stmt){
\r
217 this.fireEvent("loadexception", this, null, trans.arg, e);
\r
218 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r
221 var result = trans.reader.readRecords(rs);
\r
222 this.fireEvent("load", this, rs, trans.arg);
\r
223 trans.callback.call(trans.scope||window, result, trans.arg, true);
\r
226 processData : function(o){
\r
227 var fs = this.store.fields;
\r
230 var f = fs.key(key), v = o[key];
\r
232 if(f.type == 'date'){
\r
233 r[key] = v ? v.format('Y-m-d H:i:s') : '';
\r
234 }else if(f.type == 'boolean'){
\r
235 r[key] = v ? 1 : 0;
\r
244 onUpdate : function(ds, record){
\r
245 var changes = record.getChanges();
\r
246 var kn = this.table.keyName;
\r
247 this.table.updateBy(this.processData(changes), kn + ' = ?', [record.data[kn]]);
\r
248 record.commit(true);
\r
251 onAdd : function(ds, records, index){
\r
252 for(var i = 0, len = records.length; i < len; i++){
\r
253 this.table.insert(this.processData(records[i].data));
\r
257 onRemove : function(ds, record, index){
\r
258 var kn = this.table.keyName;
\r
259 this.table.removeBy(kn + ' = ?', [record.data[kn]]);
\r