Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jan 6, 2024
1 parent fd04dd0 commit 4a97da6
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/blazer/adapters/sql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ def run_statement(statement, comment, bind_params = [])
error = nil

begin
result = nil

in_transaction do
set_timeout(data_source.timeout) if data_source.timeout

binds = bind_params.map { |v| ActiveRecord::Relation::QueryAttribute.new(nil, v, ActiveRecord::Type::Value.new) }
result = connection_model.connection.select_all("#{statement} /*#{comment}*/", nil, binds)
columns = result.columns
result.rows.each do |untyped_row|
rows << (result.column_types.empty? ? untyped_row : columns.each_with_index.map { |c, i| untyped_row[i] && result.column_types[c] ? result.column_types[c].send(:cast_value, untyped_row[i]) : untyped_row[i] })
end
end

columns = result.columns
result.rows.each do |untyped_row|
rows << (result.column_types.empty? ? untyped_row : columns.each_with_index.map { |c, i| untyped_row[i] && result.column_types[c] ? result.column_types[c].send(:cast_value, untyped_row[i]) : untyped_row[i] })
end

# fix for non-ASCII column names and charts
if adapter_name == "Trilogy"
columns.map! { |k| k.dup.force_encoding(Encoding::UTF_8) }
end
rescue => e
error = e.message.sub(/.+ERROR: /, "")
Expand All @@ -41,11 +49,6 @@ def run_statement(statement, comment, bind_params = [])
reconnect if error.include?("PG::ConnectionBad")
end

# fix for non-ASCII column names and charts
if adapter_name == "Trilogy"
columns.map! { |k| k.dup.force_encoding(Encoding::UTF_8) }
end

[columns, rows, error]
end

Expand Down

0 comments on commit 4a97da6

Please sign in to comment.