]> granicus.if.org Git - php/commitdiff
Explicitly allow NULL values for dblib compatibility
authorAdam Baratz <adambaratz@php.net>
Mon, 9 Jan 2017 16:13:13 +0000 (11:13 -0500)
committerAdam Baratz <adambaratz@php.net>
Mon, 9 Jan 2017 16:40:25 +0000 (11:40 -0500)
MSSQL won't necessarily default columns to NULL, see:
https://msdn.microsoft.com/en-us/library/ms174979.aspx#Nullability Rules Within a Table Definition

ext/pdo/tests/pdo_018.phpt
ext/pdo/tests/pdo_024.phpt

index 57430ae20db9e1060905a796ed683a4a37eb84be..f656d085c63ed7150b6a929ea883c11332a5f15b 100644 (file)
@@ -71,7 +71,18 @@ $db->exec('CREATE TABLE classtypes(id int NOT NULL PRIMARY KEY, name VARCHAR(20)
 $db->exec('INSERT INTO classtypes VALUES(0, \'stdClass\')'); 
 $db->exec('INSERT INTO classtypes VALUES(1, \'TestBase\')'); 
 $db->exec('INSERT INTO classtypes VALUES(2, \'TestDerived\')'); 
-$db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, classtype int, val VARCHAR(255))');
+
+switch ($db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
+  case 'dblib':
+    // environment settings can influence how the table is created if specifics are missing
+    // https://msdn.microsoft.com/en-us/library/ms174979.aspx#Nullability Rules Within a Table Definition
+    $sql = 'CREATE TABLE test(id int NOT NULL PRIMARY KEY, classtype int NULL, val VARCHAR(255) NULL)';
+    break;
+  default:
+    $sql = 'CREATE TABLE test(id int NOT NULL PRIMARY KEY, classtype int, val VARCHAR(255))';
+    break;
+}
+$db->exec($sql);
 
 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
index a70e924d49a8efced6293e2e93cebe8ddc275c98..5f4baf243dfc532e9985d82691067895dbd79b0c 100644 (file)
@@ -14,7 +14,17 @@ if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE_
 require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
 $db = PDOTest::factory();
 
-$db->exec('create table test (id int, name varchar(10) null)');
+switch ($db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
+       case 'dblib':
+               // environment settings can influence how the table is created if specifics are missing
+               // https://msdn.microsoft.com/en-us/library/ms174979.aspx#Nullability Rules Within a Table Definition
+               $sql = 'create table test (id int, name varchar(10) null)';
+               break;
+       default:
+               $sql = 'create table test (id int, name varchar(10))';
+               break;
+}
+$db->exec($sql);
 
 $stmt = $db->prepare('insert into test (id, name) values(0, :name)');
 $name = NULL;