From abbd460ff991bc403ed82dfee3a363991c154e01 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sun, 8 Oct 2006 09:36:33 +0000 Subject: [PATCH] Adding getSequenceFromDB() for Oracle in order to detect the correct sequence name in the insert_record() function. --- lib/xmldb/classes/XMLDBTable.class.php | 13 ++++++++++ .../generators/XMLDBGenerator.class.php | 9 +++++++ .../generators/oci8po/oci8po.class.php | 26 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/lib/xmldb/classes/XMLDBTable.class.php b/lib/xmldb/classes/XMLDBTable.class.php index a19197e446..943564f468 100644 --- a/lib/xmldb/classes/XMLDBTable.class.php +++ b/lib/xmldb/classes/XMLDBTable.class.php @@ -1035,6 +1035,19 @@ class XMLDBTable extends XMLDBObject { } return $results; } + + /** + * This function will return the name of the sequence created for the pk of the table specified + * Just one simple wrapper over generators. Returns false if not found + * Note that not all DB use sequences (only Oracle and PostgreSQL) + */ + function getSequenceFromDB($dbtype, $prefix) { + + $classname = 'XMLDB' . $dbtype; + $generator = new $classname(); + $generator->setPrefix($prefix); + return $generator->getSequenceFromDB($this); + } } ?> diff --git a/lib/xmldb/classes/generators/XMLDBGenerator.class.php b/lib/xmldb/classes/generators/XMLDBGenerator.class.php index 839af46e8d..26d3fe2431 100644 --- a/lib/xmldb/classes/generators/XMLDBGenerator.class.php +++ b/lib/xmldb/classes/generators/XMLDBGenerator.class.php @@ -1066,6 +1066,15 @@ class XMLDBgenerator { } } + /** + * Returns the name (string) of the sequence used in the table for the autonumeric pk + * Only some DB have this implemented + */ + function getSequenceFromDB($xmldb_table) { + return false; + } + + /// ALL THESE FUNCTION MUST BE CUSTOMISED BY ALL THE XMLDGenerator classes /** diff --git a/lib/xmldb/classes/generators/oci8po/oci8po.class.php b/lib/xmldb/classes/generators/oci8po/oci8po.class.php index 3fe24ebc45..97d462e49e 100644 --- a/lib/xmldb/classes/generators/oci8po/oci8po.class.php +++ b/lib/xmldb/classes/generators/oci8po/oci8po.class.php @@ -500,6 +500,32 @@ class XMLDBoci8po extends XMLDBgenerator { return $results; } + /** + * Given one XMLDBTable returns one string with the sequence of the table + * in the table (fetched from DB) + * The sequence name for oracle is calculated by looking the corresponding + * trigger and retrieving the sequence name from it (because sequences are + * independent elements) + * If no sequence is found, returns false + */ + function getSequenceFromDB($xmldb_table) { + + $tablename = strtoupper($this->getTableName($xmldb_table)); + $sequencename = false; + + if ($trigger = get_record_sql("SELECT trigger_name, trigger_body + FROM user_triggers + WHERE table_name = '{$tablename}'")) { + /// If trigger found, regexp it looking for the sequence name + preg_match('/.*SELECT (.*)\.nextval/i', $trigger->trigger_body, $matches); + if (isset($matches[1])) { + $sequencename = $matches[1]; + } + } + + return $sequencename; + } + /** * Returns an array of reserved words (lowercase) for this DB */ -- 2.39.5