/** * Custom write() function * * @param string $session_id * @param string $session_data * * @return bool|string */ public function write($session_id, $session_data) { $hash = md5(($this->lock_to_user_agent && isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '') . ($this->lock_to_ip && isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '') . $this->security_code); /* @noinspection PhpWrongStringConcatenationInspection */ $query = 'INSERT INTO ' . $this->table_name . "\n (\n session_id,\n hash,\n session_data,\n session_expire\n )\n VALUES\n (\n '" . $this->db->escape($session_id) . "',\n '" . $this->db->escape($hash) . "',\n '" . $this->db->escape($session_data) . "',\n '" . $this->db->escape(time() + $this->session_lifetime) . "'\n )\n ON DUPLICATE KEY UPDATE\n session_data = '" . $this->db->escape($session_data) . "',\n session_expire = '" . $this->db->escape(time() + $this->session_lifetime) . "'\n "; // insert OR update session's data $result = $this->db->query($query); if ($result !== false) { return true; } else { return false; } }
/** * Error-handling for the sql-query. * * @param string $errorMsg * @param string $sql * * @throws \Exception * * @return bool */ private function queryErrorHandling($errorMsg, $sql) { if ($errorMsg === 'DB server has gone away' || $errorMsg === 'MySQL server has gone away') { static $reconnectCounter; // exit if we have more then 3 "DB server has gone away"-errors if ($reconnectCounter > 3) { $this->_debug->mailToAdmin('SQL-Fatal-Error', $errorMsg . ":\n<br />" . $sql, 5); throw new \Exception($errorMsg); } else { $this->_debug->mailToAdmin('SQL-Error', $errorMsg . ":\n<br />" . $sql); // reconnect $reconnectCounter++; $this->_db->reconnect(true); // re-run the current query return $this->execute(); } } else { $this->_debug->mailToAdmin('SQL-Warning', $errorMsg . ":\n<br />" . $sql); // this query returned an error, we must display it (only for dev) !!! $this->_debug->displayError($errorMsg . ' | ' . $sql); } return false; }
/** * Log the current query via "$this->logger". * * @param string $sql sql-query * @param int $duration * @param int $results field_count | insert_id | affected_rows * @param bool $sql_error * * @return bool */ public function logQuery($sql, $duration, $results, $sql_error = false) { $logLevelUse = strtolower($this->logger_level); if ($sql_error === false && ($logLevelUse !== 'trace' && $logLevelUse !== 'debug')) { return false; } // set log-level if ($sql_error === true) { $logLevel = 'error'; } else { $logLevel = $logLevelUse; } // get extra info $infoExtra = \mysqli_info($this->_db->getLink()); if ($infoExtra) { $infoExtra = ' | info => ' . $infoExtra; } // // logging // $info = 'time => ' . round($duration, 5) . ' | results => ' . (int) $results . $infoExtra . ' | SQL => ' . UTF8::htmlentities($sql); $fileInfo = $this->getFileAndLineFromSql(); return $this->logger(array($logLevel, '<strong>' . date('d. m. Y G:i:s') . ' (' . $fileInfo['file'] . ' line: ' . $fileInfo['line'] . '):</strong> ' . $info . '<br>', 'sql')); }
public static function insertNewInstance($instance) { $db = DB::getInstance(); $db->insert('serieinstance', ["Instance" => $instance]); return $db->insert_id(); }
/** * can handel select/insert/update/delete queries * * @param string $query sql-query * @param bool $useCache use cache? * @param int $cacheTTL cache-ttl in seconds * * @return bool|int|array "array" by "<b>SELECT</b>"-queries<br /> * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> * "true" by e.g. "DROP"-queries<br /> * "false" on error * */ public static function execSQL($query, $useCache = false, $cacheTTL = 3600) { $db = DB::getInstance(); if ($useCache === true) { $cache = new Cache(null, null, false, $useCache); $cacheKey = "sql-" . md5($query); if ($cache->getCacheIsReady() === true && $cache->existsItem($cacheKey)) { return $cache->getItem($cacheKey); } } else { $cache = false; } $result = $db->query($query); if ($result instanceof Result) { $return = $result->fetchAllArray(); if (isset($cacheKey) && $useCache === true && $cache instanceof Cache && $cache->getCacheIsReady() === true) { $cache->setItem($cacheKey, $return, $cacheTTL); } } else { $return = $result; } return $return; }
/** * Returns an instance of the database object * @return \voku\db\DB */ public function getDb() { if (is_null($this->db)) { $creds = $this->getCredentials(); $this->db = vDb::getInstance($this->credentials['host'], $this->credentials['user'], $this->credentials['password'], $this->credentials['database']); } return $this->db; }
public function testInstanceOf() { self::assertInstanceOf('voku\\db\\DB', DB::getInstance()); }