# # ActiveFacts Oracle SQL Schema Generator # # Copyright (c) 2009-2016 Clifford Heath. Read the LICENSE file. # # Reserved words gathered from: # https://docs.oracle.com/cd/B28359_01/appdev.111/b31231/appb.htm # require 'digest/sha1' require 'activefacts/metamodel' require 'activefacts/compositions' require 'activefacts/generator/sql' module ActiveFacts module Generators # Options are comma or space separated: # * underscore class SQL class Oracle < SQL def self.options super.merge({ # no: [String, "no new options defined here"] }) end def initialize composition, options = {} super(composition, {'tables' => 'shout', 'columns' => 'shout'}.merge(options)) end def table_name_max 30 end def data_type_context OracleDataTypeContext.new end def auto_assign_modifier ' GENERATED BY DEFAULT ON NULL AS IDENTITY' end def generate_schema '' end def normalise_type(type_name, length, value_constraint, options) type = MM::DataType.normalise(type_name) case type when MM::DataType::TYPE_Integer if aa = 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; if type_name =~ /^(guid|uuid)$/i && (!length || length == 16) if ![nil, ''].include?(options[:auto_assign]) options[:default] = " DEFAULT SYS_GUID()" options.delete(:auto_assign) end return ['RAW', 32] end ['LOB', length] else super end end # 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 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 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 def go s = '' "#{s};\n\n" end def open_escape '"' end def close_escape '"' end def index_kind(index) '' end class OracleDataTypeContext < SQLDataTypeContext def integer_ranges [ ['SHORTINTEGER', -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!) ['LONGINTEGER', -2**63, 2**63-1], # The standard says -10^19..10^19 (less than 64 bits) ] end def boolean_type 'BOOLEAN' end def valid_from_type 'TIMESTAMP' end # There is no performance benefit in using fixed-length CHAR fields, # and an added burden of trimming the implicitly added white-space def default_char_type (@unicode ? 'N' : '') + 'VARCHAR' end def default_varchar_type (@unicode ? 'N' : '') + 'VARCHAR' end def date_time_type 'TIMESTAMP' end end end end publish_generator SQL::Oracle end end