Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in Database_Query_Insert: bad SQL for bulk insert #100

Open
rafis opened this issue Nov 5, 2015 · 2 comments
Open

Bug in Database_Query_Insert: bad SQL for bulk insert #100

rafis opened this issue Nov 5, 2015 · 2 comments

Comments

@rafis
Copy link

rafis commented Nov 5, 2015

There is two related bugs in Database_Query_Insert.

First bug is here:
https://github.com/kohana/database/blob/3.3/master/classes/Kohana/Database/Query/Builder/Insert.php#L91

func_get_args returns array of arguments, but we except every argument to be array, we need to flattern this array by one level. This is how I would fix it:

    ...
    // Get all of the passed values
    $values = array();
    $args_values = func_get_args();
    foreach($args_values as $i => $arg_values)
    {
        if ( ! is_array($arg_values))
        {
            throw new InvalidArgumentException('Argument ' . $i . ' must be array, got ' . gettype($arg_values));
        }
    }
    foreach($args_values as $arg_values)
    {
        // Detect if it is row or array of rows
        if ( ! is_array(reset($arg_values)))
        {
            $arg_values = array($arg_values);
        }

        $values = array_merge($values, $arg_values);
    }
    ...

Second (related bug) is here:
https://github.com/kohana/database/blob/3.3/master/classes/Kohana/Database/Query/Builder/Insert.php#L141-L160

Note that variable $quote is not used, perhaps it migrated from previous code or it is just unfinished code. Actual problem is following. If we do bulk insert, it generates SQL-code INSERT INTO table(column1, column2) VALUES ((value1_1, value1_2), (value2_1, value2_2));. Notice double round braces. This SQL-code is not working in PostgreSQL and SphinxQL, it works only in MySQL. Proper SQL-code:
INSERT INTO table(column1, column2) VALUES (value1_1, value1_2), (value2_1, value2_2);. I have no fix currently.

@rafis rafis changed the title Bug in Database_Query_Insert Bug in Database_Query_Insert: bad SQL for bulk insert Nov 5, 2015
@shadowhand
Copy link
Contributor

The first is not a bug, as the expected input is:

$insert->values($row, $row, $row, $row);

The second is easily solvable my removing the out wrapping quotes.

@rafis
Copy link
Author

rafis commented Nov 5, 2015

Quickfix for second bug is to change this line https://github.com/kohana/database/blob/3.3/master/classes/Kohana/Database/Query/Builder/Insert.php#L156 by:

            // Quickfix
            $group = preg_replace('/\)\)$/', ')', preg_replace('/^\(\(/', '(', '('.implode(', ', $group)).')');
            $groups[] = $group;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants