* @since 06 Mar 2006 16:41:32
*/
function escapeString($string)
{
if (!$this->_connected) {
return false;
}
return mysql_real_escape_string($string, $this->dbh);
}
/**
* A wrapper for mysql_query. Allows us to set the database link_identifier,
* to trap errors and ease debugging.
*
* @param string $query The SQL query to execute
* @param bool $debug If true, prints debugging info
* @return resource Query identifier
*/
function query($query, $debug=false)
{
$app =& App::getInstance();
if (!$this->_connected) {
return false;
}
$this->_query_count++;
$debugqry = preg_replace("/\n[\t ]+/", "\n", $query);
if ($this->getParam('db_always_debug') || $debug) {
echo "\n";
}
// Execute!
$qid = mysql_query($query, $this->dbh);
// Error checking.
if (!$qid || mysql_error($this->dbh)) {
$app->logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($this->dbh), mysql_error($this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__);
if ($this->getParam('db_debug')) {
echo '' . wordwrap(mysql_error($this->dbh)) . '
' . htmlspecialchars($debugqry) . '
';
}
// Die if db_die_on_failure = true, or just continue without connection
return $this->_fail();
}
return $qid;
}
/**
* Loads a list of tables in the current database into an array, and returns
* true if the requested table is found. Use this function to enable/disable
* functionality based upon the current available db tables or to dynamically
* create tables if missing.
*
* @param string $table The name of the table to search.
* @param bool $strict Get fresh table info (in case DB changed).
* @return bool true if given $table exists.
*/
function tableExists($table, $use_cached_results=true)
{
$app =& App::getInstance();
if (!$this->_connected) {
return false;
}
if (!isset($this->existing_tables) || !$use_cached_results) {
$this->existing_tables = array();
$qid = $this->query("SHOW TABLES");
while (list($row) = mysql_fetch_row($qid)) {
$this->existing_tables[] = $row;
}
}
if (in_array($table, $this->existing_tables)) {
return true;
} else {
$app->logMsg(sprintf('Nonexistent DB table: %s.%s', $this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__);
return false;
}
}
/**
* Tests if the given array of columns exists in the specified table.
*
* @param string $table The name of the table to search.
* @param array $columns An array of column names.
* @param bool $strict Exact schema match, or are additional fields in the table okay?
* @param bool $strict Get fresh table info (in case DB changed).
* @return bool true if given $table exists.
*/
function columnExists($table, $columns, $strict=true, $use_cached_results=true)
{
if (!$this->_connected) {
return false;
}
// Ensure the table exists.
if (!$this->tableExists($table, $use_cached_results)) {
return false;
}
// For single-value columns.
if (!is_array($columns)) {
$columns = array($columns);
}
if (!isset($this->table_columns[$table]) || !$use_cached_results) {
// Populate and cache array of current columns for this table.
$this->table_columns[$table] = array();
$qid = $this->query("DESCRIBE $table");
while ($row = mysql_fetch_row($qid)) {
$this->table_columns[$table][] = $row[0];
}
}
if ($strict) {
// Do an exact comparison of table schemas.
sort($columns);
sort($this->table_columns[$table]);
return $this->table_columns[$table] == $columns;
} else {
// Only check that the specified columns are available in the table.
$match_columns = array_intersect($this->table_columns[$table], $columns);
sort($columns);
sort($match_columns);
return $match_columns == $columns;
}
}
/*
* Return the total number of queries run this for.
*
* @access public
* @return int Number of queries
* @author Quinn Comendant
* @version 1.0
* @since 15 Jun 2006 11:46:05
*/
function numQueries()
{
return $this->_query_count;
}
/**
* Reset cached items.
*
* @access public
* @author Quinn Comendant
* @since 28 Aug 2005 22:10:50
*/
function resetCache()
{
$this->existing_tables = null;
$this->table_columns = null;
}
} // End.
?>