Class: Dbsketch::Automation::TableImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/dbsketch/automation/table_importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ TableImporter

Returns a new instance of TableImporter.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# File 'lib/dbsketch/automation/table_importer.rb', line 13

def initialize db
	### Preconditions
	raise ArgumentError, "database is not a Dbsketch::Automation::DatabaseProxy" unless db.is_a? DatabaseProxy
	###
	@db = db
end

Instance Method Details

#import(table_name) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dbsketch/automation/table_importer.rb', line 20

def import table_name
	### Preconditions
	raise ArgumentError, "table_name '#{table_name}' is not a Symbol or a String" unless table_name.is_a? String or table_name.is_a? Symbol
	###
	db_table = @db.fetch("select object_id, name from sys.tables where name = '#{table_name.to_s}'").all.first
	### Preconditions
	raise AutomationError, "table '#{table_symbol}' not found in database" if nil == db_table
	###
	table = Dbsketch::Model::Table.new db_table[:name]
	@db.fetch("select * from information_schema.columns where table_name = '#{db_table[:name]}'").all.each do |db_column|
		table.add(parse_column db_table, db_column)
	end

	primary_key = import_primary_key db_table, table
	table.add primary_key if nil != primary_key

	@db.fetch("select name, definition from sys.check_constraints where parent_object_id = '#{db_table[:object_id]}'").all.each do |db_constraint|
		table.add(Dbsketch::Model::CheckConstraint.new db_constraint[:name], db_constraint[:definition])
	end

	@db.fetch("select name from sys.key_constraints where parent_object_id = #{db_table[:object_id]} and type = 'UQ'").all.each do |db_constraint|
		table.add(import_unique_constraint db_table, table, db_constraint[:name])
	end

	table
end