Class: Amalgalite::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/amalgalite/table.rb

Overview

a class representing the meta information about an SQLite table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sql = nil) ⇒ Table

Returns a new instance of Table.



28
29
30
31
32
33
34
35
# File 'lib/amalgalite/table.rb', line 28

def initialize( name, sql = nil )
  @name        = name
  @sql         = sql
  @indexes     = {}
  @columns     = {}
  @schema      = nil
  @primary_key = nil
end

Instance Attribute Details

#columnsObject

a hash of Column objects holding the meta information about the columns in this table. keys are the column names



26
27
28
# File 'lib/amalgalite/table.rb', line 26

def columns
  @columns
end

#indexesObject

hash of Index objects holding the meta informationa about the indexes on this table. The keys of the indexes variable is the index name



22
23
24
# File 'lib/amalgalite/table.rb', line 22

def indexes
  @indexes
end

#nameObject (readonly)

the table name



15
16
17
# File 'lib/amalgalite/table.rb', line 15

def name
  @name
end

#schemaObject

the schema object the table is associated with



12
13
14
# File 'lib/amalgalite/table.rb', line 12

def schema
  @schema
end

#sqlObject (readonly)

the original sql that was used to create this table



18
19
20
# File 'lib/amalgalite/table.rb', line 18

def sql
  @sql
end

Instance Method Details

#column_namesObject

the column names in original definition order



48
49
50
# File 'lib/amalgalite/table.rb', line 48

def column_names
  columns_in_order.map { |c| c.name }
end

#columns_in_orderObject

the Columns in original definition order



43
44
45
# File 'lib/amalgalite/table.rb', line 43

def columns_in_order
  @columns.values.sort_by { |c| c.order }
end

#primary_keyObject

the array of colmuns that make up the primary key of the table since a primary key has an index, we loop over all the indexes for the table and pick the first one that is unique, and all the columns in the index have primary_key? as true.

we do this instead of just looking for the columns where primary key is true because we want the columns in primary key order



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/amalgalite/table.rb', line 64

def primary_key
  unless @primary_key
    pk_column_names = Set.new( primary_key_columns.collect { |c| c.name } )
    unique_indexes  = indexes.values.find_all { |i| i.unique? }

    pk_result = []

    unique_indexes.each do |idx|
      idx_column_names = Set.new( idx.columns.collect { |c| c.name } )
      r = idx_column_names ^ pk_column_names
      if r.size == 0 then
        pk_result = idx.columns
        break
      end
    end

    # no joy, see about just using all the columns that say the are primary
    # keys
    if pk_result.empty? then
      pk_result = self.primary_key_columns
    end
    @primary_key = pk_result
  end
  return @primary_key
end

#primary_key_columnsObject

the columns that make up the primary key



53
54
55
# File 'lib/amalgalite/table.rb', line 53

def primary_key_columns
  @columns.values.find_all { |c| c.primary_key? }
end

#temporary?Boolean

Is the table a temporary table or not

Returns:



38
39
40
# File 'lib/amalgalite/table.rb', line 38

def temporary?
  schema.temporary?
end