conn = mysqli_init (); $conn->options (MYSQLI_READ_DEFAULT_FILE, __DIR__.'/my.cnf'); @$conn->real_connect ($host, $user, $pass, $name, $port); if (mysqli_connect_errno ()) { sleep (3); throw new Exception (mysqli_connect_errno (), mysqli_connect_error ()); return FALSE; } $this->isOpen = TRUE; $this->query ('SET CHARACTER SET utf8'); return TRUE; } /** * Closes the current connection, if it's closed does nothing. **/ function close () { if ($this->isOpen) $this->conn->close (); $this->isOpen = FALSE; } /** * Checks whether the connection is open. * * @return boolean %TRUE if connection is open, %FALSE otherwise **/ function isOpen () { return $this->isOpen; } /** * Executes the query and gets it's first row. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed[] An associative array with the first row, %NULL if error **/ function getRow ($query, $params = NULL) { $result = $this->query ($query, $params); if ($result) { $row = $result->fetch_assoc (); $result->free (); return $row; } return NULL; } /** * Executes the query and gets the first value of the first row. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed The value or %NULL if error **/ function getValue ($query, $params = NULL) { $value = NULL; $result = $this->query ($query, $params); if ($result) { $row = $result->fetch_row (); if ($row && count ($row) > 0) $value = $row[0]; $result->free (); } return $value; } /** * Executes the query stored in the specified file and gets the result. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed The value or %NULL if error **/ function queryFromFile ($file, $params = NULL) { $query = file_get_contents ($file); if ($query) return $this->query ($query, $params); return NULL; } /** * Executes the query and gets the result. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed The value or %NULL if error **/ function query ($query, $params = NULL) { $result = $this->conn->query ($this->render ($query, $params)); if (!$result) $this->checkError (); else while ($this->moreResults ()) $this->nextResult (); return $result; } /** * Execute multiple queries separated by semicolons. * * @param string $query The SQL query * @param mixed[] $params The query parameters * * @return mixed The value or %NULL if error **/ function multiQuery ($query, $params = NULL) { $success = $this->conn->multi_query ($this->render ($query, $params)); if (!$success) $this->checkError (); return $success; } function storeResult () { $result = $this->conn->store_result (); if (!$result) $this->checkError (); return $result; } function moreResults () { return $this->conn->more_results (); } function nextResult () { return $this->conn->next_result (); } /** * Check if there has been an error in the last query or multiquery, * throwing a @Db\Exception exception if any. **/ function checkError () { if ($this->conn->errno) throw new Exception ($this->conn->errno, $this->conn->error); } /** * Check if there have been warnings in the last query. * * @return boolean %TRUE if there have been warnings %FALSE otherwise **/ function checkWarnings () { return $this->conn->warning_count > 0; } /** * Renders an SQL string using the given parameters. * * @param string $query The SQL string * @param mixed[] $paramsMap The query parameters * * @return mixed The rendered SQL string **/ function render (&$query, &$paramsMap = NULL) { if (isset ($paramsMap) && is_array ($paramsMap) && count ($paramsMap) > 0) { $i = 0; $params = []; foreach ($paramsMap as $key => $value) $params[$key] = $this->renderValue ($value); $replaceFunc = function ($matches) use (&$params, &$i) { $key = substr ($matches[0], 1); if (strlen ($key) == 0) $key = $i++; if (isset ($params[$key])) return $params[$key]; return '#'. $key; }; return preg_replace_callback ('/#\w*/', $replaceFunc, $query); } else return $query; } function renderValue ($value) { if ($value !== NULL) switch (get_type ($value)) { case TYPE_BOOLEAN: return ($value) ? 'TRUE' : 'FALSE'; case TYPE_STRING: return '\'' . $this->conn->escape_string ($value) . '\''; case TYPE_DATE: return strftime ('\'%Y-%m-%d\'', $value->getTimestamp ()); case TYPE_TIME: return strftime ('\'%T\'', $value->getTimestamp ()); case TYPE_DATE_TIME: return strftime ('\'%Y-%m-%d %T\'', $value->getTimestamp ()); default: return '\'' . $this->conn->escape_string ($value) . '\''; } else return 'NULL'; } } ?>