Class: Bricolage::SQLStatement
- Inherits:
-
Object
- Object
- Bricolage::SQLStatement
- Defined in:
- lib/bricolage/sqlstatement.rb
Instance Attribute Summary collapse
- #declarations ⇒ Object
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #attributes ⇒ Object
- #bind(ctx, vars) ⇒ Object
- #hash ⇒ Object
-
#initialize(resource, declarations = nil) ⇒ SQLStatement
constructor
A new instance of SQLStatement.
- #inspect ⇒ Object
- #kind ⇒ Object
- #location ⇒ Object
- #meta_data ⇒ Object
- #raw_content ⇒ Object
- #replace(re, subst) ⇒ Object
- #source ⇒ Object
- #stripped_raw_content ⇒ Object
- #stripped_source ⇒ Object
Constructor Details
#initialize(resource, declarations = nil) ⇒ SQLStatement
Returns a new instance of SQLStatement.
15 16 17 18 19 20 |
# File 'lib/bricolage/sqlstatement.rb', line 15 def initialize(resource, declarations = nil) @resource = resource @declarations = declarations @code = nil @replace = nil end |
Instance Attribute Details
#declarations ⇒ Object
35 36 37 |
# File 'lib/bricolage/sqlstatement.rb', line 35 def declarations @declarations ||= Declarations.parse(@resource) end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
22 23 24 |
# File 'lib/bricolage/sqlstatement.rb', line 22 def resource @resource end |
Class Method Details
.delete_where(cond, location = nil) ⇒ Object
7 8 9 |
# File 'lib/bricolage/sqlstatement.rb', line 7 def SQLStatement.delete_where(cond, location = nil) for_string("delete from $dest_table where #{cond};", location) end |
.for_string(sql, location = nil) ⇒ Object
11 12 13 |
# File 'lib/bricolage/sqlstatement.rb', line 11 def SQLStatement.for_string(sql, location = nil) new(StringResource.new(sql, location)) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
24 25 26 27 |
# File 'lib/bricolage/sqlstatement.rb', line 24 def ==(other) return false unless other.kind_of?(SQLStatement) @resource == other.resource end |
#attributes ⇒ Object
93 94 95 |
# File 'lib/bricolage/sqlstatement.rb', line 93 def attributes Array(['attributes']) end |
#bind(ctx, vars) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/bricolage/sqlstatement.rb', line 46 def bind(ctx, vars) raise FatalError, "already bound SQL statement: #{@resource.name}" if @code src = ctx.parameter_file_loader.eruby(@resource.content, @resource.name || '-') @code = Variable.(apply_replace(src)) {|name| val = vars[name] val.respond_to?(:to_sql) ? val.to_sql : val.to_s } end |
#hash ⇒ Object
31 32 33 |
# File 'lib/bricolage/sqlstatement.rb', line 31 def hash @resource.hash end |
#inspect ⇒ Object
77 78 79 |
# File 'lib/bricolage/sqlstatement.rb', line 77 def inspect "\#<#{self.class} #{@resource.inspect}>" end |
#kind ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/bricolage/sqlstatement.rb', line 67 def kind # This implementation is not complete but useful first_word = @resource.content.gsub(%r<'(?:[^']+|'')*'|"(?:[^"]+|"")*"|--.*|/\*(?m:.*?)\*/>, '').strip.slice(/\A\w+/) op = first_word ? first_word.downcase : nil case op when 'with' then 'select' else op end end |
#location ⇒ Object
55 56 57 |
# File 'lib/bricolage/sqlstatement.rb', line 55 def location @resource.name end |
#meta_data ⇒ Object
89 90 91 |
# File 'lib/bricolage/sqlstatement.rb', line 89 def SQLMetaDataParser.new.parse(raw_content) end |
#raw_content ⇒ Object
81 82 83 |
# File 'lib/bricolage/sqlstatement.rb', line 81 def raw_content @resource.content end |
#replace(re, subst) ⇒ Object
41 42 43 44 |
# File 'lib/bricolage/sqlstatement.rb', line 41 def replace(re, subst) @replace = [re, subst] self end |
#source ⇒ Object
59 60 61 |
# File 'lib/bricolage/sqlstatement.rb', line 59 def source @code or raise FatalError, "unbound SQL statement: #{@resource.name}" end |
#stripped_raw_content ⇒ Object
85 86 87 |
# File 'lib/bricolage/sqlstatement.rb', line 85 def stripped_raw_content strip_sql(raw_content) end |
#stripped_source ⇒ Object
63 64 65 |
# File 'lib/bricolage/sqlstatement.rb', line 63 def stripped_source strip_sql(source) end |