class ActiveFacts::Generators::SQL::SERVER
Generate SQL
for SQL
Server for an ActiveFacts
vocabulary. Invoke as
afgen --sql/server[=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
Constants
- ColumnNameMax
- RESERVED_WORDS
Public Class Methods
new(vocabulary, *options)
click to toggle source
# File lib/activefacts/generators/sql/server.rb, line 47 def initialize(vocabulary, *options) @vocabulary = vocabulary @vocabulary = @vocabulary.Vocabulary.values[0] if ActiveFacts::API::Constellation === @vocabulary @delay_fks = options.include? "delay_fks" @underscore = options.include?("underscore") ? "_" : "" end
Private Instance Methods
check_clause(column_name, constraints)
click to toggle source
# File lib/activefacts/generators/sql/server.rb, line 251 def check_clause(column_name, constraints) return "" if constraints.empty? # REVISIT: Merge all constraints (later; now just use the first) " CHECK(" + constraints[0].all_allowed_range_sorted.map do |ar| vr = ar.value_range min = vr.minimum_bound max = vr.maximum_bound if (min && max && max.value.literal == min.value.literal) "#{column_name} = #{sql_value(min.value)}" else inequalities = [ min && "#{column_name} >#{min.is_inclusive ? "=" : ""} #{sql_value(min.value)}", max && "#{column_name} <#{max.is_inclusive ? "=" : ""} #{sql_value(max.value)}" ].compact inequalities.size > 1 ? "(" + inequalities*" AND " + ")" : inequalities[0] end end*" OR " + ")" end
escape(s)
click to toggle source
# File lib/activefacts/generators/sql/server.rb, line 63 def escape s # Escape SQL keywords and non-identifiers s = s[0...120] if s =~ /[^A-Za-z0-9_]/ || RESERVED_WORDS[s.upcase] "[#{s}]" else s end end
go(s)
click to toggle source
# File lib/activefacts/generators/sql/server.rb, line 58 def go s puts s puts "GO\n\n" end
normalise_type(type, length)
click to toggle source
Return SQL
type and (modified?) length for the passed base type
# File lib/activefacts/generators/sql/server.rb, line 74 def normalise_type(type, length) sql_type = case type when /^Auto ?Counter$/ 'int' when /^Unsigned ?Integer$/, /^Signed ?Integer$/, /^Unsigned ?Small ?Integer$/, /^Signed ?Small ?Integer$/, /^Unsigned ?Tiny ?Integer$/ s = case when length == nil 'int' when length <= 8 'tinyint' when length <= 16 'smallint' when length <= 32 'int' else 'bigint' end length = nil s when /^Decimal$/ 'decimal' when /^Fixed ?Length ?Text$/, /^Char$/ 'char' when /^Variable ?Length ?Text$/, /^String$/ 'varchar' when /^Large ?Length ?Text$/, /^Text$/ 'text' when /^Date ?And ?Time$/, /^Date ?Time$/ 'datetime' when /^Date$/ 'datetime' # SQLSVR 2K5: 'date' when /^Time$/ 'datetime' # SQLSVR 2K5: 'time' when /^Auto ?Time ?Stamp$/ 'timestamp' when /^Guid$/ 'uniqueidentifier' when /^Money$/ 'decimal' when /^Picture ?Raw ?Data$/, /^Image$/ 'image' when /^Variable ?Length ?Raw ?Data$/, /^Blob$/ 'varbinary' when /^BIT$/ 'bit' else type # raise "SQL type unknown for standard type #{type}" end [sql_type, length] end
puts(s)
click to toggle source
# File lib/activefacts/generators/sql/server.rb, line 54 def puts s @out.puts s end
sql_string(str)
click to toggle source
# File lib/activefacts/generators/sql/server.rb, line 247 def sql_string(str) "'" + str.gsub(/'/,"''") + "'" end
sql_value(value)
click to toggle source
# File lib/activefacts/generators/sql/server.rb, line 243 def sql_value(value) value.is_literal_string ? sql_string(value.literal) : value.literal end