Packagephi.db
Classpublic class Query
InheritanceQuery Inheritance flash.events.EventDispatcher
ImplementsIQuery

A IQuery implementation.

A Query object assumes these responsibilities:

See also

phi.db.Database


Public Properties
 PropertyDefined by
  database : IDatabase
[write-only]
Query
  q : String
[write-only]
Query
  queryEnd : Function
[write-only]
Query
  Records : ArrayCollection
Query
Public Methods
 MethodDefined by
  
Constructor
Query
  
arrayInsert(table:String, arr:Array):String
Execute a INSERT query on a table.
Query
  
arrayUpdate(table:String, arr:Array, cond:String):String
Execute a UPDATE query on a table.
Query
  
connect(connection:String, db:IDatabase):void
This function allow to select the connection you want to use for executing a SQL statements.
Query
  
execute(q:String, option:String):void
Execte a SQL statement.
Query
  
Get the current used connection.
Query
  
getError():String
Get the last SQL error.
Query
  
getLastInsertID():Number
Get the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
Query
  
getRecords():ArrayCollection
Get the records selected with a previous execute() method.
Query
  
getRow():Object
Get the next row from a previous selected records.
Query
  
isStackEmpty():Boolean
Query
  
toString():String
Return the query.
Query
Events
 EventSummaryDefined by
   Dispatched after the SQL statement was executed.Query
   Dispatched when the SQL statement contains errors.Query
   Dispatched befor starting execute a SQL statement.Query
Public Constants
 ConstantDefined by
  DELETE : String = "delete"
[static]
Query
  INSERT : String = "insert"
[static]
Query
  QUERY_END : String = "endQuery"
[static] The Query.QUERY_END constant defines the value of the type property of the event object for an event that is dispatched after a SQL statement has finish the execution.
Query
  QUERY_ERROR : String = "errorQuery"
[static] The Query.QUERY_ERROR constant defines the value of the type property of the event object for an event that is dispatched when a SQL error appear on a execute() method.
Query
  QUERY_START : String = "startQuery"
[static] The Query.QUERY_START constant defines the value of the type property of the event object for an event that is dispatched before a SQL statement start execute.
Query
  SELECT : String = "select"
[static]
Query
  UPDATE : String = "update"
[static]
Query
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
Constructor detail
Query()constructor
public function Query()

Constructor

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.

This function can be used to update 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.
 
cond:String — the condition after the update will be made

Returns
String — the SQL generated from array.

Example
Next example update the fname,lname of the user with id = 4:
   private functin updateUser(q:IQuery):void
   {
     var arr :Array = new Array();
   
     arr.push({key: fname, value: "New_FName"});
     arr.push({key: lname, value: "New_LName"});
   
     q.addEventListener(Query.QUERY_END, processQuery);
     q.addEventListener(Query.QUERY_ERROR, processQueryError);
     q.arrayUpdate("users", arr, "id = 4");
   }
   
   private function processQuery(evt:Object):void
   {
     ....
   }
   
   private function processQueryError(evt:Object):void
   {
     ...
   }
   

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):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 — 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 override function toString():String

Return the query.

Returns
String — the query.
Event detail
endQueryevent 
Event object type: flash.events.Event

Dispatched after the SQL statement was executed.

The endQuery event is dispatched when the execute() or arrayInsert() method is called.
At the time when this event is sent, the Query object has been retrieve the records if a SELECT statements was execute or the last inserted id if a INSERT statement was execute.

errorQueryevent  
Event object type: flash.events.Event

Dispatched when the SQL statement contains errors.

At the time when this event is sent, the Query object has been save the error message.
To retrieve the error message you should use getError() method.

startQueryevent  
Event object type: flash.events.Event

Dispatched befor starting execute a SQL statement.

The endQuery event is dispatched when the execute() or arrayInsert() method is called.

Constant detail
DELETEconstant
public static const DELETE:String = "delete"
INSERTconstant 
public static const INSERT:String = "insert"
QUERY_ENDconstant 
public static const QUERY_END:String = "endQuery"

The Query.QUERY_END constant defines the value of the type property of the event object for an event that is dispatched after a SQL statement has finish the execution.

QUERY_ERRORconstant 
public static const QUERY_ERROR:String = "errorQuery"

The Query.QUERY_ERROR constant defines the value of the type property of the event object for an event that is dispatched when a SQL error appear on a execute() method.

QUERY_STARTconstant 
public static const QUERY_START:String = "startQuery"

The Query.QUERY_START constant defines the value of the type property of the event object for an event that is dispatched before a SQL statement start execute.

SELECTconstant 
public static const SELECT:String = "select"
UPDATEconstant 
public static const UPDATE:String = "update"