Packagephi.interfaces
Interfacepublic interface IQuery extends flash.events.IEventDispatcher
ImplementorsQuery

The interface definition for a Query object.

A IQuery implementor have these responsibilities:

See also

phi.db.IDatabase


Public Properties
 PropertyDefined by
  database : IDatabase
[write-only]
IQuery
  q : String
[write-only]
IQuery
  queryEnd : Function
[write-only]
IQuery
  Records : ArrayCollection
IQuery
Public Methods
 MethodDefined by
  
arrayInsert(table:String, arr:Array):String
Execute a INSERT query on a table.
IQuery
  
arrayUpdate(table:String, arr:Array, cond:String):String
Execute a UPDATE query on a table.
IQuery
  
connect(connection:String, db:IDatabase):void
This function allow to select the connection you want to use for executing a SQL statements.
IQuery
  
execute(q:String, option:String = "select"):void
Execte a SQL statement.
IQuery
  
Get the current used connection.
IQuery
  
getError():String
Get the last SQL error.
IQuery
  
getLastInsertID():Number
Get the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
IQuery
  
getRecords():ArrayCollection
Get the records selected with a previous execute() method.
IQuery
  
getRow():Object
Get the next row from a previous selected records.
IQuery
  
isStackEmpty():Boolean
IQuery
  
toString():String
Return the query.
IQuery
Property detail
databaseproperty
database:IDatabase  [write-only]Implementation
    public function set database(value:IDatabase):void
qproperty 
q:String  [write-only]Implementation
    public function set q(value:String):void
queryEndproperty 
queryEnd:Function  [write-only]Implementation
    public function set queryEnd(value:Function):void
Recordsproperty 
Records:ArrayCollection  [read-write]

This property can be used as the source for data binding.

Implementation
    public function get Records():ArrayCollection
    public function set Records(value:ArrayCollection):void
Method detail
arrayInsert()method
public function arrayInsert(table:String, arr:Array):String

Execute a INSERT query on a table.

This function can be used to save data into database. After parsing the array this function generate a SQL statement and execute it with execute() method.

Parameters
table:String — the name of the table
 
arr:Array — the array that contains the data.

Returns
String — the SQL generated from array.

Example
Next example insert a new user in database:
   private functin insertNewUsers(q:IQuery):void
   {
     var arr :Array = new Array();
   
     arr.push({key: fname, value: "pop"});
     arr.push({key: lname, value: "lpopname"});
   
     q.addEventListener(Query.QUERY_END, processQuery);
     q.addEventListener(Query.QUERY_ERROR, processQueryError);
     q.arrayInsert("users", arr);
   }
   
   private function processQuery(evt:Object):void
   {
     ....
   }
   
   private function processQueryError(evt:Object):void
   {
     ...
   }
   

arrayUpdate()method 
public function arrayUpdate(table:String, arr:Array, cond:String):String

Execute a UPDATE query on a table.

Parameters
table:String
 
arr:Array
 
cond:String

Returns
String
connect()method 
public function connect(connection:String, db:IDatabase):void

This function allow to select the connection you want to use for executing a SQL statements.
You must create a connection with Database.connect() before using this.

You must select a connection before doing any operations with a Query object.

Parameters
connection:String — the connection name
 
db:IDatabase — a IDatabase object

See also


Example
The next exemple select the second connection:
   var db :IDatabase = Database.getInstance();
   var q  :IQuery  = new Query();
   
   db.connect("conn1", "pop", "poppass", "localhost", "yb20");
   db.connect("conn2", "pop", "poppass", "localhost", "test_db");
   
   q.connect("conn2");
   

execute()method 
public function execute(q:String, option:String = "select"):void

Execte a SQL statement. Before executing the SQL statement this function dispatch a Query.QUERY_START event and when the SQL statement has finish execution a Query.QUERY_END will be dispatched.

Parameters
q:String — the SQL string
 
option:String (default = "select") — leave this on default

Throws
— Error if there are any SQL errors.

Example
Next example will select all users from a users table:
   var db :IDatabase = Database.getInstance();
   var q  :IQuery  = new Query();
   
   private function connect():void
   {
     db.connect("conn1", "pop", "poppass", "localhost", "test_db");
     q.connect("conn1");
   }
   
   private function getUsers():void
   {
     connect();
   
     q.addEventListener(Query.QUERY_END, onGetUsers);
     q.execute("SELECT fname FROM users WHERE 1");
   }
   
   private function onGetUsers(evt:Object):void
   {
     var q  :IQuery    = evt.target as IQuery;
     var users :ArrayCollection = q.getRecords();
   }
   

getConnection()method 
public function getConnection():ConnectionData

Get the current used connection.

Returns
ConnectionData — the current used connection for execution of SQL statement.
getError()method 
public function getError():String

Get the last SQL error.

Returns
String — the SQL error.
getLastInsertID()method 
public function getLastInsertID():Number

Get the ID generated for an AUTO_INCREMENT column by the previous INSERT query.

Returns
Number — the ID generated for an AUTO_INCREMENT
getRecords()method 
public function getRecords():ArrayCollection

Get the records selected with a previous execute() method.

Returns
ArrayCollection — a ArrayCollection with all selected records.
getRow()method 
public function getRow():Object

Get the next row from a previous selected records.

This function can be used to process the selected records.

Returns
Object — a Object with the row information.

Example
Next example will select all users from a db and add "_process" string to the end on fname
   ......
   ......
   
   private functin process(q:IQuery):void
   {
     var row :Object = new Object();
     while((row = q.getRow()) != null)
     {
      row.fname += "_process";
     }
   }
   

isStackEmpty()method 
public function isStackEmpty():Boolean

Returns
Boolean — true if execute stack is empty.
toString()method 
public function toString():String

Return the query.

Returns
String — the query.