source: trunk/web/addons/job_monarch/lib/extjs-30/examples/tasks/db/ext-db.js @ 625

Last change on this file since 625 was 625, checked in by ramonb, 15 years ago

lib/extjs-30:

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