Skip to content
This repository has been archived by the owner on Jul 13, 2019. It is now read-only.

Commit

Permalink
Code update and enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabeel Shahzad committed Mar 20, 2010
1 parent e1a22d7 commit 5dc3b2e
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 73 deletions.
40 changes: 40 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
ezDB

Based on ezSQL by Justin Vincent.

Release Notes:

* Oracle8_9 class renamed to just oracle
* Updated init() function to be dynamic, instead of hard-coding mysql/mysqli/oracle, etc. To add a DB connector type, just create:

ezdb_TYPE.class.php

Then call as:

DB::init(TYPE);

* Added DB::set_error_handler($function) - pass in a function to call when there's an error
Same parameters as: http://us.php.net/manual/en/function.call-user-func-array.php

This will be called if DB::$log_errors is true:

DB::set_log_errors(true);

An alternative to try/catch, as this works through every function called in the static class

It passes one argument to it, which is an array containing debug information.
i.e.
function db_error($debug_info) { ... }

$debug info will be an array with:

$debug_info = array(
'backtrace' => $backtrace,
'sql' => self::$last_query,
'error' => self::$error,
'errno' => self::$errno,
'dbuser' => self::$dbuser,
'dbname' => self::$dbname,
'dbpass' => self::$dbpass,
'dbserver' => self::$dbserver,
);
238 changes: 169 additions & 69 deletions ezDB.class.php → ezdb.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@
* @license BSD License
* @package codon_core
*/

function ezdb_autoloader($class_name)
{
$class_name = strtolower($class_name);
include dirname(__FILE__).DIRECTORY_SEPARATOR.$class_name.'.class.php';
}

include dirname(__FILE__).'/ezDB_Base.class.php';

spl_autoload_register('ezdb_autoloader');

/**
* This is the < PHP 5.3 version
Expand All @@ -57,8 +62,16 @@ class DB

public static $throw_exceptions = true;
public static $default_type = OBJECT;
public static $show_errors = false;
public static $log_errors = false;
public static $error_handler = null;

public static $table_prefix = '';

protected static $dbuser;
protected static $dbpass;
protected static $dbname;
protected static $dbserver;

/**
* Private contructor, don't allow for
Expand All @@ -74,25 +87,6 @@ public function __destruct()
@self::$DB->close();
}

/* So it passes through to the main class */
/*public function __get($name)
{
return self::$DB->{$name};
}
public function __set($name, $value)
{
self::$DB->{$name} = $value;
}
public static function __call($name, $arguments)
{
if(method_exists(self::$DB, $name))
{
return call_user_func_array(array(self::$DB, $name), $arguments);
}
}*/

/**
* Return the singleton instance of the DB class
*
Expand All @@ -111,57 +105,41 @@ public static function get_instance()
*/
public static function init($type='mysql')
{
if($type == 'mysql' || $type == '')
{
include dirname(__FILE__).DIRECTORY_SEPARATOR.'ezDB_MySQL.class.php';

if(!self::$DB = new ezDB_mysql())
{
self::$error = self::$DB->error;
self::$errno = self::$DB->errno;

return false;
}

return true;
}
elseif($type == 'mysqli')
{
include dirname(__FILE__).DIRECTORY_SEPARATOR.'ezDB_MySQLi.class.php';

if(!self::$DB = new ezDB_mysqli())
{
self::$error = self::$DB->error;
self::$errno = self::$DB->errno;
return false;
}

return true;
}
elseif($type == 'oracle')
$class_name = 'ezdb_'.$type;
if(!self::$DB = new $class_name())
{
include dirname(__FILE__).DIRECTORY_SEPARATOR.'ezDB_Oracle.class.php';

if(!self::$DB = new ezDB_oracle8_9())
{
self::$error = self::$DB->error;
self::$errno = self::$DB->errno;
return false;
}

return true;
}
else
{
include dirname(__FILE__).DIRECTORY_SEPARATOR.'ezDB_MySQL.class.php';
self::$error = self::$DB->error;
self::$errno = self::$DB->errno;

self::$DB = new ezDB_mysql();
self::$error = 'Invalid database type';
return true;
return false;
}

return true;
}

public static function set_log_errors($bool)
{
self::$log_errors = $bool;
}

public static function set_use_exceptions($bool)
{
self::$throw_exceptions = $bool;
}

/**
* Set a function/class as an error handler. Function is the
* same parameter sent to
*
* http://us.php.net/manual/en/function.call-user-func-array.php
*
* @param $handler method name or class to call on an error
*
*/
public static function set_error_handler($function)
{
self::$error_handler = $function;
}

