FriendsOfDoctrine / dbal-clickhouse

Doctrine DBAL driver for ClickHouse database
GNU General Public License v3.0
98 stars 82 forks source link

Question mark in value of multi-insert prepared statement misbehavior #11

Closed fehomeh closed 6 years ago

fehomeh commented 6 years ago

As @Ocramius suggested here https://github.com/doctrine/dbal/issues/2981#issuecomment-357929302 I tried to do multiple insert with prepared statement (because writing of plain SQL is quite dangerous) and encountered with strange behavior - if text field value has question mark it would be replaced inline with next value to insert. Example: Table structure (taken from doc with slight modification):

$fromSchema = $conn->getSchemaManager()->createSchema();
$toSchema = clone $fromSchema;

// create new table object
$newTable = $toSchema->createTable('new_table');

// add columns
$newTable->addColumn('id', 'integer', ['unsigned' => true]);
$newTable->addColumn('payload', 'string');
$newTable->addColumn('url', 'string');
$newTable->setPrimaryKey(['id']);

// execute migration SQLs to create table in ClickHouse
$sqlArray = $fromSchema->getMigrateToSql($toSchema, $conn->getDatabasePlatform());
foreach ($sqlArray as $sql) {
    $conn->exec($sql);
}

Insert query:

$statement = $conn->prepare('INSERT INTO new_table(id, payload, url) VALUES (?, ?, ?), (?, ?, ?);');
$statement->execute([123, 'foo-bar', 'https://some.url.com/?first=param', 456, 'bar-baz', 'http://example.com?p=1']);

Error produced by code:

PHP Fatal error: Uncaught ClickHouseDB\Exception\DatabaseException: Type mismatch in IN or VALUES section. Expected: UInt32. Got: String IN:INSERT INTO new_table(id, payload, url) VALUES (123, 'foo-bar', 'https://some.url.com/456first=param'), ('bar-baz', 'http://example.com?p=1', ?);

argayash commented 6 years ago

@fehomeh you can try dev-master. issue is fixed in commit https://github.com/FriendsOfDoctrine/dbal-clickhouse/commit/4b7dbe2437f64881dae861692ec27d17c6fda177

fehomeh commented 6 years ago

Yes, it works! Thank you!