Module: SQB::Columns

Included in:
Select
Defined in:
lib/sqb/columns.rb

Instance Method Summary collapse

Instance Method Details

#column(column, options = {}) ⇒ Query

Add a column to the query

Parameters:

  • column (String, Symbol, Hash)

    the column name (or a hash with table & column name)

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :function (String)

    a function to wrap around the column

Returns:

  • (Query)

    returns the query



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sqb/columns.rb', line 10

def column(column, options = {})
  @columns ||= []
  with_table_and_column(column) do |table, column|
    @columns << [].tap do |query|
      if options[:function]
        query << "#{escape_function(options[:function])}("
      end
      query << escape_and_join(table, column)
      if options[:function]
        query << ")"
      end
      if options[:as]
        query << "AS"
        query << escape(options[:as])
      end
    end.join(' ')
  end
  self
end

#column!(*args) ⇒ Object

Replace all existing columns with the given column



31
32
33
34
# File 'lib/sqb/columns.rb', line 31

def column!(*args)
  @columns = []
  column(*args)
end