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 | Ext.data.AirDB = Ext.extend(Ext.data.SqlDB, { |
---|
8 | open : function(db, cb, scope){ |
---|
9 | this.conn = new air.SQLConnection(); |
---|
10 | |
---|
11 | var file = air.File.applicationResourceDirectory.resolve(db); |
---|
12 | |
---|
13 | this.conn.addEventListener(air.SQLEvent.OPEN, this.onOpen.createDelegate(this, [cb, scope])); |
---|
14 | this.conn.addEventListener(air.SQLEvent.CLOSE, this.onClose.createDelegate(this)); |
---|
15 | this.conn.open(file, true); |
---|
16 | }, |
---|
17 | |
---|
18 | close : function(){ |
---|
19 | this.conn.close(); |
---|
20 | }, |
---|
21 | |
---|
22 | onOpen : function(cb, scope){ |
---|
23 | this.openState = true; |
---|
24 | Ext.callback(cb, scope, [this]); |
---|
25 | this.fireEvent('open', this); |
---|
26 | }, |
---|
27 | |
---|
28 | onClose : function(){ |
---|
29 | this.fireEvent('close', this); |
---|
30 | }, |
---|
31 | |
---|
32 | onError : function(e, stmt, type, cb, scope){ |
---|
33 | Ext.callback(cb, scope, [false, e, stmt]); |
---|
34 | }, |
---|
35 | |
---|
36 | onResult : function(e, stmt, type, cb, scope){ |
---|
37 | if(type == 'exec'){ |
---|
38 | Ext.callback(cb, scope, [true, e, stmt]); |
---|
39 | }else{ |
---|
40 | var r = []; |
---|
41 | var result = stmt.getResult(); |
---|
42 | if(result && result.data){ |
---|
43 | var len = result.data.length; |
---|
44 | for(var i = 0; i < len; i++) { |
---|
45 | r[r.length] = result.data[i]; |
---|
46 | } |
---|
47 | } |
---|
48 | Ext.callback(cb, scope, [r, e, stmt]); |
---|
49 | } |
---|
50 | }, |
---|
51 | |
---|
52 | createStatement : function(type, cb, scope){ |
---|
53 | |
---|
54 | var stmt = new air.SQLStatement(); |
---|
55 | |
---|
56 | stmt.addEventListener(air.SQLErrorEvent.ERROR, this.onError.createDelegate(this, [stmt, type, cb, scope], true)); |
---|
57 | stmt.addEventListener(air.SQLEvent.RESULT, this.onResult.createDelegate(this, [stmt, type, cb, scope], true)); |
---|
58 | |
---|
59 | stmt.sqlConnection = this.conn; |
---|
60 | |
---|
61 | return stmt; |
---|
62 | }, |
---|
63 | |
---|
64 | exec : function(sql, cb, scope){ |
---|
65 | var stmt = this.createStatement('exec', cb, scope); |
---|
66 | stmt.text = sql; |
---|
67 | stmt.execute(); |
---|
68 | }, |
---|
69 | |
---|
70 | execBy : function(sql, args, cb, scope){ |
---|
71 | var stmt = this.createStatement('exec', cb, scope); |
---|
72 | stmt.text = sql; |
---|
73 | this.addParams(stmt, args); |
---|
74 | stmt.execute(); |
---|
75 | }, |
---|
76 | |
---|
77 | query : function(sql, cb, scope){ |
---|
78 | var stmt = this.createStatement('query', cb, scope); |
---|
79 | stmt.text = sql; |
---|
80 | stmt.execute(this.maxResults); |
---|
81 | }, |
---|
82 | |
---|
83 | queryBy : function(sql, args, cb, scope){ |
---|
84 | var stmt = this.createStatement('query', cb, scope); |
---|
85 | stmt.text = sql; |
---|
86 | this.addParams(stmt, args); |
---|
87 | stmt.execute(this.maxResults); |
---|
88 | }, |
---|
89 | |
---|
90 | addParams : function(stmt, args){ |
---|
91 | if(!args){ return; } |
---|
92 | for(var key in args){ |
---|
93 | if(args.hasOwnProperty(key)){ |
---|
94 | if(!isNaN(key)){ |
---|
95 | stmt.parameters[parseInt(key)+1] = args[key]; |
---|
96 | }else{ |
---|
97 | stmt.parameters[':' + key] = args[key]; |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | return stmt; |
---|
102 | } |
---|
103 | }); |
---|