mirror of
https://github.com/danog/postgres.git
synced 2024-12-02 09:27:54 +01:00
Fix typing in result sets with duplicate column names
This commit is contained in:
parent
67b821343f
commit
2916fa161c
@ -21,6 +21,9 @@ class PgSqlResultSet implements ResultSet {
|
|||||||
/** @var int[] */
|
/** @var int[] */
|
||||||
private $fieldTypes = [];
|
private $fieldTypes = [];
|
||||||
|
|
||||||
|
/** @var string[] */
|
||||||
|
private $fieldNames = [];
|
||||||
|
|
||||||
/** @var \Amp\Postgres\Internal\ArrayParser */
|
/** @var \Amp\Postgres\Internal\ArrayParser */
|
||||||
private $parser;
|
private $parser;
|
||||||
|
|
||||||
@ -32,6 +35,7 @@ class PgSqlResultSet implements ResultSet {
|
|||||||
|
|
||||||
$numFields = \pg_num_fields($this->handle);
|
$numFields = \pg_num_fields($this->handle);
|
||||||
for ($i = 0; $i < $numFields; ++$i) {
|
for ($i = 0; $i < $numFields; ++$i) {
|
||||||
|
$this->fieldNames[] = \pg_field_name($this->handle, $i);
|
||||||
$this->fieldTypes[] = \pg_field_type_oid($this->handle, $i);
|
$this->fieldTypes[] = \pg_field_type_oid($this->handle, $i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +75,7 @@ class PgSqlResultSet implements ResultSet {
|
|||||||
throw new \Error("No more rows remain in the result set");
|
throw new \Error("No more rows remain in the result set");
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = \pg_fetch_array($this->handle, null, \PGSQL_ASSOC);
|
$result = \pg_fetch_array($this->handle, null, \PGSQL_NUM);
|
||||||
|
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
$message = \pg_result_error($this->handle);
|
$message = \pg_result_error($this->handle);
|
||||||
@ -165,7 +169,7 @@ class PgSqlResultSet implements ResultSet {
|
|||||||
case 1231: // numeric[]
|
case 1231: // numeric[]
|
||||||
case 1263: // cstring[]
|
case 1263: // cstring[]
|
||||||
case 1270: // timetz[]
|
case 1270: // timetz[]
|
||||||
case 1561: // bit
|
case 1561: // bit[]
|
||||||
case 1563: // varbit[]
|
case 1563: // varbit[]
|
||||||
case 2201: // refcursor[]
|
case 2201: // refcursor[]
|
||||||
case 2207: // regprocedure[]
|
case 2207: // regprocedure[]
|
||||||
@ -188,8 +192,8 @@ class PgSqlResultSet implements ResultSet {
|
|||||||
case 3911: // tstzrange[]
|
case 3911: // tstzrange[]
|
||||||
case 3913: // daterange[]
|
case 3913: // daterange[]
|
||||||
case 3927: // int8range[]
|
case 3927: // int8range[]
|
||||||
case 4097: // regrole[]
|
|
||||||
case 4090: // regnamespace[]
|
case 4090: // regnamespace[]
|
||||||
|
case 4097: // regrole[]
|
||||||
$result[$key] = $this->parser->parse($result[$key]);
|
$result[$key] = $this->parser->parse($result[$key]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -197,16 +201,24 @@ class PgSqlResultSet implements ResultSet {
|
|||||||
++$column;
|
++$column;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($this->type) {
|
if ($this->type === self::FETCH_ARRAY) {
|
||||||
case self::FETCH_ASSOC:
|
return $this->currentRow = $result;
|
||||||
return $this->currentRow = $result;
|
|
||||||
case self::FETCH_ARRAY:
|
|
||||||
return $this->currentRow = \array_values($result);
|
|
||||||
case self::FETCH_OBJECT:
|
|
||||||
return $this->currentRow = (object) $result;
|
|
||||||
default:
|
|
||||||
throw new \Error("Invalid result fetch type");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$assoc = [];
|
||||||
|
foreach ($this->fieldNames as $index => $name) {
|
||||||
|
$assoc[$name] = $result[$index];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->type === self::FETCH_ASSOC) {
|
||||||
|
return $this->currentRow = $assoc;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->type === self::FETCH_OBJECT) {
|
||||||
|
return $this->currentRow = (object) $assoc;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \Error("Invalid result fetch type");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -231,7 +243,11 @@ class PgSqlResultSet implements ResultSet {
|
|||||||
* @throws \Error If the field number does not exist in the result.
|
* @throws \Error If the field number does not exist in the result.
|
||||||
*/
|
*/
|
||||||
public function fieldName(int $fieldNum): string {
|
public function fieldName(int $fieldNum): string {
|
||||||
return \pg_field_name($this->handle, $this->filterNameOrNum($fieldNum));
|
if (0 > $fieldNum || $this->numFields() <= $fieldNum) {
|
||||||
|
throw new \Error(\sprintf('No field with index %d in result', $fieldNum));
|
||||||
|
}
|
||||||
|
|
||||||
|
return \pg_field_name($this->handle, $fieldNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -252,6 +268,8 @@ class PgSqlResultSet implements ResultSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated \pg_field_type() performs a blocking query, so this method will be removed in a future version.
|
||||||
|
*
|
||||||
* @param int|string $fieldNameOrNum Field name or index.
|
* @param int|string $fieldNameOrNum Field name or index.
|
||||||
*
|
*
|
||||||
* @return string Name of the field type.
|
* @return string Name of the field type.
|
||||||
@ -263,6 +281,8 @@ class PgSqlResultSet implements ResultSet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated Will be removed in a future version.
|
||||||
|
*
|
||||||
* @param int|string $fieldNameOrNum Field name or index.
|
* @param int|string $fieldNameOrNum Field name or index.
|
||||||
*
|
*
|
||||||
* @return int Storage required for field. -1 indicates a variable length field.
|
* @return int Storage required for field. -1 indicates a variable length field.
|
||||||
|
Loading…
Reference in New Issue
Block a user