nativeQuery() public static méthode

Executes the SQL query - Monostate for Dibi\Connection::nativeQuery().
public static nativeQuery ( $sql ) : Dibi\Result | integer
Résultat Dibi\Result | integer result set object (if any)
 /**
  * Load initial user data (Rights, Preferences and Bookmarks).
  *
  * @see AbstractAjxpUser#load()
  */
 public function load()
 {
     $this->log('Loading all user data..');
     // update group
     $res = dibi::query('SELECT [groupPath] FROM [ajxp_users] WHERE [login] = %s', $this->getId());
     $this->groupPath = $res->fetchSingle();
     if (empty($this->groupPath)) {
         // Auto migrate from old version
         $this->setGroupPath("/");
     }
     $result_rights = dibi::query('SELECT [repo_uuid], [rights] FROM [ajxp_user_rights] WHERE [login] = %s', $this->getId());
     $this->rights = $result_rights->fetchPairs('repo_uuid', 'rights');
     // Db field returns integer or string so we are required to cast it in order to make the comparison
     if (isset($this->rights["ajxp.admin"]) && (bool) $this->rights["ajxp.admin"] === true) {
         $this->setAdmin(true);
     }
     if (isset($this->rights["ajxp.parent_user"])) {
         $this->setParent($this->rights["ajxp.parent_user"]);
     }
     if (isset($this->rights["ajxp.hidden"])) {
         $this->setHidden(true);
     }
     if ("postgre" == $this->storage->sqlDriver["driver"]) {
         dibi::nativeQuery('SET bytea_output = escape');
     }
     $result_prefs = dibi::query('SELECT [name], [val] FROM [ajxp_user_prefs] WHERE [login] = %s', $this->getId());
     $this->prefs = $result_prefs->fetchPairs('name', 'val');
     $result_bookmarks = dibi::query('SELECT [repo_uuid], [path], [title] FROM [ajxp_user_bookmarks] WHERE [login] = %s', $this->getId());
     $all_bookmarks = $result_bookmarks->fetchAll();
     if (!is_array($this->bookmarks)) {
         $this->bookmarks = array();
     }
     $this->bookmarks = array();
     foreach ($all_bookmarks as $b) {
         if (!is_array($this->bookmarks[$b['repo_uuid']])) {
             $this->bookmarks[$b['repo_uuid']] = array();
         }
         $this->bookmarks[$b['repo_uuid']][] = array('PATH' => $b['path'], 'TITLE' => $b['title']);
     }
     // COLLECT ROLES TO LOAD
     $rolesToLoad = array();
     if (isset($this->rights["ajxp.roles"])) {
         if (is_string($this->rights["ajxp.roles"])) {
             if (strpos($this->rights["ajxp.roles"], '$phpserial$') === 0) {
                 $this->rights["ajxp.roles"] = unserialize(str_replace('$phpserial$', '', $this->rights["ajxp.roles"]));
             } else {
                 if (strpos($this->rights["ajxp.roles"], '$json$') === 0) {
                     $this->rights["ajxp.roles"] = json_decode(str_replace('$json$', '', $this->rights["ajxp.roles"]), true);
                 } else {
                     $this->rights["ajxp.roles"] = unserialize($this->rights["ajxp.roles"]);
                 }
             }
         }
         if (is_array($this->rights["ajxp.roles"])) {
             $rolesToLoad = array_keys($this->rights["ajxp.roles"]);
         }
     }
     $rolesToLoad[] = "AJXP_GRP_/";
     if ($this->groupPath != null) {
         $base = "";
         $exp = explode("/", $this->groupPath);
         foreach ($exp as $pathPart) {
             if (empty($pathPart)) {
                 continue;
             }
             $base = $base . "/" . $pathPart;
             $rolesToLoad[] = "AJXP_GRP_" . $base;
         }
     }
     $rolesToLoad[] = "AJXP_USR_/" . $this->id;
     // NOW LOAD THEM
     if (count($rolesToLoad)) {
         $allRoles = AuthService::getRolesList($rolesToLoad);
         foreach ($rolesToLoad as $roleId) {
             if (isset($allRoles[$roleId])) {
                 $this->roles[$roleId] = $allRoles[$roleId];
                 $this->rights["ajxp.roles"][$roleId] = true;
                 $roleObject = $allRoles[$roleId];
                 if ($roleObject->alwaysOverrides()) {
                     if (!isset($this->rights["ajxp.roles.sticky"]) || !is_array($this->rights["ajxp.roles.sticky"])) {
                         $this->rights["ajxp.roles.sticky"] = array();
                     }
                     $this->rights["ajxp.roles.sticky"][$roleId] = true;
                 }
             } else {
                 if (is_array($this->rights["ajxp.roles"]) && isset($this->rights["ajxp.roles"][$roleId])) {
                     unset($this->rights["ajxp.roles"][$roleId]);
                 }
             }
         }
     }
     if (!isset($this->rights["ajxp.roles.order"]) && is_array($this->rights["ajxp.roles"])) {
         // Create sample order
         $this->rights["ajxp.roles.order"] = array();
         $index = 0;
         foreach ($this->rights["ajxp.roles"] as $id => $rBool) {
             $this->rights["ajxp.roles.order"][$id] = $index;
             $index++;
         }
     } else {
         $this->rights["ajxp.roles.order"] = unserialize(str_replace('$phpserial$', '', $this->rights["ajxp.roles.order"]));
     }
     // CHECK USER PERSONAL ROLE
     if (isset($this->roles["AJXP_USR_" . "/" . $this->id]) && is_a($this->roles["AJXP_USR_" . "/" . $this->id], "AJXP_Role")) {
         $this->personalRole = $this->roles["AJXP_USR_" . "/" . $this->id];
     } else {
         // MIGRATE NOW !
         $originalRights = $this->rights;
         $changes = $this->migrateRightsToPersonalRole();
         // SAVE RIGHT AND ROLE
         if ($changes > 0) {
             // There was an actual migration, let's save the changes now.
             $removedRights = array_keys(array_diff($originalRights, $this->rights));
             if (count($removedRights)) {
                 // We use (%s) instead of %in to pass everything as string ('1' instead of 1)
                 dibi::query("DELETE FROM [ajxp_user_rights] WHERE [login] = %s AND [repo_uuid] IN (%s)", $this->getId(), $removedRights);
             }
             AuthService::updateRole($this->personalRole);
         } else {
             $this->personalRole = new AJXP_Role("AJXP_USR_" . "/" . $this->id);
         }
         $this->roles["AJXP_USR_" . "/" . $this->id] = $this->personalRole;
     }
     $this->recomputeMergedRole();
 }
 public function upgradeDB()
 {
     $confDriver = ConfService::getConfStorageImpl();
     $authDriver = ConfService::getAuthDriverImpl();
     $logger = AJXP_Logger::getInstance();
     if (is_a($confDriver, "sqlConfDriver")) {
         $conf = AJXP_Utils::cleanDibiDriverParameters($confDriver->getOption("SQL_DRIVER"));
         if (!is_array($conf) || !isset($conf["driver"])) {
             return "Nothing to do";
         }
         switch ($conf["driver"]) {
             case "sqlite":
             case "sqlite3":
                 $ext = ".sqlite";
                 break;
             case "postgre":
                 $ext = ".pgsql";
                 break;
             case "mysql":
                 $ext = is_file($this->workingFolder . "/" . $this->dbUpgrade . ".mysql") ? ".mysql" : ".sql";
                 break;
             default:
                 return "ERROR!, DB driver " . $conf["driver"] . " not supported yet in __FUNCTION__";
         }
         $file = $this->dbUpgrade . $ext;
         if (!is_file($this->workingFolder . "/" . $file)) {
             return "Nothing to do.";
         }
         $sqlInstructions = file_get_contents($this->workingFolder . "/" . $file);
         $parts = array_map("trim", explode("/* SEPARATOR */", $sqlInstructions));
         $results = array();
         $errors = array();
         dibi::connect($conf);
         dibi::begin();
         foreach ($parts as $sqlPart) {
             if (empty($sqlPart)) {
                 continue;
             }
             try {
                 dibi::nativeQuery($sqlPart);
                 $results[] = $sqlPart;
             } catch (DibiException $e) {
                 $errors[] = $sqlPart . " (" . $e->getMessage() . ")";
             }
         }
         dibi::commit();
         dibi::disconnect();
         if (!count($errors)) {
             return "Database successfully upgraded";
         } else {
             return "Database upgrade failed. <br>The following statements were executed : <br>" . implode("<br>", $results) . ",<br><br> The following statements failed : <br>" . implode("<br>", $errors) . "<br><br> You should manually upgrade your DB.";
         }
     }
 }
 public function simpleStoreGet($storeID, $dataID, $dataType, &$data)
 {
     if ($this->sqlDriver["driver"] == "postgre") {
         dibi::nativeQuery("SET bytea_output=escape");
     }
     $children_results = dibi::query("SELECT * FROM [ajxp_simple_store] WHERE [store_id]=%s AND [object_id]=%s", $storeID, $dataID);
     $value = $children_results->fetchAll();
     if (!count($value)) {
         return false;
     }
     $value = $value[0];
     if ($dataType == "serial") {
         $data = unserialize($value["serialized_data"]);
     } else {
         $data = $value["binary_data"];
     }
     if (isset($value["related_object_id"])) {
         return $value["related_object_id"];
     } else {
         return false;
     }
 }
 public static function runCreateTablesQuery($p, $file)
 {
     switch ($p["driver"]) {
         case "sqlite":
         case "sqlite3":
             if (!file_exists(dirname($p["database"]))) {
                 @mkdir(dirname($p["database"]), 0755, true);
             }
             $ext = ".sqlite";
             break;
         case "mysql":
             $ext = ".mysql";
             break;
         case "postgre":
             $ext = ".pgsql";
             break;
         default:
             return "ERROR!, DB driver " . $p["driver"] . " not supported yet in __FUNCTION__";
     }
     $result = array();
     $file = dirname($file) . "/" . str_replace(".sql", $ext, basename($file));
     $sql = file_get_contents($file);
     $separators = explode("/** SEPARATOR **/", $sql);
     $allParts = array();
     foreach ($separators as $sep) {
         $explode = explode("\n", trim($sep));
         $firstLine = array_shift($explode);
         if ($firstLine == "/** BLOCK **/") {
             $allParts[] = $sep;
         } else {
             $parts = explode(";", $sep);
             $remove = array();
             for ($i = 0; $i < count($parts); $i++) {
                 $part = $parts[$i];
                 if (strpos($part, "BEGIN") && isset($parts[$i + 1])) {
                     $parts[$i] .= ';' . $parts[$i + 1];
                     $remove[] = $i + 1;
                 }
             }
             foreach ($remove as $rk) {
                 unset($parts[$rk]);
             }
             $allParts = array_merge($allParts, $parts);
         }
     }
     dibi::connect($p);
     dibi::begin();
     foreach ($allParts as $createPart) {
         $sqlPart = trim($createPart);
         if (empty($sqlPart)) {
             continue;
         }
         try {
             dibi::nativeQuery($sqlPart);
             $resKey = str_replace("\n", "", substr($sqlPart, 0, 50)) . "...";
             $result[] = "OK: {$resKey} executed successfully";
         } catch (DibiException $e) {
             $result[] = "ERROR! {$sqlPart} failed";
         }
     }
     dibi::commit();
     dibi::disconnect();
     $message = implode("\n", $result);
     if (strpos($message, "ERROR!")) {
         return $message;
     } else {
         return "SUCCESS:" . $message;
     }
 }
