Sha256: f7e32fc1d39db8706f0537ee045de2c977d36cf92efb2d77b2750950283020d6

Contents?: true

Size: 940 Bytes

Versions: 125

Compression:

Stored size: 940 Bytes

Contents

class SqlBuilder

  def initialize
    @sql = []
    @params = []
  end

  def append(*args)
    if args.size == 1
      if args[0].is_a?(Array)
        append_array(args[0])
      elsif args[0].is_a?(SqlBuilder)
        append(args[0].to_a)
      else
        append_string(args[0])
      end
    else
      append_array(args)
    end

    self
  end
  
  def and(*args)
    append_string('and (')
    
    if block_given?
      yield self
    else
      append(*args)
    end
    
    append_string(')')
  end

  def or(*args)
    append_string('or (')

    if block_given?
      yield self
    else
      append(*args)
    end

    append_string(')')
  end
  
  def to_a
    [@sql.join(' ')] + @params
  end
  
  private
  
  def append_string(sql)
    @sql << sql
  end
  
  def append_array(array)
    array.each_with_index do |x, i|
      if i == 0
        append_string(x)
      else
        @params << x
      end
    end
  end
end

Version data entries

125 entries across 125 versions & 1 rubygems

Version Path
daddy-0.9.13 lib/sql_builder.rb
daddy-0.9.12 lib/sql_builder.rb
daddy-0.9.11 lib/sql_builder.rb
daddy-0.9.10 lib/sql_builder.rb
daddy-0.9.9 lib/sql_builder.rb
daddy-0.9.8 lib/sql_builder.rb
daddy-0.9.7 lib/sql_builder.rb
daddy-0.9.6 lib/sql_builder.rb
daddy-0.9.5 lib/sql_builder.rb
daddy-0.9.4 lib/sql_builder.rb
daddy-0.9.3 lib/sql_builder.rb
daddy-0.9.2 lib/sql_builder.rb
daddy-0.9.1 lib/sql_builder.rb
daddy-0.9.0 lib/sql_builder.rb
daddy-0.8.1 lib/sql_builder.rb
daddy-0.8.0 lib/sql_builder.rb
daddy-0.7.9 lib/sql_builder.rb
daddy-0.7.8 lib/sql_builder.rb
daddy-0.7.7 lib/sql_builder.rb
daddy-0.7.6 lib/sql_builder.rb