| Package | phi.db |
| Class | public class Query |
| Inheritance | Query flash.events.EventDispatcher |
| Implements | IQuery |
IQuery implementation.
A Query object assumes these responsibilities:
See also
| Property | Defined by | ||
|---|---|---|---|
| database : IDatabase
[write-only]
| Query | ||
| q : String [write-only]
| Query | ||
| queryEnd : Function [write-only]
| Query | ||
| Records : ArrayCollection | Query | ||
| Method | Defined by | ||
|---|---|---|---|
|
Query()
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 | ||
|
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 | ||
| Event | Summary | Defined 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 | |||
| Constant | Defined 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 | ||
| database | property |
| q | property |
q:String [write-only]Implementation
public function set q(value:String):void
| queryEnd | property |
queryEnd:Function [write-only]Implementation
public function set queryEnd(value:Function):void
| Records | property |
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
| Query | () | constructor |
public function Query()Constructor
| arrayInsert | () | method |
public function arrayInsert(table:String, arr:Array):StringExecute 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.
table:String — the name of the table
|
|
arr:Array — the array that contains the data.
|
String — the SQL generated from array.
|
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):StringExecute 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.
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
|
String — the SQL generated from array.
|
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.
connection:String — the connection name
|
|
db:IDatabase — a IDatabase object
|
See also
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):voidExecte 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.
Parametersq:String — the SQL string
|
|
option:String — leave this on default
|
— Error if there are any SQL errors.
|
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():ConnectionDataGet the current used connection.
ReturnsConnectionData —
the current used connection for execution of SQL statement.
|
| getError | () | method |
public function getError():StringGet the last SQL error.
ReturnsString — the SQL error.
|
| getLastInsertID | () | method |
public function getLastInsertID():NumberGet the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
ReturnsNumber — the ID generated for an AUTO_INCREMENT
|
| getRecords | () | method |
public function getRecords():ArrayCollection
Get the records selected with a previous execute() method.
ArrayCollection — a ArrayCollection with all selected records.
|
| getRow | () | method |
public function getRow():ObjectGet the next row from a previous selected records.
This function can be used to process the selected records.
ReturnsObject — a Object with the row information.
|
......
......
private functin process(q:IQuery):void
{
var row :Object = new Object();
while((row = q.getRow()) != null)
{
row.fname += "_process";
}
}
| isStackEmpty | () | method |
public function isStackEmpty():BooleanReturns
Boolean — true if execute stack is empty.
|
| toString | () | method |
public override function toString():StringReturn the query.
ReturnsString — the query.
|
| endQuery | event |
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.
| errorQuery | event |
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.
| startQuery | event |
flash.events.Event
Dispatched befor starting execute a SQL statement.
The endQuery event is dispatched when the execute()
or arrayInsert() method is called.
| DELETE | constant |
public static const DELETE:String = "delete"
| INSERT | constant |
public static const INSERT:String = "insert"
| QUERY_END | constant |
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_ERROR | constant |
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_START | constant |
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.
| SELECT | constant |
public static const SELECT:String = "select"
| UPDATE | constant |
public static const UPDATE:String = "update"