Class: Terrazine::Builder
- Inherits:
-
Object
- Object
- Terrazine::Builder
- Defined in:
- lib/terrazine/builder.rb
Overview
build structures in to sql string
Instance Attribute Summary collapse
-
#constructor ⇒ Object
Returns the value of attribute constructor.
-
#sql ⇒ Object
Returns the value of attribute sql.
Instance Method Summary collapse
-
#build_conditions(structure) ⇒ Object
TODO? conditions like [:eq :name :Aeonax].
- #build_distinct_select(distinct) ⇒ Object
- #build_from(structure) ⇒ Object
-
#build_join(structure) ⇒ Object
TODO: -_-.
- #build_limit(limit) ⇒ Object
- #build_offset(offset) ⇒ Object
-
#build_order(structure) ⇒ Object
TODO!.
- #build_select(structure, distinct = nil) ⇒ Object
-
#build_sql(structure) ⇒ Object
TODO: update, delete, insert.….
- #build_tables(structure) ⇒ Object
-
#build_union(structure) ⇒ Object
def build_select_query(structure) puts “build_select_query, structure: #structure” sql = build_select(structure, structure) if structure [:from, :join, :where, :order, :limit, :offset].each do |i| sql = send(“build_#i”, structure) if structure end end.
- #build_where(structure) ⇒ Object
- #build_with(structure) ⇒ Object
- #conditions_constructor(structure, joiner = :and, level = nil) ⇒ Object
-
#get_sql(structure) ⇒ Object
get complete sql structure for constructor.
-
#initialize(constructor) ⇒ Builder
constructor
A new instance of Builder.
Constructor Details
#initialize(constructor) ⇒ Builder
Returns a new instance of Builder.
6 7 8 9 |
# File 'lib/terrazine/builder.rb', line 6 def initialize(constructor) @constructor = constructor @params = [] end |
Instance Attribute Details
#constructor ⇒ Object
Returns the value of attribute constructor.
4 5 6 |
# File 'lib/terrazine/builder.rb', line 4 def constructor @constructor end |
#sql ⇒ Object
Returns the value of attribute sql.
4 5 6 |
# File 'lib/terrazine/builder.rb', line 4 def sql @sql end |
Instance Method Details
#build_conditions(structure) ⇒ Object
TODO? conditions like [:eq :name :Aeonax]
114 115 116 |
# File 'lib/terrazine/builder.rb', line 114 def build_conditions(structure) conditions_constructor(structure, :and, true) + ' ' end |
#build_distinct_select(distinct) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/terrazine/builder.rb', line 53 def build_distinct_select(distinct) case distinct when Array "DISTINCT ON(#{build_columns fields}) " when true 'DISTINCT ' end end |
#build_from(structure) ⇒ Object
85 86 87 |
# File 'lib/terrazine/builder.rb', line 85 def build_from(structure) "FROM #{build_tables(structure)} " end |
#build_join(structure) ⇒ Object
TODO: -_-
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/terrazine/builder.rb', line 119 def build_join(structure) if structure.is_a? Array # TODO: hash is sux here -_- !!!!!! if structure.second.is_a? Hash name = build_tables structure.first # (name.is_a?(Array) ? name.join(' ') : name) v = structure.second "#{v[:option].to_s.upcase + ' ' if v[:option]}JOIN #{name} ON #{build_conditions v[:on]}" else structure.map { |i| build_join(i) }.join end else structure =~ /join/i ? structure : "JOIN #{structure} " end end |
#build_limit(limit) ⇒ Object
143 144 145 |
# File 'lib/terrazine/builder.rb', line 143 def build_limit(limit) "LIMIT #{limit || 8} " end |
#build_offset(offset) ⇒ Object
147 148 149 |
# File 'lib/terrazine/builder.rb', line 147 def build_offset(offset) "OFFSET #{offset || 0} " end |
#build_order(structure) ⇒ Object
TODO!
139 140 141 |
# File 'lib/terrazine/builder.rb', line 139 def build_order(structure) "ORDER BY #{structure} " end |
#build_select(structure, distinct = nil) ⇒ Object
62 63 64 65 |
# File 'lib/terrazine/builder.rb', line 62 def build_select(structure, distinct = nil) # puts "build_select, structure #{structure}" "SELECT #{build_distinct_select distinct}#{build_columns structure} " end |
#build_sql(structure) ⇒ Object
TODO: update, delete, insert.….
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/terrazine/builder.rb', line 12 def build_sql(structure) structure = structure.is_a?(Constructor) ? structure.structure : structure sql = '' sql += "WITH #{build_with(structure[:with])} " if structure[:with] # puts "build_sql, structure: #{structure}" [:union, :select, :insert, :update, :delete, :set, :from, :join, :where, :group, :order, :limit, :offset].each do |i| next unless structure[i] sql += send("build_#{i}".to_sym, structure[i]) end sql end |
#build_tables(structure) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/terrazine/builder.rb', line 67 def build_tables(structure) case structure when Array if check_alias(structure.first) # VALUES function or ...? build_function(structure) # if it's a array with strings/values elsif structure.select { |i| i.is_a? Array }.empty? # array of table_name and alias structure.join ' ' else # array of tables/values structure.map { |i| i.is_a?(Array) ? build_tables(i) : i }.join(', ') end when String, Symbol structure else raise "Undefined structure for FROM - #{structure}" end end |
#build_union(structure) ⇒ Object
def build_select_query(structure) puts “build_select_query, structure: #structure” sql = build_select(structure, structure) if structure [:from, :join, :where, :order, :limit, :offset].each do |i| sql = send(“build_#i”, structure) if structure end end
49 50 51 |
# File 'lib/terrazine/builder.rb', line 49 def build_union(structure) structure.map { |i| build_sql(i) }.join ' UNION ' end |
#build_where(structure) ⇒ Object
134 135 136 |
# File 'lib/terrazine/builder.rb', line 134 def build_where(structure) "WHERE #{build_conditions(structure)} " end |
#build_with(structure) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/terrazine/builder.rb', line 33 def build_with(structure) if structure.second.is_a? Hash "#{structure.first} AS (#{build_sql(structure.last)})" else structure.map { |v| build_with(v) }.join ', ' end end |
#conditions_constructor(structure, joiner = :and, level = nil) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/terrazine/builder.rb', line 89 def conditions_constructor(structure, joiner = :and, level = nil) case structure when Array key = structure.first # AND, OR support if key.is_a? Symbol res = structure.drop(1).map { |i| conditions_constructor(i) }.join " #{key} ".upcase level ? res : "(#{res})" # Sub Queries support - ['rgl IN ?', {...}] elsif key =~ /\?/ if [Hash, Constructor].include?(structure.second.class) key.sub(/\?/, "(#{build_sql(structure.second)})") else key.sub(/\?/, build_param(structure.second)) end else res = structure.map { |i| conditions_constructor(i) }.join " #{joiner} ".upcase level ? res : "(#{res})" end when String structure end end |
#get_sql(structure) ⇒ Object
get complete sql structure for constructor.
26 27 28 29 30 31 |
# File 'lib/terrazine/builder.rb', line 26 def get_sql(structure) sql = build_sql structure res = @params.count.positive? ? [sql, @params] : sql @params = [] res end |