module ActiveFacts::Generators::Traits::SQL::Oracle

Public Instance Methods

as_text(exprs) click to toggle source

Some or all of the SQL expressions may have non-text values. Return an SQL expression that coerces them to text.

# File lib/activefacts/generator/traits/sql/oracle.rb, line 139
def as_text exprs
  return exprs.map{|e| as_text(e)} if Array === exprs

  Expression.new("#{exprs}", MM::DataType::TYPE_String, exprs.is_mandatory)
end
choose_sql_type(type_name, value_constraint, component, options) click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 69
def choose_sql_type(type_name, value_constraint, component, options)
  type = MM::DataType.intrinsic_type(type_name)
  case type
  when MM::DataType::TYPE_Integer
    # The :auto_assign key is set for auto-assigned types, but with a nil value in foreign keys
    if options.has_key?(:auto_assign)
      options[:default] = ' GENERATED BY DEFAULT ON NULL AS IDENTITY' if options[:auto_assign]
      'LONGINTEGER'
    else
      super
    end

  when MM::DataType::TYPE_Money
    'MONEY'

  when MM::DataType::TYPE_DateTime
    'DATETIME'

  when MM::DataType::TYPE_Timestamp
    'DATETIME'

  when MM::DataType::TYPE_Binary
    case binary_surrogate(type_name, value_constraint, options)
    when :guid_fk             # A surrogate that's auto-assigned elsewhere
      options[:length] = 16   # This will display as 32 hex digits
      'RAW'
    when :guid                # A GUID
      options[:length] = 16
      options[:default] = " DEFAULT SYS_GUID()"
      'RAW'
    when :hash                # A hash of the natural key
      options[:length] = 20   # Assuming SHA-1. SHA-256 would need 32 bytes
      leaves = component.root.natural_index.all_index_field.map(&:component)
      options[:default] = " GENERATED ALWAYS AS #{hash(concatenate(coalesce(as_text(safe_column_exprs(leaves)))))}"
      'RAW'
    else
      'LOB'                   # Not a surrogate, just use the type
    end
  else
    super
  end
end
close_escape() click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 216
def close_escape
  '"'
end
column_name_max() click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 49
def column_name_max
  128
end
concatenate(expressions) click to toggle source

Return an SQL expression that concatenates the given expressions (which must yield a string type)

# File lib/activefacts/generator/traits/sql/oracle.rb, line 146
def concatenate expressions
  Expression.new(
    "'|' || " +
    expressions.map(&:to_s) * " || '|' || " +
    " || '|'",
    MM::DataType::TYPE_String,
    true
  )
end
data_type_context_class() click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 39
def data_type_context_class
  OracleDataTypeContext
end
defaults_and_options(options) click to toggle source

The options parameter overrides any default options set by sub-traits

# File lib/activefacts/generator/traits/sql/oracle.rb, line 31
def defaults_and_options options
  {'tables' => 'shout', 'columns' => 'shout'}.merge(options)
end
hash(expr, algo = 'SHA-1') click to toggle source

Return an expression that yields a hash of the given expression

# File lib/activefacts/generator/traits/sql/oracle.rb, line 157
def hash expr, algo = 'SHA-1'
  # Since Oracle 12.1:
  Expression.new("STANDARD_HASH(#{expr}, '#{algo}')", MM::DataType::TYPE_Binary, expr.is_mandatory)

  # Expression.new("utl_raw.cast_to_raw(dbms_crypto.hash(#{expr}, dbms_crypto.hash_#{algo}))", MM::DataType::TYPE_Binary, expr.is_mandatory)
end
index_kind(index) click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 65
def index_kind(index)
  super
end
index_name_max() click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 53
def index_name_max
  128
