jargon
[ class tree: jargon ] [ index: jargon ] [ all elements ]

Class: Query

Source Location: /jargon/Query.php

Class Overview


Class for representing a SQL query for RETRIEVING results from a database.


Author(s):

Version:

  • $Revision: 1.7 $

Variables

Methods


Child classes:

PagedQuery
Class for representing a SQL query for retrieving paged results from a database.

Class Details

[line 38]
Class for representing a SQL query for RETRIEVING results from a database.

Note that this class is for retrieving results and not performing updates.

Eventually this class may include methods which allow building of queries. Currently this just provides some convenience functions like getRows(), getCol() (based on PEAR::DB methods) and getDataSet().

This class is extended by PagedQuery, a convenience class for handling paged queries.




Tags:

version:  $Revision: 1.7 $
author:  Hans Lellelid <hans@xmpl.org>


[ Top ]


Class Variables

$conn =

[line 41]



Tags:

access:  protected

Type:   Connection


[ Top ]

$max =  0

[line 47]



Tags:

var:  Max rows to return (0 means all)
access:  protected

Type:   int


[ Top ]

$sql =

[line 44]



Tags:

var:  The SQL query.
access:  protected

Type:   string


[ Top ]

$start =  0

[line 50]



Tags:

var:  Start row (offset)
access:  protected

Type:   int


[ Top ]



Class Methods


constructor __construct [line 57]

Query __construct( Connection $conn, [string $sql = null])

Create a new Query.



Tags:

access:  public


Overridden in child classes as:

PagedQuery::__construct()
Create a new Query.

Parameters:

Connection   $conn  
string   $sql  

[ Top ]

method getAssoc [line 210]

array getAssoc( [boolean $scalar = false])

Fetch the entire result set of a query and return it as an associative array using the first column as the key.

Note: column names are not preserved when using this function.

  1. For example, if the table 'mytable' contains:
  2.  
  3. ID TEXT DATE
  4. --------------------------------
  5. 1 'one' 944679408
  6. 2 'two' 944679408
  7. 3 'three' 944679408
  8.  
  9. $q = new Query("SELECT id, text FROM mytable")
  10. $q->getAssoc() returns:
  11. array(
  12. '1' => array('one'),
  13. '2' => array('two'),
  14. '3' => array('three'),
  15. )
  16.  
  17. ... or call $q->getAssoc($scalar=true) to avoid wrapping results in an array (only
  18. applies if only 2 cols are returned):
  19. array(
  20. '1' => 'one',
  21. '2' => 'two',
  22. '3' => 'three',
  23. )
Keep in mind that database functions in PHP usually return string values for results regardless of the database's internal type.




Tags:

return:  Associative array with results from the query.
access:  public
author:  Lukas Smith <smith@backendmedia.com> (MDB)


Parameters:

boolean   $scalar   Used only when the query returns exactly two columns. If TRUE, the values of second column are not wrapped in an array. Default here is false, in order to assure expected behavior.

[ Top ]

method getCol [line 135]

array getCol( )

Gets array of values for first column in result set.



Tags:

return:  string[] Array of values for first column.
access:  public
throws:  SQLException


[ Top ]

method getDataSet [line 256]

QueryDataSet getDataSet( )

Gets a QueryDataSet representing results of this query.

The QueryDataSet that is returned will be ready to use (records will already have been fetched). Currently only QueryDataSets are returned, so you will not be able to manipulate (update/delete/insert) Record objects in returned DataSet.

  1. $q = new Query("SELECT * FROM author");
  2. $q->setLimit(10);
  3. $qds = $q->getDataSet();
  4. foreach($q->getDataSet() as $rec) {
  5. $rec->getValue("name");
  6. }




Tags:

return:  QDS containing the results.
access:  public


[ Top ]

method getOne [line 155]

string getOne( )

Gets value of first column of first returned row.



Tags:

return:  Value for first column in first row.
access:  public
throws:  SQLException


[ Top ]

method getRow [line 117]

array getRow( )

Gets first rows (hash).

Frees resultset.




Tags:

return:  string[] First row.
access:  public
throws:  SQLException


[ Top ]

method getRows [line 96]

array getRows( )

Gets array of rows (hashes).



Tags:

return:  string[][] Array of row hashes.
access:  public
throws:  SQLException


[ Top ]

method setMax [line 86]

void setMax( int $v)

Sets max rows (limit).



Tags:

access:  public


Parameters:

int   $v  

[ Top ]

method setSql [line 67]

void setSql( string $sql)

Sets the SQL we are using.



Tags:

access:  public


Parameters:

string   $sql  

[ Top ]

method setStart [line 76]

void setStart( int $v)

Sets the start row or offset.



Tags:

access:  public


Parameters:

int   $v  

[ Top ]


Documentation generated on Mon, 23 Aug 2004 21:52:51 -0400 by phpDocumentor 1.3.0RC3