Class: DbCompile::Construct

Inherits:
Object
  • Object
show all
Defined in:
lib/dbcompile/construct.rb

Overview

Anything that can be expressed in SQL

Direct Known Subclasses

Function, Trigger, View

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, root_path) ⇒ Construct



9
10
11
12
13
# File 'lib/dbcompile/construct.rb', line 9

def initialize(name, root_path)
  @name = name
  @root_path = root_path
  build_path
end

Instance Attribute Details

#dependenciesObject

Returns the value of attribute dependencies.



7
8
9
# File 'lib/dbcompile/construct.rb', line 7

def dependencies
  @dependencies
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/dbcompile/construct.rb', line 4

def name
  @name
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/dbcompile/construct.rb', line 5

def path
  @path
end

#root_pathObject

Returns the value of attribute root_path.



6
7
8
# File 'lib/dbcompile/construct.rb', line 6

def root_path
  @root_path
end

Instance Method Details

#build_pathObject

Override this to set path the SQL source



16
17
# File 'lib/dbcompile/construct.rb', line 16

def build_path
end

#does_one_exist?(sql) ⇒ Boolean

checks to see if one object exists based on the sql string generated by an individual construct



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dbcompile/construct.rb', line 43

def does_one_exist?(sql)
  result = ActiveRecord::Base.connection.execute(sql)
  case ActiveRecord::Base.connection.class.to_s
  when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
    return result.num_tuples == 1
  when "ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter"
    row_count= 0
    while f = result.fetch
      row_count += 1
    end
    return row_count == 1
  else
    return result.length == 1
  end
end

#executeObject

Execute the source to create contruct in database



20
21
22
# File 'lib/dbcompile/construct.rb', line 20

def execute
  ActiveRecord::Base.connection.execute(source)
end

#sourceObject

Return the SQL source. Do any magic wrapping here.



25
26
27
28
29
30
# File 'lib/dbcompile/construct.rb', line 25

def source
  f = File.open(File.join(root_path, path))
  data = f.read
  f.close
  data
end

#verifyObject

Override to verify the existence of the construct Return true for verified successs Return false for verified failure Return nil otherwise



36
37
# File 'lib/dbcompile/construct.rb', line 36

def verify
end