### Home Classes Methods

### In Files

- lib/amalgalite/schema.rb

### Parent

Object

### Methods

- ::new
- #current_version
- #dirty?
- #load_columns
- #load_indexes
- #load_schema!
- #load_table
- #load_tables
- #load_view
- #load_views
- #tables
- #views

### Files

- HISTORY.rdoc
- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- Amalgalite
- Amalgalite::Aggregate
- Amalgalite::Blob
- Amalgalite::Blob::Error
- Amalgalite::Boolean
- Amalgalite::BusyHandler
- Amalgalite::BusyTimeout
- Amalgalite::CSVTableImporter
- Amalgalite::Column
- Amalgalite::Database
- Amalgalite::Database::AggregateError
- Amalgalite::Database::BusyHandlerError
- Amalgalite::Database::FunctionError
- Amalgalite::Database::InvalidModeError
- Amalgalite::Database::ProgressHandlerError
- Amalgalite::Database::TransactionBehavior
- Amalgalite::Error
- Amalgalite::Function
- Amalgalite::Index
- Amalgalite::MemoryDatabase
- Amalgalite::Packer
- Amalgalite::Paths
- Amalgalite::ProfileSampler
- Amalgalite::ProfileTap
- Amalgalite::ProgressHandler
- Amalgalite::Requires
- Amalgalite::SQLite3
- Amalgalite::SQLite3::Constants
- Amalgalite::SQLite3::Constants::Config
- Amalgalite::SQLite3::Constants::DBStatus
- Amalgalite::SQLite3::Constants::DataType
- Amalgalite::SQLite3::Constants::Helpers
- Amalgalite::SQLite3::Constants::Open
- Amalgalite::SQLite3::Constants::ResultCode
- Amalgalite::SQLite3::Constants::StatementStatus
- Amalgalite::SQLite3::Constants::Status
- Amalgalite::SQLite3::Database
- Amalgalite::SQLite3::Database::DBStatus
- Amalgalite::SQLite3::Database::Function
- Amalgalite::SQLite3::Database::Stat
- Amalgalite::SQLite3::Stat
- Amalgalite::SQLite3::Status
- Amalgalite::SQLite3::Version
- Amalgalite::Schema
- Amalgalite::Statement
- Amalgalite::Table
- Amalgalite::Taps
- Amalgalite::Taps::IO
- Amalgalite::Taps::Stderr
- Amalgalite::Taps::Stdout
- Amalgalite::Taps::StringIO
- Amalgalite::TraceTap
- Amalgalite::TypeMap
- Amalgalite::TypeMaps
- Amalgalite::TypeMaps::DefaultMap
- Amalgalite::TypeMaps::StorageMap
- Amalgalite::TypeMaps::TextMap
- Amalgalite::Version
- Amalgalite::View
- Kernel
- Object

No matching classes.

# Amalgalite::Schema

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.

### Attributes

catalog[R]
db[R]
dirty[W]
schema[R]
schema_version[R]

### Public Class Methods

new( db, catalog = 'main', schema = 'sqlite') click to toggle source

Create a new instance of Schema

    # File lib/amalgalite/schema.rb, line 27
    def initialize( db, catalog = 'main', schema = 'sqlite')
      @db = db
      @catalog = catalog
      @schema = schema
      @schema_version = nil
      @tables = {}
      @views  = {}
      load_schema!
    end

### Public Instance Methods

current_version() click to toggle source

    # File lib/amalgalite/schema.rb, line 41
    def current_version
      @db.first_value_from("PRAGMA schema_version")
    end

dirty?() click to toggle source

    # File lib/amalgalite/schema.rb, line 37
    def dirty?()
      return (@schema_version != self.current_version)
    end

load_columns( table ) click to toggle source

