Class: TwoWaySQL::Template
- Inherits:
-
Object
- Object
- TwoWaySQL::Template
- Defined in:
- lib/twowaysql/template.rb
Overview
TwoWaySQL::Template represents template object, acts as a Facade for this package. Template is stateless, reentrant object so template object is cacheable.
Class Method Summary collapse
-
.parse(sql_io, opts = {}) ⇒ Object
parse TwoWaySQL-style SQL then return TwoWaySQL::Template object.
Instance Method Summary collapse
-
#merge(data) ⇒ Object
(also: #mungle)
merge data with template.
Class Method Details
.parse(sql_io, opts = {}) ⇒ Object
parse TwoWaySQL-style SQL then return TwoWaySQL::Template object
Usage
sql = "SELECT * FROM emp WHERE job = /*ctx[:job]*/'CLERK' AND deptno = /*ctx[:deptno]*/20"
template = TwoWaySQL::Template.parse(sql)
Arguments
sql_io
-
IO-like object that contains TwoWaySQL-style SQL
opts
-
(optional) Hash of parse options to pass internal lexer. default is empty hash.
Return
TwoWaySQL::Template object that represents parse result
26 27 28 29 30 |
# File 'lib/twowaysql/template.rb', line 26 def Template.parse(sql_io, opts={}) parser = Parser.new(opts) root = parser.parse(sql_io) Template.new(root) end |
Instance Method Details
#merge(data) ⇒ Object Also known as: mungle
merge data with template
Usage
merged = template.merge(:job => "HOGE", :deptno => 30)
merged.sql #=> "SELECT * FROM emp WHERE job = ? AND deptno = ?"
merged.bound_variables #=> ["HOGE", 30]
Arguments
data
-
Hash-like object that contains data to merge. This data will evaluated as name ‘ctx’.
Return
TwoWaySQL::Result object that represents merge result
49 50 51 52 53 |
# File 'lib/twowaysql/template.rb', line 49 def merge(data) c = Context.new(data) @root.accept(c) Result.new(c) end |