Class: Babik::QuerySet::SQLRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/babik/queryset/components/sql_renderer.rb

Overview

SQL renderer

Constant Summary collapse

TEMPLATE_PATH =

Where the SQL templates are

"#{__dir__}/../templates"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queryset) ⇒ SQLRenderer

Construct a new SQL rendered for a QuerySet

Parameters:

  • queryset (QuerySet)

    QuerySet to be rendered.



18
19
20
# File 'lib/babik/queryset/components/sql_renderer.rb', line 18

def initialize(queryset)
  @queryset = queryset
end

Instance Attribute Details

#querysetObject (readonly)

Returns the value of attribute queryset.



11
12
13
# File 'lib/babik/queryset/components/sql_renderer.rb', line 11

def queryset
  @queryset
end

Instance Method Details

#deleteString

Render the DELETE statement

Returns:

  • (String)

    SQL DELETE statement for this QuerySet.



40
41
42
43
44
45
# File 'lib/babik/queryset/components/sql_renderer.rb', line 40

def delete
  @queryset.project(['id'])
  sql = _render('delete/main.sql.erb')
  @queryset.unproject
  sql
end

#left_joinsString

Return the SQL representation of all joins of the QuerySet

Returns:

  • (String)

    A String with all LEFT JOIN statements required for this QuerySet.



49
50
51
52
# File 'lib/babik/queryset/components/sql_renderer.rb', line 49

def left_joins
  # Join all left joins and return a string with the SQL code
  @queryset.left_joins_by_alias.values.map(&:sql).join("\n")
end

#selectString

Render the SELECT statement

Returns:

  • (String)

    SQL SELECT statement for this QuerySet.



24
25
26
# File 'lib/babik/queryset/components/sql_renderer.rb', line 24

def select
  _render('select/main.sql.erb')
end

#update(update_command) ⇒ String

Render the UPDATE statement

Parameters:

  • update_command (Hash{field: value})

    Runs the update query.

Returns:

  • (String)

    SQL UPDATE statement for this QuerySet.



31
32
33
34
35
36
# File 'lib/babik/queryset/components/sql_renderer.rb', line 31

def update(update_command)
  @queryset.project!(['id'])
  sql = _render('update/main.sql.erb', {update_command: update_command})
  @queryset.unproject!
  sql
end