From 95ab4b3e931521ce3c56068478311f0c04f713cc Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 25 Jun 2014 13:40:32 +0200 Subject: [PATCH 1/3] [ticket/12448] Allow null as default value for columns PHPBB3-12448 --- phpBB/phpbb/db/tools.php | 31 +++++++++++++++++++++++++------ tests/dbal/db_tools_test.php | 3 +++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/phpBB/phpbb/db/tools.php b/phpBB/phpbb/db/tools.php index 2ec46ed239..874fe4dc11 100644 --- a/phpBB/phpbb/db/tools.php +++ b/phpBB/phpbb/db/tools.php @@ -1487,8 +1487,16 @@ class tools $return_array['textimage'] = $column_type === '[text]'; - $sql .= 'NOT NULL'; - $sql_default .= 'NOT NULL'; + if (!is_null($column_data[1])) + { + $sql .= 'NOT NULL'; + $sql_default .= 'NOT NULL'; + } + else + { + $sql .= 'NULL'; + $sql_default .= 'NULL'; + } $return_array['column_type_sql_default'] = $sql_default; @@ -1503,7 +1511,15 @@ class tools { $sql .= (strpos($column_data[1], '0x') === 0) ? "DEFAULT {$column_data[1]} " : "DEFAULT '{$column_data[1]}' "; } - $sql .= 'NOT NULL'; + + if (!is_null($column_data[1])) + { + $sql .= 'NOT NULL'; + } + else + { + $sql .= 'NULL'; + } if (isset($column_data[2])) { @@ -1528,7 +1544,7 @@ class tools // Oracle does not like setting NOT NULL on a column that is already NOT NULL (this happens only on number fields) if (!preg_match('/number/i', $column_type)) { - $sql .= ($column_data[1] === '') ? '' : 'NOT NULL'; + $sql .= ($column_data[1] === '' || $column_data[1] === null) ? '' : 'NOT NULL'; } $return_array['auto_increment'] = false; @@ -1588,8 +1604,11 @@ class tools $sql .= ' ' . $column_type; } - $sql .= ' NOT NULL '; - $sql .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}'" : ''; + if (!is_null($column_data[1])) + { + $sql .= ' NOT NULL '; + $sql .= "DEFAULT '{$column_data[1]}'"; + } break; } diff --git a/tests/dbal/db_tools_test.php b/tests/dbal/db_tools_test.php index f3c6888c8d..6cc2f8ec0f 100644 --- a/tests/dbal/db_tools_test.php +++ b/tests/dbal/db_tools_test.php @@ -46,6 +46,7 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case 'c_bool' => array('BOOL', 1), 'c_vchar' => array('VCHAR', 'foo'), 'c_vchar_size' => array('VCHAR:4', 'foo'), + 'c_vchar_null' => array('VCHAR', null), 'c_char_size' => array('CHAR:4', 'foo'), 'c_xstext' => array('XSTEXT', 'foo'), 'c_stext' => array('STEXT', 'foo'), @@ -111,6 +112,7 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case 'c_bool' => 0, 'c_vchar' => '', 'c_vchar_size' => '', + 'c_vchar_null' => null, 'c_char_size' => 'abcd', 'c_xstext' => '', 'c_stext' => '', @@ -144,6 +146,7 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case array('c_bool', 0), array('c_vchar', str_repeat('a', 255)), array('c_vchar_size', str_repeat('a', 4)), + array('c_vchar_null', str_repeat('a', 4)), array('c_char_size', str_repeat('a', 4)), array('c_xstext', str_repeat('a', 1000)), array('c_stext', str_repeat('a', 3000)), From b2044884ffab5e32fa1870ec68fe956a7cb05e1e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 25 Jun 2014 18:37:46 +0200 Subject: [PATCH 2/3] [ticket/12448] Fix null columns for postgres PHPBB3-12448 --- phpBB/phpbb/db/tools.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpBB/phpbb/db/tools.php b/phpBB/phpbb/db/tools.php index 874fe4dc11..54075b464c 100644 --- a/phpBB/phpbb/db/tools.php +++ b/phpBB/phpbb/db/tools.php @@ -1572,6 +1572,12 @@ class tools $return_array['null'] = 'NOT NULL'; $sql .= 'NOT NULL '; } + else + { + $default_val = "'" . $column_data[1] . "'"; + $return_array['null'] = 'NULL'; + $sql .= 'NULL '; + } $return_array['default'] = $default_val; From 4917ebe93a7490d814848f2ce6a5c80343599dcf Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 25 Jun 2014 18:39:10 +0200 Subject: [PATCH 3/3] [ticket/12448] Fix null columns for MS SQL PHPBB3-12448 --- phpBB/phpbb/db/tools.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/tools.php b/phpBB/phpbb/db/tools.php index 54075b464c..bafd547917 100644 --- a/phpBB/phpbb/db/tools.php +++ b/phpBB/phpbb/db/tools.php @@ -1487,7 +1487,7 @@ class tools $return_array['textimage'] = $column_type === '[text]'; - if (!is_null($column_data[1])) + if (!is_null($column_data[1]) || (isset($column_data[2]) && $column_data[2] == 'auto_increment')) { $sql .= 'NOT NULL'; $sql_default .= 'NOT NULL';