diff options
| author | Mark Newnham <mark@newnhams.com> | 2019-11-28 11:38:07 -0700 |
|---|---|---|
| committer | Damien Regad <dregad@mantisbt.org> | 2020-01-12 18:09:51 +0100 |
| commit | 1d4de5ee7a72cff039308f33b8a4a34d92c9c4b4 (patch) | |
| tree | 413b35afcaffacc073d7c4e6e975cdca20218659 /datadict | |
| parent | dbff2a1eb4a6be34c03e380099d34b1f3889a409 (diff) | |
| download | adodb-1d4de5ee7a72cff039308f33b8a4a34d92c9c4b4.tar.gz adodb-1d4de5ee7a72cff039308f33b8a4a34d92c9c4b4.tar.bz2 adodb-1d4de5ee7a72cff039308f33b8a4a34d92c9c4b4.zip | |
oci8: create compact trigger/sequence names
This enhancement provides a method of creating an emulated auto-
increment column for a table with a name greater than 25 characters,
built using createTableSql() and specifying an "AUTO" option on the
required column.
Instead of using the table name, a CRC32 value of the tablename is
created and used for the name of the sequence and trigger, i.e. instead
of a trigger called "TRIG_EMPLOYEES" for a table called "EMPLOYEES", it
creates a trigger "TRIG_3129131776", and similarly, a trigger for a
table named "EMPLOYEE_HOURLY_PAY_RATES" becomes "TRIG_3754194288". The
name of the trigger and sequence can be easily obtained after the fact
by finding the CRC32 value of the table name.
The feature is provided with a compatibility flag that must be enabled.
Existing configurations that already use this feature are unchanged. To
activate, set the dictionary class variable
`$dict->useCompactAutoIncrements = true` when building the table, and
the connection class variable
`$db->useCompactAutoIncrements = true` when using the table in ADOdb.
The feature is global for the database and cannot be specified on a
table level basis.
Fixes #565
Diffstat (limited to 'datadict')
| -rw-r--r-- | datadict/datadict-oci8.inc.php | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/datadict/datadict-oci8.inc.php b/datadict/datadict-oci8.inc.php index de76392b..7054e4bb 100644 --- a/datadict/datadict-oci8.inc.php +++ b/datadict/datadict-oci8.inc.php @@ -25,8 +25,15 @@ class ADODB2_oci8 extends ADODB_DataDict { var $alterCol = ' MODIFY '; var $typeX = 'VARCHAR(4000)'; var $typeXL = 'CLOB'; + + /* + * Legacy compatibility for sequence names for emulated auto-increments + * If set to true, creates sequences and triggers as TRIG_394545594 + * instead of TRIG_possibly_too_long_tablename + */ + public $useCompactAutoIncrements = false; - function MetaType($t, $len=-1, $fieldobj=false) + function metaType($t, $len=-1, $fieldobj=false) { if (is_object($t)) { $fieldobj = $t; @@ -185,32 +192,52 @@ class ADODB2_oci8 extends ADODB_DataDict { return $suffix; } -/* -CREATE or replace TRIGGER jaddress_insert -before insert on jaddress -for each row -begin -select seqaddress.nextval into :new.A_ID from dual; -end; -*/ + /** + * Creates an insert trigger to emulate an auto-increment column + * in a table + * + * @param str $tabname The name of the table + * @param str[] $tableoptions Optional configuration items + * + * @return str[] The SQL statements to create the trigger + */ function _Triggers($tabname,$tableoptions) { + if (!$this->seqField) return array(); - if ($this->schema) { + if ($this->schema) + { $t = strpos($tabname,'.'); - if ($t !== false) $tab = substr($tabname,$t+1); - else $tab = $tabname; + if ($t !== false) + $tab = substr($tabname,$t+1); + else + $tab = $tabname; + + if ($this->connection->useCompactAutoIncrements) + $id = sprintf('%u',crc32(strtolower($tab))); + else + $id = $tab; + $seqname = $this->schema.'.'.$this->seqPrefix.$tab; $trigname = $this->schema.'.'.$this->trigPrefix.$this->seqPrefix.$tab; - } else { - $seqname = $this->seqPrefix.$tabname; - $trigname = $this->trigPrefix.$seqname; + + } + else + { + if ($this->connection->useCompactAutoIncrements) + $id = sprintf('%u',crc32(strtolower($tabname))); + else + $id = $tabname; + + $seqname = $this->seqPrefix.$id; + $trigname = $this->trigPrefix.$id; } if (strlen($seqname) > 30) { $seqname = $this->seqPrefix.uniqid(''); } // end if + if (strlen($trigname) > 30) { $trigname = $this->trigPrefix.uniqid(''); } // end if @@ -222,7 +249,7 @@ end; if (isset($tableoptions['SEQUENCE_INCREMENT'])){$seqIncr = ' INCREMENT BY '.$tableoptions['SEQUENCE_INCREMENT'];} $seqStart = ''; if (isset($tableoptions['SEQUENCE_START'])){$seqStart = ' START WITH '.$tableoptions['SEQUENCE_START'];} - $sql[] = "CREATE SEQUENCE $seqname $seqStart $seqIncr $seqCache"; + $sql[] = "CREATE SEQUENCE $seqname MINVALUE 1 $seqStart $seqIncr $seqCache"; $sql[] = "CREATE OR REPLACE TRIGGER $trigname BEFORE insert ON $tabname FOR EACH ROW WHEN (NEW.$this->seqField IS NULL OR NEW.$this->seqField = 0) BEGIN select $seqname.nextval into :new.$this->seqField from dual; END;"; $this->seqField = false; |
