public static function castResult($result, array $a, Stub $stub, $isNested) { $a['num rows'] = pg_num_rows($result); $a['status'] = pg_result_status($result); if (isset(self::$resultStatus[$a['status']])) { $a['status'] = new ConstStub(self::$resultStatus[$a['status']], $a['status']); } $a['command-completion tag'] = pg_result_status($result, PGSQL_STATUS_STRING); if (-1 === $a['num rows']) { foreach (self::$diagCodes as $k => $v) { $a['error'][$k] = pg_result_error_field($result, $v); } } $a['affected rows'] = pg_affected_rows($result); $a['last OID'] = pg_last_oid($result); $fields = pg_num_fields($result); for ($i = 0; $i < $fields; ++$i) { $field = array('name' => pg_field_name($result, $i), 'table' => sprintf('%s (OID: %s)', pg_field_table($result, $i), pg_field_table($result, $i, true)), 'type' => sprintf('%s (OID: %s)', pg_field_type($result, $i), pg_field_type_oid($result, $i)), 'nullable' => (bool) pg_field_is_null($result, $i), 'storage' => pg_field_size($result, $i) . ' bytes', 'display' => pg_field_prtlen($result, $i) . ' chars'); if (' (OID: )' === $field['table']) { $field['table'] = null; } if ('-1 bytes' === $field['storage']) { $field['storage'] = 'variable size'; } elseif ('1 bytes' === $field['storage']) { $field['storage'] = '1 byte'; } if ('1 chars' === $field['display']) { $field['display'] = '1 char'; } $a['fields'][] = new EnumStub($field); } return $a; }
/** * Constructor. * * @param resource $resource SQL result resource. * @param TypeConverterFactory $factory * @param array $types Types information, used to convert output values (overrides auto-generated types). * @throws exceptions\InvalidArgumentException */ public function __construct($resource, TypeConverterFactory $factory, array $types = array()) { if (!is_resource($resource) || 'pgsql result' !== get_resource_type($resource)) { throw new exceptions\InvalidArgumentException(sprintf("%s requires a query result resource, '%s' given", __CLASS__, is_resource($resource) ? 'resource(' . get_resource_type($resource) . ')' : gettype($resource))); } $this->_resource = $resource; $this->_converterFactory = $factory; $this->_numRows = pg_num_rows($this->_resource); $this->_numFields = pg_num_fields($this->_resource); $oids = array(); for ($i = 0; $i < $this->_numFields; $i++) { $this->_namesHash[pg_field_name($this->_resource, $i)] = $i; $oids[$i] = pg_field_type_oid($this->_resource, $i); } // first set the explicitly given types... foreach ($types as $index => $type) { $this->setType($index, $type); } // ...then use type factory to create default converters for ($i = 0; $i < $this->_numFields; $i++) { if (!isset($this->_converters[$i])) { $this->_converters[$i] = $this->_converterFactory->getConverter($oids[$i]); } } }
public function __construct($result, $bytea_oid) { $this->result = $result; $this->bytea_oid = $bytea_oid; // find out if there are any blobs $numrows = pg_num_fields($result); for ($i = 0; $i < $numrows; $i++) { $type_oid = pg_field_type_oid($result, $i); if ($type_oid == $this->bytea_oid) { $this->blobs[] = pg_field_name($result, $i); } } $this->current = $this->fetch_next(); }
/** * Get a number of records as an array of objects using a SQL statement. * * Return value is like: * @see function get_records. * * @param string $sql the SQL select query to execute. The first column of this SELECT statement * must be a unique value (usually the 'id' field), as it will be used as the key of the * returned array. * @param array $params array of sql parameters * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set). * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set). * @return array of objects, or empty array if no records were found * @throws dml_exception A DML specific exception is thrown for any errors. */ public function get_records_sql($sql, array $params = null, $limitfrom = 0, $limitnum = 0) { list($limitfrom, $limitnum) = $this->normalise_limit_from_num($limitfrom, $limitnum); if ($limitfrom or $limitnum) { if ($limitnum < 1) { $limitnum = "ALL"; } else { if (PHP_INT_MAX - $limitnum < $limitfrom) { // this is a workaround for weird max int problem $limitnum = "ALL"; } } $sql .= " LIMIT {$limitnum} OFFSET {$limitfrom}"; } list($sql, $params, $type) = $this->fix_sql_params($sql, $params); $this->query_start($sql, $params, SQL_QUERY_SELECT); $result = pg_query_params($this->pgsql, $sql, $params); $this->query_end($result); // find out if there are any blobs $numrows = pg_num_fields($result); $blobs = array(); for ($i = 0; $i < $numrows; $i++) { $type_oid = pg_field_type_oid($result, $i); if ($type_oid == $this->bytea_oid) { $blobs[] = pg_field_name($result, $i); } } $rows = pg_fetch_all($result); pg_free_result($result); $return = array(); if ($rows) { foreach ($rows as $row) { $id = reset($row); if ($blobs) { foreach ($blobs as $blob) { // note: in PostgreSQL 9.0 the returned blobs are hexencoded by default - see http://www.postgresql.org/docs/9.0/static/runtime-config-client.html#GUC-BYTEA-OUTPUT $row[$blob] = $row[$blob] !== null ? pg_unescape_bytea($row[$blob]) : null; } } if (isset($return[$id])) { $colname = key($row); debugging("Did you remember to make the first column something unique in your call to get_records? Duplicate value '{$id}' found in column '{$colname}'.", DEBUG_DEVELOPER); } $return[$id] = (object) $row; } } return $return; }
/** * Get a number of records as an array of objects using a SQL statement. * * Return value as for @see function get_records. * * @param string $sql the SQL select query to execute. The first column of this SELECT statement * must be a unique value (usually the 'id' field), as it will be used as the key of the * returned array. * @param array $params array of sql parameters * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set). * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set). * @return mixed an array of objects, or empty array if no records were found * @throws dml_exception if error */ public function get_records_sql($sql, array $params = null, $limitfrom = 0, $limitnum = 0) { $limitfrom = (int) $limitfrom; $limitnum = (int) $limitnum; $limitfrom = $limitfrom < 0 ? 0 : $limitfrom; $limitnum = $limitnum < 0 ? 0 : $limitnum; if ($limitfrom or $limitnum) { if ($limitnum < 1) { $limitnum = "18446744073709551615"; } $sql .= " LIMIT {$limitnum} OFFSET {$limitfrom}"; } list($sql, $params, $type) = $this->fix_sql_params($sql, $params); $this->query_start($sql, $params, SQL_QUERY_SELECT); $result = pg_query_params($this->pgsql, $sql, $params); $this->query_end($result); // find out if there are any blobs $numrows = pg_num_fields($result); $blobs = array(); for ($i = 0; $i < $numrows; $i++) { $type_oid = pg_field_type_oid($result, $i); if ($type_oid == $this->bytea_oid) { $blobs[] = pg_field_name($result, $i); } } $rows = pg_fetch_all($result); pg_free_result($result); $return = array(); if ($rows) { foreach ($rows as $row) { $id = reset($row); if ($blobs) { foreach ($blobs as $blob) { $row[$blob] = pg_unescape_bytea($row[$blob]); } } if (isset($return[$id])) { $colname = key($row); debugging("Did you remember to make the first column something unique in your call to get_records? Duplicate value '{$id}' found in column '{$colname}'.", DEBUG_DEVELOPER); } $return[$id] = (object) $row; } } return $return; }
public function FieldTypeOID($fieldNo) { return pg_field_type_oid($this->ds, $fieldNo); }
/** * getTypeOid * * Return the type oid of the given field. * * @access public * @param string $name * @throws FoundationException on error * @return int */ public function getTypeOid($name) { $ret = pg_field_type_oid($this->handler, $name); if ($ret === false) { throw new FoundationException(sprintf("Error while fetching type oid for field '%s'.", $name)); } return $ret; }
/** * Constructor * * @param resource $result * @param array $oids */ public function __construct($result, $oids = null) { $this->result = $result; if ($oids) { $this->oidTypeNames = $oids; } $this->rowCount = pg_num_rows($this->result); $this->columnCount = pg_num_fields($this->result); if ($this->columnCount) { for ($i = 0; $i < $this->columnCount; $i++) { $fieldName = pg_field_name($this->result, $i); $oid = pg_field_type_oid($this->result, $i); $this->columnOids[$fieldName] = $oid; $this->columnTypes[$fieldName] = isset($this->oidTypeNames[$oid]) ? $this->oidTypeNames[$oid] : pg_field_type($this->result, $i); } } }