Class: Ronin::Code::SQL::Program

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/code/sql/program.rb

Direct Known Subclasses

Injection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Program

Returns a new instance of Program.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ronin/code/sql/program.rb', line 50

def initialize(options={},&block)
  options[:dialect] ||= :common
  options[:symbols] ||= {}

  if options.has_key?(:multiline)
    @multiline = options[:multiline]
  else
    @multiline = true
  end

  if options.has_key?(:lowercase)
    @lowercase = options[:lowercase]
  else
    @lowercase = false
  end

  if options.has_key?(:less_parens)
    @less_parens = options[:less_parens]
  else
    @less_parens = false
  end

  @space = Chars::CharSet.new(options[:space] || ' ')
  @newline = Chars::CharSet.new(options[:newline] || "\n")

  @dialect = Dialect.get(options[:dialect]).new(options[:symbols])

  instance_eval(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object (protected)

Raises:

  • (NoMethodError)


208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ronin/code/sql/program.rb', line 208

def method_missing(name,*arguments,&block)
  if @dialect.has_statement?(name)
    return @dialect.enqueue_statement(name,*arguments,&block)
  elsif @dialect.methods.include?(name.to_s)
    return @dialect.send(name,*arguments,&block)
  elsif (arguments.empty? && block.nil?)
    if @dialect.symbols.has_symbol?(name)
      return @dialect.symbols[name]
    end
  end

  raise(NoMethodError,name.id2name)
end

Instance Attribute Details

#dialectObject (readonly)

Name of the dialect



33
34
35
# File 'lib/ronin/code/sql/program.rb', line 33

def dialect
  @dialect
end

#less_parenthesisObject

Compile with less parenthesis



42
43
44
# File 'lib/ronin/code/sql/program.rb', line 42

def less_parenthesis
  @less_parenthesis
end

#lowercaseObject

Use lowercase style



39
40
41
# File 'lib/ronin/code/sql/program.rb', line 39

def lowercase
  @lowercase
end

#multilineObject

Use single-line or multi-line style



36
37
38
# File 'lib/ronin/code/sql/program.rb', line 36

def multiline
  @multiline
end

#newlineObject

New-line string



48
49
50
# File 'lib/ronin/code/sql/program.rb', line 48

def newline
  @newline
end

#spaceObject

Space string



45
46
47
# File 'lib/ronin/code/sql/program.rb', line 45

def space
  @space
end

Class Method Details

.compile(options = {}, &block) ⇒ Object



80
81
82
# File 'lib/ronin/code/sql/program.rb', line 80

def self.compile(options={},&block)
  self.new(options,&block).compile
end

Instance Method Details

#compileObject Also known as: to_s



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ronin/code/sql/program.rb', line 92

def compile
  sql = []
  stmt = ['']
  prev = nil

  each_string do |current|
    if current == ';'
      sql << stmt
      stmt = ['']
    elsif current == '('
      next if @less_parens

      stmt << current
    elsif current == ')'
      next if @less_parens

      stmt.last << current
    elsif (current == ',' || prev == '(')
      stmt.last << current
    elsif prev == ','
      if @less_parens
        stmt.last << current
      else
        stmt << current
      end
    else
      stmt << current
    end

    prev = current
  end

  sql_string = ''

  sql.each_with_index do |stmt,stmt_index|
    stmt_string = ''

    stmt.each_with_index do |token,token_index|
      unless token.empty?
        sql_string << token

        unless token_index == (stmt.length - 1)
          sql_string << space_token
        end
      end
    end

    sql_string << stmt_string

    unless stmt_index == (sql.length - 1)
      if @multiline
        sql_string << newline_token
      else
        sql_string << ';'
        sql_string << space_token
      end
    end
  end

  return sql_string
end

#select(*arguments, &block) ⇒ Object



88
89
90
# File 'lib/ronin/code/sql/program.rb', line 88

def select(*arguments,&block)
  @dialect.statement(:select,*arguments,&block)
end

#symbolsObject



84
85
86
# File 'lib/ronin/code/sql/program.rb', line 84

def symbols
  @dialect.symbols
end