Class: SqlServerSchemaReflector::TableReflection

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlserver_schema_reflector/table_reflection.rb

Instance Method Summary collapse

Constructor Details

#initialize(properties, options = {}) ⇒ TableReflection

Returns a new instance of TableReflection.



2
3
4
5
6
7
8
9
# File 'lib/sqlserver_schema_reflector/table_reflection.rb', line 2

def initialize(properties, options = {})
  @properties = properties
  defaults = {
    :schema => meta[:owner]
  }
  defaults.merge! options
  meta.merge! defaults
end

Instance Method Details

#columnsObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sqlserver_schema_reflector/table_reflection.rb', line 13

def columns
  if @columns.nil?
    @columns = []
    column_hashes = @properties.find_hashes [:column_name, :type]
    column_hashes = [column_hashes] if column_hashes.class.name == 'Hash'
    column_hashes.each do |column|
      @columns.push(SqlServerSchemaReflector::ColumnReflection.new(column, identity))
    end
  end
  @columns
end

#constraintsObject



39
40
41
42
43
44
45
# File 'lib/sqlserver_schema_reflector/table_reflection.rb', line 39

def constraints
  if @constraints.nil?
    @constraints =  @properties.find_hashes [:constraint_name, :constraint_type]
    @constraints = [@constraints] if @constraints.class.name === 'Hash'
  end
  @constraints
end

#create_sqlObject



46
47
48
49
50
51
52
# File 'lib/sqlserver_schema_reflector/table_reflection.rb', line 46

def create_sql
  sql = "CREATE TABLE [#{meta[:schema]}].[#{meta[:name]}]("
  columns.each do |c|
    sql << "\n\t#{c.sql},"
  end
  sql << "\n)"
end

#identityObject



24
25
26
27
28
29
30
31
# File 'lib/sqlserver_schema_reflector/table_reflection.rb', line 24

def identity
  if @identity.nil?
    @identity = @properties.find_hashes [:identity, :seed, :increment]
    @identity[:seed] = @identity[:seed].to_f.to_i
    @identity[:increment] = @identity[:increment].to_f.to_i
  end
  @identity
end

#indexesObject



32
33
34
35
36
37
38
# File 'lib/sqlserver_schema_reflector/table_reflection.rb', line 32

def indexes
  if @indexes.nil?
    @indexes = @properties.find_hashes [:index_name, :index_description, :index_keys]
    @indexes = [@indexes] if @indexes.class.name === 'Hash'
  end
  @indexes
end

#metaObject



10
11
12
# File 'lib/sqlserver_schema_reflector/table_reflection.rb', line 10

def meta
  @meta ||= @properties.find_hashes [:name, :owner, :type]
end