Sha256: 58efd1bcd08ad3fc1486ea5bfe76eac28360c1238f2c2212cd9b9fc9ffa04a97

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 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)
          if schema = result.schema
            column_types = schema.param_types.map { |key, sql_type| [key.to_s, lookup_cast_type(sql_type.to_s)] }.to_h
            rows = result.all.collect { |row| row.values_at(*schema.headers) }
            columns = schema.headers.map(&:to_s)
            ActiveRecord::Result.new(columns, rows, column_types)
          else
            ActiveRecord::Result.new([], [], {})
          end
        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.3 lib/active_record/connection_adapters/bigquery/database_statements.rb