Sha256: 012079057bf0d0bc5147cb15a6aaf69ca3e7d79dbb3153f611b9bd445001e2ee

Contents?: true

Size: 869 Bytes

Versions: 7

Compression:

Stored size: 869 Bytes

Contents

class Simple::SQL::Connection::Scope
  def order_by(sql_fragment)
    duplicate.send(:order_by!, sql_fragment)
  end

  def limit(limit)
    raise ArgumentError, "limit must be >= 0" unless limit >= 0

    duplicate.send(:limit!, limit)
  end

  def offset(offset)
    raise ArgumentError, "offset must be >= 0" unless offset >= 0

    duplicate.send(:offset!, offset)
  end

  private

  # Adjust sort order
  def order_by!(sql_fragment)
    @order_by_fragment = sql_fragment
    self
  end

  # Adjust sort order
  def limit!(count)
    @limit = count
    self
  end

  def offset!(offset)
    @offset = offset
    self
  end

  # called from to_sql
  def apply_order_and_limit(sql)
    sql = "#{sql} ORDER BY #{@order_by_fragment}" if @order_by_fragment
    sql = "#{sql} LIMIT #{@limit}" if @limit
    sql = "#{sql} OFFSET #{@offset}" if @offset

    sql
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
simple-sql-0.5.37 lib/simple/sql/connection/scope/order.rb
simple-sql-0.5.36 lib/simple/sql/connection/scope/order.rb
simple-sql-0.5.35 lib/simple/sql/connection/scope/order.rb
simple-sql-0.5.34 lib/simple/sql/connection/scope/order.rb
simple-sql-0.5.33 lib/simple/sql/connection/scope/order.rb
simple-sql-0.5.32 lib/simple/sql/connection/scope/order.rb
simple-sql-0.5.31 lib/simple/sql/connection/scope/order.rb