load all the columns for a particular table

    # File lib/amalgalite/schema.rb, line 134
    def load_columns( table )
      cols = {}
      idx = 0
      @db.execute("PRAGMA table_info(#{@db.quote(table.name)})") do |row|
        col = Amalgalite::Column.new( "main", table.name, row['name'], row['cid'])

        col.default_value       = row['dflt_value']

        col.declared_data_type  = row['type']
        col.not_null_constraint = row['notnull']
        col.primary_key         = row['pk']

        # need to remove leading and trailing ' or " from the default value
        if col.default_value and col.default_value.kind_of?( String ) and ( col.default_value.length >= 2 ) then
          fc = col.default_value[0].chr
          lc = col.default_value[-1].chr
          if fc == lc and ( fc == "'" || fc == '"' ) then
            col.default_value = col.default_value[1..-2]
          end
        end

        unless table.temporary? then
          # get more exact information
          @db.api.table_column_metadata( "main", table.name, col.name ).each_pair do |key, value|
            col.send("#{key}=", value)
          end
        end
        col.schema = self
        cols[col.name] = col
        idx += 1
      end
      return cols
    end

load_indexes( table ) click to toggle source

load all the indexes for a particular table

    # File lib/amalgalite/schema.rb, line 103
    def load_indexes( table )
      indexes = {}

      @db.prepare("SELECT name, sql FROM sqlite_master WHERE type ='index' and tbl_name = $name") do |idx_stmt|
        idx_stmt.execute( "$name" => table.name) do |idx_info|
          indexes[idx_info['name']] = Amalgalite::Index.new( idx_info['name'], idx_info['sql'], table )
        end
      end

      @db.execute("PRAGMA index_list( #{@db.quote(table.name)} );") do |idx_list|
        idx = indexes[idx_list['name']]

        # temporary indexes do not show up in the previous list
        if idx.nil? then
          idx = Amalgalite::Index.new( idx_list['name'], nil, table )
          indexes[idx_list['name']] = idx
        end

        idx.sequence_number = idx_list['seq']
        idx.unique          = Boolean.to_bool( idx_list['unique'] )

        @db.execute("PRAGMA index_info( #{@db.quote(idx.name)} );") do |col_info|
          idx.columns << table.columns[col_info['name']]
        end
      end
      return indexes
    end

load_schema!() click to toggle source

load the schema from the database

    # File lib/amalgalite/schema.rb, line 47
    def load_schema!
      load_tables
      load_views
      @schema_version = self.current_version
      nil
    end

load_table( table_name ) click to toggle source

Load a single table

    # File lib/amalgalite/schema.rb, line 76
    def load_table( table_name )
      rows = @db.execute("SELECT tbl_name, sql FROM sqlite_master WHERE type = 'table' AND tbl_name = ?", table_name)
      table_info = rows.first
      table = nil
      if table_info then
        table = Amalgalite::Table.new( table_info['tbl_name'], table_info['sql'] )
        table.columns = load_columns( table )
        table.schema = self
        table.indexes = load_indexes( table )
        @tables[table.name] = table
      else
        # might be a temporary table
        table = Amalgalite::Table.new( table_name, nil )
        cols = load_columns( table )
        if cols.size > 0 then
          table.columns = cols
          table.schema = self
          table.indexes = load_indexes( table )
          @tables[table.name] = table
        end
      end
      return table
    end

load_tables() click to toggle source

load all the tables

    # File lib/amalgalite/schema.rb, line 64
    def load_tables
      @tables = {}
      @db.execute("SELECT tbl_name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence'") do |table_info|
        table = load_table( table_info['tbl_name'] )
        table.indexes = load_indexes( table )
        @tables[table.name] = table
      end
      return @tables
    end

load_view( name ) click to toggle source

load a single view

    # File lib/amalgalite/schema.rb, line 179
    def load_view( name )
      rows = @db.execute("SELECT name, sql FROM sqlite_master WHERE type = 'view' AND name = ?", name )
      view_info = rows.first
      view = Amalgalite::View.new( view_info['name'], view_info['sql'] )
      view.schema = self
      return view
    end

load_views() click to toggle source

load all the views for the database

    # File lib/amalgalite/schema.rb, line 190
    def load_views
      @db.execute("SELECT name, sql FROM sqlite_master WHERE type = 'view'") do |view_info|
        view = load_view( view_info['name'] )
        @views[view.name] = view
      end
      return @views
    end

tables() click to toggle source

return the tables, reloading if dirty

    # File lib/amalgalite/schema.rb, line 56
    def tables
      load_schema! if dirty?
      return @tables
    end

views() click to toggle source

return the views, reloading if dirty

    # File lib/amalgalite/schema.rb, line 171
    def views
      reload_schema! if dirty?
      return @views
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.