Skip to content

Commit

Permalink
Merge pull request #5981 from kenjis/feat-getFieldData-add-nullable-o…
Browse files Browse the repository at this point in the history
…n-postgre

feat: `getFieldData()` returns nullable data on PostgreSQL
  • Loading branch information
kenjis authored Jun 1, 2022
2 parents c740fbf + cca471a commit 16dfed0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 4 additions & 3 deletions system/Database/Postgre/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ protected function _listColumns(string $table = ''): string
*/
protected function _fieldData(string $table): array
{
$sql = 'SELECT "column_name", "data_type", "character_maximum_length", "numeric_precision", "column_default"
FROM "information_schema"."columns"
WHERE LOWER("table_name") = '
$sql = 'SELECT "column_name", "data_type", "character_maximum_length", "numeric_precision", "column_default", "is_nullable"
FROM "information_schema"."columns"
WHERE LOWER("table_name") = '
. $this->escape(strtolower($table))
. ' ORDER BY "ordinal_position"';

Expand All @@ -252,6 +252,7 @@ protected function _fieldData(string $table): array

$retVal[$i]->name = $query[$i]->column_name;
$retVal[$i]->type = $query[$i]->data_type;
$retVal[$i]->nullable = $query[$i]->is_nullable === 'YES';
$retVal[$i]->default = $query[$i]->column_default;
$retVal[$i]->max_length = $query[$i]->character_maximum_length > 0 ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
}
Expand Down
20 changes: 16 additions & 4 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,31 +835,43 @@ public function testAddFields()

$this->assertNull($fieldsData[0]->default);
$this->assertNull($fieldsData[1]->default);

$this->assertSame(1, (int) $fieldsData[0]->primary_key);

$this->assertSame(255, (int) $fieldsData[1]->max_length);
} elseif ($this->db->DBDriver === 'Postgre') {
$this->assertSame('integer', $fieldsData[0]->type);
$this->assertSame('character varying', $fieldsData[1]->type);

$this->assertFalse($fieldsData[0]->nullable);
$this->assertFalse($fieldsData[1]->nullable);

$this->assertSame(32, (int) $fieldsData[0]->max_length);
$this->assertNull($fieldsData[1]->default);
$this->assertSame(255, (int) $fieldsData[1]->max_length);

$this->assertNull($fieldsData[1]->default);
} elseif ($this->db->DBDriver === 'SQLite3') {
$this->assertSame('integer', strtolower($fieldsData[0]->type));
$this->assertSame('varchar', strtolower($fieldsData[1]->type));

$this->assertNull($fieldsData[1]->default);
} elseif ($this->db->DBDriver === 'SQLSRV') {
$this->assertSame('int', $fieldsData[0]->type);
$this->assertSame(10, (int) $fieldsData[0]->max_length);
$this->assertSame('varchar', $fieldsData[1]->type);
$this->assertNull($fieldsData[1]->default);

$this->assertSame(10, (int) $fieldsData[0]->max_length);
$this->assertSame(255, (int) $fieldsData[1]->max_length);

$this->assertNull($fieldsData[1]->default);
} elseif ($this->db->DBDriver === 'OCI8') {
// Check types
$this->assertSame('NUMBER', $fieldsData[0]->type);
$this->assertSame('VARCHAR2', $fieldsData[1]->type);

$this->assertSame('11', $fieldsData[0]->max_length);
$this->assertSame('', $fieldsData[1]->default);
$this->assertSame('255', $fieldsData[1]->max_length);

$this->assertSame('', $fieldsData[1]->default);
} else {
$this->fail(sprintf('DB driver "%s" is not supported.', $this->db->DBDriver));
}
Expand Down

0 comments on commit 16dfed0

Please sign in to comment.