class Eatr::Sql::TableGenerator

Public Class Methods

new(schema_path) click to toggle source
# File lib/eatr/sql/table_generator.rb, line 4
def initialize(schema_path)
  @schema = Schema.new(YAML.load(File.read(schema_path)))
end

Public Instance Methods

statement() click to toggle source
# File lib/eatr/sql/table_generator.rb, line 8
      def statement
        <<-STATEMENT
CREATE TABLE #{@schema.table_name} (
  #{column_defs.join(",\n  ")}
);
        STATEMENT
      end

Private Instance Methods

column_defs() click to toggle source
# File lib/eatr/sql/table_generator.rb, line 18
def column_defs
  @schema.flat_fields.map do |f|
    "#{f.name} #{type(f)}#{nullness(f)}"
  end
end
nullness(f) click to toggle source
# File lib/eatr/sql/table_generator.rb, line 45
def nullness(f)
  if f.required?
    " NOT NULL"
  end
end
type(f) click to toggle source
# File lib/eatr/sql/table_generator.rb, line 24
def type(f)
  case f.type
  when nil,'string',''
    if f.length
      "CHAR(#{f.length})"
    elsif f.max_length
      "VARCHAR(#{f.max_length})"
    else
      'TEXT'
    end
  when 'integer'
    'INT'
  when 'float'
    'REAL'
  when 'timestamp'
    'TIMESTAMP'
  when 'boolean'
    'BOOLEAN'
  end
end