From: <var...@us...> - 2013-05-16 08:47:33
|
Revision: 8779 http://sourceforge.net/p/phpwiki/code/8779 Author: vargenau Date: 2013-05-16 08:47:28 +0000 (Thu, 16 May 2013) Log Message: ----------- Upgrade adodb Modified Paths: -------------- trunk/lib/WikiDB/adodb/adodb-csvlib.inc.php trunk/lib/WikiDB/adodb/adodb-error.inc.php trunk/lib/WikiDB/adodb/adodb-errorhandler.inc.php trunk/lib/WikiDB/adodb/adodb-errorpear.inc.php trunk/lib/WikiDB/adodb/adodb-exceptions.inc.php trunk/lib/WikiDB/adodb/adodb-iterator.inc.php trunk/lib/WikiDB/adodb/adodb-lib.inc.php trunk/lib/WikiDB/adodb/adodb-pear.inc.php trunk/lib/WikiDB/adodb/adodb-php4.inc.php trunk/lib/WikiDB/adodb/adodb-time.inc.php trunk/lib/WikiDB/adodb/adodb.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-access.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-ado.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-ado_access.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-ado_mssql.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-borland_ibase.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-csv.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-db2.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-fbsql.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-firebird.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-ibase.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-informix.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-informix72.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-ldap.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-mssql.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-mssqlnative.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-mssqlpo.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-mysql.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-mysqli.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-mysqlt.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-netezza.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-oci8.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-oci805.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-oci8po.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-odbc.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-odbc_mssql.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-odbc_oracle.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-odbtp.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-odbtp_unicode.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-oracle.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-postgres.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-postgres64.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-postgres7.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-proxy.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-sapdb.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-sqlanywhere.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-sqlite.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-sybase.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-vfp.inc.php trunk/lib/WikiDB/adodb/license.txt trunk/lib/WikiDB/adodb/readme.txt Added Paths: ----------- trunk/lib/WikiDB/adodb/adodb-active-record.inc.php trunk/lib/WikiDB/adodb/adodb-active-recordx.inc.php trunk/lib/WikiDB/adodb/adodb-datadict.inc.php trunk/lib/WikiDB/adodb/adodb-memcache.lib.inc.php trunk/lib/WikiDB/adodb/adodb-pager.inc.php trunk/lib/WikiDB/adodb/adodb-perf.inc.php trunk/lib/WikiDB/adodb/adodb-xmlschema.inc.php trunk/lib/WikiDB/adodb/adodb-xmlschema03.inc.php trunk/lib/WikiDB/adodb/contrib/ trunk/lib/WikiDB/adodb/contrib/toxmlrpc.inc.php trunk/lib/WikiDB/adodb/cute_icons_for_site/ trunk/lib/WikiDB/adodb/cute_icons_for_site/adodb.gif trunk/lib/WikiDB/adodb/cute_icons_for_site/adodb2.gif trunk/lib/WikiDB/adodb/datadict/ trunk/lib/WikiDB/adodb/datadict/datadict-access.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-db2.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-firebird.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-generic.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-ibase.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-informix.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-mssql.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-mssqlnative.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-mysql.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-oci8.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-postgres.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-sapdb.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-sqlite.inc.php trunk/lib/WikiDB/adodb/datadict/datadict-sybase.inc.php trunk/lib/WikiDB/adodb/docs/ trunk/lib/WikiDB/adodb/docs/docs-active-record.htm trunk/lib/WikiDB/adodb/docs/docs-adodb.htm trunk/lib/WikiDB/adodb/docs/docs-datadict.htm trunk/lib/WikiDB/adodb/docs/docs-oracle.htm trunk/lib/WikiDB/adodb/docs/docs-perf.htm trunk/lib/WikiDB/adodb/docs/docs-session.htm trunk/lib/WikiDB/adodb/docs/docs-session.old.htm trunk/lib/WikiDB/adodb/docs/old-changelog.htm trunk/lib/WikiDB/adodb/docs/readme.htm trunk/lib/WikiDB/adodb/docs/tips_portable_sql.htm trunk/lib/WikiDB/adodb/docs/tute.htm trunk/lib/WikiDB/adodb/drivers/adodb-ado5.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-ads.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-db2oci.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-db2ora.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-mssql_n.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-mysqlpo.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-odbc_db2.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-pdo.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-pdo_mssql.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-pdo_mysql.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-pdo_oci.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-pdo_pgsql.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-pdo_sqlite.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-postgres8.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-postgres9.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-sqlite3.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-sqlitepo.inc.php trunk/lib/WikiDB/adodb/drivers/adodb-sybase_ase.inc.php trunk/lib/WikiDB/adodb/lang/ trunk/lib/WikiDB/adodb/lang/adodb-da.inc.php trunk/lib/WikiDB/adodb/lang/adodb-de.inc.php trunk/lib/WikiDB/adodb/lang/adodb-en.inc.php trunk/lib/WikiDB/adodb/lang/adodb-es.inc.php trunk/lib/WikiDB/adodb/lang/adodb-esperanto.inc.php trunk/lib/WikiDB/adodb/lang/adodb-fa.inc.php trunk/lib/WikiDB/adodb/lang/adodb-fr.inc.php trunk/lib/WikiDB/adodb/lang/adodb-it.inc.php trunk/lib/WikiDB/adodb/lang/adodb-nl.inc.php trunk/lib/WikiDB/adodb/lang/adodb-ro.inc.php trunk/lib/WikiDB/adodb/pear/ trunk/lib/WikiDB/adodb/pear/Auth/ trunk/lib/WikiDB/adodb/pear/Auth/Container/ trunk/lib/WikiDB/adodb/pear/Auth/Container/ADOdb.php trunk/lib/WikiDB/adodb/pear/readme.Auth.txt trunk/lib/WikiDB/adodb/perf/ trunk/lib/WikiDB/adodb/perf/perf-db2.inc.php trunk/lib/WikiDB/adodb/perf/perf-informix.inc.php trunk/lib/WikiDB/adodb/perf/perf-mssql.inc.php trunk/lib/WikiDB/adodb/perf/perf-mssqlnative.inc.php trunk/lib/WikiDB/adodb/perf/perf-mysql.inc.php trunk/lib/WikiDB/adodb/perf/perf-oci8.inc.php trunk/lib/WikiDB/adodb/perf/perf-postgres.inc.php trunk/lib/WikiDB/adodb/pivottable.inc.php trunk/lib/WikiDB/adodb/rsfilter.inc.php trunk/lib/WikiDB/adodb/server.php trunk/lib/WikiDB/adodb/session/ trunk/lib/WikiDB/adodb/session/adodb-compress-bzip2.php trunk/lib/WikiDB/adodb/session/adodb-compress-gzip.php trunk/lib/WikiDB/adodb/session/adodb-cryptsession.php trunk/lib/WikiDB/adodb/session/adodb-cryptsession2.php trunk/lib/WikiDB/adodb/session/adodb-encrypt-mcrypt.php trunk/lib/WikiDB/adodb/session/adodb-encrypt-md5.php trunk/lib/WikiDB/adodb/session/adodb-encrypt-secret.php trunk/lib/WikiDB/adodb/session/adodb-encrypt-sha1.php trunk/lib/WikiDB/adodb/session/adodb-sess.txt trunk/lib/WikiDB/adodb/session/adodb-session-clob.php trunk/lib/WikiDB/adodb/session/adodb-session-clob2.php trunk/lib/WikiDB/adodb/session/adodb-session.php trunk/lib/WikiDB/adodb/session/adodb-session2.php trunk/lib/WikiDB/adodb/session/adodb-sessions.mysql.sql trunk/lib/WikiDB/adodb/session/adodb-sessions.oracle.clob.sql trunk/lib/WikiDB/adodb/session/adodb-sessions.oracle.sql trunk/lib/WikiDB/adodb/session/crypt.inc.php trunk/lib/WikiDB/adodb/session/old/ trunk/lib/WikiDB/adodb/session/old/adodb-cryptsession.php trunk/lib/WikiDB/adodb/session/old/adodb-session-clob.php trunk/lib/WikiDB/adodb/session/old/adodb-session.php trunk/lib/WikiDB/adodb/session/old/crypt.inc.php trunk/lib/WikiDB/adodb/session/session_schema.xml trunk/lib/WikiDB/adodb/session/session_schema2.xml trunk/lib/WikiDB/adodb/tests/ trunk/lib/WikiDB/adodb/tests/benchmark.php trunk/lib/WikiDB/adodb/tests/client.php trunk/lib/WikiDB/adodb/tests/pdo.php trunk/lib/WikiDB/adodb/tests/test-active-record.php trunk/lib/WikiDB/adodb/tests/test-active-recs2.php trunk/lib/WikiDB/adodb/tests/test-active-relations.php trunk/lib/WikiDB/adodb/tests/test-active-relationsx.php trunk/lib/WikiDB/adodb/tests/test-datadict.php trunk/lib/WikiDB/adodb/tests/test-perf.php trunk/lib/WikiDB/adodb/tests/test-pgblob.php trunk/lib/WikiDB/adodb/tests/test-php5.php trunk/lib/WikiDB/adodb/tests/test-xmlschema.php trunk/lib/WikiDB/adodb/tests/test.php trunk/lib/WikiDB/adodb/tests/test2.php trunk/lib/WikiDB/adodb/tests/test3.php trunk/lib/WikiDB/adodb/tests/test4.php trunk/lib/WikiDB/adodb/tests/test5.php trunk/lib/WikiDB/adodb/tests/test_rs_array.php trunk/lib/WikiDB/adodb/tests/testcache.php trunk/lib/WikiDB/adodb/tests/testdatabases.inc.php trunk/lib/WikiDB/adodb/tests/testgenid.php trunk/lib/WikiDB/adodb/tests/testmssql.php trunk/lib/WikiDB/adodb/tests/testoci8.php trunk/lib/WikiDB/adodb/tests/testoci8cursor.php trunk/lib/WikiDB/adodb/tests/testpaging.php trunk/lib/WikiDB/adodb/tests/testpear.php trunk/lib/WikiDB/adodb/tests/testsessions.php trunk/lib/WikiDB/adodb/tests/time.php trunk/lib/WikiDB/adodb/tests/tmssql.php trunk/lib/WikiDB/adodb/tests/xmlschema-mssql.xml trunk/lib/WikiDB/adodb/tests/xmlschema.xml trunk/lib/WikiDB/adodb/toexport.inc.php trunk/lib/WikiDB/adodb/tohtml.inc.php trunk/lib/WikiDB/adodb/xmlschema.dtd trunk/lib/WikiDB/adodb/xmlschema03.dtd trunk/lib/WikiDB/adodb/xsl/ trunk/lib/WikiDB/adodb/xsl/convert-0.1-0.2.xsl trunk/lib/WikiDB/adodb/xsl/convert-0.1-0.3.xsl trunk/lib/WikiDB/adodb/xsl/convert-0.2-0.1.xsl trunk/lib/WikiDB/adodb/xsl/convert-0.2-0.3.xsl trunk/lib/WikiDB/adodb/xsl/remove-0.2.xsl trunk/lib/WikiDB/adodb/xsl/remove-0.3.xsl Removed Paths: ------------- trunk/lib/WikiDB/adodb/readme.htm Added: trunk/lib/WikiDB/adodb/adodb-active-record.inc.php =================================================================== --- trunk/lib/WikiDB/adodb/adodb-active-record.inc.php (rev 0) +++ trunk/lib/WikiDB/adodb/adodb-active-record.inc.php 2013-05-16 08:47:28 UTC (rev 8779) @@ -0,0 +1,1002 @@ +<?php +/* + +@version V5.18 3 Sep 2012 (c) 2000-2012 John Lim (jlim#natsoft.com). 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.92 + + 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 info +global $ACTIVE_RECORD_SAFETY; // set to false to disable safety checks +global $ADODB_ACTIVE_DEFVALS; // use default values of table definition when creating new active record. + +// array of ADODB_Active_DB's, indexed by ADODB_Active_Record->_dbat +$_ADODB_ACTIVE_DBS = array(); +$ACTIVE_RECORD_SAFETY = true; +$ADODB_ACTIVE_DEFVALS = false; +$ADODB_ACTIVE_CACHESECS = 0; + +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 + var $_belongsTo = array(); + var $_hasMany = array(); +} + +// $db = database connection +// $index = name of index - can be associative, for an example see +// http://phplens.com/lens/lensforum/msgs.php?id=17790 +// returns index into $_ADODB_ACTIVE_DBS +function ADODB_SetDatabaseAdapter(&$db, $index=false) +{ + 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(); + + if ($index == false) $index = sizeof($_ADODB_ACTIVE_DBS); + + + $_ADODB_ACTIVE_DBS[$index] = $obj; + + return sizeof($_ADODB_ACTIVE_DBS)-1; +} + + +class ADODB_Active_Record { + static $_changeNames = true; // dynamically pluralize table names + static $_quoteNames = false; + + static $_foreignSuffix = '_id'; // + 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 + + var $foreignName; // CFR: class name when in a relationship + + var $lockMode = ' for update '; // you might want to change to + + static function UseDefaultValues($bool=null) + { + global $ADODB_ACTIVE_DEFVALS; + if (isset($bool)) $ADODB_ACTIVE_DEFVALS = $bool; + return $ADODB_ACTIVE_DEFVALS; + } + + // should be static + static function SetDatabaseAdapter(&$db, $index=false) + { + return ADODB_SetDatabaseAdapter($db, $index); + } + + + public function __set($name, $value) + { + $name = str_replace(' ', '_', $name); + $this->$name = $value; + } + + // 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)); + } + $this->foreignName = strtolower(get_class($this)); // CFR: default foreign name + if ($db) { + $this->_dbat = ADODB_Active_Record::SetDatabaseAdapter($db); + } else if (!isset($this->_dbat)) { + if (sizeof($_ADODB_ACTIVE_DBS) == 0) $this->Error("No database connection set; use ADOdb_Active_Record::SetDatabaseAdapter(\$db)",'ADODB_Active_Record::__constructor'); + end($_ADODB_ACTIVE_DBS); + $this->_dbat = key($_ADODB_ACTIVE_DBS); + } + + $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) + { + if (!ADODB_Active_Record::$_changeNames) return $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'; + } + } + + // CFR Lamest singular inflector ever - @todo Make it real! + // Note: There is an assumption here...and it is that the argument's length >= 4 + function _singularize($tables) + { + + if (!ADODB_Active_Record::$_changeNames) return $table; + + $ut = strtoupper($tables); + $len = strlen($tables); + if($ut[$len-1] != 'S') + return $tables; // I know...forget oxen + if($ut[$len-2] != 'E') + return substr($tables, 0, $len-1); + switch($ut[$len-3]) + { + case 'S': + case 'X': + return substr($tables, 0, $len-2); + case 'I': + return substr($tables, 0, $len-3) . 'y'; + case 'H'; + if($ut[$len-4] == 'C' || $ut[$len-4] == 'S') + return substr($tables, 0, $len-2); + default: + return substr($tables, 0, $len-1); // ? + } + } + + function hasMany($foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record') + { + $ar = new $foreignClass($foreignRef); + $ar->foreignName = $foreignRef; + $ar->UpdateActiveTable(); + $ar->foreignKey = ($foreignKey) ? $foreignKey : $foreignRef.ADODB_Active_Record::$_foreignSuffix; + $table =& $this->TableInfo(); + $table->_hasMany[$foreignRef] = $ar; + # $this->$foreignRef = $this->_hasMany[$foreignRef]; // WATCHME Removed assignment by ref. to please __get() + } + + // use when you don't want ADOdb to auto-pluralize tablename + static function TableHasMany($table, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record') + { + $ar = new ADODB_Active_Record($table); + $ar->hasMany($foreignRef, $foreignKey, $foreignClass); + } + + // use when you don't want ADOdb to auto-pluralize tablename + static function TableKeyHasMany($table, $tablePKey, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record') + { + if (!is_array($tablePKey)) $tablePKey = array($tablePKey); + $ar = new ADODB_Active_Record($table,$tablePKey); + $ar->hasMany($foreignRef, $foreignKey, $foreignClass); + } + + + // use when you want ADOdb to auto-pluralize tablename for you. Note that the class must already be defined. + // e.g. class Person will generate relationship for table Persons + static function ClassHasMany($parentclass, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record') + { + $ar = new $parentclass(); + $ar->hasMany($foreignRef, $foreignKey, $foreignClass); + } + + + function belongsTo($foreignRef,$foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record') + { + global $inflector; + + $ar = new $parentClass($this->_pluralize($foreignRef)); + $ar->foreignName = $foreignRef; + $ar->parentKey = $parentKey; + $ar->UpdateActiveTable(); + $ar->foreignKey = ($foreignKey) ? $foreignKey : $foreignRef.ADODB_Active_Record::$_foreignSuffix; + + $table =& $this->TableInfo(); + $table->_belongsTo[$foreignRef] = $ar; + # $this->$foreignRef = $this->_belongsTo[$foreignRef]; + } + + static function ClassBelongsTo($class, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record') + { + $ar = new $class(); + $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass); + } + + static function TableBelongsTo($table, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record') + { + $ar = new ADOdb_Active_Record($table); + $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass); + } + + static function TableKeyBelongsTo($table, $tablePKey, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record') + { + if (!is_array($tablePKey)) $tablePKey = array($tablePKey); + $ar = new ADOdb_Active_Record($table, $tablePKey); + $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass); + } + + + /** + * __get Access properties - used for lazy loading + * + * @param mixed $name + * @access protected + * @return mixed + */ + function __get($name) + { + return $this->LoadRelations($name, '', -1, -1); + } + + /** + * @param string $name + * @param string $whereOrderBy : eg. ' AND field1 = value ORDER BY field2' + * @param offset + * @param limit + * @return mixed + */ + function LoadRelations($name, $whereOrderBy='', $offset=-1,$limit=-1) + { + $extras = array(); + $table = $this->TableInfo(); + if ($limit >= 0) $extras['limit'] = $limit; + if ($offset >= 0) $extras['offset'] = $offset; + + if (strlen($whereOrderBy)) + if (!preg_match('/^[ \n\r]*AND/i',$whereOrderBy)) + if (!preg_match('/^[ \n\r]*ORDER[ \n\r]/i',$whereOrderBy)) + $whereOrderBy = 'AND '.$whereOrderBy; + + if(!empty($table->_belongsTo[$name])) + { + $obj = $table->_belongsTo[$name]; + $columnName = $obj->foreignKey; + if(empty($this->$columnName)) + $this->$name = null; + else + { + if ($obj->parentKey) $key = $obj->parentKey; + else $key = reset($table->keys); + + $arrayOfOne = $obj->Find($key.'='.$this->$columnName.' '.$whereOrderBy,false,false,$extras); + if ($arrayOfOne) { + $this->$name = $arrayOfOne[0]; + return $arrayOfOne[0]; + } + } + } + if(!empty($table->_hasMany[$name])) + { + $obj = $table->_hasMany[$name]; + $key = reset($table->keys); + $id = @$this->$key; + if (!is_numeric($id)) { + $db = $this->DB(); + $id = $db->qstr($id); + } + $objs = $obj->Find($obj->foreignKey.'='.$id. ' '.$whereOrderBy,false,false,$extras); + if (!$objs) $objs = array(); + $this->$name = $objs; + return $objs; + } + + return array(); + } + ////////////////////////////////// + + // update metadata + function UpdateActiveTable($pkeys=false,$forceUpdate=false) + { + global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS; + global $ADODB_ACTIVE_DEFVALS,$ADODB_FETCH_MODE; + + $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; + + $table = $this->_table; + $tables = $activedb->tables; + $tableat = $this->_tableat; + if (!$forceUpdate && !empty($tables[$tableat])) { + + $acttab = $tables[$tableat]; + foreach($acttab->flds as $name => $fld) { + if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) + $this->$name = $fld->default_value; + else + $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 + + foreach($acttab->flds as $name => $fld) { + if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) + $this->$name = $fld->default_value; + else + $this->$name = null; + } + + $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; + + $save = $ADODB_FETCH_MODE; + $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; + if ($db->fetchMode !== false) $savem = $db->SetFetchMode(false); + + $cols = $db->MetaColumns($table); + + if (isset($savem)) $db->SetFetchMode($savem); + $ADODB_FETCH_MODE = $save; + + 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); + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) + $this->$name = $fldobj->default_value; + else + $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); + + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) + $this->$name = $fldobj->default_value; + else + $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); + + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) + $this->$name = $fldobj->default_value; + else + $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); + } + if (isset($activedb->tables[$table])) { + $oldtab = $activedb->tables[$table]; + + if ($oldtab) $activetab->_belongsTo = $oldtab->_belongsTo; + if ($oldtab) $activetab->_hasMany = $oldtab->_hasMany; + } + $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 + function &TableInfo() + { + global $_ADODB_ACTIVE_DBS; + $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; + $table = $activedb->tables[$this->_tableat]; + return $table; + } + + + // I have an ON INSERT trigger on a table that sets other columns in the table. + // So, I find that for myTable, I want to reload an active record after saving it. -- Malcolm Cook + function Reload() + { + $db =& $this->DB(); if (!$db) return false; + $table =& $this->TableInfo(); + $where = $this->GenWhere($db, $table); + return($this->Load($where)); + } + + + // set a numeric array (using natural table field ordering) as object properties + function Set(&$row) + { + global $ACTIVE_RECORD_SAFETY; + + $db = $this->DB(); + + if (!$row) { + $this->_saved = false; + return false; + } + + $this->_saved = true; + + $table = $this->TableInfo(); + if ($ACTIVE_RECORD_SAFETY && sizeof($table->flds) != sizeof($row)) { + # <AP> + $bad_size = TRUE; + if (sizeof($row) == 2 * sizeof($table->flds)) { + // Only keep string keys + $keys = array_filter(array_keys($row), 'is_string'); + if (sizeof($keys) == sizeof($table->flds)) + $bad_size = FALSE; + } + if ($bad_size) { + $this->Error("Table structure of $this->_table has changed","Load"); + return false; + } + # </AP> + } + else + $keys = array_keys($row); + + # <AP> + reset($keys); + $this->_original = array(); + foreach($table->flds as $name=>$fld) { + $value = $row[current($keys)]; + $this->$name = $value; + $this->_original[] = $value; + next($keys); + } + + # </AP> + return true; + } + + // get last inserted id for INSERT + function LastInsertID(&$db,$fieldname) + { + if ($db->hasInsertID) + $val = $db->Insert_ID($this->_table,$fieldname); + else + $val = false; + + if (is_null($val) || $val === false) { + // this might not work reliably in multi-user environment + return $db->GetOne("select max(".$fieldname.") from ".$this->_table); + } + return $val; + } + + // quote data in where clause + function doquote(&$db, $val,$t) + { + switch($t) { + case 'L': + if (strpos($db->databaseType,'postgres') !== false) return $db->qstr($val); + case 'D': + case 'T': + if (empty($val)) return 'null'; + + case 'B': + case 'N': + case 'C': + case 'X': + if (is_null($val)) return 'null'; + + if (strlen($val)>0 && + (strncmp($val,"'",1) != 0 || substr($val,strlen($val)-1,1) != "'")) { + return $db->qstr($val); + break; + } + default: + return $val; + break; + } + } + + // generate where clause for an UPDATE/SELECT + function GenWhere(&$db, &$table) + { + $keys = $table->keys; + $parr = array(); + + foreach($keys as $k) { + $f = $table->flds[$k]; + if ($f) { + $parr[] = $k.' = '.$this->doquote($db,$this->$k,$db->MetaType($f->type)); + } + } + return implode(' and ', $parr); + } + + + function _QName($n,$db=false) + { + if (!ADODB_Active_Record::$_quoteNames) return $n; + if (!$db) $db = $this->DB(); if (!$db) return false; + return $db->nameQuote.$n.$db->nameQuote; + } + + //------------------------------------------------------------ Public functions below + + function Load($where=null,$bindarr=false, $lock = false) + { + global $ADODB_FETCH_MODE; + + $db = $this->DB(); if (!$db) return false; + $this->_where = $where; + + $save = $ADODB_FETCH_MODE; + $ADODB_FETCH_MODE = ADODB_FETCH_NUM; + if ($db->fetchMode !== false) $savem = $db->SetFetchMode(false); + + $qry = "select * from ".$this->_table; + + if($where) { + $qry .= ' WHERE '.$where; + } + if ($lock) $qry .= $this->lockMode; + + $row = $db->GetRow($qry,$bindarr); + + if (isset($savem)) $db->SetFetchMode($savem); + $ADODB_FETCH_MODE = $save; + + return $this->Set($row); + } + + function LoadLocked($where=null, $bindarr=false) + { + $this->Load($where,$bindarr,true); + } + + # useful for multiple record inserts + # see http://phplens.com/lens/lensforum/msgs.php?id=17795 + function Reset() + { + $this->_where=null; + $this->_saved = false; + $this->_lasterr = false; + $this->_original = false; + $vars=get_object_vars($this); + foreach($vars as $k=>$v){ + if(substr($k,0,1)!=='_'){ + $this->{$k}=null; + } + } + $this->foreignName=strtolower(get_class($this)); + return true; + } + + // false on error + function Save() + { + if ($this->_saved) $ok = $this->Update(); + else $ok = $this->Insert(); + + return $ok; + } + + + // false on error + function Insert() + { + $db = $this->DB(); if (!$db) return false; + $cnt = 0; + $table = $this->TableInfo(); + + $valarr = array(); + $names = array(); + $valstr = array(); + + foreach($table->flds as $name=>$fld) { + $val = $this->$name; + if(!is_array($val) || !is_null($val) || !array_key_exists($name, $table->keys)) { + $valarr[] = $val; + $names[] = $this->_QName($name,$db); + $valstr[] = $db->Param($cnt); + $cnt += 1; + } + } + + if (empty($names)){ + foreach($table->flds as $name=>$fld) { + $valarr[] = null; + $names[] = $name; + $valstr[] = $db->Param($cnt); + $cnt += 1; + } + } + $sql = 'INSERT INTO '.$this->_table."(".implode(',',$names).') VALUES ('.implode(',',$valstr).')'; + $ok = $db->Execute($sql,$valarr); + + if ($ok) { + $this->_saved = true; + $autoinc = false; + foreach($table->keys as $k) { + if (is_null($this->$k)) { + $autoinc = true; + break; + } + } + if ($autoinc && sizeof($table->keys) == 1) { + $k = reset($table->keys); + $this->$k = $this->LastInsertID($db,$k); + } + } + + $this->_original = $valarr; + return !empty($ok); + } + + function Delete() + { + $db = $this->DB(); if (!$db) return false; + $table = $this->TableInfo(); + + $where = $this->GenWhere($db,$table); + $sql = 'DELETE FROM '.$this->_table.' WHERE '.$where; + $ok = $db->Execute($sql); + + return $ok ? true : false; + } + + // returns an array of active record objects + function Find($whereOrderBy,$bindarr=false,$pkeysArr=false,$extra=array()) + { + $db = $this->DB(); if (!$db || empty($this->_table)) return false; + $arr = $db->GetActiveRecordsClass(get_class($this),$this->_table, $whereOrderBy,$bindarr,$pkeysArr,$extra); + return $arr; + } + + // returns 0 on error, 1 on update, 2 on insert + function Replace() + { + global $ADODB_ASSOC_CASE; + + $db = $this->DB(); if (!$db) return false; + $table = $this->TableInfo(); + + $pkey = $table->keys; + + foreach($table->flds as $name=>$fld) { + $val = $this->$name; + /* + if (is_null($val)) { + if (isset($fld->not_null) && $fld->not_null) { + if (isset($fld->default_value) && strlen($fld->default_value)) continue; + else { + $this->Error("Cannot update null into $name","Replace"); + return false; + } + } + }*/ + if (is_null($val) && !empty($fld->auto_increment)) { + continue; + } + + if (is_array($val)) continue; + + $t = $db->MetaType($fld->type); + $arr[$name] = $this->doquote($db,$val,$t); + $valarr[] = $val; + } + + if (!is_array($pkey)) $pkey = array($pkey); + + + if ($ADODB_ASSOC_CASE == 0) + foreach($pkey as $k => $v) + $pkey[$k] = strtolower($v); + elseif ($ADODB_ASSOC_CASE == 1) + foreach($pkey as $k => $v) + $pkey[$k] = strtoupper($v); + + $ok = $db->Replace($this->_table,$arr,$pkey); + if ($ok) { + $this->_saved = true; // 1= update 2=insert + if ($ok == 2) { + $autoinc = false; + foreach($table->keys as $k) { + if (is_null($this->$k)) { + $autoinc = true; + break; + } + } + if ($autoinc && sizeof($table->keys) == 1) { + $k = reset($table->keys); + $this->$k = $this->LastInsertID($db,$k); + } + } + + $this->_original = $valarr; + } + return $ok; + } + + // returns 0 on error, 1 on update, -1 if no change in data (no update) + function Update() + { + $db = $this->DB(); if (!$db) return false; + $table = $this->TableInfo(); + + $where = $this->GenWhere($db, $table); + + if (!$where) { + $this->error("Where missing for table $table", "Update"); + return false; + } + $valarr = array(); + $neworig = array(); + $pairs = array(); + $i = -1; + $cnt = 0; + foreach($table->flds as $name=>$fld) { + $i += 1; + $val = $this->$name; + $neworig[] = $val; + + if (isset($table->keys[$name]) || is_array($val)) + continue; + + if (is_null($val)) { + if (isset($fld->not_null) && $fld->not_null) { + if (isset($fld->default_value) && strlen($fld->default_value)) continue; + else { + $this->Error("Cannot set field $name to NULL","Update"); + return false; + } + } + } + + if (isset($this->_original[$i]) && strcmp($val,$this->_original[$i]) == 0) continue; + + if (is_null($this->_original[$i]) && is_null($val)) continue; + + $valarr[] = $val; + $pairs[] = $this->_QName($name,$db).'='.$db->Param($cnt); + $cnt += 1; + } + + + if (!$cnt) return -1; + $sql = 'UPDATE '.$this->_table." SET ".implode(",",$pairs)." WHERE ".$where; + $ok = $db->Execute($sql,$valarr); + if ($ok) { + $this->_original = $neworig; + return 1; + } + return 0; + } + + function GetAttributeNames() + { + $table = $this->TableInfo(); + if (!$table) return false; + return array_keys($table->flds); + } + +}; + +function adodb_GetActiveRecordsClass(&$db, $class, $table,$whereOrderBy,$bindarr, $primkeyArr, + $extra) +{ +global $_ADODB_ACTIVE_DBS; + + + $save = $db->SetFetchMode(ADODB_FETCH_NUM); + $qry = "select * from ".$table; + + if (!empty($whereOrderBy)) + $qry .= ' WHERE '.$whereOrderBy; + if(isset($extra['limit'])) + { + $rows = false; + if(isset($extra['offset'])) { + $rs = $db->SelectLimit($qry, $extra['limit'], $extra['offset'],$bindarr); + } else { + $rs = $db->SelectLimit($qry, $extra['limit'],-1,$bindarr); + } + if ($rs) { + while (!$rs->EOF) { + $rows[] = $rs->fields; + $rs->MoveNext(); + } + } + } else + $rows = $db->GetAll($qry,$bindarr); + + $db->SetFetchMode($save); + + $false = false; + + if ($rows === false) { + return $false; + } + + + if (!class_exists($class)) { + $db->outp_throw("Unknown class $class in GetActiveRecordsClass()",'GetActiveRecordsClass'); + return $false; + } + $arr = array(); + // arrRef will be the structure that knows about our objects. + // It is an associative array. + // We will, however, return arr, preserving regular 0.. order so that + // obj[0] can be used by app developpers. + $arrRef = array(); + $bTos = array(); // Will store belongTo's indices if any + foreach($rows as $row) { + + $obj = new $class($table,$primkeyArr,$db); + if ($obj->ErrorNo()){ + $db->_errorMsg = $obj->ErrorMsg(); + return $false; + } + $obj->Set($row); + $arr[] = $obj; + } // foreach($rows as $row) + + return $arr; +} +?> \ No newline at end of file Added: trunk/lib/WikiDB/adodb/adodb-active-recordx.inc.php =================================================================== --- trunk/lib/WikiDB/adodb/adodb-active-recordx.inc.php (rev 0) +++ trunk/lib/WikiDB/adodb/adodb-active-recordx.inc.php 2013-05-16 08:47:28 UTC (rev 8779) @@ -0,0 +1,1421 @@ +<?php +/* + +@version V5.06 29 Sept 2008 (c) 2000-2012 John Lim (jlim#natsoft.com). 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. + + This is "Active Record eXtended" to support JOIN, WORK and LAZY mode by Chris Ravenscroft chris#voilaweb.com + + Version 0.9 + + See http://www-128.ibm.com/developerworks/java/library/j-cb03076/?ca=dgr-lnxw01ActiveRecord + for info on Ruby on Rails Active Record implementation +*/ + + + // CFR: Active Records Definitions +define('ADODB_JOIN_AR', 0x01); +define('ADODB_WORK_AR', 0x02); +define('ADODB_LAZY_AR', 0x03); + + +global $_ADODB_ACTIVE_DBS; +global $ADODB_ACTIVE_CACHESECS; // set to true to enable caching of metadata such as field info +global $ACTIVE_RECORD_SAFETY; // set to false to disable safety checks +global $ADODB_ACTIVE_DEFVALS; // use default values of table definition when creating new active record. + +// array of ADODB_Active_DB's, indexed by ADODB_Active_Record->_dbat +$_ADODB_ACTIVE_DBS = array(); +$ACTIVE_RECORD_SAFETY = true; // CFR: disabled while playing with relations +$ADODB_ACTIVE_DEFVALS = false; + +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 + var $_belongsTo = array(); + var $_hasMany = array(); + var $_colsCount; // total columns count, including relations + + function updateColsCount() + { + $this->_colsCount = sizeof($this->flds); + foreach($this->_belongsTo as $foreignTable) + $this->_colsCount += sizeof($foreignTable->TableInfo()->flds); + foreach($this->_hasMany as $foreignTable) + $this->_colsCount += sizeof($foreignTable->TableInfo()->flds); + } +} + +// returns index into $_ADODB_ACTIVE_DBS +function 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 { + static $_changeNames = true; // dynamically pluralize table names + static $_foreignSuffix = '_id'; // + 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 $_sTable; // singularized table name + var $_pTable; // pluralized 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 + + var $foreignName; // CFR: class name when in a relationship + + static function UseDefaultValues($bool=null) + { + global $ADODB_ACTIVE_DEFVALS; + if (isset($bool)) $ADODB_ACTIVE_DEFVALS = $bool; + return $ADODB_ACTIVE_DEFVALS; + } + + // should be static + static function SetDatabaseAdapter(&$db) + { + return ADODB_SetDatabaseAdapter($db); + } + + + public function __set($name, $value) + { + $name = str_replace(' ', '_', $name); + $this->$name = $value; + } + + // php5 constructor + // Note: if $table is defined, then we will use it as our table name + // Otherwise we will use our classname... + // In our database, table names are pluralized (because there can be + // more than one row!) + // Similarly, if $table is defined here, it has to be plural form. + // + // $options is an array that allows us to tweak the constructor's behaviour + // if $options['refresh'] is true, we re-scan our metadata information + // if $options['new'] is true, we forget all relations + function __construct($table = false, $pkeyarr=false, $db=false, $options=array()) + { + global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS; + + if ($db == false && is_object($pkeyarr)) { + $db = $pkeyarr; + $pkeyarr = false; + } + + if($table) + { + // table argument exists. It is expected to be + // already plural form. + $this->_pTable = $table; + $this->_sTable = $this->_singularize($this->_pTable); + } + else + { + // We will use current classname as table name. + // We need to pluralize it for the real table name. + $this->_sTable = strtolower(get_class($this)); + $this->_pTable = $this->_pluralize($this->_sTable); + } + $this->_table = &$this->_pTable; + + $this->foreignName = $this->_sTable; // CFR: default foreign name (singular) + + 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->_tableat = $this->_table; # reserved for setting the assoc value to a non-table name, eg. the sql string in future + + // CFR: Just added this option because UpdateActiveTable() can refresh its information + // but there was no way to ask it to do that. + $forceUpdate = (isset($options['refresh']) && true === $options['refresh']); + $this->UpdateActiveTable($pkeyarr, $forceUpdate); + if(isset($options['new']) && true === $options['new']) + { + $table =& $this->TableInfo(); + unset($table->_hasMany); + unset($table->_belongsTo); + $table->_hasMany = array(); + $table->_belongsTo = array(); + } + } + + function __wakeup() + { + $class = get_class($this); + new $class; + } + + // CFR: Constants found in Rails + static $IrregularP = array( + 'PERSON' => 'people', + 'MAN' => 'men', + 'WOMAN' => 'women', + 'CHILD' => 'children', + 'COW' => 'kine', + ); + + static $IrregularS = array( + 'PEOPLE' => 'PERSON', + 'MEN' => 'man', + 'WOMEN' => 'woman', + 'CHILDREN' => 'child', + 'KINE' => 'cow', + ); + + static $WeIsI = array( + 'EQUIPMENT' => true, + 'INFORMATION' => true, + 'RICE' => true, + 'MONEY' => true, + 'SPECIES' => true, + 'SERIES' => true, + 'FISH' => true, + 'SHEEP' => true, + ); + + function _pluralize($table) + { + if (!ADODB_Active_Record::$_changeNames) return $table; + + $ut = strtoupper($table); + if(isset(self::$WeIsI[$ut])) + { + return $table; + } + if(isset(self::$IrregularP[$ut])) + { + return self::$IrregularP[$ut]; + } + $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'; + } + } + + // CFR Lamest singular inflector ever - @todo Make it real! + // Note: There is an assumption here...and it is that the argument's length >= 4 + function _singularize($table) + { + + if (!ADODB_Active_Record::$_changeNames) return $table; + + $ut = strtoupper($table); + if(isset(self::$WeIsI[$ut])) + { + return $table; + } + if(isset(self::$IrregularS[$ut])) + { + return self::$IrregularS[$ut]; + } + $len = strlen($table); + if($ut[$len-1] != 'S') + return $table; // I know...forget oxen + if($ut[$len-2] != 'E') + return substr($table, 0, $len-1); + switch($ut[$len-3]) + { + case 'S': + case 'X': + return substr($table, 0, $len-2); + case 'I': + return substr($table, 0, $len-3) . 'y'; + case 'H'; + if($ut[$len-4] == 'C' || $ut[$len-4] == 'S') + return substr($table, 0, $len-2); + default: + return substr($table, 0, $len-1); // ? + } + } + + /* + * ar->foreignName will contain the name of the tables associated with this table because + * these other tables' rows may also be referenced by this table using theirname_id or the provided + * foreign keys (this index name is stored in ar->foreignKey) + * + * this-table.id = other-table-#1.this-table_id + * = other-table-#2.this-table_id + */ + function hasMany($foreignRef,$foreignKey=false) + { + $ar = new ADODB_Active_Record($foreignRef); + $ar->foreignName = $foreignRef; + $ar->UpdateActiveTable(); + $ar->foreignKey = ($foreignKey) ? $foreignKey : strtolower(get_class($this)) . self::$_foreignSuffix; + + $table =& $this->TableInfo(); + if(!isset($table->_hasMany[$foreignRef])) + { + $table->_hasMany[$foreignRef] = $ar; + $table->updateColsCount(); + } +# @todo Can I make this guy be lazy? + $this->$foreignRef = $table->_hasMany[$foreignRef]; // WATCHME Removed assignment by ref. to please __get() + } + + /** + * ar->foreignName will contain the name of the tables associated with this table because + * this table's rows may also be referenced by those tables using thistable_id or the provided + * foreign keys (this index name is stored in ar->foreignKey) + * + * this-table.other-table_id = other-table.id + */ + function belongsTo($foreignRef,$foreignKey=false) + { + global $inflector; + + $ar = new ADODB_Active_Record($this->_pluralize($foreignRef)); + $ar->foreignName = $foreignRef; + $ar->UpdateActiveTable(); + $ar->foreignKey = ($foreignKey) ? $foreignKey : $ar->foreignName . self::$_foreignSuffix; + + $table =& $this->TableInfo(); + if(!isset($table->_belongsTo[$foreignRef])) + { + $table->_belongsTo[$foreignRef] = $ar; + $table->updateColsCount(); + } + $this->$foreignRef = $table->_belongsTo[$foreignRef]; + } + + /** + * __get Access properties - used for lazy loading + * + * @param mixed $name + * @access protected + * @return void + */ + function __get($name) + { + return $this->LoadRelations($name, '', -1. -1); + } + + function LoadRelations($name, $whereOrderBy, $offset=-1, $limit=-1) + { + $extras = array(); + if($offset >= 0) $extras['offset'] = $offset; + if($limit >= 0) $extras['limit'] = $limit; + $table =& $this->TableInfo(); + + if (strlen($whereOrderBy)) + if (!preg_match('/^[ \n\r]*AND/i',$whereOrderBy)) + if (!preg_match('/^[ \n\r]*ORDER[ \n\r]/i',$whereOrderBy)) + $whereOrderBy = 'AND '.$whereOrderBy; + + if(!empty($table->_belongsTo[$name])) + { + $obj = $table->_belongsTo[$name]; + $columnName = $obj->foreignKey; + if(empty($this->$columnName)) + $this->$name = null; + else + { + if(($k = reset($obj->TableInfo()->keys))) + $belongsToId = $k; + else + $belongsToId = 'id'; + + $arrayOfOne = + $obj->Find( + $belongsToId.'='.$this->$columnName.' '.$whereOrderBy, false, false, $extras); + $this->$name = $arrayOfOne[0]; + } + return $this->$name; + } + if(!empty($table->_hasMany[$name])) + { + $obj = $table->_hasMany[$name]; + if(($k = reset($table->keys))) + $hasManyId = $k; + else + $hasManyId = 'id'; + + $this->$name = + $obj->Find( + $obj->foreignKey.'='.$this->$hasManyId.' '.$whereOrderBy, false, false, $extras); + return $this->$name; + } + } + ////////////////////////////////// + + // update metadata + function UpdateActiveTable($pkeys=false,$forceUpdate=false) + { + global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS; + global $ADODB_ACTIVE_DEFVALS, $ADODB_FETCH_MODE; + + $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) { + if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) + $this->$name = $fld->default_value; + else + $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; + + $save = $ADODB_FETCH_MODE; + $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; + if ($db->fetchMode !== false) $savem = $db->SetFetchMode(false); + + $cols = $db->MetaColumns($table); + + if (isset($savem)) $db->SetFetchMode($savem); + $ADODB_FETCH_MODE = $save; + + 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); + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) + $this->$name = $fldobj->default_value; + else + $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); + + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) + $this->$name = $fldobj->default_value; + else + $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); + + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) + $this->$name = $fldobj->default_value; + else + $this->$name = null; + $attr[$name] = $fldobj; + } + foreach($pkeys as $k => $name) { + $keys[$name] = $cols[$name]->name; + } + break; + } + + $activetab->keys = $keys; + $activetab->flds = $attr; + $activetab->updateColsCount(); + + 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); + } + if (isset($activedb->tables[$table])) { + $oldtab = $activedb->tables[$table]; + + if ($oldtab) $activetab->_belongsTo = $oldtab->_belongsTo; + if ($oldtab) $activetab->_hasMany = $oldtab->_hasMany; + } + $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 + function &TableInfo() + { + global $_ADODB_ACTIVE_DBS; + + $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; + $table = $activedb->tables[$this->_tableat]; + return $table; + } + + + // I have an ON INSERT trigger on a table that sets other columns in the table. + // So, I find that for myTable, I want to reload an active record after saving it. -- Malcolm Cook + function Reload() + { + $db =& $this->DB(); if (!$db) return false; + $table =& $this->TableInfo(); + $where = $this->GenWhere($db, $table); + return($this->Load($where)); + } + + + // set a numeric array (using natural table field ordering) as object properties + function Set(&$row) + { + global $ACTIVE_RECORD_SAFETY; + + $db = $this->DB(); + + if (!$row) { + $this->_saved = false; + return false; + } + + $this->_saved = true; + + $table = $this->TableInfo(); + $sizeofFlds = sizeof($table->flds); + $sizeofRow = sizeof($row); + if ($ACTIVE_RECORD_SAFETY && $table->_colsCount != $sizeofRow && $sizeofFlds != $sizeofRow) { + # <AP> + $bad_size = TRUE; + if($sizeofRow == 2 * $table->_colsCount || $sizeofRow == 2 * $sizeofFlds) { + // Only keep string keys + $keys = array_filter(array_keys($row), 'is_string'); + if (sizeof($keys) == sizeof($table->flds)) + $bad_size = FALSE; + } + if ($bad_size) { + $this->Error("Table structure of $this->_table has changed","Load"); + return false; + } + # </AP> + } + else + $keys = array_keys($row); + # <AP> + reset($keys); + $this->_original = array(); + foreach($table->flds as $name=>$fld) + { + $value = $row[current($keys)]; + $this->$name = $value; + $this->_original[] = $value; + if(!next($keys)) break; + } + $table =& $this->TableInfo(); + foreach($table->_belongsTo as $foreignTable) + { + $ft = $foreignTable->TableInfo(); + $propertyName = $ft->name; + foreach($ft->flds as $name=>$fld) + { + $value = $row[current($keys)]; + $foreignTable->$name = $value; + $foreignTable->_original[] = $value; + if(!next($keys)) break; + } + } + foreach($table->_hasMany as $foreignTable) + { + $ft = $foreignTable->TableInfo(); + foreach($ft->flds as $name=>$fld) + { + $value = $row[current($keys)]; + $foreignTable->$name = $value; + $foreignTable->_original[] = $value; + if(!next($keys)) break; + } + } + # </AP> + return true; + } + + // get last inserted id for INSERT + function LastInsertID(&$db,$fieldname) + { + if ($db->hasInsertID) + $val = $db->Insert_ID($this->_table,$fieldname); + else + $val = false; + + if (is_null($val) || $val === false) { + // this might not work reliably in multi-user environment + return $db->GetOne("select max(".$fieldname.") from ".$this->_table); + } + return $val; + } + + // quote data in where clause + function doquote(&$db, $val,$t) + { + switch($t) { + case 'D': + case 'T': + if (empty($val)) return 'null'; + + case 'C': + case 'X': + if (is_null($val)) return 'null'; + + if (strlen($val)>0 && + (strncmp($val,"'",1) != 0 || substr($val,strlen($val)-1,1) != "'")) { + return $db->qstr($val); + break; + } + default: + return $val; + break; + } + } + + // generate where clause for an UPDATE/SELECT + function GenWhere(&$db, &$table) + { + $keys = $table->keys; + $parr = array(); + + foreach($keys as $k) { + $f = $table->flds[$k]; + if ($f) { + $parr[] = $k.' = '.$this->doquote($db,$this->$k,$db->MetaType($f->type)); + } + } + return implode(' and ', $parr); + } + + + //-----------------------------------------------... [truncated message content] |