Sha256: 9ab4c27795d54877915d3333195e0826345a6d867663242d515a439e645bdac1

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    module Bigquery
      module DatabaseStatements

        def execute(sql, name = nil, binds = [])
          log(sql, name) do
            types, params = to_types_and_params(binds)
            @connection.query(sql, params: params, types: types)
          end
        end

        def exec_query(sql, name = 'SQL', binds = [], prepare: false)
          result = execute(sql, name, binds)
          column_types = if schema = result.schema
            schema.param_types.map { |key, sql_type| [key.to_s, lookup_cast_type(sql_type.to_s)] }.to_h
          else
            {}
          end

          rows = result.all.collect { |row| row.values_at(*result.headers) }
          columns = result.headers.map(&:to_s)
          ActiveRecord::Result.new(columns, rows, column_types)
        end

        private

        # Translates binds to BigQuery types and params.
        def to_types_and_params(binds)
          types = binds.enum_for(:each_with_index).map do |bind, i|
            type = :INT64
            if bind.respond_to?(:type)
              type = ActiveRecord::Type::Bigquery::BigqueryActiveRecordConverter
                     .convert_active_model_type_to_bigquery(bind.type)
            end
            ["p#{i + 1}", type]
          end.to_h
          params = binds.enum_for(:each_with_index).map do |bind, i|
            type = bind.respond_to?(:type) ? bind.type : ActiveModel::Type::Integer
            value = bind
            value = type.serialize bind.value, :dml if type.respond_to?(:serialize) && type.method(:serialize).arity < 0
            value = type.serialize bind.value if type.respond_to?(:serialize) && type.method(:serialize).arity >= 0

            ["p#{i + 1}", value]
          end.to_h
          [types, params]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-bigquery-adapter-1.0.1 lib/active_record/connection_adapters/bigquery/database_statements.rb