class ActiveFacts::Generators::Traits::SQL::SQLDataTypeContext

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/activefacts/generator/traits/sql.rb, line 450
def initialize options = {}
  @surrogate_method = options.delete(:surrogate_method) || "counter"
  super
end

Public Instance Methods

boolean_expr(safe_column_name) click to toggle source

safe_column_name is an expression which yields boolean_type. We use boolean_expr when we want to use it in a conditional context.

# File lib/activefacts/generator/traits/sql.rb, line 494
def boolean_expr safe_column_name
  safe_column_name
end
boolean_type() click to toggle source

Note that BOOLEAN is an optional data type in SQL99. Official literals are TRUE, FALSE and UNKNOWN. Almost nothing (except Postgres) implements BOOLEAN, and even that doesn't implement UNKNOWN (which works like NULL)

# File lib/activefacts/generator/traits/sql.rb, line 488
def boolean_type
  'BOOLEAN'
end
date_time_type() click to toggle source
# File lib/activefacts/generator/traits/sql.rb, line 515
def date_time_type
  'TIMESTAMP'
end
default_autoincrement_length() click to toggle source

Number of bits in an auto-counter

# File lib/activefacts/generator/traits/sql.rb, line 530
def default_autoincrement_length
  64
end
default_char_type() click to toggle source
# File lib/activefacts/generator/traits/sql.rb, line 519
def default_char_type
  (@unicode ? 'NATIONAL ' : '') +
  'CHARACTER'
end
default_length(data_type, type_name) click to toggle source
# File lib/activefacts/generator/traits/sql.rb, line 463
def default_length data_type, type_name
  case data_type
  when MM::DataType::TYPE_Real
    53        # IEEE Double precision floating point
  when MM::DataType::TYPE_Integer
    case type_name
    when /([a-z ]|\b)Tiny([a-z ]|\b)/i
      8
    when /([a-z ]|\b)Small([a-z ]|\b)/i,
      /([a-z ]|\b)Short([a-z ]|\b)/i
      16
    when /([a-z ]|\b)Big(INT)?([a-z ]|\b)/i
      64
    else
      32
    end
  else
    nil
  end
end
default_text_type() click to toggle source
# File lib/activefacts/generator/traits/sql.rb, line 534
def default_text_type
  default_varchar_type
end
default_varchar_type() click to toggle source
# File lib/activefacts/generator/traits/sql.rb, line 524
def default_varchar_type
  (@unicode ? 'NATIONAL ' : '') +
  'VARCHAR'
end
hash_type() click to toggle source
# File lib/activefacts/generator/traits/sql.rb, line 498
def hash_type
  ['BINARY', {length: 32, auto_assign: 'hash' }]
end
integer_ranges() click to toggle source
# File lib/activefacts/generator/traits/sql.rb, line 455
def integer_ranges
  [
    ['SMALLINT', -2**15, 2**15-1],  # The standard says -10^5..10^5 (less than 16 bits)
    ['INTEGER', -2**31, 2**31-1],   # The standard says -10^10..10^10 (more than 32 bits!)
    ['BIGINT', -2**63, 2**63-1],    # The standard says -10^19..10^19 (less than 64 bits)
  ]
end
surrogate_type() click to toggle source

What type to use for a Metamodel::SurrogateKey

# File lib/activefacts/generator/traits/sql.rb, line 503
def surrogate_type
  case @surrogate_method
  when 'guid'
    ["GUID", {auto_assign: 'guid'}]
  when 'hash'
    hash_type
  else  # counter
    type_name, min, max, length = choose_integer_range(0, 2**(default_autoincrement_length-1)-1)
    type_name
  end
end