Sha256: bc25c6a14dd5ddc15c288d1fa9aee81d32a56d67bbd6f9798cd216e574bea7c7

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

module ActiveHouse
  module PreparedStatement
    def self.prepare_sql(sql, *bindings)
      return sql if bindings.empty?
      parts = sql_parts(sql)
      raise ArgumentError, 'wrong number of bindings' if parts.size != bindings.size + 1
      parts.map.with_index do |part, idx|
        value = idx + 1 > bindings.size ? nil : format_value(bindings[idx])
        "#{part}#{value}"
      end.join
      sql
    end

    def self.format_value(value)
      return 'NULL' if value.nil?
      if value.is_a?(Array)
        "(#{value.map { |val| format_value(val) }.join(', ')})"
      elsif value.is_a?(String)
        "'#{value.gsub("'", "\\'")}'"
      elsif value.is_a?(Time)
        if value.respond_to?(:zone)
          "toDateTime('#{value.strftime('%Y-%m-%d %H:%M:%S')}', '#{value.zone}')"
        else
          "toDateTime('#{value.strftime('%Y-%m-%d %H:%M:%S')}')"
        end
      else
        value.to_s
      end
    end

    def self.sql_parts(sql)
      # TODO: except prepended with backslash or inside brackets
      parts = sql.split('?')
      parts.push('') if sql.end_with?('?')
      parts
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_house-0.1.3 lib/active_house/prepared_statement.rb
active_house-0.1.2 lib/active_house/prepared_statement.rb
active_house-0.1.1 lib/active_house/prepared_statement.rb
active_house-0.1.0 lib/active_house/prepared_statement.rb