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

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

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 2.6 KB
Line 
1/*
2 * Ext JS Library 2.2.1
3 * Copyright(c) 2006-2009, Ext JS, LLC.
4 * licensing@extjs.com
5 *
6 * http://extjs.com/license
7 */
8
9 Ext.data.AirDB = Ext.extend(Ext.data.SqlDB, {
10        open : function(db, cb, scope){
11                this.conn = new air.SQLConnection();
12
13                var file = air.File.applicationResourceDirectory.resolve(db);
14
15                this.conn.addEventListener(air.SQLEvent.OPEN, this.onOpen.createDelegate(this, [cb, scope]));
16                this.conn.addEventListener(air.SQLEvent.CLOSE, this.onClose.createDelegate(this));
17                this.conn.open(file, true);
18        },
19
20        close : function(){
21                this.conn.close();
22        },
23
24        onOpen : function(cb, scope){
25                this.openState = true;
26                Ext.callback(cb, scope, [this]);
27                this.fireEvent('open', this);
28        },
29
30        onClose : function(){
31                this.fireEvent('close', this);
32        },
33
34        onError : function(e, stmt, type, cb, scope){
35                Ext.callback(cb, scope, [false, e, stmt]);
36        },
37
38        onResult : function(e, stmt, type, cb, scope){
39                if(type == 'exec'){
40                        Ext.callback(cb, scope, [true, e, stmt]);
41                }else{
42                        var r = [];
43                        var result = stmt.getResult();
44                        if(result && result.data){
45                        var len = result.data.length;
46                        for(var i = 0; i < len; i++) {
47                            r[r.length] = result.data[i];
48                        }
49                    }
50                        Ext.callback(cb, scope, [r, e, stmt]);
51                }
52        },
53
54        createStatement : function(type, cb, scope){
55
56                var stmt = new air.SQLStatement();
57
58                stmt.addEventListener(air.SQLErrorEvent.ERROR, this.onError.createDelegate(this, [stmt, type, cb, scope], true));
59                stmt.addEventListener(air.SQLEvent.RESULT, this.onResult.createDelegate(this, [stmt, type, cb, scope], true));
60
61                stmt.sqlConnection = this.conn;
62
63                return stmt;
64        },
65
66        exec : function(sql, cb, scope){
67                var stmt = this.createStatement('exec', cb, scope);
68                stmt.text = sql;
69                stmt.execute();
70        },
71
72        execBy : function(sql, args, cb, scope){
73                var stmt = this.createStatement('exec', cb, scope);
74                stmt.text = sql;
75                this.addParams(stmt, args);
76                stmt.execute();
77        },
78
79        query : function(sql, cb, scope){
80                var stmt = this.createStatement('query', cb, scope);
81                stmt.text = sql;
82                stmt.execute(this.maxResults);
83        },
84
85        queryBy : function(sql, args, cb, scope){
86                var stmt = this.createStatement('query', cb, scope);
87                stmt.text = sql;
88                this.addParams(stmt, args);
89                stmt.execute(this.maxResults);
90        },
91
92    addParams : function(stmt, args){
93                if(!args){ return; }
94                for(var key in args){
95                        if(args.hasOwnProperty(key)){
96                                if(!isNaN(key)){
97                                        stmt.parameters[parseInt(key)+1] = args[key];
98                                }else{
99                                        stmt.parameters[':' + key] = args[key];
100                                }
101                        }
102                }
103                return stmt;
104        }
105});
Note: See TracBrowser for help on using the repository browser.