3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
7 Ext.data.AirDB = Ext.extend(Ext.data.SqlDB, {
8 open : function(db, cb, scope){
9 this.conn = new air.SQLConnection();
11 var file = air.File.applicationResourceDirectory.resolve(db);
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);
22 onOpen : function(cb, scope){
23 this.openState = true;
24 Ext.callback(cb, scope, [this]);
25 this.fireEvent('open', this);
29 this.fireEvent('close', this);
32 onError : function(e, stmt, type, cb, scope){
33 Ext.callback(cb, scope, [false, e, stmt]);
36 onResult : function(e, stmt, type, cb, scope){
38 Ext.callback(cb, scope, [true, e, stmt]);
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];
48 Ext.callback(cb, scope, [r, e, stmt]);
52 createStatement : function(type, cb, scope){
54 var stmt = new air.SQLStatement();
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));
59 stmt.sqlConnection = this.conn;
64 exec : function(sql, cb, scope){
65 var stmt = this.createStatement('exec', cb, scope);
70 execBy : function(sql, args, cb, scope){
71 var stmt = this.createStatement('exec', cb, scope);
73 this.addParams(stmt, args);
77 query : function(sql, cb, scope){
78 var stmt = this.createStatement('query', cb, scope);
80 stmt.execute(this.maxResults);
83 queryBy : function(sql, args, cb, scope){
84 var stmt = this.createStatement('query', cb, scope);
86 this.addParams(stmt, args);
87 stmt.execute(this.maxResults);
90 addParams : function(stmt, args){
93 if(args.hasOwnProperty(key)){
95 stmt.parameters[parseInt(key)+1] = args[key];
97 stmt.parameters[':' + key] = args[key];