Exemple #5
0
     $test = AJXP_Utils::cleanDibiDriverParameters($confDriver->getOption("SQL_DRIVER"));
 }
 if (is_array($test) && isset($test["driver"]) && $test["driver"] == "mysql") {
     echo "Upgrading MYSQL database ...";
     $parts = array_map("trim", explode("/* SEPARATOR */", $dbInst));
     $results = array();
     $errors = array();
     require_once AJXP_BIN_FOLDER . "/dibi.compact.php";
     dibi::connect($test);
     dibi::begin();
     foreach ($parts as $sqlPart) {
         if (empty($sqlPart)) {
             continue;
         }
         try {
             dibi::nativeQuery($sqlPart);
             echo "<div class='upgrade_result success'>{$sqlPart} ... OK</div>";
         } catch (DibiException $e) {
             $errors[] = $e->getMessage();
             echo "<div class='upgrade_result success'>{$sqlPart} ... FAILED (" . $e->getMessage() . ")</div>";
         }
     }
     dibi::commit();
     dibi::disconnect();
 } else {
     if (is_array($test) && $test["driver"] != "mysql") {
         echo "Cannot auto-upgrade Sqlite or PostgreSql DB automatically, please review the update instructions.";
     } else {
         echo "Nothing to do for the DB";
     }
 }