| Class | Amalgalite::Schema |
| In: |
lib/amalgalite/schema.rb
|
| Parent: | Object |
An object view of the schema in the SQLite database. If the schema changes after this class is created, it has no knowledge of that.
| catalog | [R] | |
| db | [R] | |
| schema | [R] | |
| tables | [R] | |
| views | [R] |
load all the columns for a particular table
# File lib/amalgalite/schema.rb, line 87
87: def load_columns( table )
88: cols = {}
89: @db.execute("PRAGMA table_info(#{table.name})") do |row|
90: col = Amalgalite::Column.new( "main", table.name, row['name'] )
91:
92: col.default_value = row['dflt_value']
93: @db.api.table_column_metadata( "main", table.name, col.name ).each_pair do |key, value|
94: col.send("#{key}=", value)
95: end
96: col.schema = self
97: cols[col.name] = col
98: end
99: cols
100: end
load all the indexes for a particular table
# File lib/amalgalite/schema.rb, line 62
62: def load_indexes( table )
63: indexes = {}
64:
65: @db.prepare("SELECT name, sql FROM sqlite_master WHERE type ='index' and tbl_name = $name") do |idx_stmt|
66: idx_stmt.execute( "$name" => table.name) do |idx_info|
67: indexes[idx_info['name']] = Amalgalite::Index.new( idx_info['name'], idx_info['sql'], table )
68: end
69: end
70:
71: @db.execute("PRAGMA index_list( #{table.name} );") do |idx_list|
72: idx = indexes[idx_list['name']]
73:
74: idx.sequence_number = idx_list['seq']
75: idx.unique = Boolean.to_bool( idx_list['unique'] )
76:
77: @db.execute("PRAGMA index_info( #{idx.name} );") do |col_info|
78: idx.columns << table.columns[col_info['name']]
79: end
80: end
81: return indexes
82: end
load the schema from the database
# File lib/amalgalite/schema.rb, line 37
37: def load_schema!
38: load_tables
39: load_views
40: end
load all the tables
# File lib/amalgalite/schema.rb, line 45
45: def load_tables
46: @tables = {}
47: @db.execute("SELECT tbl_name, sql FROM sqlite_master WHERE type = 'table'") do |table_info|
48: table = Amalgalite::Table.new( table_info['tbl_name'], table_info['sql'] )
49: table.columns = load_columns( table )
50: table.schema = self
51: table.indexes = load_indexes( table )
52:
53: @tables[table.name] = table
54: end
55:
56: @tables
57: end
load all the views for the database
# File lib/amalgalite/schema.rb, line 105
105: def load_views
106: @views = {}
107: @db.execute("SELECT name, sql FROM sqlite_master WHERE type = 'view'") do |view_info|
108: view = Amalgalite::View.new( view_info['name'], view_info['sql'] )
109: view.schema = self
110: @views[view.name] = view
111: end
112: @views
113: end