Class: ActiveFacts::Generate::SQL::MYSQL
- Inherits:
-
Object
- Object
- ActiveFacts::Generate::SQL::MYSQL
- Includes:
- Persistence
- Defined in:
- lib/activefacts/generate/sql/mysql.rb
Overview
Generate SQL for MySQL for an ActiveFacts vocabulary. Invoke as
afgen --sql/mysql[=options] <file>.cql
Options are comma or space separated:
-
delay_fks Leave all foreign keys until the end, not just those that contain forward-references
Instance Method Summary collapse
-
#generate(out = $>) ⇒ Object
:nodoc:.
Methods included from Persistence
rails_name_trunc, rails_plural_name, rails_singular_name
Instance Method Details
#generate(out = $>) ⇒ Object
:nodoc:
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/activefacts/generate/sql/mysql.rb', line 157 def generate(out = $>) #:nodoc: @out = out #go "CREATE SCHEMA #{@vocabulary.name}" tables_emitted = {} delayed_foreign_keys = [] @vocabulary.tables.each do |table| puts "CREATE TABLE #{escape table.name.gsub(' ','')} (" pk = table.identifier_columns identity_column = pk[0] if pk[0].is_auto_assigned fk_refs = table.references_from.select{|ref| ref.is_simple_reference } fk_columns = table.columns.select do |column| column.references[0].is_simple_reference end # We sort the columns here, not in the persistence layer, because it affects # the ordering of columns in an index :-(. columns = table.columns.sort_by { |column| column.name(nil) }.map do |column| name = escape column.name("") padding = " "*(name.size >= ColumnNameMax ? 1 : ColumnNameMax-name.size) type, params, constraints = column.type constraints = [] if (fk_columns.include?(column)) # Don't enforce VT constraints on FK columns length = params[:length] length &&= length.to_i scale = params[:scale] scale &&= scale.to_i type, length = normalise_type(type, length) sql_type = "#{type}#{ if !length "" else "(" + length.to_s + (scale ? ", #{scale}" : "") + ")" end }" identity = column == identity_column ? " AUTO_INCREMENT" : "" null = (column.is_mandatory ? "NOT " : "") + "NULL" check = check_clause(name, constraints) comment = column.comment [ "-- #{comment}", "#{name}#{padding}#{sql_type}#{identity} #{null}#{check}" ] end.flatten pk_def = (pk.detect{|column| !column.is_mandatory} ? "UNIQUE(" : "PRIMARY KEY(") + pk.map{|column| escape column.name("")}*", " + ")" inline_fks = [] table.foreign_keys.each do |fk| fk_text = "FOREIGN KEY (" + fk.from_columns.map{|column| column.name}*", " + ") REFERENCES #{escape fk.to.name.gsub(' ','')} (" + fk.to_columns.map{|column| column.name}*", " + ")" if !@delay_fks and # We don't want to delay all Fks (tables_emitted[fk.to] or # The target table has been emitted fk.to == table && !fk.to_columns.detect{|column| !column.is_mandatory}) # The reference columns already have the required indexes inline_fks << fk_text else delayed_foreign_keys << ("ALTER TABLE #{escape fk.from.name.gsub(' ','')}\n\tADD " + fk_text) end end indices = table.indices inline_indices = [] delayed_indices = [] indices.each do |index| next if index.over == table && index.is_primary # Already did the primary keys abbreviated_column_names = index.abbreviated_column_names*"" column_names = index.column_names column_name_list = column_names.map{|n| escape(n)}*", " inline_indices << "UNIQUE(#{column_name_list})" end tables_emitted[table] = true puts("\t" + (columns + [pk_def] + inline_indices + inline_fks)*",\n\t") go ")" delayed_indices.each {|index_text| go index_text } end delayed_foreign_keys.each do |fk| go fk end end |