registry =& $registry; } // ################################################################### /** * Connect to a the specified database * * @access public * * @param string Server name * @param string User name * @param string Password * @param string Database name * @param bool Use p-connect? * * @return bool Result of connect */ function connect($server, $user, $password, $database, $pconnect) { define('ISSO_DB_LAYER', 'db_mysql_pdo'); if ($this->dblink == false) { $this->dblink = new PDO("mysql:dbname=$database;host=$server", $user, $password); if ($this->dblink == false) { $this->error('DB-Link == false, cannot connect'); return false; } return true; } } // ################################################################### /** * Send a query to the open database link * * @access public * * @param string Query string * * @return integer Result */ function query($string) { $time = microtime(); $this->querystr = $string; $this->result = $this->dblink->query($string); if (!$this->result) { $this->error('Invalid SQL query'); } $this->history[] = $history = array('query' => $string, 'time' => ($this->registry->is_loaded('functions') ? $this->registry->modules['functions']->fetch_microtime_diff($time) : 0), 'trace' => $this->registry->format_debug_trace(debug_backtrace())); if (defined('ISSO_SHOW_QUERIES_LIVE')) { if (constant('ISSO_SHOW_QUERIES_LIVE')) { print($this->construct_query_debug($history)); } } return $this->result; } // ################################################################### /** * Escape a string (depending on character set, if supported) * * @access public * * @param string String to be escaped * * @return string Escaped string */ function escape_string($string) { $q = $this->dblink->quote($string, PDO::PARAM_STR); // This will put quotes arround the string, which is incompatible // with how queries are written in Bugdar. Remove them here. return substr($q, 1, -1); } // ################################################################### /** * Escapes a binary string for insertion into the database * * @access public * * @param string Unescaped data * * @return string Escaped binary data */ function escape_binary($binary) { return $this->escape_string($binary); } // ################################################################### /** * Fetch the query result as an array * * @access public * * @param integer Result * @param bool Return an associative array? * * @return array A row of the query result */ function fetch_array($result, $assoc = true) { $style = $assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM; return $result->fetch($style); } // ################################################################### /** * Fetch the query result as an object * * @access public * * @param integer Result * * @return object An object with the query result */ function fetch_object($result) { return $result->fetch(PDO::FETCH_OBJ); } // ################################################################### /** * Send a query and return the first row of the results * * @access public * * @param string Query string * @param string Result return function (in the database layer) * * @return mixed Results in variable formats */ function query_first($string, $callback = 'fetch_array') { $resource = $this->query($string); if ($resource) { $return = $this->$callback($resource); $this->free_result($resource); return $return; } else { return false; } } // ################################################################### /** * Free the current query result * * @access public * * @param integer Result */ function free_result($result) { $result->closeCursor(); $this->result = null; $this->querystr = ''; } // ################################################################### /** * Fetch the unique ID of the record just inserted * * @access public * * @return integer Insert-ID */ function insert_id() { return $this->dblink->lastInsertId(); } // ################################################################### /** * Fetch the number of rows in the result * * @access public * * @param integer Result * * @return integer Number of rows */ function num_rows($result) { return $result->rowCount(); } // ################################################################### /** * Fetch the number of rows affected by the query * * @access public * * @param integer Result * * @return integer Number of affected rows */ function affected_rows($result) { return $result->rowCount(); } // ################################################################### /** * Constructs a table of query information output that is used in some * other modules to display a list of queries. This merely formats * a DB->history array entry nicely * * @access public * * @param array An entry from DB->history * * @return string A formatted table block */ function construct_query_debug($query) { $block = "Query:\n\n
" . $this->registry->entity_encode($query['query']) . "
\n"; $block .= "\n\t\n\t\t"; $block .= "Time: $query[time]
\n\t\t
\n\t\t"; $block .= "Backtrace:\n\t\t
" . implode("
\n", $query['trace']) . "
\n\t\n"; return $this->registry->message('Query Debug', $block, 1, true, false, 0); } // ################################################################### /** * Error wrapper for ISSO->message() * * @access protected * * @param string User defined error message */ function error($message) { if ($this->showerrors) { if ($this->dblink) { $err = $this->dblink->errorInfo(); $this->errnum = $err[0]; $this->errstr = $err[1] . ': ' . $err[2]; } $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;'; $message_prepped = "
\n

"; $message_prepped .= "\n\t» Query:\n

" . htmlspecialchars($this->querystr) ."
\n
"; $message_prepped .= "\n\t» Error Number: " . $this->errnum . "\n
"; $message_prepped .= "\n\t» Error Message: " . $this->errstr . "\n
"; $message_prepped .= "\n\t» Additional Notes: " . $message . "\n
"; $message_prepped .= "\n\t» File: " . $_SERVER['PHP_SELF'] . "\n"; $message_prepped .= "\n

\n
"; $this->registry->message('Database Error in `' . $this->registry->application . '`', $message_prepped, 3); exit; } } }