#!/usr/bin/env ruby require 'bundler/setup' require 'odbc_adapter' require 'pry' require 'odbc_adapter/adapters/postgresql_odbc_adapter' ODBCAdapter.register(/snowflake/, ODBCAdapter::Adapters::PostgreSQLODBCAdapter) do # Here until we upgrade the ODBC driver class DecimalCaster attr_reader :column def initialize(column) @column = column end def cast(value) return if value.nil? column.scale.zero? ? value.to_i : value.to_f end end # Handles strings that aren't utf-8 encoded class StringEncodingCaster def cast(value) value.is_a?(String) ? value.force_encoding('UTF-8') : value end end def quote_column_name(name) name.to_s end private # Monkey-patch the type casting for SQL_DECIMAL columns until we upgrade the ODBC driver def dbms_type_cast(columns, values) casters = Hash.new { |h, k| h[k] = [StringEncodingCaster.new] } columns.each_with_index do |column, idx| casters[idx] << DecimalCaster.new(column) if column.type == ODBC::SQL_DECIMAL end values.each do |row| row.each_index do |idx| casters[idx].each { |caster| row[idx] = caster.cast(row[idx]) } end end values end end ActiveRecord::Base.establish_connection(adapter: 'odbc', dsn: 'LocalyticsProductionSnowflake') binding.pry # class FactSession < ActiveRecord::Base # end # # puts FactSession.where(device_new: true).to_sql