SourceForge.net LogoPHPGallue
[ class tree: libbase ] [ index: libbase ] [ all elements ]

Class: GALExpressionList

Source Location: /libbase/table.inc.php

Class Overview

GALExpression
   |
   --GALExpressionList

Creates SQL strings that check if a set of fields equals to one tuple of the passed list of value tuples.


Methods


Inherited Methods

Class: GALExpression

GALExpression::eand()
Connect two or more statements with AND.
GALExpression::enot()
Invert this logical expression.
GALExpression::eor()
Connect two or more statements with OR.
GALExpression::__toString()
String representation of this logical expression.

Class Details

[line 168]
Creates SQL strings that check if a set of fields equals to one tuple of the passed list of value tuples.

This class generates SQL snippets that check if one or more fields have a value constellation that equals to one of the given constant value constellations. It is introduced to allow specifying a list of values (or value tuples), for example a list of IDs to return exactly the rows where the specified field match one of the given IDs. If a table's primary key consists of more than one field, one had to write the "... ) OR (`key1`='IDn' AND `key2`='IDm') OR (`key1`='IDn+1' AND `key2`='IDm+1') OR (..." strings by hand which are generated if more than one field is given. I know, this is rather complicated, but writing this every time you need it is even more annoying than this class. Note that escaping and quoting is done automatically - you can pass whatever data you want as field names, table names or values without risking SQL injection (provided there are no bugs...). If you want to check a single field, pass the field name in the constructor as string and pass the values as one-dimensional array. If you want to check multiple fields, pass the field names as one-dimensional array and the values as an array of arrays, where the child array's element counts match the number of fields. The first value of a child array is compared to the first field, the second to the second and so on. This look like this:

  1.  $db new GALDatabase(...);
  2.  $table "myTable";
  3.  $fields array("field1""field2");
  4.  $values array(
  5.            array(12),
  6.                array(420)
  7.            );
  8.  $compare new GALExpressionList($db$table$fields$values);
  9.  echo $compare->__toString();
This will print ((`myTable`.`field1`='1' AND `myTable`.`field2`='2') OR (`myTable`.`field1`='42' AND `myTable`.`field2`='0')) whereas
  1.  $db new GALDatabase(...);
  2.  $table "myTable";
  3.  $field "field"
  4.  $values array('a''b''c');
  5.  $compare new GALExpressionList($db$table$field$values);
  6.  echo $compare->__toString();
will print (`myTable`.`field` IN ('a', 'b', 'c')). Note: You can also pass a single field as array with one component and the values as array of arrays with one component. This leads to the same result as the second example. If there are not enough or too many values in a second-level array, this leads to a GAL_EExpression.




[ Top ]


Class Methods


constructor __construct [line 195]

GALExpressionList __construct( GALDatabase $database, mixed $table, mixed $fields, array $values)

Construct with database information, list of fields and list of values.

The table can be given as string, in this case a GALTable object is constructed from this string by calling GALDatabase::table().




Tags:

access:  public


Parameters:

GALDatabase   $database   A database connection.
mixed   $table   The referenced table, either GALTable or string.
mixed   $fields   The names of the compared fields, either array or string.
array   $values   The values the fields are compared with, either array or array of arrays.

[ Top ]

method __toString [line 258]

string __toString( )

SQL string representation of the expression.



Tags:

access:  public


Overrides GALExpression::__toString() (String representation of this logical expression.)

[ Top ]


Documentation generated on Wed, 16 Aug 2006 15:36:45 +0200 by phpDocumentor 1.3.0RC6