Class: Yql::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/yql/query_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, args = {}) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yql/query_builder.rb', line 9

def initialize(table, args = {})
  @select                     = args[:select]
  @table                      = table
  @use_statement              = args[:use]
  @conditions                 = args[:conditions]
  @limit                      = args[:limit]
  @tail                       = args[:tail]
  @reverse                    = args[:reverse]
  @unique                     = args[:unique]
  @sanitize                   = args[:sanitize]
  @sort_descending            = args[:sort_descending]
  @sanitize_field             = args[:sanitize_field]
  @sort_field                 = args[:sort_field]
  @current_pipe_command_types = []
  @per_page                   = args[:per_page]
end

Instance Attribute Details

#conditionsObject

Conditions can either be provided as hash or plane string



58
59
60
# File 'lib/yql/query_builder.rb', line 58

def conditions
  @conditions
end

#current_pageObject

Returns the value of attribute current_page.



5
6
7
# File 'lib/yql/query_builder.rb', line 5

def current_page
  @current_page
end

#current_pipe_command_typesObject

Returns the value of attribute current_pipe_command_types.



5
6
7
# File 'lib/yql/query_builder.rb', line 5

def current_pipe_command_types
  @current_pipe_command_types
end

#limitObject

Returns the value of attribute limit.



5
6
7
# File 'lib/yql/query_builder.rb', line 5

def limit
  @limit
end

#per_pageObject

Returns the value of attribute per_page.



5
6
7
# File 'lib/yql/query_builder.rb', line 5

def per_page
  @per_page
end

#sanitize_fieldObject

Returns the value of attribute sanitize_field.



5
6
7
# File 'lib/yql/query_builder.rb', line 5

def sanitize_field
  @sanitize_field
end

#selectObject

Returns the value of attribute select.



5
6
7
# File 'lib/yql/query_builder.rb', line 5

def select
  @select
end

#sort_descendingObject

Returns the value of attribute sort_descending.



5
6
7
# File 'lib/yql/query_builder.rb', line 5

def sort_descending
  @sort_descending
end

#sort_fieldObject

Returns the value of attribute sort_field.



5
6
7
# File 'lib/yql/query_builder.rb', line 5

def sort_field
  @sort_field
end

#tableObject

Returns the value of attribute table.



5
6
7
# File 'lib/yql/query_builder.rb', line 5

def table
  @table
end

#truncateObject

Cption can be piped Gets the first count items (rows)



97
98
99
# File 'lib/yql/query_builder.rb', line 97

def truncate
  @truncate
end

Class Method Details

.describe_table(table) ⇒ Object



40
41
42
# File 'lib/yql/query_builder.rb', line 40

def self.describe_table(table)
  "desc #{table};"
end

.show_tablesObject



36
37
38
# File 'lib/yql/query_builder.rb', line 36

def self.show_tables
  "show tables;"
end

Instance Method Details

#describe_tableObject



44
45
46
# File 'lib/yql/query_builder.rb', line 44

def describe_table
  Yql::QueryBuilder.describle_table(table)
end

#findObject



26
27
28
29
# File 'lib/yql/query_builder.rb', line 26

def find
  self.limit = 1
  "#{construct_query}"
end

#find_allObject



31
32
33
34
# File 'lib/yql/query_builder.rb', line 31

def find_all
  self.limit = nil
  construct_query
end

#remove_pipe_command(command) ⇒ Object

Remove a command that will be piped to the yql query



147
148
149
# File 'lib/yql/query_builder.rb', line 147

def remove_pipe_command(command)
  current_pipe_command_types.delete(command)
end

#reorder_pipe_command(args) ⇒ Object

Its always advisable to order the pipe when there are more than one pipe commands available else unexpected results might get returned reorder_pipe_command => 1, :to => 0 the values in the hash are the element numbers



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/yql/query_builder.rb', line 129

def reorder_pipe_command(args)
  return if current_pipe_command_types.empty?
  if args[:from].nil? or args[:to].nil?
    raise Yql::Error, "Not able to move pipe commands. Wrong element numbers. Please try again"
  end
  args.values.each do |element|
    if element > current_pipe_command_types.size-1
      raise Yql::Error, "Not able to move pipe commands. Wrong element numbers. Please try again"
    end
  end 
  element_to_be_inserted_at = args[:from] < args[:to] ? args[:to]+1 : args[:to]
  element_to_be_removed = args[:from] < args[:to] ? args[:from] : args[:from]+1
  current_pipe_command_types.insert(element_to_be_inserted_at, current_pipe_command_types.at(args[:from]))
  current_pipe_command_types.delete_at(element_to_be_removed)
end

#reverseObject

Cption can be piped Reverses the order of the rows



104
105
106
107
# File 'lib/yql/query_builder.rb', line 104

def reverse
  return unless @reverse
  "reverse()"
end

#sanitizeObject

Cption can be piped Sanitizes the output for HTML-safe rendering. To sanitize all returned fields, omit the field parameter.



118
119
120
121
122
# File 'lib/yql/query_builder.rb', line 118

def sanitize
  return unless @sanitize
  return "sanitize()" unless @sanitize_field
  "sanitize(field='#{@sanitize_field}')"
end

#sortObject

Cption can be piped Sorts the result set according to the specified field (column) in the result set.



82
83
84
85
86
# File 'lib/yql/query_builder.rb', line 82

def sort
  return unless @sort_field
  return "sort(field='#{@sort_field}')" unless @sort_descending
  return "sort(field='#{@sort_field}', descending='true')"
end

#tailObject

Cption can be piped Gets the last count items



90
91
92
93
# File 'lib/yql/query_builder.rb', line 90

def tail
  return unless @tail
  "tail(count=#{@tail})"
end

#to_sObject



48
49
50
# File 'lib/yql/query_builder.rb', line 48

def to_s
  construct_query
end

#uniqueObject

Cption can be piped Removes items (rows) with duplicate values in the specified field (column)



111
112
113
114
# File 'lib/yql/query_builder.rb', line 111

def unique
  return unless @unique
  "unique(field='#{@unique}')"
end