2 * (c) 2005-2008 Richard Cowin (http://openrico.org)
3 * (c) 2005-2008 Matt Brown (http://dowdybrown.com)
5 * Rico is licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6 * file except in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
10 if(typeof Rico=='undefined') throw("LiveGridAir requires the Rico JavaScript framework");
11 if(typeof RicoUtil=='undefined') throw("LiveGridAir requires the RicoUtil object");
12 if(typeof Rico.Buffer=='undefined') throw("LiveGridAir requires the Rico.Buffer object");
13 if(typeof window.runtime=='undefined') throw("LiveGridAir requires the Adobe AIR runtime");
16 Rico.writeDebugMsg = function(msg) {
17 window.runtime.trace(this.timeStamp()+msg);
22 * Data source is an Adobe AIR SQLite database
24 Rico.Buffer.AirSQL = Class.create();
26 Rico.Buffer.AirSQL.prototype = {
28 initialize: function(dbConn, fromClause, options) {
29 Object.extend(this, new Rico.Buffer.AjaxSQL(null, options));
30 Object.extend(this, new Rico.Buffer.AirSQLMethods());
31 this.dataSource=this.airFetch;
34 this.fromClause=' from '+fromClause;
36 this.SQLStatement = window.runtime.flash.data.SQLStatement;
37 this.options.sortParmFmt='index';
42 Rico.Buffer.AirSQLMethods = function() {};
44 Rico.Buffer.AirSQLMethods.prototype = {
46 addColumn: function(sql,name) {
47 this.colsql.push(sql);
48 this.colnames.push(name);
51 allColumnsSql: function() {
53 for (var i=0; i<this.colnames.length; i++) {
56 if (this.colnames[i]) s+=" AS '"+this.colnames[i]+"'";
62 formQueryHashSQL: function(startPos,fetchSize) {
63 var queryHash=this.formQueryHashXML(startPos,fetchSize);
64 Object.extend(queryHash,this.sortParm);
68 for (var n=0; n<this.liveGrid.columns.length; n++) {
69 var c=this.liveGrid.columns[n];
70 if (c.filterType == Rico.TableColumn.UNFILTERED) continue;
71 var colnum=c.format.filterUI && c.format.filterUI.length > 1 ? parseInt(c.format.filterUI.substr(1),10) : c.index;
75 f.values=c.filterValues;
76 queryHash.filters.push(f);
81 addCondition: function(whereClause,colnum,op,value) {
82 var field=this.colsql[colnum];
83 whereClause+=(whereClause ? ' and ' : ' where ');
84 whereClause+='('+field+op+value+')';
88 airFetch: function(options) {
89 Rico.writeDebugMsg("airFetch");
92 var parms=options.parameters;
94 var sqlorder=parms.sort_dir ? ' order by '+(parms.sort_col+1)+' '+parms.sort_dir : '';
95 for (var n=0; n<parms.filters.length; n++) {
96 var f=parms.filters[n];
101 sqlwhere=this.addCondition(sqlwhere,f.columnIndex,'=','?');
105 sqlwhere=this.addCondition(sqlwhere,f.columnIndex,'<=','?');
109 sqlwhere=this.addCondition(sqlwhere,f.columnIndex,'>=','?');
113 for (i=0; i<f.values.length; i++) {
116 sqlparms.push(f.values[i]);
119 sqlwhere=this.addCondition(sqlwhere,f.columnIndex,' NOT IN ',ne);
122 if (v0.indexOf('*')==-1) v0='*'+v0+'*';
123 sqlparms.push(v0.replace(/\*/g,"%"));
124 sqlwhere=this.addCondition(sqlwhere,f.columnIndex,' LIKE ','?');
128 if (typeof(this.sqltotalrows)=='undefined' || options.parameters.get_total=='true') {
129 stmt = new this.SQLStatement();
130 stmt.sqlConnection = this.dbConn;
131 stmt.text = "select count(*) as cnt"+this.fromClause+sqlwhere;
132 for (i=0; i<sqlparms.length; i++) stmt.parameters[i]=sqlparms[i];
134 this.sqltotalrows=stmt.getResult().data[0].cnt;
136 stmt = new this.SQLStatement();
137 stmt.sqlConnection = this.dbConn;
139 var offset=options.parameters.offset;
140 var limit=Math.min(this.sqltotalrows-offset,options.parameters.page_size);
141 stmt.text = "select "+this.allColumnsSql()+this.fromClause+sqlwhere+sqlorder+" LIMIT "+limit+" OFFSET "+offset;
142 for (i=0; i<sqlparms.length; i++) stmt.parameters[i]=sqlparms[i];
143 Rico.writeDebugMsg(stmt.text);
145 var result = stmt.getResult();
146 if( result.data == null ) {
147 Rico.writeDebugMsg('no data');
149 for (i = 0; i < result.data.length; i++) {
150 var dataRow = result.data[i];
152 for (j=0; j<this.colnames.length; j++)
153 newRow.push(dataRow[this.colnames[j]]);
154 newRows.push(newRow);
157 options.onComplete(newRows,false,this.sqltotalrows);
162 Rico.includeLoaded('ricoLiveGridAir.js');