Merge pull request #2138 from fabrixxm/dbstructure_multicolumn_primarykey
Allow multicolumn primary keys in dbstructure
This commit is contained in:
commit
8a5fada863
|
@ -142,24 +142,36 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
|
||||||
if (is_null($definition))
|
if (is_null($definition))
|
||||||
$definition = db_definition();
|
$definition = db_definition();
|
||||||
|
|
||||||
|
|
||||||
// Compare it
|
// Compare it
|
||||||
foreach ($definition AS $name => $structure) {
|
foreach ($definition AS $name => $structure) {
|
||||||
|
$is_new_table = False;
|
||||||
$sql3="";
|
$sql3="";
|
||||||
if (!isset($database[$name])) {
|
if (!isset($database[$name])) {
|
||||||
$r = db_create_table($name, $structure["fields"], $verbose, $action);
|
$r = db_create_table($name, $structure["fields"], $verbose, $action, $structure['indexes']);
|
||||||
if(false === $r)
|
if(false === $r) {
|
||||||
$errors .= t('Errors encountered creating database tables.').$name.EOL;
|
$errors .= t('Errors encountered creating database tables.').$name.EOL;
|
||||||
|
}
|
||||||
|
$is_new_table = True;
|
||||||
} else {
|
} else {
|
||||||
// Drop the index if it isn't present in the definition and index name doesn't start with "local_"
|
// Drop the index if it isn't present in the definition
|
||||||
foreach ($database[$name]["indexes"] AS $indexname => $fieldnames)
|
// or the definition differ from current status
|
||||||
if (!isset($structure["indexes"][$indexname]) && substr($indexname, 0, 6) != 'local_') {
|
// and index name doesn't start with "local_"
|
||||||
|
foreach ($database[$name]["indexes"] AS $indexname => $fieldnames) {
|
||||||
|
$current_index_definition = implode(",",$fieldnames);
|
||||||
|
if (isset($structure["indexes"][$indexname])) {
|
||||||
|
$new_index_definition = implode(",",$structure["indexes"][$indexname]);
|
||||||
|
} else {
|
||||||
|
$new_index_definition = "__NOT_SET__";
|
||||||
|
}
|
||||||
|
if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
|
||||||
$sql2=db_drop_index($indexname);
|
$sql2=db_drop_index($indexname);
|
||||||
if ($sql3 == "")
|
if ($sql3 == "")
|
||||||
$sql3 = "ALTER TABLE `".$name."` ".$sql2;
|
$sql3 = "ALTER TABLE `".$name."` ".$sql2;
|
||||||
else
|
else
|
||||||
$sql3 .= ", ".$sql2;
|
$sql3 .= ", ".$sql2;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Compare the field structure field by field
|
// Compare the field structure field by field
|
||||||
foreach ($structure["fields"] AS $fieldname => $parameters) {
|
foreach ($structure["fields"] AS $fieldname => $parameters) {
|
||||||
if (!isset($database[$name]["fields"][$fieldname])) {
|
if (!isset($database[$name]["fields"][$fieldname])) {
|
||||||
|
@ -184,9 +196,18 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the index
|
// Create the index if the index don't exists in database
|
||||||
|
// or the definition differ from the current status.
|
||||||
|
// Don't create keys if table is new
|
||||||
|
if (!$is_new_table) {
|
||||||
foreach ($structure["indexes"] AS $indexname => $fieldnames) {
|
foreach ($structure["indexes"] AS $indexname => $fieldnames) {
|
||||||
if (!isset($database[$name]["indexes"][$indexname])) {
|
if (isset($database[$name]["indexes"][$indexname])) {
|
||||||
|
$current_index_definition = implode(",",$database[$name]["indexes"][$indexname]);
|
||||||
|
} else {
|
||||||
|
$current_index_definition = "__NOT_SET__";
|
||||||
|
}
|
||||||
|
$new_index_definition = implode(",",$fieldnames);
|
||||||
|
if ($current_index_definition != $new_index_definition) {
|
||||||
$sql2=db_create_index($indexname, $fieldnames);
|
$sql2=db_create_index($indexname, $fieldnames);
|
||||||
if ($sql2 != "") {
|
if ($sql2 != "") {
|
||||||
if ($sql3 == "")
|
if ($sql3 == "")
|
||||||
|
@ -196,7 +217,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if ($sql3 != "") {
|
if ($sql3 != "") {
|
||||||
$sql3 .= ";";
|
$sql3 .= ";";
|
||||||
|
|
||||||
|
@ -230,8 +251,8 @@ function db_field_command($parameters, $create = true) {
|
||||||
if ($parameters["extra"] != "")
|
if ($parameters["extra"] != "")
|
||||||
$fieldstruct .= " ".$parameters["extra"];
|
$fieldstruct .= " ".$parameters["extra"];
|
||||||
|
|
||||||
if (($parameters["primary"] != "") AND $create)
|
/*if (($parameters["primary"] != "") AND $create)
|
||||||
$fieldstruct .= " PRIMARY KEY";
|
$fieldstruct .= " PRIMARY KEY";*/
|
||||||
|
|
||||||
return($fieldstruct);
|
return($fieldstruct);
|
||||||
}
|
}
|
||||||
|
@ -242,13 +263,17 @@ function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
|
||||||
$r = true;
|
$r = true;
|
||||||
|
|
||||||
$sql = "";
|
$sql = "";
|
||||||
|
|
||||||
$sql_rows = array();
|
$sql_rows = array();
|
||||||
|
$primary_keys = array();
|
||||||
foreach($fields AS $fieldname => $field) {
|
foreach($fields AS $fieldname => $field) {
|
||||||
$sql_rows[] = "`".dbesc($fieldname)."` ".db_field_command($field);
|
$sql_rows[] = "`".dbesc($fieldname)."` ".db_field_command($field);
|
||||||
|
if (x($field,'primary') and $field['primary']!=''){
|
||||||
|
$primary_keys[] = $fieldname;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_null($indexes)) {
|
if (!is_null($indexes)) {
|
||||||
|
|
||||||
foreach ($indexes AS $indexname => $fieldnames) {
|
foreach ($indexes AS $indexname => $fieldnames) {
|
||||||
$sql_index = db_create_index($indexname, $fieldnames, "");
|
$sql_index = db_create_index($indexname, $fieldnames, "");
|
||||||
if (!is_null($sql_index)) $sql_rows[] = $sql_index;
|
if (!is_null($sql_index)) $sql_rows[] = $sql_index;
|
||||||
|
@ -258,7 +283,6 @@ function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
|
||||||
$sql = implode(",\n\t", $sql_rows);
|
$sql = implode(",\n\t", $sql_rows);
|
||||||
|
|
||||||
$sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8";
|
$sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8";
|
||||||
|
|
||||||
if ($verbose)
|
if ($verbose)
|
||||||
echo $sql.";\n";
|
echo $sql.";\n";
|
||||||
|
|
||||||
|
@ -285,8 +309,16 @@ function db_drop_index($indexname) {
|
||||||
|
|
||||||
function db_create_index($indexname, $fieldnames, $method="ADD") {
|
function db_create_index($indexname, $fieldnames, $method="ADD") {
|
||||||
|
|
||||||
if ($indexname == "PRIMARY")
|
$method = strtoupper(trim($method));
|
||||||
return;
|
if ($method!="" && $method!="ADD") {
|
||||||
|
throw new Exception("Invalid parameter 'method' in db_create_index(): '$method'");
|
||||||
|
killme();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if ($indexname == "PRIMARY") {
|
||||||
|
return sprintf("%s PRIMARY KEY(`%s`)", $method, implode("`,`", $fieldnames));
|
||||||
|
}
|
||||||
|
|
||||||
$names = "";
|
$names = "";
|
||||||
foreach ($fieldnames AS $fieldname) {
|
foreach ($fieldnames AS $fieldname) {
|
||||||
|
@ -299,11 +331,6 @@ function db_create_index($indexname, $fieldnames, $method="ADD") {
|
||||||
$names .= "`".dbesc($fieldname)."`";
|
$names .= "`".dbesc($fieldname)."`";
|
||||||
}
|
}
|
||||||
|
|
||||||
$method = strtoupper(trim($method));
|
|
||||||
if ($method!="" && $method!="ADD") {
|
|
||||||
throw new Exception("Invalid parameter 'method' in db_create_index(): '$method'");
|
|
||||||
killme();
|
|
||||||
}
|
|
||||||
|
|
||||||
$sql = sprintf("%s INDEX `%s` (%s)", $method, dbesc($indexname), $names);
|
$sql = sprintf("%s INDEX `%s` (%s)", $method, dbesc($indexname), $names);
|
||||||
return($sql);
|
return($sql);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user