From dc8756845d6cec1d367610b30f2a6194fb089163 Mon Sep 17 00:00:00 2001 From: Ryan Kerr Date: Fri, 6 Dec 2024 15:05:16 -0500 Subject: [PATCH] Specify no format in :exec_insert and :exec_insert_all instead of inferring within FormatManager --- .../clickhouse/schema_statements.rb | 6 +++--- .../clickhouse/statement/format_manager.rb | 14 +++++--------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/active_record/connection_adapters/clickhouse/schema_statements.rb b/lib/active_record/connection_adapters/clickhouse/schema_statements.rb index 35b74a08..5140384f 100644 --- a/lib/active_record/connection_adapters/clickhouse/schema_statements.rb +++ b/lib/active_record/connection_adapters/clickhouse/schema_statements.rb @@ -18,7 +18,7 @@ def with_settings(**settings) # Request a specific format for the duration of the provided block. # Pass `nil` to explicitly send the SQL statement without a `FORMAT` clause. - # @param [String] format + # @param [String, nil] format # # @example Specify CSVWithNamesAndTypes format # with_response_format('CSVWithNamesAndTypes') do @@ -49,7 +49,7 @@ def execute(sql, name = nil, format: @response_format, settings: {}) def exec_insert(sql, name, _binds, _pk = nil, _sequence_name = nil, returning: nil) new_sql = sql.sub(/ (DEFAULT )?VALUES/, " VALUES") - execute(new_sql, name) + with_response_format(nil) { execute(new_sql, name) } true end @@ -70,7 +70,7 @@ def internal_exec_query(sql, name = nil, binds = [], prepare: false, async: fals end def exec_insert_all(sql, name) - execute(sql, name) + with_response_format(nil) { execute(sql, name) } true end diff --git a/lib/active_record/connection_adapters/clickhouse/statement/format_manager.rb b/lib/active_record/connection_adapters/clickhouse/statement/format_manager.rb index 0e8f1f9b..b17da791 100644 --- a/lib/active_record/connection_adapters/clickhouse/statement/format_manager.rb +++ b/lib/active_record/connection_adapters/clickhouse/statement/format_manager.rb @@ -7,7 +7,7 @@ class Statement class FormatManager def initialize(sql, format:) - @sql = sql + @sql = sql.strip @format = format end @@ -18,21 +18,17 @@ def apply end def skip_format? - for_insert? || system_command? || schema_command? || format_specified? || delete? + system_command? || schema_command? || format_specified? || delete? end private - def for_insert? - /^insert into/i.match?(@sql) - end - def system_command? - /^system|^optimize/i.match?(@sql) + /\Asystem|\Aoptimize/i.match?(@sql) end def schema_command? - /^create|^alter|^drop|^rename/i.match?(@sql) + /\Acreate|\Aalter|\Adrop|\Arename/i.match?(@sql) end def format_specified? @@ -40,7 +36,7 @@ def format_specified? end def delete? - /^delete from/i.match?(@sql) + /\Adelete from/i.match?(@sql) end end