class SQLPP::Formatter

Public Class Methods

new(projections: nil) click to toggle source
# File lib/sqlpp/formatter.rb, line 3
def initialize(projections: nil)
  @indent = nil
  @state = nil

  @projections = projections
end

Public Instance Methods

_format_Alias(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 146
def _format_Alias(node)
  format(node.expr) + " " + format(node.name)
end
_format_As(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 142
def _format_As(node)
  format(node.expr) + " AS " + format(node.name)
end
_format_Atom(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 102
def _format_Atom(node)
  output = ""

  case node.type
    when :range
      output << format(node.left) << " AND " << format(node.right)
    when :list
      output << "(" << node.left.map { |c| format(c) }.join(", ") << ")"
    when :func
      output << format(node.left) << "("
      output << node.right.map { |c| format(c) }.join(", ")
      output << ")"
    when :lit
      output << node.left
    when :attr
      output << node.left
      output << "." << node.right if node.right
    when :case
      output << "CASE "
      output << format(node.left) << " " if node.left
      node.right.each do |child|
        if child.is_a?(Array)
          output << "WHEN " << format(child[0]) << " "
          output << "THEN " << format(child[1]) << " "
        else
          output << "ELSE " << format(child) << " "
        end
      end
      output << "END"
    else
      raise ArgumentError, "unknown atom type #{node.type.inspect}"
  end

  output
end
_format_Expr(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 77
def _format_Expr(node)
  output = format(node.left)
  if node.op
    op = node.op.to_s.upcase

    if @state == :where && %w(AND OR).include?(op)
      output << "\n#{_indent}"
    else
      output << " "
    end

    output << "NOT " if node.not
    output << op << " "
    output << format(node.right)
  end
  output
end
_format_Join(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 150
def _format_Join(node)
  output = ""

  output << format(node.left)
  output << "\n#{_indent}"
  output << node.type.upcase << " JOIN "
  output << format(node.right)
  output << "\n#{_indent}ON " << format(node.on) if node.on

  output
end
_format_Limit(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 174
def _format_Limit(node)
  "LIMIT #{format(node.expr)}"
end
_format_Offset(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 178
def _format_Offset(node)
  "OFFSET #{format(node.expr)}"
end
_format_Parens(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 138
def _format_Parens(node)
  "(" + format(node.value) + ")"
end
_format_Select(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 15
def _format_Select(node)
  output = ""

  if @indent.nil?
    @indent = 0
  else
    @indent += 2
    output << "\n"
  end

  output << (select = "#{_indent}SELECT ")
  output << "DISTINCT " if node.distinct
  link = ","
  link << ((@projections == :wrap) ? "\n#{" " * select.length}" : " ")
  output << node.projections.map { |c| format(c) }.join(link)
  output << "\n"

  if node.froms
    output << "#{_indent}FROM "
    output << node.froms.map { |c| format(c) }.join(", ")
    output << "\n"
  end

  if node.wheres
    save, @state = @state, :where
    output << "#{_indent}WHERE "
    output << format(node.wheres)
    output << "\n"
    @state = save
  end

  if node.groups
    output << "#{_indent}GROUP BY "
    output << node.groups.map { |c| format(c) }.join(", ")
    output << "\n"
  end

  if node.orders
    output << "#{_indent}ORDER BY "
    output << node.orders.map { |c| format(c) }.join(", ")
    output << "\n"
  end

  if node.limit || node.offset
    output << _indent

    if node.limit
      output << format(node.limit)
      output << " " if node.offset
    end

    output << format(node.offset) if node.offset

    output << "\n"
  end

  @indent -= 2
  @indent = nil if @indent < 0

  output << _indent
end
_format_SortKey(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 162
def _format_SortKey(node)
  output = ""
  output << format(node.key)

  if node.options.any?
    output << " "
    output << node.options.map { |opt| opt.upcase }.join(" ")
  end

  output
end
_format_String(string) click to toggle source
# File lib/sqlpp/formatter.rb, line 182
def _format_String(string)
  string
end
_format_Subscript(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 186
def _format_Subscript(node)
  output = ""
  output << format(node.left)
  output << "[" << format(node.right) << "]"
  output
end
_format_TypeCast(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 193
def _format_TypeCast(node)
  format(node.value) << '::' << format(node.type)
end
_format_Unary(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 95
def _format_Unary(node)
  op = node.op.to_s.upcase
  output = op
  output << " " if op =~ /\w/
  output << format(node.expr)
end
_indent() click to toggle source
# File lib/sqlpp/formatter.rb, line 197
def _indent
  " " * (@indent || 0)
end
format(node) click to toggle source
# File lib/sqlpp/formatter.rb, line 10
def format(node)
  name = node.class.to_s.split(/::/).last
  send(:"_format_#{name}", node)
end