class Ferret::Schema

Public Class Methods

new(schema_source) click to toggle source
Calls superclass method
# File lib/sql-ferret.rb, line 475
def initialize schema_source
  raise 'type mismatch' unless schema_source.is_a? String
  super()
  @tables = {} # keyed by forced-lowercase names
  lineno = 0
  curtable = nil
  relocs = [] # a list of [[Proc]]:s
  @used_ids = Set.new
      # so we can avoid clashes when generating aliases;
      # forced downcase
  schema_source.each_line do |line|
    line.strip!
    lineno += 1
    ugh? context: 'parsing-ferret-schema',
        input: line,
        lineno: lineno do
      if line.empty? or line[0] == ?# then
        next
      elsif line =~ /^\[\s*(\w+)\s*\]\s*(#|$)/ then
        name = $1
        dname = name.downcase
        ugh 'duplicate table name', table: name \
            if @tables.has_key? dname
        curtable = @tables[dname] = Ferret::Table.new name
        @used_ids.add dname
      elsif line =~ /^(\w+)\s*:\s*/ then
        name, spec = $1, $'
        # Note that [[add_field]] will check the field's name
        # for uniquity.
        curtable.add_field(
            Ferret::Field.new(curtable, name, spec) do |thunk|
              relocs.push thunk
            end)
        @used_ids.add name.downcase
      else
        ugh 'unparseable-line'
      end
    end
  end
  # Now that we have loaded everything, we can resolve the
  # pointers.
  @tables.each_value do |table|
    ugh 'table-without-columns',
            table: table.name \
        unless table.has_columns?
  end
  relocs.each do |thunk|
    thunk.call self
  end
  return
end

Public Instance Methods

[](name) click to toggle source
# File lib/sql-ferret.rb, line 531
def [] name
  return @tables[name.downcase]
end
alias_generator() click to toggle source
# File lib/sql-ferret.rb, line 527
def alias_generator
  return Ferret::Alias_Generator.new(@used_ids)
end
sql_to_create_table(name) click to toggle source
# File lib/sql-ferret.rb, line 539
def sql_to_create_table name
  table = self[name]
  unless table then
    ugh 'unknown-table',
        table: name
  end
  return table.sql_to_create
end
tables() click to toggle source
# File lib/sql-ferret.rb, line 535
def tables
  return @tables.values
end