Class: Lafcadio::CreateTableStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/lafcadio/schema.rb

Overview

:nodoc:

Constant Summary collapse

@@simple_field_clauses =
{
	BooleanField => 'bool', BinaryField => 'blob', DateField => 'date', 
	DomainObjectField => 'int', FloatField => 'float',
	DateTimeField => 'datetime', IntegerField => 'int',
	StringField => 'varchar(255)', TextListField => 'varchar(255)',
	TimeStampField => 'timestamp'
}

Instance Method Summary collapse

Constructor Details

#initialize(domain_class) ⇒ CreateTableStatement

Returns a new instance of CreateTableStatement.



13
14
15
# File 'lib/lafcadio/schema.rb', line 13

def initialize( domain_class )
	@domain_class = domain_class
end

Instance Method Details

#definition_terms(field) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/lafcadio/schema.rb', line 17

def definition_terms( field )
	definitionTerms = []
	definitionTerms << field.db_field_name
	definitionTerms << type_clause( field )
	definitionTerms << 'not null' if field.not_nil
	definitionTerms.join( ' ' )
end

#to_sqlObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lafcadio/schema.rb', line 25

def to_sql
	createDefinitions = []
	createDefinitions <<
		"#{ @domain_class.sql_primary_key_name } int not null auto_increment"
	createDefinitions <<
			"primary key (#{ @domain_class.sql_primary_key_name })"
	@domain_class.class_fields.each { |field|
		createDefinitions << definition_terms( field )
	}
	<<-SQL
create table #{ @domain_class.table_name } (
#{ createDefinitions.join(",\n  ") }
);
	SQL
end

#type_clause(field) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/lafcadio/schema.rb', line 41

def type_clause( field )
	if ( type_clause = @@simple_field_clauses[field.class] )
		type_clause
	elsif ( field.class <= EnumField )
		singleQuotedValues = field.enums.keys.collect! { |enumValue|
			"'#{ enumValue }'"
		}
		"enum( #{ singleQuotedValues.join( ', ' ) } )"
	end
end