Class: Ronin::Code::SQL::Program
- Inherits:
-
Object
- Object
- Ronin::Code::SQL::Program
- Defined in:
- lib/ronin/code/sql/program.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dialect ⇒ Object
readonly
Name of the dialect.
-
#less_parenthesis ⇒ Object
Compile with less parenthesis.
-
#lowercase ⇒ Object
Use lowercase style.
-
#multiline ⇒ Object
Use single-line or multi-line style.
-
#newline ⇒ Object
New-line string.
-
#space ⇒ Object
Space string.
Class Method Summary collapse
Instance Method Summary collapse
- #compile ⇒ Object (also: #to_s)
-
#initialize(options = {}, &block) ⇒ Program
constructor
A new instance of Program.
- #select(*arguments, &block) ⇒ Object
- #symbols ⇒ Object
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(={},&block) [:dialect] ||= :common [:symbols] ||= {} if .has_key?(:multiline) @multiline = [:multiline] else @multiline = true end if .has_key?(:lowercase) @lowercase = [:lowercase] else @lowercase = false end if .has_key?(:less_parens) @less_parens = [:less_parens] else @less_parens = false end @space = Chars::CharSet.new([:space] || ' ') @newline = Chars::CharSet.new([:newline] || "\n") @dialect = Dialect.get([:dialect]).new([: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)
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
#dialect ⇒ Object (readonly)
Name of the dialect
33 34 35 |
# File 'lib/ronin/code/sql/program.rb', line 33 def dialect @dialect end |
#less_parenthesis ⇒ Object
Compile with less parenthesis
42 43 44 |
# File 'lib/ronin/code/sql/program.rb', line 42 def less_parenthesis @less_parenthesis end |
#lowercase ⇒ Object
Use lowercase style
39 40 41 |
# File 'lib/ronin/code/sql/program.rb', line 39 def lowercase @lowercase end |
#multiline ⇒ Object
Use single-line or multi-line style
36 37 38 |
# File 'lib/ronin/code/sql/program.rb', line 36 def multiline @multiline end |
#newline ⇒ Object
New-line string
48 49 50 |
# File 'lib/ronin/code/sql/program.rb', line 48 def newline @newline end |
#space ⇒ Object
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(={},&block) self.new(,&block).compile end |
Instance Method Details
#compile ⇒ Object 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 |
#symbols ⇒ Object
84 85 86 |
# File 'lib/ronin/code/sql/program.rb', line 84 def symbols @dialect.symbols end |