/** * convert variable from string values * * @param MongoDB $mongodb MongoDB instance * @param string $dataType data type * @param string $format string format * @param string $value string value * @param integer $integerValue integer value * @param long $longValue long value * @param string $doubleValue float value * @param string $boolValue boolea value * @param string $mixedValue mixed value (array or object) * @return mixed * @throws Exception */ protected function _convertValue($mongodb, $dataType, $format, $value, $integerValue, $longValue, $doubleValue, $boolValue, $mixedValue) { $realValue = null; switch ($dataType) { case "integer": if (class_exists("MongoInt32")) { $realValue = new MongoInt32($integerValue); } else { $realValue = intval($realValue); } break; case "long": if (class_exists("MongoInt64")) { $realValue = new MongoInt64($longValue); } else { $realValue = $longValue; } break; case "float": case "double": $realValue = doubleval($doubleValue); break; case "string": $realValue = $value; break; case "boolean": $realValue = $boolValue == "true"; break; case "null": $realValue = NULL; break; case "mixed": $eval = new VarEval($mixedValue, $format, $mongodb); $realValue = $eval->execute(); if ($realValue === false) { throw new Exception("Unable to parse mixed value, just check syntax!"); } break; } return $realValue; }
/** modify one row **/ public function doModifyRow() { $this->db = xn("db"); $this->collection = xn("collection"); $id = rock_real_id(xn("id")); //selected format last time $this->last_format = rock_cookie("rock_format", "json"); import("lib.mongo.RQuery"); $query = new RQuery($this->_mongo, $this->db, $this->collection); $this->row = $query->id($id)->findOne(); if (empty($this->row)) { $this->error = "Record is not found."; $this->display(); return; } $this->data = $this->row; unset($this->data["_id"]); import("classes.VarExportor"); $export = new VarExportor($query->db(), $this->data); $this->data = $export->export($this->last_format); if ($this->last_format == "json") { $this->data = json_unicode_to_utf8($this->data); } if ($this->isPost()) { $this->data = xn("data"); $format = x("format"); $this->last_format = $format; $row = null; $eval = new VarEval($this->data, $format, $this->_mongo->selectDb($this->db)); $row = $eval->execute(); if ($row === false || !is_array($row)) { $this->error = "Only valid {$format} is accepted."; $this->display(); return; } $query = new RQuery($this->_mongo, $this->db, $this->collection); $obj = $query->id($this->row["_id"])->find(); $oldAttrs = $obj->attrs(); $obj->setAttrs($row); foreach ($oldAttrs as $oldAttr => $oldValue) { if ($oldAttr == "_id") { continue; } if (!array_key_exists($oldAttr, $row)) { $obj->remove($oldAttr); } } try { $obj->save(); } catch (Exception $e) { $this->error = $e->getMessage(); $this->display(); return; } //remember format choice $this->_rememberFormat($format); $this->message = "Updated successfully."; } $this->display(); }
/** * Get real value from one string * * @param MongoDB $mongodb current mongodb * @param integer $dataType data type * @param string $format data format * @param string $value value in string format * @return mixed * @throws Exception * @since 1.1.0 */ function rock_real_value($mongodb, $dataType, $format, $value) { $realValue = null; switch ($dataType) { case "integer": case "float": case "double": $realValue = doubleval($value); break; case "string": $realValue = $value; break; case "boolean": $realValue = $value == "true"; break; case "null": $realValue = NULL; break; case "mixed": $eval = new VarEval($value, $format, $mongodb); $realValue = $eval->execute(); if ($realValue === false) { throw new Exception("Unable to parse mixed value, just check syntax!"); } break; } return $realValue; }
/** execute command **/ public function doCommand() { $ret = $this->_server->listDbs(); $this->dbs = $ret["databases"]; if (!$this->isPost()) { x("command", json_format("{listCommands:1}")); if (!x("db")) { x("db", "admin"); } } if ($this->isPost()) { $command = xn("command"); $format = x("format"); if ($format == "json") { $command = $this->_decodeJson($command); } else { $eval = new VarEval($command); $command = $eval->execute(); } if (!is_array($command)) { $this->message = "You should send a valid command"; $this->display(); return; } $this->ret = $this->_highlight($this->_mongo->selectDB(xn("db"))->command($command), $format); } $this->display(); }