Exemple #1
0
 /**
  * Execute a query and return the result
  *
  * Executes a query with optional parameters (which are automatically
  * escaped). If a name is provided, then the query will be prepared
  * and saved if necessary (or simply executed if it was already
  * prepared). Unnamed queries are prepared and executed each time they
  * are called. Note that the second and later times a named statement
  * is called, the query string is disregarded (as the statement is
  * already prepared). There is no way to reallocate a name for a
  * query.
  *
  * @param string $str The query string to execute
  * @param array $params The query parameters
  * @param string $name The name of the query
  * @throws DatabaseException
  * @return resource
  */
 public static function query($str, $params = array(), $name = null)
 {
     $p =& static::$prepared;
     if ($name && !array_key_exists($name, $p) || !$name) {
         if ($name) {
             $p[$name] = true;
         } else {
             $name = '';
         }
         error_log("Preparing query ({$name}): {$str}");
         $prepped = pg_prepare(Database::connect(), $name, $str);
         if ($prepped === false) {
             $pgerr = pg_last_error();
             if ($name) {
                 $msg = "Could not prepare query {$name}: {$pgerr}";
             } else {
                 $msg = "Could not prepare query: {$pgerr}";
             }
             throw new DatabaseException($msg);
         }
     }
     error_log("Executing query {$name}");
     $resource = pg_execute(Database::connect(), $name, $params);
     if ($resource === false) {
         $pgerr = pg_last_error();
         if ($name) {
             $msg = "Could not execute query {$name}: {$pgerr}";
         } else {
             $msg = "Could not execute query: {$pgerr}";
         }
         throw new DatabaseException($msg);
     }
     return $resource;
 }