Module: SchemaPlus::DbDefault::Middleware::Query::Exec::Postgresql

Defined in:
lib/schema_plus/db_default/middleware.rb

Instance Method Summary collapse

Instance Method Details

#before(env) ⇒ Object

Middleware to replace each ActiveRecord::DB_DEFAULT with a literal DEFAULT in the sql string, for postgresql. The underlying pg gem provides no way to bind a value that will replace $n with DEFAULT.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/schema_plus/db_default/middleware.rb', line 12

def before(env)
  if env.binds.any?{ |col, val| val.equal? ::ActiveRecord::DB_DEFAULT}
    j = 0
    env.binds.each_with_index do |(col, val), i|
      if val.equal? ::ActiveRecord::DB_DEFAULT
        env.sql = env.sql.sub(/\$#{i+1}/, 'DEFAULT')
      else
        env.sql = env.sql.sub(/\$#{i+1}\b/, "$#{j+1}") if i != j
        j += 1
      end
    end
    env.binds = env.binds.reject{|col, val| val.equal? ::ActiveRecord::DB_DEFAULT}
  end
end