Class: Terrazine::Constructor
- Inherits:
-
Object
- Object
- Terrazine::Constructor
- Defined in:
- lib/terrazine/constructor.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#structure ⇒ Object
readonly
Returns the value of attribute structure.
Instance Method Summary collapse
-
#build_sql ⇒ Object
constructor.build_sql => ‘SELECT .…
-
#distinct_select(structure, fields = nil) ⇒ Object
distinct_select select_structure distinct_select select_structure, distinct_field distinct_select select_structure, [*distinct_fields].
-
#from(structure) ⇒ Object
TODO: from construction from [:mrgl, :m] from [:_values, [1, 2], :rgl, [:zgl, :gl]] => [[:mrgl, :m], [:_values, [1, 2], :rgl, [:zgl, :gl]]].
-
#initialize(structure = {}) ⇒ Constructor
constructor
A new instance of Constructor.
-
#join(structure) ⇒ Object
TODO: join constructor AND better syntax join ‘users u ON u.id = m.user_id’ join [‘users u ON u.id = m.user_id’, ‘skills s ON u.id = s.user_id’] join [[:user, :u], { on: ‘rgl = 123’ }] join [[[:user, :u], { option: :full, on: [:or, ‘mrgl = 2’, ‘rgl = 22’] }], [:master, { on: [‘z = 12’, ‘mrgl = 12’] }]].
-
#limit(per) ⇒ Object
TODO: with -_- with [:alias_name, { select: true, from: :users}] with [[:alias_name, { select: true, from: :users}], [:alias_name_2, { select: [:name, :email], from: :rgl}]].
-
#merge(params) ⇒ Object
just rewrite data.
-
#paginate(params) ⇒ Object
TODO: serve - return count of all rows params - hash with keys :per, :page.
-
#select(structure) ⇒ Object
construct it constructor = SQLConstructor.new from: [:users, :u], join [[:mrgl, :m], { on: ‘m.user_id = u.id’}] constructor.select :name constructor.select [:id, _some_count: [:_count, another_constructor]] if smthng constructor.select [:rgl, :zgl] if another_smthng constructor.build_sql => ‘SELECT name, u.id, COUNT(SELECT …) AS some_count, r.rgl, zgl FROM …’.
-
#structure_constructor(structure, modifier) ⇒ Object
TODO? join hash inside array? TODO!! join values of existing keys.
-
#where(structure) ⇒ Object
constructor.where [‘u.categories_cache ~ ?’, { select: :path, from: :categories, where: [‘id = ?’, s_params] }] constructor.where(‘m.cashless IS TRUE’).
Constructor Details
#initialize(structure = {}) ⇒ Constructor
Returns a new instance of Constructor.
4 5 6 7 8 |
# File 'lib/terrazine/constructor.rb', line 4 def initialize(structure = {}) @structure = structure # @params = [] @builder = Builder.new(self) end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
3 4 5 |
# File 'lib/terrazine/constructor.rb', line 3 def params @params end |
#structure ⇒ Object (readonly)
Returns the value of attribute structure.
3 4 5 |
# File 'lib/terrazine/constructor.rb', line 3 def structure @structure end |
Instance Method Details
#build_sql ⇒ Object
constructor.build_sql
> ‘SELECT .… FROM …’
> [‘SELECT .… FROM .… WHERE id = $1’, [22]]
159 160 161 |
# File 'lib/terrazine/constructor.rb', line 159 def build_sql @builder.get_sql @structure end |
#distinct_select(structure, fields = nil) ⇒ Object
distinct_select select_structure distinct_select select_structure, distinct_field distinct_select select_structure, [*distinct_fields]
74 75 76 77 78 |
# File 'lib/terrazine/constructor.rb', line 74 def distinct_select(structure, fields = nil) @structure[:distinct] = fields || true select structure self end |
#from(structure) ⇒ Object
TODO: from construction from [:mrgl, :m] from [:_values, [1, 2], :rgl, [:zgl, :gl]]
> [[:mrgl, :m], [:_values, [1, 2], :rgl, [:zgl, :gl]]]
84 85 86 87 |
# File 'lib/terrazine/constructor.rb', line 84 def from(structure) @structure[:from] = structure self end |
#join(structure) ⇒ Object
TODO: join constructor AND better syntax join ‘users u ON u.id = m.user_id’ join [‘users u ON u.id = m.user_id’,
'skills s ON u.id = s.user_id']
join [[:user, :u], { on: ‘rgl = 123’ }] join [[[:user, :u], { option: :full, on: [:or, ‘mrgl = 2’, ‘rgl = 22’] }],
[:master, { on: ['z = 12', 'mrgl = 12'] }]]
96 97 98 99 100 |
# File 'lib/terrazine/constructor.rb', line 96 def join(structure) @structure[:join] = structure # puts @structure[:join] self end |
#limit(per) ⇒ Object
TODO: with -_- with [:alias_name, { select: true, from: :users}] with [[:alias_name, { select: true, from: :users}],
[:alias_name_2, { select: {u: [:name, :email]},
from: :rgl}]]
136 137 138 139 |
# File 'lib/terrazine/constructor.rb', line 136 def limit(per) @structure[:limit] = (per || 8).to_i self end |
#merge(params) ⇒ Object
just rewrite data. TODO: merge with merge without loss of data? constructor.merge(select: :content, order_by: ‘f.id DESC’, limit: 1)
151 152 153 154 |
# File 'lib/terrazine/constructor.rb', line 151 def merge(params) @structure.merge! params self end |
#paginate(params) ⇒ Object
TODO: serve - return count of all rows params - hash with keys :per, :page
143 144 145 146 147 |
# File 'lib/terrazine/constructor.rb', line 143 def paginate(params) limit params[:per] @structure[:offset] = ((params[:page]&.to_i || 1) - 1) * @structure[:limit] self end |
#select(structure) ⇒ Object
construct it constructor = SQLConstructor.new from: [:users, :u],
join [[:mrgl, :m], { on: 'm.user_id = u.id'}]
constructor.select :name constructor.select [:id, _some_count: [:_count, another_constructor]] if smthng constructor.select [:rgl, :zgl] if another_smthng constructor.build_sql
> ‘SELECT name, u.id, COUNT(SELECT …) AS some_count, r.rgl, zgl FROM …’
66 67 68 69 |
# File 'lib/terrazine/constructor.rb', line 66 def select(structure) @structure[:select] = structure_constructor(@structure[:select], structure) self end |
#structure_constructor(structure, modifier) ⇒ Object
TODO? join hash inside array? TODO!! join values of existing keys
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/terrazine/constructor.rb', line 12 def structure_constructor(structure, modifier) return modifier unless structure if structure.is_a?(Hash) && modifier.is_a?(Hash) modifier.each do |k, v| structure[k] = structure_constructor(structure[k], v) end structure else structure = structure.is_a?(Array) ? structure : [structure] if modifier.is_a?(Array) modifier.each { |i| structure_constructor structure, i } else structure << modifier end structure.uniq end end |
#where(structure) ⇒ Object
constructor.where [‘u.categories_cache ~ ?’,
{ select: :path, from: :categories,
where: ['id = ?', s_params[:category_id]] }]
constructor.where(‘m.cashless IS TRUE’)
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/terrazine/constructor.rb', line 118 def where(structure) w = @structure[:where] if w.is_a?(Array) && w.first.is_a?(Array) @structure[:where].push structure elsif w @structure[:where] = [w, structure] else @structure[:where] = structure end self end |