Class: Ronin::Code::SQL::Statement

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expr

#===, #in?, #not_in?

Constructor Details

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

Creates a new Statement object connected to the specified dialect. If a block is given, it will be evaluated within the newly created Statement object.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ronin/code/sql/statement.rb', line 43

def initialize(dialect,options={},&block)
  super()

  @dialect = dialect
  @clauses = []

  options.each do |name,args|
    if self.class.has_clause?(name)
      clause(name,*args)
    end
  end

  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)



167
168
169
170
171
172
173
174
175
# File 'lib/ronin/code/sql/statement.rb', line 167

def method_missing(name,*arguments,&block)
  if @dialect.has_statement?(name)
    return @dialect.statement(name,*arguments,&block)
  elsif @dialect.class.public_method_defined?(name)
    return @dialect.send(name,*arguments,&block)
  elsif (arguments.empty? && block.nil?)
    return @dialect.field(name)
  end
end

Instance Attribute Details

#clausesObject (readonly)

Returns the value of attribute clauses.



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

def clauses
  @clauses
end

Class Method Details

.clause_orderObject

Returns the Array denoting the precedence of clauses provided by the statement.



62
63
64
# File 'lib/ronin/code/sql/statement.rb', line 62

def self.clause_order
  @@clause_order ||= []
end

.clausesObject

Returns the Hash of the clause names and the Clause classes provided by the statement.



70
71
72
# File 'lib/ronin/code/sql/statement.rb', line 70

def self.clauses
  @@clauses ||= {}
end

.has_clause?(name) ⇒ Boolean

Returns true if the statement provides a clause with the specified name, returns false otherwise.

Returns:

  • (Boolean)


78
79
80
# File 'lib/ronin/code/sql/statement.rb', line 78

def self.has_clause?(name)
  self.clauses.has_key?(name.to_sym)
end

Instance Method Details

#emitObject

Returns an Array of unformatted tokens that represent the statement.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ronin/code/sql/statement.rb', line 105

def emit
  tokens = []

  @clauses.each do |clause|
    if clause
      tokens += clause.emit
    end
  end

  return tokens
end

#get_clause(name) ⇒ Object

Returns the clause with the specified name.



95
96
97
98
99
# File 'lib/ronin/code/sql/statement.rb', line 95

def get_clause(name)
  index = self.class.clause_order.index(name.to_sym)

  return @clauses[index]
end

#has_clause?(name) ⇒ Boolean

Returns true if the statement has a clause with the specified name, returns false otherwise.

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/ronin/code/sql/statement.rb', line 86

def has_clause?(name)
  index = self.class.clause_order.index(name.to_sym)

  return !(@clauses[index].nil?)
end