* * @Author: shacka :: shacka@weandcat.te.ua * @URL: http://www.weandcat.te.ua/ * * ------------------------------------------------------------- */ /****************[ Fetch modes ]****************/ /* define("FM_BOOL", 1); define("FM_SIMPLE", 2); define("FM_ARR_ARRAY", 4); define("FM_ARR_SIMPLE", 8); define("FM_COL_ARRAY", 16); define("FM_COL_SIMPLE", 32); define("FM_ASSOC_ARRAY", 64); define("FM_ASSOC_SIMPLE", 128); define("FM_OBJ_ARRAY", 256); define("FM_OBJ_SIMPLE", 512); define("FM_KEY_SIMPLE", 1024); */ /********************[ main ]********************/ /** * @property-read $tblVolunteerGoal string * @property-read $tblMember2Volunteer string * @property-read $tblDMncarMemberType string * @property-read $tblDMncarLMemberType string * @property-read $tblDMiscMemberType string * @property-read $tblDMemberStatus string * @property-read $tblVolunteer string * @property-read $tblMember string * @property-read $tblLocation string */ class S_db2 { /* var $is_connected; // Determine is connection are opened or not var $id; // Resourse handler for this connection var $last_query; // Last query executed var $last_error; // Last error var $name; // DB name var $user; // DB user var $pwd; // DB password var $host; // DB host var $prefix; // DB prefix // function S_db2( [$name, $user, $pwd, $host, $prefix[, $new]] | [res_id] ) function S_db2() { $this->is_connected = false; if (func_num_args() > 1) { $this->name = func_get_arg(0); $this->user = func_get_arg(1); $this->pwd = func_get_arg(2); $this->host = func_get_arg(3); $this->prefix = func_get_arg(4); if (func_num_args() > 5) $new = func_get_arg(5); else $new = false; if (!$this->id = @mysql_connect($this->host, $this->user, $this->pwd, $new)) { $this->error(); return false; } if (!@mysql_select_db($this->name, $this->id)) { $this->error(); return false; } } else { $this->id = func_get_arg(0); } $this->is_connected = true; $this->_initTables(); } function error() { $this->last_error = "Database error:(".mysql_errno().")".mysql_error(); if ($this->last_query) $this->last_error .= "
Query:
{$this->last_query}
"; trigger_error($this->last_error, E_USER_WARNING); } function query($query) { $this->last_query = $query; if (!@mysql_query($query, $this->id)) { $this->error(); return false; } if (preg_match('/^insert/i', $query)) { $insert_id = mysql_insert_id($this->id); if ($insert_id) return $insert_id; } return true; } function query_result($query, $fetch_mode = FM_OBJ_ARRAY) { $this->last_query = $query; if (!$result = @mysql_query($query, $this->id)) { $this->error(); $res = false; } else { $count_rows = mysql_num_rows($result); if ($count_rows) { switch ($fetch_mode) { case FM_SIMPLE: case FM_ARR_SIMPLE: case FM_ARR_ARRAY: case FM_COL_ARRAY: case FM_COL_SIMPLE: case FM_KEY_SIMPLE: $fetch_func = "mysql_fetch_row"; break; case FM_ASSOC_ARRAY: case FM_ASSOC_SIMPLE: $fetch_func = "mysql_fetch_assoc"; break; case FM_OBJ_ARRAY: case FM_OBJ_SIMPLE: default: $fetch_func = "mysql_fetch_object"; break; } if ($fetch_mode == FM_BOOL) { $res = true; } elseif ($fetch_mode == FM_SIMPLE) { $res = $fetch_func($result); $res = $res[0]; } elseif ( $fetch_mode == FM_KEY_SIMPLE ) { $res = array(); while (list ($key, $val) = $fetch_func($result)){ $res[$key] = $val; } } elseif (in_array($fetch_mode, array(FM_ARR_SIMPLE, FM_ASSOC_SIMPLE, FM_OBJ_SIMPLE))) { $res = $fetch_func($result); } elseif (in_array($fetch_mode, array(FM_COL_ARRAY, FM_COL_SIMPLE))) { $res = array(); for ($i = 0; $i < $count_rows; $i++) { $row = $fetch_func($result); $j = 0; foreach ($row as $field) { $res[$j][$i] = $row[$j]; $j++; } } if ($fetch_mode == FM_COL_SIMPLE) $res = $res[0]; } else { $res = array(); for ($i = 0; $i < $count_rows; $i++) { $res[] = $fetch_func($result); } } mysql_freeresult($result); } else { switch ($fetch_mode) { case FM_BOOL: case FM_SIMPLE: case FM_ARR_SIMPLE: case FM_COL_SIMPLE: case FM_ASSOC_SIMPLE: case FM_OBJ_SIMPLE: $res = false; break; case FM_ASSOC_ARRAY: case FM_ARR_ARRAY: case FM_COL_ARRAY: case FM_OBJ_ARRAY: default: $res = array(); break; } } } return $res; } */ public static function quoteBigInt($value) { $quotedValue = '0'; if (preg_match('/^( [+-]? # optional sign (?: 0[Xx][\da-fA-F]+ # ODBC-style hexadecimal |\d+ # decimal or octal, or MySQL ZEROFILL decimal (?:[eE][+-]?\d+)? # optional exponent on decimals or octals ) )/x', (string)$value, $matches) ) { $quotedValue = $matches[1]; } return $quotedValue; } function initTables(&$DB, $prefix) { $TABLES = $DB->queryCol('SHOW TABLES'); if ($TABLES) foreach ($TABLES as $TABLE) { $tblName = 'tbl'.preg_replace("/^$prefix/", '', $TABLE); $DB->$tblName = $TABLE; } return true; } /** * @return MDB2_Driver_mysql */ function & getInstance() { if (!isset($GLOBALS['skDB']) || !is_object($GLOBALS['skDB']) ) { require_once('MDB2.php'); $opt = array( 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL, 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL, ); if(defined('DEBUG') && DEBUG && defined('DEBUG_DUMP_SQL') && DEBUG_DUMP_SQL){ $opt['debug'] = true; $opt['debug_handler'] = 'S_db2_debug_dump_query'; } $GLOBALS['skDB'] =& MDB2::factory(DB_DSN, $opt); $GLOBALS['skDB']->setFetchMode(MDB2_FETCHMODE_ASSOC); S_db2::initTables($GLOBALS['skDB'], DB_PREFIX); global $__CONFIG; if(isset($__CONFIG['DB_STARTUP_CODE'])){ $sql = $__CONFIG['DB_STARTUP_CODE']; if(is_array($sql)){ foreach($sql as $line){ if(isset($line) && is_string($line) && $line != '') $GLOBALS['skDB']->exec($line); } } elseif(is_string($sql) && ($sql != '')){ $GLOBALS['skDB']->exec($sql); } } } return $GLOBALS['skDB']; } /* function quote($str) { return mysql_real_escape_string($str); } function quoteSmart($in) { if (is_int($in) || is_double($in)) { return $in; } elseif (is_bool($in)) { return $in ? 1 : 0; } elseif (is_null($in)) { return 'NULL'; } else { return "'" . $this->quote($in) . "'"; } } function now() { return date('Y-m-d H:i:s'); } */ } if (!class_exists('S_db')) { class S_db extends S_db2 { } } $SQL_DebugQueryIndex = 1; function S_db2_debug_dump_query($object, $scope, $message, $context) { global $SQL_DebugQueryIndex; debugLog($scope . ' ('.($SQL_DebugQueryIndex++).'): ' .$message); } ?>