The second argument is an ***array of parameters*** to execute the prepared statement with.
-If the third argument is present, an ***array with pg_type OIDs***, those types will be used for the parameters. See [Using types](pq/: Using Types) for details.
+If the third argument is present, an ***array with pg_type OIDs***, those types will be used for the parameters. See [Using types](pq/Types/: Overview) for details.
<?php
$result = $c->execParams("SELECT int($1) * s, int($2) * s, int($3) * s FROM generate_series(1,3) s", array(1,2,3));
## Explicit prepare & execute of a named statement
-pq\Connection::prepare() requires the ***statement name*** as string as first argument. This name is used later to refer to this prepared statement. See [Prepared statements](pq/: Prepared Statements) for details.
+pq\Connection::prepare() requires the ***statement name*** as string as first argument. This name is used later to refer to this prepared statement. See [Prepared statements](pq/Statement) for details.
The second argument is a ***query string*** containing a single SQL query, which will be prepared on the server.
-If the third argument is present, an ***array with pg_type OIDs***, those types will be used for the parameters. See [Using types](pq/: Using Types) for details.
+If the third argument is present, an ***array with pg_type OIDs***, those types will be used for the parameters. See [Using types](pq/Types/: Overview) for details.
<?php
?>
-An object of class pq\Statement will be returned on success. See [Prepared statements](pq/: Prepared Statements) for details.
+An object of class pq\Statement will be returned on success. See [Prepared statements](pq/Statement) for details.
-# pq\Types AKA pg_type #
+# pq\Types AKA pg_type
-The ```pq\Types``` class provides an easy interface to access information stored in PostgreSQL's pg_type relation, f.e. type OIDs and names.
+The pq\Types class provides an easy interface to access information stored in PostgreSQL's pg_type relation, f.e. type OIDs and names.
-The first argument to the ```pq\Types``` constructor must be an ***instance*** of ```pq\Connection```.
-An ***optional array of namespace names*** is expected as second argument, where 'public' and 'pg_catalog' are queried by default if no namespaces are specified.
+The first argument to the pq\Types constructor must be an instance of pq\Connection.
+An optional array of namespace names is expected as second argument, where 'public' and 'pg_catalog' are queried by default if no namespaces are specified.
-```
-#!php
-<?php
-$types = new pq\Types(new pq\Connection);
-```
+ <?php
+
+ $types = new pq\Types(new pq\Connection);
+
+ ?>
The types are standard objects indexed by OID and by name and accessible by array key/index:
-```
-#!php
-<?php
-$text = $types["text"];
-$text = $types[25];
-printf("TEXT type (oid=%d, name=%s)\n", $text->oid, $text->typname);
-```
+ <?php
+
+ $text = $types["text"];
+ $text = $types[25];
+
+ printf("TEXT type (oid=%d, name=%s)\n", $text->oid, $text->typname);
+
+ ?>
-> ***NOTE:***
-> The pg_type relation fields have a ``typ`` prefix.
+> ***NOTE:***
+ The pg_type relation fields have a ``typ`` prefix.
-## Usage when [executing queries](QueryExecution) ##
+## Usage when [executing queries](pq/Connection/: Executing Queries)
-```
-#!php
-<?php
-$connection = new pq\Connection;
-$types = new pq\Types($connection);
-$result = $connection->execParams("SELECT \$1 + \$2", array(10, 20),
- array($types["int4"]->oid, $types["int4"]->oid));
-```
+
+ <?php
+
+ $connection = new pq\Connection;
+ $types = new pq\Types($connection);
+ $result = $connection->execParams(
+ "SELECT \$1 + \$2",
+ array(10, 20),
+ array($types["int4"]->oid, $types["int4"]->oid));
+
+ ?>
You can pass a type OID for each parameter of a pepared statement. The PostgreSQL server will try to infer a type by context for any parameters for which no type OID was specified.
-## Static types ##
+## Static types
-When ext-pq has been built with PostgreSQL server headers (*) present, then ```pq\Types``` has class constants for the standard types' oids already defined.
+pq\Types has class constants for the standard types' OIDs already defined.
-```
-#!php
-<?php
-$connection = new pq\Connection;
-$result = $connection->execParams("SELECT \$1 + \$2", array(10, 20),
- array(pq\Types::INT4, pq\Types::INT4));
-```
+ <?php
+ $connection = new pq\Connection;
+ $result = $connection->execParams("SELECT \$1 + \$2",
+ array(10, 20),
+ array(pq\Types::INT4, pq\Types::INT4));
-> ***NOTE:***
-> You can test for static type support with ```pq\Types::DEFINED === true```
-(*) ```include/postgresql/server/catalog/pg_type.h```
+### pq\Datetime
-### pq\Datetime ###
-With static type support, date(time) values will automatically be converted to ```pq\Datetime``` objects.
+Date/time values will automatically be converted to pq\Datetime objects.
-```
-#!php
-<?php
+ <?php
-$conn = new pq\Connection();
-$data = $conn->exec("
- SELECT
- NOW()::date
- ,NOW()::abstime
- ,NOW()::timestamp
- ,NOW()::timestamptz
-")->fetchRow(pq\Result::FETCH_ARRAY);
+ $conn = new pq\Connection();
+ $data = $conn->exec("
+ SELECT
+ NOW()::date
+ ,NOW()::abstime
+ ,NOW()::timestamp
+ ,NOW()::timestamptz
+ ")->fetchRow(pq\Result::FETCH_ARRAY);
-foreach ($data as $datetime) {
- printf("%-40s (%s)\n", $datetime, $datetime->format);
-}
-```
+ foreach ($data as $datetime) {
+ printf("%-40s (%s)\n", $datetime, $datetime->format);
+ }
+
+ ?>
Outputs:
-```
-2013-09-20 (Y-m-d)
-2013-09-20 14:34:38 (Y-m-d H:i:s)
-2013-09-20 14:34:38.494886 (Y-m-d H:i:s.u)
-2013-09-20 14:34:38.494886+0200 (Y-m-d H:i:s.uO)
-```
-## Custom converters ##
+ 2013-09-20 (Y-m-d)
+ 2013-09-20 14:34:38 (Y-m-d H:i:s)
+ 2013-09-20 14:34:38.494886 (Y-m-d H:i:s.u)
+ 2013-09-20 14:34:38.494886+0200 (Y-m-d H:i:s.uO)
-ext-pq provides an interface called ```pq\Converter``` which one can implement to perform custom type conversions in a transparent manner. Consider the following naive example converting ```HSTORE``` columns on the fly:
+## Custom converters
-```
-#!php
-<?php
+ext-pq provides the interface pq\Converter which one can implement to perform custom type conversions in a transparent manner. Consider the following naive example converting `HSTORE` columns on the fly:
-class Hstore implements pq\Converter
-{
- private $oid;
-
- function __construct(pq\Types $types) {
- $this->oid = $types["hstore"]->oid;
- }
-
- function convertTypes() {
- return [$this->oid];
- }
-
- function convertFromString($string) {
- return eval("return [$string];");
- }
-
- function convertToString($data) {
- $string = "";
- foreach ($data as $k => $v) {
- $string .= "\"".addslashes($k)."\"=>";
- if (isset($v)) {
- $string .= "\"".addslashes($v)."\",";
- } else {
- $string .= "NULL,";
+ <?php
+
+ class Hstore implements pq\Converter
+ {
+ private $oid;
+
+ function __construct(pq\Types $types) {
+ $this->oid = $types["hstore"]->oid;
+ }
+
+ function convertTypes() {
+ return [$this->oid];
+ }
+
+ function convertFromString($string, $type) {
+ return eval("return [$string];");
+ }
+
+ function convertToString($data, $type) {
+ $string = "";
+ foreach ($data as $k => $v) {
+ $string .= "\"".addslashes($k)."\"=>";
+ if (isset($v)) {
+ $string .= "\"".addslashes($v)."\",";
+ } else {
+ $string .= "NULL,";
+ }
}
+ return $string;
}
- return $string;
}
-}
-$conn = new pq\Connection();
-$type = new pq\Types($conn);
-$conv = new Hstore($type);
-$conn->setConverter($conv);
-$conn->exec("SELECT '\"foo\"=>\"bar\"'::hstore hs")->fetchCol(0, $data);
+ $conn = new pq\Connection();
+ $type = new pq\Types($conn);
+ $conv = new Hstore($type);
+ $conn->setConverter($conv);
+ $conn->exec("SELECT '\"foo\"=>\"bar\"'::hstore hs")->fetchCol(0, $data);
-print_r($data);
+ print_r($data);
-```
+ ?>
Output would be the following:
-```
-Array
-(
- [foo] => bar
-)
-```
+
+ Array
+ (
+ [foo] => bar
+ )