Skip to content

Commit

Permalink
Merge pull request #7301 from kenjis/feat-SQLSRV-getFieldData-nullable
Browse files Browse the repository at this point in the history
feat: [SQLSRV] getFieldData() supports nullable
  • Loading branch information
kenjis authored Mar 4, 2023
2 parents 7c7d789 + a98658b commit 82079cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
10 changes: 7 additions & 3 deletions system/Database/SQLSRV/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,11 @@ protected function _enableForeignKeyChecks()
*/
protected function _fieldData(string $table): array
{
$sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME= ' . $this->escape(($table));
$sql = 'SELECT
COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION,
COLUMN_DEFAULT, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME= ' . $this->escape(($table));

if (($query = $this->query($sql)) === false) {
throw new DatabaseException(lang('Database.failGetFieldData'));
Expand All @@ -362,6 +364,8 @@ protected function _fieldData(string $table): array
$retVal[$i]->max_length = $query[$i]->CHARACTER_MAXIMUM_LENGTH > 0
? $query[$i]->CHARACTER_MAXIMUM_LENGTH
: $query[$i]->NUMERIC_PRECISION;

$retVal[$i]->nullable = $query[$i]->IS_NULLABLE !== 'NO';
}

return $retVal;
Expand Down
15 changes: 10 additions & 5 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ public function testAddFields()
'name' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => true,
],
'active' => [
'type' => 'INTEGER',
Expand Down Expand Up @@ -889,7 +890,7 @@ public function testAddFields()
'name' => 'name',
'type' => 'varchar',
'max_length' => 255,
'nullable' => false,
'nullable' => true,
'default' => null,
'primary_key' => 0,
],
Expand Down Expand Up @@ -929,7 +930,7 @@ public function testAddFields()
2 => [
'name' => 'name',
'type' => 'character varying',
'nullable' => false,
'nullable' => true,
'default' => null,
'max_length' => '255',
],
Expand Down Expand Up @@ -965,7 +966,7 @@ public function testAddFields()
'max_length' => null,
'default' => null,
'primary_key' => false,
'nullable' => false,
'nullable' => true,
],
3 => [
'name' => 'active',
Expand All @@ -983,24 +984,28 @@ public function testAddFields()
'type' => 'int',
'default' => null,
'max_length' => 10,
'nullable' => false,
],
1 => [
'name' => 'username',
'type' => 'varchar',
'default' => null,
'max_length' => 255,
'nullable' => false,
],
2 => [
'name' => 'name',
'type' => 'varchar',
'default' => null,
'max_length' => 255,
'nullable' => true,
],
3 => [
'name' => 'active',
'type' => 'int',
'default' => '((0))', // Why?
'max_length' => 10,
'nullable' => false,
],
];
} elseif ($this->db->DBDriver === 'OCI8') {
Expand All @@ -1023,8 +1028,8 @@ public function testAddFields()
'name' => 'name',
'type' => 'VARCHAR2',
'max_length' => '255',
'default' => '',
'nullable' => false,
'default' => null,
'nullable' => true,
],
3 => [
'name' => 'active',
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Others

- **MySQLi:** Added the ``numberNative`` attribute to the Database Config to keep the variable type obtained after SQL Query consistent with the type set in the database.
See :ref:`Database Configuration <database-configuration-explanation-of-values>`.
- **SQLSRV:** Field Metadata now includes ``nullable``. See :ref:`db-metadata-getfielddata`.

Model
=====
Expand Down
6 changes: 5 additions & 1 deletion user_guide_src/source/database/metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ performing an action. Returns a boolean true/false. Usage example:
Retrieve Field Metadata
=======================

.. _db-metadata-getfielddata:

$db->getFieldData()
-------------------

Expand All @@ -104,9 +106,11 @@ database:
- type - the type of the column
- max_length - maximum length of the column
- primary_key - integer ``1`` if the column is a primary key (all integer ``1``, even if there are multiple primary keys), otherwise integer ``0`` (This field is currently only available for MySQL and SQLite3)
- nullable - boolean ``true`` if the column is nullable, otherwise boolean ``false`` (This field is currently not available in SQL Server)
- nullable - boolean ``true`` if the column is nullable, otherwise boolean ``false``
- default - the default value

.. note:: Since v4.4.0, SQLSRV supported ``nullable``.

List the Indexes in a Table
===========================

Expand Down

0 comments on commit 82079cb

Please sign in to comment.