diff --git a/lib/ddl/mssql_sql_generator.php b/lib/ddl/mssql_sql_generator.php index e3543d763fc..bfa03753d56 100644 --- a/lib/ddl/mssql_sql_generator.php +++ b/lib/ddl/mssql_sql_generator.php @@ -140,6 +140,36 @@ class mssql_sql_generator extends sql_generator { return $tablename; } + public function getCreateIndexSQL($xmldb_table, $xmldb_index) { + list($indexsql) = parent::getCreateIndexSQL($xmldb_table, $xmldb_index); + + // Unique indexes need to work-around non-standard SQL server behaviour. + if ($xmldb_index->getUnique()) { + // Find any nullable columns. We need to add a + // WHERE field IS NOT NULL to the index definition for each one. + // + // For example if you have a unique index on the three columns + // (required, option1, option2) where the first one is non-null, + // and the others nullable, then the SQL will end up as + // + // CREATE UNIQUE INDEX index_name ON table_name (required, option1, option2) + // WHERE option1 IS NOT NULL AND option2 IS NOT NULL + // + // The first line comes from parent calls above. The WHERE is added below. + $extraconditions = []; + foreach ($this->get_nullable_fields_in_index($xmldb_table, $xmldb_index) as $fieldname) { + $extraconditions[] = $this->getEncQuoted($fieldname) . + ' IS NOT NULL'; + } + + if ($extraconditions) { + $indexsql .= ' WHERE ' . implode(' AND ', $extraconditions); + } + } + + return [$indexsql]; + } + /** * Given one correct xmldb_table, returns the SQL statements * to create temporary table (inside one array). diff --git a/lib/ddl/oracle_sql_generator.php b/lib/ddl/oracle_sql_generator.php index 8deccf98fbe..5e1325c53b6 100644 --- a/lib/ddl/oracle_sql_generator.php +++ b/lib/ddl/oracle_sql_generator.php @@ -131,6 +131,61 @@ class oracle_sql_generator extends sql_generator { return $tablename; } + public function getCreateIndexSQL($xmldb_table, $xmldb_index) { + if ($error = $xmldb_index->validateDefinition($xmldb_table)) { + throw new coding_exception($error); + } + + $indexfields = $this->getEncQuoted($xmldb_index->getFields()); + + $unique = ''; + $suffix = 'ix'; + if ($xmldb_index->getUnique()) { + $unique = ' UNIQUE'; + $suffix = 'uix'; + + $nullablefields = $this->get_nullable_fields_in_index($xmldb_table, $xmldb_index); + if ($nullablefields) { + // If this is a unique index with nullable fields, then we have to + // apply the work-around from https://community.oracle.com/message/9518046#9518046. + // + // For example if you have a unique index on the three columns + // (required, option1, option2) where the first one is non-null, + // and the others nullable, then the SQL will end up as + // + // CREATE UNIQUE INDEX index_name ON table_name ( + // CASE WHEN option1 IS NOT NULL AND option2 IS NOT NULL THEN required ELSE NULL END, + // CASE WHEN option1 IS NOT NULL AND option2 IS NOT NULL THEN option1 ELSE NULL END, + // CASE WHEN option1 IS NOT NULL AND option2 IS NOT NULL THEN option2 ELSE NULL END) + // + // Basically Oracle behaves according to the standard if either + // none of the columns are NULL or all columns contain NULL. Therefore, + // if any column is NULL, we treat them all as NULL for the index. + $conditions = []; + foreach ($nullablefields as $fieldname) { + $conditions[] = $this->getEncQuoted($fieldname) . + ' IS NOT NULL'; + } + $condition = implode(' AND ', $conditions); + + $updatedindexfields = []; + foreach ($indexfields as $fieldname) { + $updatedindexfields[] = 'CASE WHEN ' . $condition . ' THEN ' . + $fieldname . ' ELSE NULL END'; + } + $indexfields = $updatedindexfields; + } + + } + + $index = 'CREATE' . $unique . ' INDEX '; + $index .= $this->getNameForObject($xmldb_table->getName(), implode(', ', $xmldb_index->getFields()), $suffix); + $index .= ' ON ' . $this->getTableName($xmldb_table); + $index .= ' (' . implode(', ', $indexfields) . ')'; + + return array($index); + } + /** * Given one correct xmldb_table, returns the SQL statements * to create temporary table (inside one array). diff --git a/lib/ddl/sql_generator.php b/lib/ddl/sql_generator.php index 64fa5d73177..9ad419f512d 100644 --- a/lib/ddl/sql_generator.php +++ b/lib/ddl/sql_generator.php @@ -1408,4 +1408,44 @@ abstract class sql_generator { $s = str_replace("'", "\\'", $s); return $s; } + + /** + * Get the fields from an index definition that might be null. + * @param xmldb_table $xmldb_table the table + * @param xmldb_index $xmldb_index the index + * @return array list of fields in the index definition that might be null. + */ + public function get_nullable_fields_in_index($xmldb_table, $xmldb_index) { + global $DB; + + // If we don't have the field info passed in, we need to query it from the DB. + $fieldsfromdb = null; + + $nullablefields = []; + foreach ($xmldb_index->getFields() as $fieldname) { + if ($field = $xmldb_table->getField($fieldname)) { + // We have the field details in the table definition. + if ($field->getNotNull() !== XMLDB_NOTNULL) { + $nullablefields[] = $fieldname; + } + + } else { + // We don't have the table definition loaded. Need to + // inspect the database. + if ($fieldsfromdb === null) { + $fieldsfromdb = $DB->get_columns($xmldb_table->getName(), false); + } + if (!isset($fieldsfromdb[$fieldname])) { + throw new coding_exception('Unknown field ' . $fieldname . + ' in index ' . $xmldb_index->getName()); + } + + if (!$fieldsfromdb[$fieldname]->not_null) { + $nullablefields[] = $fieldname; + } + } + } + + return $nullablefields; + } } diff --git a/lib/ddl/tests/ddl_test.php b/lib/ddl/tests/ddl_test.php index fb01903e1aa..4f0b190ab5e 100644 --- a/lib/ddl/tests/ddl_test.php +++ b/lib/ddl/tests/ddl_test.php @@ -2291,6 +2291,28 @@ class core_ddl_testcase extends database_driver_testcase { } } + public function test_get_nullable_fields_in_index() { + $DB = $this->tdb; + $gen = $DB->get_manager()->generator; + + $indexwithoutnulls = $this->tables['test_table0']->getIndex('type-name'); + $this->assertSame([], $gen->get_nullable_fields_in_index( + $this->tables['test_table0'], $indexwithoutnulls)); + + $indexwithnulls = new xmldb_index('course-grade', XMLDB_INDEX_UNIQUE, ['course', 'grade']); + $this->assertSame(['grade'], $gen->get_nullable_fields_in_index( + $this->tables['test_table0'], $indexwithnulls)); + + $this->create_deftable('test_table0'); + + // Now test using a minimal xmldb_table, to ensure we get the data from the DB. + $table = new xmldb_table('test_table0'); + $this->assertSame([], $gen->get_nullable_fields_in_index( + $table, $indexwithoutnulls)); + $this->assertSame(['grade'], $gen->get_nullable_fields_in_index( + $table, $indexwithnulls)); + } + // Following methods are not supported == Do not test. /* public function testRenameIndex() { diff --git a/lib/dml/tests/dml_test.php b/lib/dml/tests/dml_test.php index 866625da722..cea23a8de36 100644 --- a/lib/dml/tests/dml_test.php +++ b/lib/dml/tests/dml_test.php @@ -2407,6 +2407,44 @@ class core_dml_testcase extends database_driver_testcase { } } + public function test_insert_record_with_nullable_unique_index() { + $DB = $this->tdb; + $dbman = $DB->get_manager(); + + $table = $this->get_test_table(); + $tablename = $table->getName(); + + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('notnull1', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('nullable1', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_field('nullable2', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_index('notnull1-nullable1-nullable2', XMLDB_INDEX_UNIQUE, + array('notnull1', 'nullable1', 'nullable2')); + $dbman->create_table($table); + + // Insert one record. Should be OK (no exception). + $DB->insert_record($tablename, (object) ['notnull1' => 1, 'nullable1' => 1, 'nullable2' => 1]); + + // Inserting a duplicate should fail. + try { + $DB->insert_record($tablename, (object) ['notnull1' => 1, 'nullable1' => 1, 'nullable2' => 1]); + $this->fail('dml_write_exception expected when a record violates a unique index'); + } catch (moodle_exception $e) { + $this->assertInstanceOf('dml_write_exception', $e); + } + + // Inserting a record with nulls in the nullable columns should work. + $DB->insert_record($tablename, (object) ['notnull1' => 1, 'nullable1' => null, 'nullable2' => null]); + + // And it should be possible to insert a duplicate. + $DB->insert_record($tablename, (object) ['notnull1' => 1, 'nullable1' => null, 'nullable2' => null]); + + // Same, but with only one of the nullable columns being null. + $DB->insert_record($tablename, (object) ['notnull1' => 1, 'nullable1' => 1, 'nullable2' => null]); + $DB->insert_record($tablename, (object) ['notnull1' => 1, 'nullable1' => 1, 'nullable2' => null]); + } + public function test_import_record() { // All the information in this test is fetched from DB by get_recordset() so we // have such method properly tested against nulls, empties and friends...