diff --git a/lang/en/error.php b/lang/en/error.php index f9c0f3a18dd..4e5537a8ff3 100755 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -186,6 +186,7 @@ $string['ddldependencyerror'] = '{$a->targettype} "{$a->targetname}" cannot be m $string['ddlexecuteerror'] = 'DDL sql execution error'; $string['ddlfieldalreadyexists'] = 'Field "{$a}" already exists'; $string['ddlfieldnotexist'] = 'Field "{$a->fieldname}" does not exist in table "{$a->tablename}"'; +$string['ddsequenceerror'] = 'Incorrect table "{$a}" definition; there can be only one auto column and it must be defined as a key.'; $string['ddltablealreadyexists'] = 'Table "{$a}" already exists'; $string['ddltablenotexist'] = 'Table "{$a}" does not exist'; $string['ddlunknownerror'] = 'Unknown DDL library error'; diff --git a/lib/ddl/sql_generator.php b/lib/ddl/sql_generator.php index 615cfc8474d..515e7f51d64 100644 --- a/lib/ddl/sql_generator.php +++ b/lib/ddl/sql_generator.php @@ -233,8 +233,13 @@ abstract class sql_generator { return $results; } + $sequencefield = null; + /// Add the fields, separated by commas foreach ($xmldb_fields as $xmldb_field) { + if ($xmldb_field->getSequence()) { + $sequencefield = $xmldb_field->getName(); + } $table .= "\n " . $this->getFieldSQL($xmldb_field); $table .= ','; } @@ -252,8 +257,20 @@ abstract class sql_generator { $table .= "\nCONSTRAINT " . $keytext . ','; } } + /// make sure sequence field is unique + if ($sequencefield and $xmldb_key->getType() == XMLDB_KEY_PRIMARY) { + $field = reset($xmldb_key->getFields()); + if ($sequencefield === $field) { + $sequencefield = null; + } + } } } + /// throw error if sequence field does not have unique key defined + if ($sequencefield) { + throw new ddl_exception('ddsequenceerror', $xmldb_table->getName()); + } + /// Table footer, trim the latest comma $table = trim($table,','); $table .= "\n)";