Ejemplo n.º 1
0
function db_create_table($name, $fields, $verbose, $action, $indexes = null)
{
    global $a, $db;
    $r = true;
    $sql = "";
    $sql_rows = array();
    foreach ($fields as $fieldname => $field) {
        $sql_rows[] = "`" . dbesc($fieldname) . "` " . db_field_command($field);
    }
    if (!is_null($indexes)) {
        foreach ($indexes as $indexname => $fieldnames) {
            $sql_index = db_create_index($indexname, $fieldnames, "");
            if (!is_null($sql_index)) {
                $sql_rows[] = $sql_index;
            }
        }
    }
    $sql = implode(",\n\t", $sql_rows);
    $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)) . $sql . "\n) DEFAULT CHARSET=utf8";
    if ($verbose) {
        echo $sql . ";\n";
    }
    if ($action) {
        $r = @$db->q($sql);
    }
    return $r;
}
Ejemplo n.º 2
0
function update_structure($verbose, $action)
{
    global $a, $db;
    $errors = false;
    logger('updating structure', LOGGER_DEBUG);
    // Get the current structure
    $database = array();
    $tables = q("show tables");
    foreach ($tables as $table) {
        $table = current($table);
        $database[$table] = table_structure($table);
    }
    // Get the definition
    $definition = db_definition();
    // Compare it
    foreach ($definition as $name => $structure) {
        $sql3 = "";
        if (!isset($database[$name])) {
            $r = db_create_table($name, $structure["fields"], $verbose, $action);
            if (false === $r) {
                $errors .= t('Errors encountered creating database tables.') . $name . EOL;
            }
        } else {
            // Drop the index if it isn't present in the definition
            foreach ($database[$name]["indexes"] as $indexname => $fieldnames) {
                if (!isset($structure["indexes"][$indexname])) {
                    $sql2 = db_drop_index($indexname);
                    if ($sql3 == "") {
                        $sql3 = "ALTER TABLE `" . $name . "` " . $sql2;
                    } else {
                        $sql3 .= ", " . $sql2;
                    }
                }
            }
            // Compare the field structure field by field
            foreach ($structure["fields"] as $fieldname => $parameters) {
                if (!isset($database[$name]["fields"][$fieldname])) {
                    $sql2 = db_add_table_field($fieldname, $parameters);
                    if ($sql3 == "") {
                        $sql3 = "ALTER TABLE `" . $name . "` " . $sql2;
                    } else {
                        $sql3 .= ", " . $sql2;
                    }
                } else {
                    // Compare the field definition
                    $current_field_definition = implode($database[$name]["fields"][$fieldname]);
                    $new_field_definition = implode($parameters);
                    if ($current_field_definition != $new_field_definition) {
                        $sql2 = db_modify_table_field($fieldname, $parameters);
                        if ($sql3 == "") {
                            $sql3 = "ALTER TABLE `" . $name . "` " . $sql2;
                        } else {
                            $sql3 .= ", " . $sql2;
                        }
                    }
                }
            }
        }
        // Create the index
        foreach ($structure["indexes"] as $indexname => $fieldnames) {
            if (!isset($database[$name]["indexes"][$indexname])) {
                $sql2 = db_create_index($indexname, $fieldnames);
                if ($sql2 != "") {
                    if ($sql3 == "") {
                        $sql3 = "ALTER TABLE `" . $name . "` " . $sql2;
                    } else {
                        $sql3 .= ", " . $sql2;
                    }
                }
            }
        }
        if ($sql3 != "") {
            $sql3 .= ";";
            if ($verbose) {
                echo $sql3 . "\n";
            }
            if ($action) {
                $r = @$db->q($sql3);
                if (false === $r) {
                    $errors .= t('Errors encountered performing database changes.') . $sql3 . EOL;
                }
            }
        }
    }
    return $errors;
}