end
key_words() click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 192
def key_words
  # These keywords should not be used for columns or tables:
  @oracle_key_words ||= %w{
    ANALYZE ARCHIVE ARCHIVELOG BACKUP BECOME BLOCK BODY
    CACHE CANCEL CHANGE CHECKPOINT COMPILE CONTENTS CONTROLFILE
    DATABASE DATAFILE DBA DISABLE DISMOUNT DUMP ENABLE
    EVENTS EXCEPTIONS EXPLAIN EXTENT EXTERNALLY FLUSH FORCE
    FREELIST FREELISTS INITRANS LAYER LISTS LOGFILE MANAGE
    MANUAL MAXDATAFILES MAXINSTANCES MAXLOGFILES MAXLOGHISTORY
    MAXLOGMEMBERS MAXTRANS MINEXTENTS MOUNT NOARCHIVELOG
    NOCACHE NOCYCLE NOMAXVALUE NOMINVALUE NOORDER NORESETLOGS
    NORMAL NOSORT OPTIMAL OWN PACKAGE PARALLEL PCTINCREASE
    PCTUSED PLAN PRIVATE PROFILE QUOTA RECOVER RESETLOGS
    RESTRICTED REUSE ROLES SCN SEGMENT SHARED SNAPSHOT SORT
    STATEMENT_ID STATISTICS STOP STORAGE SWITCH TABLES
    TABLESPACE THREAD TRACING TRIGGERS UNLIMITED USE
  }
  super + @oracle_key_words
end
open_escape() click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 212
def open_escape
  '"'
end
options() click to toggle source

Options available in this flavour of SQL

# File lib/activefacts/generator/traits/sql/oracle.rb, line 24
def options
  super.merge({
    # no: [String, "no new options defined here"]
  })
end
process_options(options) click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 35
def process_options options
  super
end
reserved_words() click to toggle source

Reserved words cannot be used anywhere without quoting. Keywords have existing definitions, so should not be used without quoting. Both lists here are added to the supertype's lists

# File lib/activefacts/generator/traits/sql/oracle.rb, line 167
def reserved_words
  @oracle_reserved_words ||= %w{
    ACCESS ARRAYLEN AUDIT CLUSTER COMMENT COMPRESS EXCLUSIVE
    IDENTIFIED INDEX INITIAL LOCK LONG MAXEXTENTS MINUS
    MODE MODIFY NOAUDIT NOCOMPRESS NOTFOUND NOWAIT OFFLINE
    ONLINE PCTFREE RAW RENAME RESOURCE ROWID ROWLABEL ROWNUM
    SHARE SQLBUF SUCCESSFUL SYNONYM SYSDATE UID VALIDATE
    VARCHAR2
  }
  @oracle_plsql_reserved_words ||= %w{
    ABORT ACCEPT ACCESS ARRAYLEN ASSERT ASSIGN BASE_TABLE
    BINARY_INTEGER BODY CHAR_BASE CLUSTER CLUSTERS COLAUTH
    COMPRESS CONSTANT CRASH CURRVAL DATABASE DATA_BASE DBA
    DEBUGOFF DEBUGON DEFINITION DELAY DELTA DIGITS DISPOSE
    ELSIF ENTRY EXCEPTION_INIT FORM GENERIC IDENTIFIED INDEX
    INDEXES LIMITED MINUS MLSLABEL MODE NEXTVAL NOCOMPRESS
    NUMBER_BASE PACKAGE PCTFREE POSITIVE PRAGMA PRIVATE
    RAISE RECORD REMR RENAME RESOURCE REVERSE ROWID ROWLABEL
    ROWNUM ROWTYPE RUN SEPARATE SQLERRM STDDEV SUBTYPE
    TABAUTH TABLES TASK TERMINATE USE VARCHAR2 VARIANCE
    VIEWS XOR
  }
  super + @oracle_reserved_words
end
schema_name_max() click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 57
def schema_name_max
  128   # Was 8 characters
end
schema_prefix() click to toggle source
# File lib/activefacts/generator/traits/sql/oracle.rb, line 61
def schema_prefix
  ''
end
table_name_max() click to toggle source

See docs.oracle.com/database/122/NEWFT/new-features.htm Identifier lengths were 30 bytes prior to Oracle 12C

# File lib/activefacts/generator/traits/sql/oracle.rb, line 45
def table_name_max
  128
end