Class: DynamicRecordsMeritfront::DynamicSqlVariables

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic-records-meritfront.rb

Constant Summary collapse

DB_TYPE_MAPS =

api.rubyonrails.org/files/activemodel/lib/active_model/type_rb.html active_model/type/helpers active_model/type/value active_model/type/big_integer active_model/type/binary active_model/type/boolean active_model/type/date active_model/type/date_time active_model/type/decimal active_model/type/float active_model/type/immutable_string active_model/type/integer active_model/type/string active_model/type/time active_model

{
    String => ActiveModel::Type::String,
    Symbol => ActiveModel::Type::String,
    ActiveSupport::SafeBuffer => ActiveModel::Type::String,
    Integer => ActiveModel::Type::BigInteger,
    BigDecimal => ActiveRecord::Type::Decimal,
    TrueClass => ActiveModel::Type::Boolean,
    FalseClass => ActiveModel::Type::Boolean,
    Date => ActiveModel::Type::Date,
    DateTime => ActiveModel::Type::DateTime,
    Time => ActiveModel::Type::Time,
    Float => ActiveModel::Type::Float,
		NilClass => ActiveModel::Type::Boolean,
    Array =>  Proc.new{ |first_el_class| ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new(DB_TYPE_MAPS[first_el_class].new) } #this one was a mistake
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ DynamicSqlVariables

Returns a new instance of DynamicSqlVariables.



31
32
33
34
# File 'lib/dynamic-records-meritfront.rb', line 31

def initialize(params)
    @sql_hash = {}
    self.params = params
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



30
31
32
# File 'lib/dynamic-records-meritfront.rb', line 30

def params
  @params
end

#sql_hashObject

Returns the value of attribute sql_hash.



29
30
31
# File 'lib/dynamic-records-meritfront.rb', line 29

def sql_hash
  @sql_hash
end

Instance Method Details

#add_key_value(key, value = nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/dynamic-records-meritfront.rb', line 42

def add_key_value(key, value = nil)
    value = params[key] if value.nil?
    #tracks the variable and returns the keys sql variable number
    sql_hash[key] ||= convert_to_query_attribute(key, value)
    return key_index(key)
end

#convert_to_query_attribute(name, v) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dynamic-records-meritfront.rb', line 94

def convert_to_query_attribute(name, v)
    # https://stackoverflow.com/questions/40407700/rails-exec-query-bindings-ignored
    # binds = [ ActiveRecord::Relation::QueryAttribute.new(
    # 	"id", 6, ActiveRecord::Type::Integer.new
    # )]
    # ApplicationRecord.connection.exec_query(
    # 	'SELECT * FROM users WHERE id = $1', 'sql', binds
    # )

    return v if v.kind_of? ActiveRecord::Relation::QueryAttribute	#so users can have fine-grained control if they are trying to do something
        #that we didn't handle properly.

    type = DB_TYPE_MAPS[v.class]
    if type.nil?
        raise StandardError.new("#{name} (of value: #{v}, class: #{v.class}) unsupported class for DynamicRecordsMeritfront#headache_sql")
    elsif type.class == Proc
        a = v[0]
        # if a.nil?
        # 	a = Integer
        # elsif a.class == Array
        a = a.nil? ? Integer : a.class
        type = type.call(a)
    else
        type = type.new
    end	
    ActiveRecord::Relation::QueryAttribute.new( name, v, type )
end

#get_array_for_exec_queryObject



54
55
56
# File 'lib/dynamic-records-meritfront.rb', line 54

def get_array_for_exec_query
    sql_hash.values
end

#key_index(key) ⇒ Object



36
37
38
39
40
# File 'lib/dynamic-records-meritfront.rb', line 36

def key_index(key)
    k = sql_hash.keys.index(key)
    k += 1 unless k.nil?
    k
end

#next_sql_numObject



49
50
51
52
# File 'lib/dynamic-records-meritfront.rb', line 49

def next_sql_num
    #gets the next sql variable number
    sql_hash.keys.length + 1
end