Beispiel #1
0
 public function build()
 {
     if (parent::build()) {
         $result = $this->db->query("\n                ALTER TABLE {$this->forest_name}\n                ADD COLUMN label VARCHAR(50) DEFAULT '' NOT NULL");
         if (!$result) {
             throw new sp_MySQL_Error($this->db);
         }
     }
 }
Beispiel #2
0
 public function build()
 {
     if (parent::build()) {
         // the table wasn't existing and has been built
         $result = $this->db->query("\n                ALTER TABLE {$this->forest_name}\n                ADD COLUMN name VARCHAR(50) NOT NULL\n                ");
         if (!$result) {
             throw new sp_MySQL_Error($this->db);
         }
     }
 }
Beispiel #3
0
 public static function &import($db, $forest_name, $data)
 {
     if (is_string($data)) {
         $data = json_decode($data, true);
     }
     if (!$data || empty($data)) {
         $x = NULL;
         return $x;
     }
     // nothing to import
     // check if the table exists before doing anything else
     $sql_utils = new sp_SQLUtils($db);
     if (!$sql_utils->table_exists($forest_name)) {
         throw new sp_Error("Table `{$forest_name}` does not exist");
     }
     $ar_out = array();
     $db->query("START TRANSACTION");
     try {
         foreach ($data as $tmp_data) {
             if (empty($tmp_data["values"])) {
                 continue;
             }
             // get the tree id, if any
             $tree_id = NULL;
             if (isset($tmp_data["tree_id"])) {
                 $tree_id = intval($tmp_data["tree_id"]);
             } else {
                 $idx = array_search("tree_id", $tmp_data["fields"]);
                 if (FALSE !== $idx) {
                     // all the tree ids are equals, get the first
                     $tree_id = intval($tmp_data["values"][0][$idx]);
                 }
             }
             if (!$tree_id) {
                 // check both NULL and 0
                 // there isn't a tree_id, we must get one
                 // find a new tree_id
                 $query = "SELECT IFNULL(MAX(tree_id),0)+1 as new_id FROM {$forest_name}";
                 $result = $db->query($query, MYSQLI_STORE_RESULT);
                 if (!$result) {
                     throw new sp_MySQL_Error($db);
                 }
                 $row = $result->fetch_row();
                 $tree_id = intval($row[0]);
                 $result->close();
             } else {
                 // ensure the tree_id isn't in use
                 $query = "SELECT DISTINCT tree_id FROM {$forest_name} WHERE tree_id={$tree_id}";
                 $result = $db->query($query, MYSQLI_STORE_RESULT);
                 if (!$result) {
                     throw new sp_MySQL_Error($db);
                 }
                 $tree_exists = $result->num_rows != 0;
                 $result->close();
                 if ($tree_exists) {
                     throw new sp_Error("ImportError: tree_id {$tree_id} yet in use");
                 }
             }
             $tree = new Baobab($db, $forest_name, $tree_id);
             // if there are only standard fields we can also try to build the table
             //   (otherwise it must yet exist because we can't guess field types)
             $standard_fields = array("id", "lft", "rgt", "tree_id");
             if (count($tmp_data["fields"]) <= 4) {
                 $is_standard = TRUE;
                 foreach ($tmp_data["fields"] as $fieldName) {
                     if (FALSE === array_search($fieldName, $standard_fields)) {
                         $is_standard = FALSE;
                         break;
                     }
                 }
                 if ($is_standard) {
                     try {
                         $tree->build();
                         // if yet exists is harmless
                     } catch (sp_MySQL_Error $e) {
                         if ($db->errno != 1050) {
                             throw $e;
                         }
                         // 1050 is "table exists"
                     }
                 }
             }
             // check that the table has the involved fields
             $tree->_sql_check_fields($tmp_data["fields"]);
             $values = array();
             $nodes = array($tmp_data["values"]);
             while (!empty($nodes)) {
                 // get data of current node
                 $last_node = array_pop($nodes);
                 //save his children's array
                 $children = array_pop($last_node);
                 // append the children to the nodes to iterate over
                 if (count($children)) {
                     $nodes = array_merge($nodes, $children);
                 }
                 // keep the data
                 $values[] = $last_node;
             }
             // get the tree_id to use
             if (FALSE === array_search("tree_id", $tmp_data["fields"])) {
                 // add tree_id to fields
                 $tmp_data["fields"][] = "tree_id";
                 // add tree_id to the each row
                 foreach ($values as &$row) {
                     $row[] = $tree_id;
                 }
             }
             // add the values
             $result = $db->query("INSERT INTO {$forest_name}(" . join(",", $tmp_data["fields"]) . ") VALUES " . join(", ", sp_Lib::map_method($values, $sql_utils, "vector_to_sql_tuple")), MYSQLI_STORE_RESULT);
             if (!$result) {
                 throw new sp_MySQL_Error($db);
             }
             $ar_out[] = $tree;
         }
         // end foreach $data
     } catch (Exception $e) {
         // whatever happens we must rollback
         $db->query("ROLLBACK");
         throw $e;
     }
     $result = $db->query("COMMIT");
     if (!$result) {
         throw new sp_MySQL_Error($db);
     }
     return $ar_out;
 }