⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adodb-active-record.inc.php

📁 远程教育系统
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php/*@version V4.94 23 Jan 2007  (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.  Latest version is available at http://adodb.sourceforge.net   Released under both BSD license and Lesser GPL library license.   Whenever there is any discrepancy between the two licenses,   the BSD license will take precedence.    Active Record implementation. Superset of Zend Framework's.    Version 0.07    See http://www-128.ibm.com/developerworks/java/library/j-cb03076/?ca=dgr-lnxw01ActiveRecord   	for info on Ruby on Rails Active Record implementation*/global $_ADODB_ACTIVE_DBS;global $ADODB_ACTIVE_CACHESECS; // set to true to enable caching of metadata such as field infoglobal $ACTIVE_RECORD_SAFETY; // set to false to disable safety checks// array of ADODB_Active_DB's, indexed by ADODB_Active_Record->_dbat$_ADODB_ACTIVE_DBS = array();$ACTIVE_RECORD_SAFETY = true;class ADODB_Active_DB {	var $db; // ADOConnection	var $tables; // assoc array of ADODB_Active_Table objects, indexed by tablename}class ADODB_Active_Table {	var $name; // table name	var $flds; // assoc array of adofieldobjs, indexed by fieldname	var $keys; // assoc array of primary keys, indexed by fieldname	var $_created; // only used when stored as a cached file}// returns index into $_ADODB_ACTIVE_DBSfunction ADODB_SetDatabaseAdapter(&$db){	global $_ADODB_ACTIVE_DBS;			foreach($_ADODB_ACTIVE_DBS as $k => $d) {			if (PHP_VERSION >= 5) {				if ($d->db === $db) return $k;			} else {				if ($d->db->_connectionID === $db->_connectionID && $db->database == $d->db->database) 					return $k;			}		}				$obj = new ADODB_Active_DB();		$obj->db =& $db;		$obj->tables = array();				$_ADODB_ACTIVE_DBS[] = $obj;				return sizeof($_ADODB_ACTIVE_DBS)-1;}class ADODB_Active_Record {	var $_dbat; // associative index pointing to ADODB_Active_DB eg. $ADODB_Active_DBS[_dbat]	var $_table; // tablename, if set in class definition then use it as table name	var $_tableat; // associative index pointing to ADODB_Active_Table, eg $ADODB_Active_DBS[_dbat]->tables[$this->_tableat]	var $_where; // where clause set in Load()	var $_saved = false; // indicates whether data is already inserted.	var $_lasterr = false; // last error message	var $_original = false; // the original values loaded or inserted, refreshed on update		// should be static	function SetDatabaseAdapter(&$db) 	{		return ADODB_SetDatabaseAdapter($db);	}		// php4 constructor	function ADODB_Active_Record($table = false, $pkeyarr=false, $db=false)	{		ADODB_Active_Record::__construct($table,$pkeyarr,$db);	}		// php5 constructor	function __construct($table = false, $pkeyarr=false, $db=false)	{	global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS;			if ($db == false && is_object($pkeyarr)) {			$db = $pkeyarr;			$pkeyarr = false;		}				if (!$table) { 			if (!empty($this->_table)) $table = $this->_table;			else $table = $this->_pluralize(get_class($this));		}		if ($db) {			$this->_dbat = ADODB_Active_Record::SetDatabaseAdapter($db);		} else			$this->_dbat = sizeof($_ADODB_ACTIVE_DBS)-1;						if ($this->_dbat < 0) $this->Error("No database connection set; use ADOdb_Active_Record::SetDatabaseAdapter(\$db)",'ADODB_Active_Record::__constructor');				$this->_table = $table;		$this->_tableat = $table; # reserved for setting the assoc value to a non-table name, eg. the sql string in future		$this->UpdateActiveTable($pkeyarr);	}		function __wakeup()	{  		$class = get_class($this);  		new $class;	}		function _pluralize($table)	{		$ut = strtoupper($table);		$len = strlen($table);		$lastc = $ut[$len-1];		$lastc2 = substr($ut,$len-2);		switch ($lastc) {		case 'S':			return $table.'es';			case 'Y':			return substr($table,0,$len-1).'ies';		case 'X':				return $table.'es';		case 'H': 			if ($lastc2 == 'CH' || $lastc2 == 'SH')				return $table.'es';		default:			return $table.'s';		}	}		//////////////////////////////////		// update metadata	function UpdateActiveTable($pkeys=false,$forceUpdate=false)	{	global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS;			$activedb =& $_ADODB_ACTIVE_DBS[$this->_dbat];		$table = $this->_table;		$tables = $activedb->tables;		$tableat = $this->_tableat;		if (!$forceUpdate && !empty($tables[$tableat])) {			$tobj =& $tables[$tableat];			foreach($tobj->flds as $name => $fld) 				$this->$name = null;			return;		}				$db =& $activedb->db;		$fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType . '_active_'. $table . '.cache';		if (!$forceUpdate && $ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR && file_exists($fname)) {			$fp = fopen($fname,'r');			@flock($fp, LOCK_SH);			$acttab = unserialize(fread($fp,100000));			fclose($fp);			if ($acttab->_created + $ADODB_ACTIVE_CACHESECS - (abs(rand()) % 16) > time()) { 				// abs(rand()) randomizes deletion, reducing contention to delete/refresh file				// ideally, you should cache at least 32 secs				$activedb->tables[$table] = $acttab;								//if ($db->debug) ADOConnection::outp("Reading cached active record file: $fname");			  	return;			} else if ($db->debug) {				ADOConnection::outp("Refreshing cached active record file: $fname");			}		}		$activetab = new ADODB_Active_Table();		$activetab->name = $table;						$cols = $db->MetaColumns($table);		if (!$cols) {			$this->Error("Invalid table name: $table",'UpdateActiveTable'); 			return false;		}		$fld = reset($cols);		if (!$pkeys) {			if (isset($fld->primary_key)) {				$pkeys = array();				foreach($cols as $name => $fld) {					if (!empty($fld->primary_key)) $pkeys[] = $name;				}			} else					$pkeys = $this->GetPrimaryKeys($db, $table);		}		if (empty($pkeys)) {			$this->Error("No primary key found for table $table",'UpdateActiveTable');			return false;		}				$attr = array();		$keys = array();				switch($ADODB_ASSOC_CASE) {		case 0:			foreach($cols as $name => $fldobj) {				$name = strtolower($name);				$this->$name = null;				$attr[$name] = $fldobj;			}			foreach($pkeys as $k => $name) {				$keys[strtolower($name)] = strtolower($name);			}			break;					case 1: 			foreach($cols as $name => $fldobj) {				$name = strtoupper($name);				$this->$name = null;				$attr[$name] = $fldobj;			}						foreach($pkeys as $k => $name) {				$keys[strtoupper($name)] = strtoupper($name);			}			break;		default:			foreach($cols as $name => $fldobj) {				$name = ($fldobj->name);				$this->$name = null;				$attr[$name] = $fldobj;			}			foreach($pkeys as $k => $name) {				$keys[$name] = $cols[$name]->name;			}			break;		}				$activetab->keys = $keys;		$activetab->flds = $attr;		if ($ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR) {			$activetab->_created = time();			$s = serialize($activetab);			if (!function_exists('adodb_write_file')) include(ADODB_DIR.'/adodb-csvlib.inc.php');			adodb_write_file($fname,$s);		}		$activedb->tables[$table] = $activetab;	}		function GetPrimaryKeys(&$db, $table)	{		return $db->MetaPrimaryKeys($table);	}		// error handler for both PHP4+5. 	function Error($err,$fn)	{	global $_ADODB_ACTIVE_DBS;			$fn = get_class($this).'::'.$fn;		$this->_lasterr = $fn.': '.$err;				if ($this->_dbat < 0) $db = false;		else {			$activedb = $_ADODB_ACTIVE_DBS[$this->_dbat];			$db =& $activedb->db;		}				if (function_exists('adodb_throw')) {				if (!$db) adodb_throw('ADOdb_Active_Record', $fn, -1, $err, 0, 0, false);			else adodb_throw($db->databaseType, $fn, -1, $err, 0, 0, $db);		} else			if (!$db || $db->debug) ADOConnection::outp($this->_lasterr);			}		// return last error message	function ErrorMsg()	{		if (!function_exists('adodb_throw')) {			if ($this->_dbat < 0) $db = false;			else $db = $this->DB();					// last error could be database error too			if ($db && $db->ErrorMsg()) return $db->ErrorMsg();		}		return $this->_lasterr;	}		function ErrorNo() 	{		if ($this->_dbat < 0) return -9999; // no database connection...		$db = $this->DB();				return (int) $db->ErrorNo();	}	// retrieve ADOConnection from _ADODB_Active_DBs	function &DB()	{	global $_ADODB_ACTIVE_DBS;			if ($this->_dbat < 0) {			$false = false;			$this->Error("No database connection set: use ADOdb_Active_Record::SetDatabaseAdaptor(\$db)", "DB");			return $false;		}		$activedb = $_ADODB_ACTIVE_DBS[$this->_dbat];		$db =& $activedb->db;		return $db;	}		// retrieve ADODB_Active_Table

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -