]> git.mxchange.org Git - friendica.git/blobdiff - include/dbstructure.php
Merge pull request #1487 from fabrixxm/projects
[friendica.git] / include / dbstructure.php
index c078e405102332ac32005e5653e45ead93190597..083255b8ae87ae4baf9ca612ec43c57500006922 100644 (file)
@@ -106,33 +106,21 @@ function table_structure($table) {
 }
 
 function print_structure($database) {
+       echo "-- ------------------------------------------\n";
+       echo "-- ".FRIENDICA_PLATFORM." ".FRIENDICA_VERSION." (".FRIENDICA_CODENAME,")\n";
+       echo "-- DB_UPDATE_VERSION ".DB_UPDATE_VERSION."\n";
+       echo "-- ------------------------------------------\n\n\n";
        foreach ($database AS $name => $structure) {
-               echo "\t".'$database["'.$name."\"] = array(\n";
+               echo "--\n";
+               echo "-- TABLE $name\n";
+               echo "--\n";
+               db_create_table($name, $structure['fields'], true, false, $structure["indexes"]);
 
-               echo "\t\t\t".'"fields" => array('."\n";
-               foreach ($structure["fields"] AS $fieldname => $parameters) {
-                       echo "\t\t\t\t\t".'"'.$fieldname.'" => array(';
-
-                       $data = "";
-                       foreach ($parameters AS $name => $value) {
-                               if ($data != "")
-                                       $data .= ", ";
-                               $data .= '"'.$name.'" => "'.$value.'"';
-                       }
-
-                       echo $data."),\n";
-               }
-               echo "\t\t\t\t\t),\n";
-               echo "\t\t\t".'"indexes" => array('."\n";
-               foreach ($structure["indexes"] AS $indexname => $fieldnames) {
-                       echo "\t\t\t\t\t".'"'.$indexname.'" => array("'.implode($fieldnames, '","').'"'."),\n";
-               }
-               echo "\t\t\t\t\t)\n";
-               echo "\t\t\t);\n";
+               echo "\n";
        }
 }
 
-function update_structure($verbose, $action) {
+function update_structure($verbose, $action, $tables=null, $definition=null) {
        global $a, $db;
 
        $errors = false;
@@ -142,7 +130,8 @@ function update_structure($verbose, $action) {
        // Get the current structure
        $database = array();
 
-       $tables = q("show tables");
+       if (is_null($tables))
+               $tables = q("show tables");
 
        foreach ($tables AS $table) {
                $table = current($table);
@@ -151,7 +140,8 @@ function update_structure($verbose, $action) {
        }
 
        // Get the definition
-       $definition = db_definition();
+       if (is_null($definition))
+               $definition = db_definition();
 
        // Compare it
        foreach ($definition AS $name => $structure) {
@@ -231,9 +221,13 @@ function db_field_command($parameters, $create = true) {
        if ($parameters["not null"])
                $fieldstruct .= " NOT NULL";
 
-       if (isset($parameters["default"]))
-               $fieldstruct .= " DEFAULT '".$parameters["default"]."'";
-
+       if (isset($parameters["default"])){
+               if (strpos(strtolower($parameters["type"]),"int")!==false) {
+                       $fieldstruct .= " DEFAULT ".$parameters["default"];
+               } else {
+                       $fieldstruct .= " DEFAULT '".$parameters["default"]."'";
+               }
+       }
        if ($parameters["extra"] != "")
                $fieldstruct .= " ".$parameters["extra"];
 
@@ -243,20 +237,28 @@ function db_field_command($parameters, $create = true) {
        return($fieldstruct);
 }
 
-function db_create_table($name, $fields, $verbose, $action) {
+function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
        global $a, $db;
 
        $r = true;
 
        $sql = "";
+       $sql_rows = array();
        foreach($fields AS $fieldname => $field) {
-               if ($sql != "")
-                       $sql .= ",\n";
+               $sql_rows[] = "`".dbesc($fieldname)."` ".db_field_command($field);
+       }
+
+       if (!is_null($indexes)) {
 
-               $sql .= "`".dbesc($fieldname)."` ".db_field_command($field);
+               foreach ($indexes AS $indexname => $fieldnames) {
+                       $sql_index = db_create_index($indexname, $fieldnames, "");
+                       if (!is_null($sql_index)) $sql_rows[] = $sql_index;
+               }
        }
 
-       $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8";
+       $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";
@@ -282,7 +284,7 @@ function db_drop_index($indexname) {
        return($sql);
 }
 
-function db_create_index($indexname, $fieldnames) {
+function db_create_index($indexname, $fieldnames, $method="ADD") {
 
        if ($indexname == "PRIMARY")
                return;
@@ -298,7 +300,13 @@ function db_create_index($indexname, $fieldnames) {
                        $names .= "`".dbesc($fieldname)."`";
        }
 
-       $sql = sprintf("ADD INDEX `%s` (%s)", dbesc($indexname), $names);
+       $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);
        return($sql);
 }
 
@@ -447,6 +455,7 @@ function db_definition() {
                                        "uri-date" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
                                        "avatar-date" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
                                        "term-date" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+                                       "last-item" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
                                        "priority" => array("type" => "tinyint(3)", "not null" => "1", "default" => "0"),
                                        "blocked" => array("type" => "tinyint(1)", "not null" => "1", "default" => "1"),
                                        "readonly" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
@@ -1203,7 +1212,7 @@ function db_definition() {
                                        "oid_otype_type_term" => array("oid","otype","type","term"),
                                        "uid_term_tid" => array("uid","term","tid"),
                                        "type_term" => array("type","term"),
-                                       "uid_otype_type_term_created" => array("uid","otype","type","term","created"),
+                                       "uid_otype_type_term_global_created" => array("uid","otype","type","term","global","created"),
                                        "otype_type_term_tid" => array("otype","type","term","tid"),
                                        "guid" => array("guid"),
                                        )
@@ -1357,7 +1366,29 @@ function dbstructure_run(&$argv, &$argc) {
                        unset($db_host, $db_user, $db_pass, $db_data);
        }
 
-       update_structure(true, true);
+       if ($argc==2) {
+               switch ($argv[1]) {
+                       case "update":
+                               update_structure(true, true);
+                               return;
+                       case "dumpsql":
+                               print_structure(db_definition());
+                               return;
+               }
+       }
+
+
+       // print help
+       echo $argv[0]." <command>\n";
+       echo "\n";
+       echo "commands:\n";
+       echo "update            update database schema\n";
+       echo "dumpsql           dump database schema\n";
+       return;
+
+
+
+
 }
 
 if (array_search(__file__,get_included_files())===0){