Skip to content

Bug in Database_Query_Insert: bad SQL for bulk insert #100

@rafis

Description

@rafis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions