Skip to content

Commit

Permalink
Fix INSERT into SQLite with single quote
Browse files Browse the repository at this point in the history
Previously it leads to syntax error, due to incorrect escaping of single
quotes for SQLite, "\'" had been used instead of "''"

So set output_format_values_escape_quote_with_quote=true for SQLite to
fix this.

v2: prepare modified Context for writing on storage creation
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
  • Loading branch information
azat committed Feb 15, 2024
1 parent 64dd479 commit 1b1c1ef
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/Storages/StorageSQLite.cpp
Expand Up @@ -19,6 +19,20 @@
#include <QueryPipeline/Pipe.h>
#include <Common/filesystemHelpers.h>

namespace
{

using namespace DB;

ContextPtr makeSQLiteWriteContext(ContextPtr context)
{
auto write_context = Context::createCopy(context);
write_context->setSetting("output_format_values_escape_quote_with_quote", Field(true));
return write_context;
}

}


namespace DB
{
Expand All @@ -43,6 +57,7 @@ StorageSQLite::StorageSQLite(
, database_path(database_path_)
, sqlite_db(sqlite_db_)
, log(getLogger("StorageSQLite (" + table_id_.table_name + ")"))
, write_context(makeSQLiteWriteContext(getContext()))
{
StorageInMemoryMetadata storage_metadata;

Expand Down Expand Up @@ -144,7 +159,7 @@ class SQLiteSink : public SinkToStorage

sqlbuf << ") VALUES ";

auto writer = FormatFactory::instance().getOutputFormat("Values", sqlbuf, metadata_snapshot->getSampleBlock(), storage.getContext());
auto writer = FormatFactory::instance().getOutputFormat("Values", sqlbuf, metadata_snapshot->getSampleBlock(), storage.write_context);
writer->write(block);

sqlbuf << ";";
Expand Down
3 changes: 3 additions & 0 deletions src/Storages/StorageSQLite.h
Expand Up @@ -47,10 +47,13 @@ class StorageSQLite final : public IStorage, public WithContext
const String & table);

private:
friend class SQLiteSink; /// for write_context

String remote_table_name;
String database_path;
SQLitePtr sqlite_db;
LoggerPtr log;
ContextPtr write_context;
};

}
Expand Down
Expand Up @@ -29,7 +29,7 @@ CREATE TABLE default.sqlite_table3\n(\n `col1` String,\n `col2` Int32\n)\n
not a null 2
3
4
line6 6
line\'6 6
7
test table function
line1 1
Expand Down
2 changes: 1 addition & 1 deletion tests/queries/0_stateless/01889_sqlite_read_write.sh
Expand Up @@ -76,7 +76,7 @@ ${CLICKHOUSE_CLIENT} --query='DROP TABLE IF EXISTS sqlite_table3'
${CLICKHOUSE_CLIENT} --query="CREATE TABLE sqlite_table3 (col1 String, col2 Int32) ENGINE = SQLite('${DB_PATH}', 'table3')"

${CLICKHOUSE_CLIENT} --query='SHOW CREATE TABLE sqlite_table3;' | sed -r 's/(.*SQLite)(.*)/\1/'
${CLICKHOUSE_CLIENT} --query="INSERT INTO sqlite_table3 VALUES ('line6', 6);"
${CLICKHOUSE_CLIENT} --query="INSERT INTO sqlite_table3 VALUES ('line\'6', 6);"
${CLICKHOUSE_CLIENT} --query="INSERT INTO sqlite_table3 VALUES (NULL, 7);"

${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_table3 ORDER BY col2'
Expand Down

0 comments on commit 1b1c1ef

Please sign in to comment.