Class: Bricolage::SQLStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/bricolage/sqlstatement.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#declarationsObject



35
36
37
# File 'lib/bricolage/sqlstatement.rb', line 35

def declarations
  @declarations ||= Declarations.parse(@resource)
end

#resourceObject (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

#attributesObject



93
94
95
# File 'lib/bricolage/sqlstatement.rb', line 93

def attributes
  Array(['attributes'])
end

#bind(ctx, vars) ⇒ Object

Raises:



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.expand_string(apply_replace(src)) {|name|
    val = vars[name]
    val.respond_to?(:to_sql) ? val.to_sql : val.to_s
  }
end

#hashObject



31
32
33
# File 'lib/bricolage/sqlstatement.rb', line 31

def hash
  @resource.hash
end

#inspectObject



77
78
79
# File 'lib/bricolage/sqlstatement.rb', line 77

def inspect
  "\#<#{self.class} #{@resource.inspect}>"
end

#kindObject



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

#locationObject



55
56
57
# File 'lib/bricolage/sqlstatement.rb', line 55

def location
  @resource.name
end

#meta_dataObject



89
90
91
# File 'lib/bricolage/sqlstatement.rb', line 89

def 
  SQLMetaDataParser.new.parse(raw_content)
end

#raw_contentObject



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

#sourceObject



59
60
61
# File 'lib/bricolage/sqlstatement.rb', line 59

def source
  @code or raise FatalError, "unbound SQL statement: #{@resource.name}"
end

#stripped_raw_contentObject



85
86
87
# File 'lib/bricolage/sqlstatement.rb', line 85

def stripped_raw_content
  strip_sql(raw_content)
end

#stripped_sourceObject



63
64
65
# File 'lib/bricolage/sqlstatement.rb', line 63

def stripped_source
  strip_sql(source)
end