Class: MiqSqlite3DB::MiqSqlite3Table

Inherits:
Object
  • Object
show all
Defined in:
lib/db/MiqSqlite/MiqSqlite3Table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, pagenum, cell) ⇒ MiqSqlite3Table

Returns a new instance of MiqSqlite3Table.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 49

def initialize(db, pagenum, cell)
  @pagenum = pagenum
  @db      = db
  @fields  = cell.fields
  @type    = @fields[0]['data']
  @name    = @fields[1]['data']
  @desc    = @fields[2]['data']
  @data    = @fields[3]['data']
  @sql     = @fields[4]['data']
  @columns = nil
  decodeSchema
end

Instance Attribute Details

#nameObject (readonly)

Instance Methods



47
48
49
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 47

def name
  @name
end

#typeObject (readonly)

Instance Methods



47
48
49
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 47

def type
  @type
end

Class Method Details

.each(db) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 32

def self.each(db)
  root_page = MiqSqlite3Page.getPage(db, 1)

  root_page.each_child do |child|
    page = MiqSqlite3Page.getPage(db, child)
    page.each_cell do |cell|
      yield MiqSqlite3Table.new(db, child, cell)
    end
  end
end

.getTable(db, name) ⇒ Object



25
26
27
28
29
30
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 25

def self.getTable(db, name)
  MiqSqlite3Table.each(db) do |table|
    return table if name == table.name
  end
  nil
end

.index_names(db) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 17

def self.index_names(db)
  names = []
  MiqSqlite3Table.each(db) do |table|
    names << table.name if 'index' == table.type
  end
  names
end

.table_names(db) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 9

def self.table_names(db)
  names = []
  MiqSqlite3Table.each(db) do |table|
    names << table.name if 'table' == table.type
  end
  names
end

Instance Method Details

#decodeSchemaObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 62

def decodeSchema
  return if @type != "table" || @name[0..6] == "sqlite_"  # Names beginning with sqlite_ are internal to engine
  @columns = []
  sql = @sql.gsub(/[\n\r]/, "")
  re1 = /\s*CREATE\s+TABLE\s+(\w+)\s*\((.*)\)\s*/
  m = re1.match(sql)
  tname = m[1].to_s.chomp
  raise "Inconsistent Table Name" if tname != @name
  cols  = m[2].to_s

  cols.split(",").each do |c|
    words = c.split
    defn = {}
    defn['name'] = words[0]
    defn['type'] = words[1].downcase
    defn['type'] = "key" if words[2] && words[2].upcase == "PRIMARY"
    if defn['type'] == "key"
      @key = defn['name']
    else
      @columns << defn
    end
  end
end

#dumpObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 105

def dump
  puts "================="
  puts "Page:                            #{@pagenum}"
  puts "Length:                          #{@len}"
  puts "Type:                            #{@type}"
  puts "Name:                            #{@name}"
  puts "Description:                     #{@desc}"
  puts "Data Begins on Page:             #{@data}"
  puts "SQL:                             #{@sql}"
  puts "Key:                             #{@key}"  if @key
  if @columns
    for i in 1..@columns.size
      puts "Column #{i}:                        #{@columns[i - 1]['name']} => #{@columns[i - 1]['type']}"
    end
  end
end

#each_columnObject



101
102
103
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 101

def each_column
  @columns.each { |col| yield col }
end

#each_rowObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/db/MiqSqlite/MiqSqlite3Table.rb', line 86

def each_row
  MiqSqlite3Page.getPage(@db, @data).leaves do |leaf|
    leaf.each_cell do |cell|
      row = {}
      row[@key] = cell.key if @key
      i = @key ? 1 : 0
      each_column do |col|
        row[col['name']] = cell.fields[i]['data']
        i += 1
      end
      yield row
    end
  end
end