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