Skip to content

Commit 82079cb

Browse files
authored
Merge pull request #7301 from kenjis/feat-SQLSRV-getFieldData-nullable
feat: [SQLSRV] getFieldData() supports nullable
2 parents 7c7d789 + a98658b commit 82079cb

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

system/Database/SQLSRV/Connection.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,11 @@ protected function _enableForeignKeyChecks()
341341
*/
342342
protected function _fieldData(string $table): array
343343
{
344-
$sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
345-
FROM INFORMATION_SCHEMA.COLUMNS
346-
WHERE TABLE_NAME= ' . $this->escape(($table));
344+
$sql = 'SELECT
345+
COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION,
346+
COLUMN_DEFAULT, IS_NULLABLE
347+
FROM INFORMATION_SCHEMA.COLUMNS
348+
WHERE TABLE_NAME= ' . $this->escape(($table));
347349

348350
if (($query = $this->query($sql)) === false) {
349351
throw new DatabaseException(lang('Database.failGetFieldData'));
@@ -362,6 +364,8 @@ protected function _fieldData(string $table): array
362364
$retVal[$i]->max_length = $query[$i]->CHARACTER_MAXIMUM_LENGTH > 0
363365
? $query[$i]->CHARACTER_MAXIMUM_LENGTH
364366
: $query[$i]->NUMERIC_PRECISION;
367+
368+
$retVal[$i]->nullable = $query[$i]->IS_NULLABLE !== 'NO';
365369
}
366370

367371
return $retVal;

tests/system/Database/Live/ForgeTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ public function testAddFields()
848848
'name' => [
849849
'type' => 'VARCHAR',
850850
'constraint' => 255,
851+
'null' => true,
851852
],
852853
'active' => [
853854
'type' => 'INTEGER',
@@ -889,7 +890,7 @@ public function testAddFields()
889890
'name' => 'name',
890891
'type' => 'varchar',
891892
'max_length' => 255,
892-
'nullable' => false,
893+
'nullable' => true,
893894
'default' => null,
894895
'primary_key' => 0,
895896
],
@@ -929,7 +930,7 @@ public function testAddFields()
929930
2 => [
930931
'name' => 'name',
931932
'type' => 'character varying',
932-
'nullable' => false,
933+
'nullable' => true,
933934
'default' => null,
934935
'max_length' => '255',
935936
],
@@ -965,7 +966,7 @@ public function testAddFields()
965966
'max_length' => null,
966967
'default' => null,
967968
'primary_key' => false,
968-
'nullable' => false,
969+
'nullable' => true,
969970
],
970971
3 => [
971972
'name' => 'active',
@@ -983,24 +984,28 @@ public function testAddFields()
983984
'type' => 'int',
984985
'default' => null,
985986
'max_length' => 10,
987+
'nullable' => false,
986988
],
987989
1 => [
988990
'name' => 'username',
989991
'type' => 'varchar',
990992
'default' => null,
991993
'max_length' => 255,
994+
'nullable' => false,
992995
],
993996
2 => [
994997
'name' => 'name',
995998
'type' => 'varchar',
996999
'default' => null,
9971000
'max_length' => 255,
1001+
'nullable' => true,
9981002
],
9991003
3 => [
10001004
'name' => 'active',
10011005
'type' => 'int',
10021006
'default' => '((0))', // Why?
10031007
'max_length' => 10,
1008+
'nullable' => false,
10041009
],
10051010
];
10061011
} elseif ($this->db->DBDriver === 'OCI8') {
@@ -1023,8 +1028,8 @@ public function testAddFields()
10231028
'name' => 'name',
10241029
'type' => 'VARCHAR2',
10251030
'max_length' => '255',
1026-
'default' => '',
1027-
'nullable' => false,
1031+
'default' => null,
1032+
'nullable' => true,
10281033
],
10291034
3 => [
10301035
'name' => 'active',

user_guide_src/source/changelogs/v4.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Others
6161

6262
- **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.
6363
See :ref:`Database Configuration <database-configuration-explanation-of-values>`.
64+
- **SQLSRV:** Field Metadata now includes ``nullable``. See :ref:`db-metadata-getfielddata`.
6465

6566
Model
6667
=====

user_guide_src/source/database/metadata.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ performing an action. Returns a boolean true/false. Usage example:
7878
Retrieve Field Metadata
7979
=======================
8080

81+
.. _db-metadata-getfielddata:
82+
8183
$db->getFieldData()
8284
-------------------
8385

@@ -104,9 +106,11 @@ database:
104106
- type - the type of the column
105107
- max_length - maximum length of the column
106108
- 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)
107-
- nullable - boolean ``true`` if the column is nullable, otherwise boolean ``false`` (This field is currently not available in SQL Server)
109+
- nullable - boolean ``true`` if the column is nullable, otherwise boolean ``false``
108110
- default - the default value
109111

112+
.. note:: Since v4.4.0, SQLSRV supported ``nullable``.
113+
110114
List the Indexes in a Table
111115
===========================
112116

0 commit comments

Comments
 (0)