/**
* Enable or disable caching, can be set per-query
Expand Down Expand Up @@ -245,6 +223,11 @@ public static function connect($user='', $pass='', $name='', $server='')
return false;
}

self::$dbuser = $user;
self::$dbpass = $pass;
self::$dbname = $name;
self::$dbserver = $server;

self::$DB->throw_exceptions = self::$throw_exceptions;
self::$connected = true;
return true;
Expand Down Expand Up @@ -302,11 +285,11 @@ public static function set_table_prefix($prefix)
* @param constant $type
* @return resultset
*/
public static function quick_select($table, $fields, $cond='', $type=OBJECT)
public static function quick_select($params)
{
self::$DB->throw_exceptions = self::$throw_exceptions;

return self::$DB->quick_select($table, $fields, $cond, $type);
return self::$DB->quick_select($params);
}

/**
Expand Down Expand Up @@ -340,6 +323,104 @@ public static function quick_update($table, $fields, $cond='', $allowed_cols='')
return self::$DB->quick_update($table, $fields, $cond, $allowed_cols);
}


/**
* Build a WHERE clause for an SQL statement with supplied parameters
*
* @param array $fields associative array with column=>value
* @return string string where
*
*/
public static function build_where($fields)
{
return self::$DB->build_where($fields);
}


/**
* Build the update clause (after the SET and before WHERE)
*
* @param array $fields associative array (col_name=>value)
* @return string the SQL string
*
*/
public static function build_update($fields)
{
if(!is_array($fields) || empty($fields))
{
return false;
}

$sql = '';
$sql_cols = array();

foreach($fields as $col => $value)
{

/* If there's a value just added */
if(is_int($col))
{
$sql_cols[] = $value;
continue;
}

$tmp = "`{$col}`=";

if(is_int($value))
{
$tmp .= $value;
}
else
{
if($value === "NOW()")
{
$tmp.='NOW()';
}
else
{
$value = DB::escape($value);
$tmp.="'{$value}'";
}
}

$sql_cols[] = $tmp;
}

$sql .= implode(', ', $sql_cols);
unset($sql_cols);

return $sql;
}

/**
* Write out the last query to a debug log, or error
*
* @return mixed This is the return value description
*
*/
public static function write_debug()
{
if(self::$error_handler === null || self::$log_errors == false)
{
return;
}

$backtrace = debug_backtrace();

$debug_info = array(
'backtrace' => $backtrace,
'sql' => self::$last_query,
'error' => self::$error,
'errno' => self::$errno,
'dbuser' => self::$dbuser,
'dbname' => self::$dbname,
'dbpass' => self::$dbpass,
'dbserver' => self::$dbserver,
);

call_user_func_array(self::$error_handler, array($debug_info));
}

/**
* Return array of results. Default returns array of
* objects. Can be ARRAY_A, ARRAY_N or OBJECT, for
Expand All @@ -363,6 +444,12 @@ public static function get_results($query, $type='')
self::$num_rows = self::$DB->num_rows;
self::$last_query = $query;

// Log any erronious queries
if(self::$DB->errno != 0)
{
self::write_debug();
}

return $ret;
}

Expand All @@ -386,6 +473,12 @@ public static function get_row($query, $type='', $y=0)
self::$errno = self::$DB->errno;
self::$last_query = $query;

// Log any erronious queries
if(self::$DB->errno != 0)
{
self::write_debug();
}

return $ret;
}

Expand All @@ -407,6 +500,12 @@ public static function query($query)
self::$insert_id = self::$DB->insert_id;
self::$last_query = $query;

// Log any erronious queries
if(self::$DB->errno != 0)
{
self::write_debug();
}

return $ret; //self::$insert_id;
}

Expand Down Expand Up @@ -515,6 +614,7 @@ public static function escape($val)

public static function debug($return = false)
{
return self::$DB->debug($return);
if(self::$show_errors === true)
return self::$DB->debug($return);
}
}
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions ezDB_MySQLi.class.php → ezdb_mysqli.class.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,6 @@ function execute($query, $params)
$stmt->execute();
}*/
}

?>
2 changes: 1 addition & 1 deletion ezDB_Oracle.class.php → ezdb_oracle.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*/


class ezDB_oracle8_9 extends ezDB_Base
class ezDB_oracle extends ezDB_Base
{
/**
* Connects to database immediately, unless $dbname is blank
Expand Down

0 comments on commit 5dc3b2e

Please sign in to comment.