[GH-ISSUE #11] NULL values are not handled correctly in INSERT and UPDATE statemens #5

Closed
opened 2026-02-25 21:33:00 +03:00 by kerem · 2 comments
Owner

Originally created by @cinzko on GitHub (Jun 4, 2016).
Original GitHub issue: https://github.com/DBDiff/DBDiff/issues/11

When a column contains NULL values the INSERT statement always genereates quotes with an empty string.
This can be fixed in "\DBDiff\SQLGen\DiffToSQL\InsertDataSQL"
by replacing

        $values = array_map(function ($el) {
            return "'".addslashes($el)."'";
        }, $values);

with

        $values = array_map(function ($el) {
            if(!is_null($el)) {
                return "'" . addslashes($el) . "'";
            }
            else {
                return 'NULL';
            }
        }, $values);

and also in "\DBDiff\SQLGen\DiffToSQL\UpdateDataSQL" by replacing

        array_walk($values, function(&$diff, $column) {
            $diff = '`'.$column."` = '".addslashes($diff->getNewValue())."'";
        });

with

        array_walk($values, function(&$diff, $column) {
            if(!is_null($diff->getNewValue())) {
                $diff = '`' . $column . "` = '" . addslashes($diff->getNewValue()) . "'";
            }
            else {
                $diff = '`' . $column . "` = NULL";
            }
        });

Best regards,
Christian
Originally created by @cinzko on GitHub (Jun 4, 2016). Original GitHub issue: https://github.com/DBDiff/DBDiff/issues/11 When a column contains NULL values the INSERT statement always genereates quotes with an empty string. This can be fixed in "\DBDiff\SQLGen\DiffToSQL\InsertDataSQL" by replacing ``` $values = array_map(function ($el) { return "'".addslashes($el)."'"; }, $values); ``` with ``` $values = array_map(function ($el) { if(!is_null($el)) { return "'" . addslashes($el) . "'"; } else { return 'NULL'; } }, $values); ``` and also in "\DBDiff\SQLGen\DiffToSQL\UpdateDataSQL" by replacing ``` array_walk($values, function(&$diff, $column) { $diff = '`'.$column."` = '".addslashes($diff->getNewValue())."'"; }); ``` with ``` array_walk($values, function(&$diff, $column) { if(!is_null($diff->getNewValue())) { $diff = '`' . $column . "` = '" . addslashes($diff->getNewValue()) . "'"; } else { $diff = '`' . $column . "` = NULL"; } }); Best regards, Christian ```
kerem closed this issue 2026-02-25 21:33:00 +03:00
Author
Owner

@hawle commented on GitHub (Jul 7, 2016):

+1 please merge!!

<!-- gh-comment-id:231137920 --> @hawle commented on GitHub (Jul 7, 2016): +1 please merge!!
Author
Owner

@jasdeepkhalsa commented on GitHub (Jul 8, 2016):

Fixed in https://github.com/DBDiff/DBDiff/pull/12

<!-- gh-comment-id:231307558 --> @jasdeepkhalsa commented on GitHub (Jul 8, 2016): Fixed in https://github.com/DBDiff/DBDiff/pull/12
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/DBDiff#5
No description provided.