X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=extlib%2FDB%2FDataObject.php;h=1a7b34665d174224ef50528dd4355b9e348cbdf7;hb=64ac344efab356ffc9debe2ace001ebeb91b7c6f;hp=811d775965012ba5fd0cd037dad8cc23222c8a26;hpb=87d46e1ae5e5effcc985021ff5af3f10815f3d3c;p=quix0rs-gnu-social.git diff --git a/extlib/DB/DataObject.php b/extlib/DB/DataObject.php index 811d775965..1a7b34665d 100644 --- a/extlib/DB/DataObject.php +++ b/extlib/DB/DataObject.php @@ -15,7 +15,7 @@ * @author Alan Knowles * @copyright 1997-2006 The PHP Group * @license http://www.php.net/license/3_01.txt PHP License 3.01 - * @version CVS: $Id: DataObject.php 301030 2010-07-07 02:26:31Z alan_k $ + * @version CVS: $Id: DataObject.php 320069 2011-11-28 04:34:08Z alan_k $ * @link http://pear.php.net/package/DB_DataObject */ @@ -176,10 +176,11 @@ $GLOBALS['_DB_DATAOBJECT']['QUERYENDTIME'] = 0; // this will be horrifically slow!!!! -// NOTE: Overload SEGFAULTS ON PHP4 + Zend Optimizer (see define before..) // these two are BC/FC handlers for call in PHP4/5 -if ( substr(phpversion(),0,1) == 5) { + +if (!defined('DB_DATAOBJECT_NO_OVERLOAD')) { + class DB_DataObject_Overload { function __call($method,$args) @@ -194,27 +195,10 @@ if ( substr(phpversion(),0,1) == 5) { } } } else { - if (version_compare(phpversion(),'4.3.10','eq') && !defined('DB_DATAOBJECT_NO_OVERLOAD')) { - trigger_error( - "overload does not work with PHP4.3.10, either upgrade - (snaps.php.net) or more recent version - or define DB_DATAOBJECT_NO_OVERLOAD as per the manual. - ",E_USER_ERROR); - } - - if (!function_exists('clone')) { - // emulate clone - as per php_compact, slow but really the correct behaviour.. - eval('function clone($t) { $r = $t; if (method_exists($r,"__clone")) { $r->__clone(); } return $r; }'); - } - eval(' - class DB_DataObject_Overload { - function __call($method,$args,&$return) { - return $this->_call($method,$args,$return); - } - } - '); + class DB_DataObject_Overload {} } + @@ -235,7 +219,7 @@ class DB_DataObject extends DB_DataObject_Overload * @access private * @var string */ - var $_DB_DataObject_version = "1.9.5"; + var $_DB_DataObject_version = "1.11.3"; /** * The Database table (used by table extends) @@ -290,7 +274,7 @@ class DB_DataObject extends DB_DataObject_Overload $v = $k; $keys = $this->keys(); if (!$keys) { - $this->raiseError("No Keys available for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); + $this->raiseError("No Keys available for {$this->tableName()}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return false; } $k = $keys[0]; @@ -306,68 +290,37 @@ class DB_DataObject extends DB_DataObject_Overload $this->$k = $v; return $this->find(1); } - + /** - * An autoloading, caching static get method using key, value (based on get) - * (depreciated - use ::get / and your own caching method) - * - * Usage: - * $object = DB_DataObject::staticGet("DbTable_mytable",12); - * or - * $object = DB_DataObject::staticGet("DbTable_mytable","name","fred"); + * Get the value of the primary id * - * or write it into your extended class: - * function &staticGet($k,$v=NULL) { return DB_DataObject::staticGet("This_Class",$k,$v); } + * While I normally use 'id' as the PRIMARY KEY value, some database use + * {table}_id as the column name. * - * @param string $class class name - * @param string $k column (or value if using keys) - * @param string $v value (optional) - * @access public - * @return object + * To save a bit of typing, + * + * $id = $do->pid(); + * + * @return the id */ - function &staticGet($class, $k, $v = null) + function pid() { - $lclass = strtolower($class); - global $_DB_DATAOBJECT; - if (empty($_DB_DATAOBJECT['CONFIG'])) { - DB_DataObject::_loadConfig(); - } - - - - $key = "$k:$v"; - if ($v === null) { - $key = $k; - } - if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - DB_DataObject::debug("$class $key","STATIC GET - TRY CACHE"); - } - if (!empty($_DB_DATAOBJECT['CACHE'][$lclass][$key])) { - return $_DB_DATAOBJECT['CACHE'][$lclass][$key]; - } - if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - DB_DataObject::debug("$class $key","STATIC GET - NOT IN CACHE"); - } - - $obj = DB_DataObject::factory(substr($class,strlen($_DB_DATAOBJECT['CONFIG']['class_prefix']))); - if (PEAR::isError($obj)) { - DB_DataObject::raiseError("could not autoload $class", DB_DATAOBJECT_ERROR_NOCLASS); - $r = false; - return $r; - } - - if (!isset($_DB_DATAOBJECT['CACHE'][$lclass])) { - $_DB_DATAOBJECT['CACHE'][$lclass] = array(); + $keys = $this->keys(); + if (!$keys) { + $this->raiseError("No Keys available for {$this->tableName()}", + DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; } - if (!$obj->get($k,$v)) { - DB_DataObject::raiseError("No Data return from get $k $v", DB_DATAOBJECT_ERROR_NODATA); - - $r = false; - return $r; + $k = $keys[0]; + if (empty($this->$k)) { // we do not + $this->raiseError("pid() called on Object where primary key value not available", + DB_DATAOBJECT_ERROR_NODATA); + return false; } - $_DB_DATAOBJECT['CACHE'][$lclass][$key] = $obj; - return $_DB_DATAOBJECT['CACHE'][$lclass][$key]; + return $this->$k; } + + /** * build the basic select query. @@ -381,11 +334,35 @@ class DB_DataObject extends DB_DataObject_Overload $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); if ($quoteIdentifiers) { $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + } + $tn = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName()) ; + if (!empty($this->_query['derive_table']) && !empty($this->_query['derive_select']) ) { + + // this is a derived select.. + // not much support in the api yet.. + + $sql = 'SELECT ' . + $this->_query['derive_select'] + .' FROM ( SELECT'. + $this->_query['data_select'] . " \n" . + " FROM $tn " . $this->_query['useindex'] . " \n" . + $this->_join . " \n" . + $this->_query['condition'] . " \n" . + $this->_query['group_by'] . " \n" . + $this->_query['having'] . " \n" . + ') ' . $this->_query['derive_table']; + + return $sql; + + } + + + $sql = 'SELECT ' . $this->_query['data_select'] . " \n" . - ' FROM ' . ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table) . " \n" . + " FROM $tn " . $this->_query['useindex'] . " \n" . $this->_join . " \n" . $this->_query['condition'] . " \n" . $this->_query['group_by'] . " \n" . @@ -408,6 +385,10 @@ class DB_DataObject extends DB_DataObject_Overload * will set $object->N to number of rows, and expects next command to fetch rows * will return $object->N * + * if an error occurs $object->N will be set to false and return value will also be false; + * if numRows is not supported it will + * + * * @param boolean $n Fetch first result * @access public * @return mixed (number of rows returned, or true if numRows fetching is not supported) @@ -439,7 +420,7 @@ class DB_DataObject extends DB_DataObject_Overload $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; $sql = $this->_build_select(); @@ -467,7 +448,10 @@ class DB_DataObject extends DB_DataObject_Overload } - $this->_query($sql); + $err = $this->_query($sql); + if (is_a($err,'PEAR_Error')) { + return false; + } if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug("CHECK autofetchd $n", "find", 1); @@ -539,7 +523,7 @@ class DB_DataObject extends DB_DataObject_Overload } if (empty($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]) || - !is_object($result = &$_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) + !is_object($result = $_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) { if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug('fetched on object after fetch completed (no results found)'); @@ -584,10 +568,12 @@ class DB_DataObject extends DB_DataObject_Overload // note: we dont declare this to keep the print_r size down. $_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]= array_flip(array_keys($array)); } - + $replace = array('.', ' '); foreach($array as $k=>$v) { - $kk = str_replace(".", "_", $k); - $kk = str_replace(" ", "_", $kk); + // use strpos as str_replace is slow. + $kk = (strpos($k, '.') === false && strpos($k, ' ') === false) ? + $k : str_replace($replace, '_', $k); + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug("$kk = ". $array[$k], "fetchrow LINE", 3); } @@ -597,7 +583,7 @@ class DB_DataObject extends DB_DataObject_Overload // set link flag $this->_link_loaded=false; if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - $this->debug("{$this->__table} DONE", "fetchrow",2); + $this->debug("{$this->tableName()} DONE", "fetchrow",2); } if (($this->_query !== false) && empty($_DB_DATAOBJECT['CONFIG']['keep_query_after_fetch'])) { $this->_query = false; @@ -759,8 +745,9 @@ class DB_DataObject extends DB_DataObject_Overload $ar = array(); foreach($list as $k) { settype($k, $type); - $ar[] = $type =='string' ? $this->_quote($k) : $k; + $ar[] = $type == 'string' ? $this->_quote($k) : $k; } + if (!$ar) { return $not ? $this->_query['condition'] : $this->whereAdd("1=0"); } @@ -875,6 +862,43 @@ class DB_DataObject extends DB_DataObject_Overload $this->_query['having'] .= " AND {$having}"; } + /** + * Adds a using Index + * + * $object->useIndex(); //reset the use Index + * $object->useIndex("some_index"); + * + * Note do not put unfiltered user input into theis method. + * This is mysql specific at present? - might need altering to support other databases. + * + * @param string|array $index index or indexes to use. + * @access public + * @return none|PEAR::Error - invalid args only + */ + function useIndex($index = false) + { + if ($this->_query === false) { + $this->raiseError( + "You cannot do two queries on the same object (copy it before finding)", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + if ($index=== false) { + $this->_query['useindex'] = ''; + return; + } + // check input...= 0 or ' ' == error! + if ((is_string($index) && !trim($index)) || (is_array($index) && !count($index)) ) { + return $this->raiseError("Having: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS); + } + $index = is_array($index) ? implode(', ', $index) : $index; + + if (!$this->_query['useindex']) { + $this->_query['useindex'] = " USE INDEX ({$index}) "; + return; + } + $this->_query['useindex'] = substr($this->_query['useindex'],0, -2) . ", {$index}) "; + } /** * Sets the Limit * @@ -912,7 +936,7 @@ class DB_DataObject extends DB_DataObject_Overload } global $_DB_DATAOBJECT; $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; $this->_query['limit_start'] = ($b == null) ? 0 : (int)$a; $this->_query['limit_count'] = ($b == null) ? (int)$a : (int)$b; @@ -992,9 +1016,9 @@ class DB_DataObject extends DB_DataObject_Overload } - $table = $this->__table; + $table = $this->tableName(); if (is_object($from)) { - $table = $from->__table; + $table = $from->tableName(); $from = array_keys($from->table()); } @@ -1004,7 +1028,7 @@ class DB_DataObject extends DB_DataObject_Overload $s = '%s'; if (!empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers'])) { $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; $s = $DB->quoteIdentifier($s); $format = $DB->quoteIdentifier($format); } @@ -1042,25 +1066,24 @@ class DB_DataObject extends DB_DataObject_Overload $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; - $items = isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) ? - $_DB_DATAOBJECT['INI'][$this->_database][$this->__table] : $this->table(); + $items = $this->table(); if (!$items) { - $this->raiseError("insert:No table definition for {$this->__table}", + $this->raiseError("insert:No table definition for {$this->tableName()}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return false; } - $options = &$_DB_DATAOBJECT['CONFIG']; + $options = $_DB_DATAOBJECT['CONFIG']; $datasaved = 1; $leftq = ''; $rightq = ''; - $seqKeys = isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table]) ? - $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] : + $seqKeys = isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()]) ? + $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] : $this->sequenceKey(); $key = isset($seqKeys[0]) ? $seqKeys[0] : false; @@ -1077,7 +1100,7 @@ class DB_DataObject extends DB_DataObject_Overload if (($key !== false) && !$useNative) { if (!$seq) { - $keyvalue = $DB->nextId($this->__table); + $keyvalue = $DB->nextId($this->tableName()); } else { $f = $DB->getOption('seqname_format'); $DB->setOption('seqname_format','%s'); @@ -1104,11 +1127,11 @@ class DB_DataObject extends DB_DataObject_Overload continue; } - - - - // Ignore variables which aren't set to a value - if ( !isset($this->$k) && $ignore_null) { + + // Ignore INTEGERS which aren't set to a value - or empty string.. + if ( (!isset($this->$k) || ($v == 1 && $this->$k === '')) + && $ignore_null + ) { continue; } // dont insert data into mysql timestamps @@ -1124,7 +1147,7 @@ class DB_DataObject extends DB_DataObject_Overload $leftq .= ($quoteIdentifiers ? ($DB->quoteIdentifier($k) . ' ') : "$k "); - if (is_a($this->$k,'DB_DataObject_Cast')) { + if (is_object($this->$k) && is_a($this->$k,'DB_DataObject_Cast')) { $value = $this->$k->toString($v,$DB); if (PEAR::isError($value)) { $this->raiseError($value->toString() ,DB_DATAOBJECT_ERROR_INVALIDARGS); @@ -1179,7 +1202,7 @@ class DB_DataObject extends DB_DataObject_Overload if ($leftq || $useNative) { - $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table); + $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName()); if (($dbtype == 'pgsql') && empty($leftq)) { @@ -1231,7 +1254,7 @@ class DB_DataObject extends DB_DataObject_Overload case 'pgsql': if (!$seq) { - $seq = $DB->getSequenceName(strtolower($this->__table)); + $seq = $DB->getSequenceName(strtolower($this->tableName())); } $db_driver = empty($options['db_driver']) ? 'DB' : $options['db_driver']; $method = ($db_driver == 'DB') ? 'getOne' : 'queryOne'; @@ -1314,8 +1337,7 @@ class DB_DataObject extends DB_DataObject_Overload $original_query = $this->_query; - $items = isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) ? - $_DB_DATAOBJECT['INI'][$this->_database][$this->__table] : $this->table(); + $items = $this->table(); // only apply update against sequence key if it is set????? @@ -1326,7 +1348,7 @@ class DB_DataObject extends DB_DataObject_Overload $this->raiseError("update: trying to perform an update without the key set, and argument to update is not DB_DATAOBJECT_WHEREADD_ONLY - ", DB_DATAOBJECT_ERROR_INVALIDARGS); + ". print_r(array('seq' => $seq , 'keys'=>$keys), true), DB_DATAOBJECT_ERROR_INVALIDARGS); return false; } } else { @@ -1335,14 +1357,14 @@ class DB_DataObject extends DB_DataObject_Overload if (!$items) { - $this->raiseError("update:No table definition for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); + $this->raiseError("update:No table definition for {$this->tableName()}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return false; } $datasaved = 1; $settings = ''; $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; $dbtype = $DB->dsn["phptype"]; $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); $options = $_DB_DATAOBJECT['CONFIG']; @@ -1352,11 +1374,14 @@ class DB_DataObject extends DB_DataObject_Overload || !is_string($options['disable_null_strings']) || strtolower($options['disable_null_strings']) !== 'full' ; - + foreach($items as $k => $v) { - if (!isset($this->$k) && $ignore_null) { - continue; + // I think this is ignoring empty vlalues + if ((!isset($this->$k) || ($v == 1 && $this->$k === '')) + && $ignore_null + ) { + continue; } // ignore stuff thats @@ -1383,7 +1408,7 @@ class DB_DataObject extends DB_DataObject_Overload $kSql = ($quoteIdentifiers ? $DB->quoteIdentifier($k) : $k); - if (is_a($this->$k,'DB_DataObject_Cast')) { + if (is_object($this->$k) && is_a($this->$k,'DB_DataObject_Cast')) { $value = $this->$k->toString($v,$DB); if (PEAR::isError($value)) { $this->raiseError($value->getMessage() ,DB_DATAOBJECT_ERROR_INVALIDARG); @@ -1427,7 +1452,7 @@ class DB_DataObject extends DB_DataObject_Overload // - V2 may store additional data about float/int $settings .= "$kSql = " . intval($this->$k) . ' '; } - + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug("got keys as ".serialize($keys),3); @@ -1449,7 +1474,7 @@ class DB_DataObject extends DB_DataObject_Overload // echo " $settings, $this->condition "; if ($settings && isset($this->_query) && $this->_query['condition']) { - $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table); + $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName()); $r = $this->_query("UPDATE {$table} SET {$settings} {$this->_query['condition']} "); @@ -1512,7 +1537,7 @@ class DB_DataObject extends DB_DataObject_Overload global $_DB_DATAOBJECT; // connect will load the config! $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); $extra_cond = ' ' . (isset($this->_query['order_by']) ? $this->_query['order_by'] : ''); @@ -1534,7 +1559,7 @@ class DB_DataObject extends DB_DataObject_Overload // don't delete without a condition if (($this->_query !== false) && $this->_query['condition']) { - $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table); + $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName()); $sql = "DELETE "; // using a joined delete. - with useWhere.. $sql .= (!empty($this->_join) && $useWhere) ? @@ -1596,9 +1621,9 @@ class DB_DataObject extends DB_DataObject_Overload $this->_loadConfig(); } if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); + $this->debug("{$this->tableName()} $row of {$this->N}", "fetchrow",3); } - if (!$this->__table) { + if (!$this->tableName()) { $this->raiseError("fetchrow: No table", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return false; } @@ -1611,19 +1636,22 @@ class DB_DataObject extends DB_DataObject_Overload return false; } if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); + $this->debug("{$this->tableName()} $row of {$this->N}", "fetchrow",3); } - $result = &$_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]; + $result = $_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]; $array = $result->fetchrow(DB_DATAOBJECT_FETCHMODE_ASSOC,$row); if (!is_array($array)) { $this->raiseError("fetchrow: No results available", DB_DATAOBJECT_ERROR_NODATA); return false; } - + $replace = array('.', ' '); foreach($array as $k => $v) { - $kk = str_replace(".", "_", $k); + // use strpos as str_replace is slow. + $kk = (strpos($k, '.') === false && strpos($k, ' ') === false) ? + $k : str_replace($replace, '_', $k); + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug("$kk = ". $array[$k], "fetchrow LINE", 3); } @@ -1631,7 +1659,7 @@ class DB_DataObject extends DB_DataObject_Overload } if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - $this->debug("{$this->__table} DONE", "fetchrow", 3); + $this->debug("{$this->tableName()} DONE", "fetchrow", 3); } return true; } @@ -1685,7 +1713,7 @@ class DB_DataObject extends DB_DataObject_Overload return false; } $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; if (!$whereAddOnly && $items) { @@ -1700,7 +1728,7 @@ class DB_DataObject extends DB_DataObject_Overload return false; } - $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table); + $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->tableName()) : $this->tableName()); $key_col = empty($keys[0]) ? '' : (($quoteIdentifiers ? $DB->quoteIdentifier($keys[0]) : $keys[0])); $as = ($quoteIdentifiers ? $DB->quoteIdentifier('DATAOBJECT_NUM') : 'DATAOBJECT_NUM'); @@ -1717,11 +1745,13 @@ class DB_DataObject extends DB_DataObject_Overload return false; } - $result = &$_DB_DATAOBJECT['RESULTS'][$t->_DB_resultid]; + $result = $_DB_DATAOBJECT['RESULTS'][$t->_DB_resultid]; $l = $result->fetchRow(DB_DATAOBJECT_FETCHMODE_ORDERED); // free the results - essential on oracle. $t->free(); - + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug('Count returned '. $l[0] ,1); + } return (int) $l[0]; } @@ -1755,7 +1785,7 @@ class DB_DataObject extends DB_DataObject_Overload { global $_DB_DATAOBJECT; $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; // mdb2 uses escape... $dd = empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ? 'DB' : $_DB_DATAOBJECT['CONFIG']['db_driver']; $ret = ($dd == 'DB') ? $DB->escapeSimple($string) : $DB->escape($string); @@ -1814,10 +1844,13 @@ class DB_DataObject extends DB_DataObject_Overload 'group_by' => '', // the GROUP BY condition 'order_by' => '', // the ORDER BY condition 'having' => '', // the HAVING condition + 'useindex' => '', // the USE INDEX condition 'limit_start' => '', // the LIMIT condition 'limit_count' => '', // the LIMIT condition 'data_select' => '*', // the columns to be SELECTed - 'unions' => array(), // the added unions + 'unions' => array(), // the added unions, + 'derive_table' => '', // derived table name (BETA) + 'derive_select' => '', // derived table select (BETA) ); @@ -1886,7 +1919,7 @@ class DB_DataObject extends DB_DataObject_Overload $x = new DB_DataObject; $x->_database = $args[0]; $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; $tables = $DB->getListOf('tables'); class_exists('DB_DataObject_Generator') ? '' : @@ -1917,32 +1950,41 @@ class DB_DataObject extends DB_DataObject_Overload $this->_connect(); } - // loaded already? - if (!empty($_DB_DATAOBJECT['INI'][$this->_database])) { - - // database loaded - but this is table is not available.. - if ( - empty($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) - && !empty($_DB_DATAOBJECT['CONFIG']['proxy']) - ) { - if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - $this->debug("Loading Generator to fetch Schema",1); - } - class_exists('DB_DataObject_Generator') ? '' : - require_once 'DB/DataObject/Generator.php'; - - - $x = new DB_DataObject_Generator; - $x->fillTableSchema($this->_database,$this->__table); - } + + // if this table is already loaded this table.. + if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) { return true; } - + // initialize the ini data.. if empt.. + if (empty($_DB_DATAOBJECT['INI'][$this->_database])) { + $_DB_DATAOBJECT['INI'][$this->_database] = array(); + } + if (empty($_DB_DATAOBJECT['CONFIG'])) { DB_DataObject::_loadConfig(); } + // we do not have the data for this table yet... + + // if we are configured to use the proxy.. + + if ( !empty($_DB_DATAOBJECT['CONFIG']['proxy']) ) { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("Loading Generator to fetch Schema",1); + } + class_exists('DB_DataObject_Generator') ? '' : + require_once 'DB/DataObject/Generator.php'; + + + $x = new DB_DataObject_Generator; + $x->fillTableSchema($this->_database,$this->tableName()); + return true; + } + + + + // if you supply this with arguments, then it will take those // as the database and links array... @@ -1980,9 +2022,18 @@ class DB_DataObject extends DB_DataObject_Overload } } + // are table name lowecased.. + if (!empty($_DB_DATAOBJECT['CONFIG']['portability']) && $_DB_DATAOBJECT['CONFIG']['portability'] & 1) { + foreach($_DB_DATAOBJECT['INI'][$this->_database] as $k=>$v) { + // results in duplicate cols.. but not a big issue.. + $_DB_DATAOBJECT['INI'][$this->_database][strtolower($k)] = $v; + } + } + + // now have we loaded the structure.. - if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->__table])) { + if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) { return true; } // - if not try building it.. @@ -1991,11 +2042,11 @@ class DB_DataObject extends DB_DataObject_Overload require_once 'DB/DataObject/Generator.php'; $x = new DB_DataObject_Generator; - $x->fillTableSchema($this->_database,$this->__table); + $x->fillTableSchema($this->_database,$this->tableName()); // should this fail!!!??? return true; } - $this->debug("Cant find database schema: {$this->_database}/{$this->__table} \n". + $this->debug("Cant find database schema: {$this->_database}/{$this->tableName()} \n". "in links file data: " . print_r($_DB_DATAOBJECT['INI'],true),"databaseStructure",5); // we have to die here!! - it causes chaos if we dont (including looping forever!) $this->raiseError( "Unable to load schema for database and table (turn debugging up to 5 for full error message)", DB_DATAOBJECT_ERROR_INVALIDARGS, PEAR_ERROR_DIE); @@ -2017,10 +2068,14 @@ class DB_DataObject extends DB_DataObject_Overload */ function tableName() { + global $_DB_DATAOBJECT; $args = func_get_args(); if (count($args)) { $this->__table = $args[0]; } + if (!empty($_DB_DATAOBJECT['CONFIG']['portability']) && $_DB_DATAOBJECT['CONFIG']['portability'] & 1) { + return strtolower($this->__table); + } return $this->__table; } @@ -2036,7 +2091,10 @@ class DB_DataObject extends DB_DataObject_Overload $args = func_get_args(); if (count($args)) { $this->_database = $args[0]; + } else { + $this->_connect(); } + return $this->_database; } @@ -2065,17 +2123,17 @@ class DB_DataObject extends DB_DataObject_Overload if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { $this->_connect(); } - - if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table])) { - return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table]; + + if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) { + return $_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()]; } $this->databaseStructure(); $ret = array(); - if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table])) { - $ret = $_DB_DATAOBJECT['INI'][$this->_database][$this->__table]; + if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) { + $ret = $_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()]; } return $ret; @@ -2090,7 +2148,7 @@ class DB_DataObject extends DB_DataObject_Overload * or you do not want to use ini tables, you can override this. * @param string optional set the key * @param * optional set more keys - * @access private + * @access public * @return array */ function keys() @@ -2109,13 +2167,13 @@ class DB_DataObject extends DB_DataObject_Overload if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { $this->_connect(); } - if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) { - return array_keys($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"]); + if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()."__keys"])) { + return array_keys($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()."__keys"]); } $this->databaseStructure(); - if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) { - return array_keys($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"]); + if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()."__keys"])) { + return array_keys($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()."__keys"]); } return array(); } @@ -2130,7 +2188,7 @@ class DB_DataObject extends DB_DataObject_Overload * @param string optional the key sequence/autoinc. key * @param boolean optional use native increment. default false * @param false|string optional native sequence name - * @access private + * @access public * @return array (column,use_native,sequence_name) */ function sequenceKey() @@ -2151,10 +2209,10 @@ class DB_DataObject extends DB_DataObject_Overload if (count($args)) { $args[1] = isset($args[1]) ? $args[1] : false; $args[2] = isset($args[2]) ? $args[2] : false; - $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = $args; + $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = $args; } - if (isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table])) { - return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table]; + if (isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()])) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()]; } // end call setting (eg. $do->sequenceKeys(a,b,c); ) @@ -2163,13 +2221,12 @@ class DB_DataObject extends DB_DataObject_Overload $keys = $this->keys(); if (!$keys) { - return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array(false,false,false); } - $table = isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) ? - $_DB_DATAOBJECT['INI'][$this->_database][$this->__table] : $this->table(); + $table = $this->table(); $dbtype = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype']; @@ -2179,8 +2236,8 @@ class DB_DataObject extends DB_DataObject_Overload $seqname = false; - if (!empty($_DB_DATAOBJECT['CONFIG']['sequence_'.$this->__table])) { - $seqname = $_DB_DATAOBJECT['CONFIG']['sequence_'.$this->__table]; + if (!empty($_DB_DATAOBJECT['CONFIG']['sequence_'.$this->tableName()])) { + $seqname = $_DB_DATAOBJECT['CONFIG']['sequence_'.$this->tableName()]; if (strpos($seqname,':') !== false) { list($usekey,$seqname) = explode(':',$seqname); } @@ -2189,25 +2246,25 @@ class DB_DataObject extends DB_DataObject_Overload // if the key is not an integer - then it's not a sequence or native if (empty($table[$usekey]) || !($table[$usekey] & DB_DATAOBJECT_INT)) { - return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array(false,false,false); + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array(false,false,false); } if (!empty($_DB_DATAOBJECT['CONFIG']['ignore_sequence_keys'])) { $ignore = $_DB_DATAOBJECT['CONFIG']['ignore_sequence_keys']; if (is_string($ignore) && (strtoupper($ignore) == 'ALL')) { - return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array(false,false,$seqname); + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array(false,false,$seqname); } if (is_string($ignore)) { $ignore = $_DB_DATAOBJECT['CONFIG']['ignore_sequence_keys'] = explode(',',$ignore); } - if (in_array($this->__table,$ignore)) { - return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array(false,false,$seqname); + if (in_array($this->tableName(),$ignore)) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array(false,false,$seqname); } } - $realkeys = $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"]; + $realkeys = $_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()."__keys"]; // if you are using an old ini file - go back to old behaviour... if (is_numeric($realkeys[$usekey])) { @@ -2216,25 +2273,38 @@ class DB_DataObject extends DB_DataObject_Overload // multiple unique primary keys without a native sequence... if (($realkeys[$usekey] == 'K') && (count($keys) > 1)) { - return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array(false,false,$seqname); + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array(false,false,$seqname); } // use native sequence keys... // technically postgres native here... // we need to get the new improved tabledata sorted out first. + // support named sequence keys.. - currently postgres only.. + + if ( in_array($dbtype , array('pgsql')) && + ($table[$usekey] & DB_DATAOBJECT_INT) && + isset($realkeys[$usekey]) && strlen($realkeys[$usekey]) > 1) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array($usekey,true, $realkeys[$usekey]); + } + if ( in_array($dbtype , array('pgsql', 'mysql', 'mysqli', 'mssql', 'ifx')) && ($table[$usekey] & DB_DATAOBJECT_INT) && isset($realkeys[$usekey]) && ($realkeys[$usekey] == 'N') ) { - return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array($usekey,true,$seqname); + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array($usekey,true,$seqname); } + + // if not a native autoinc, and we have not assumed all primary keys are sequence if (($realkeys[$usekey] != 'N') && !empty($_DB_DATAOBJECT['CONFIG']['dont_use_pear_sequences'])) { return array(false,false,false); } + + + // I assume it's going to try and be a nextval DB sequence.. (not native) - return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array($usekey,false,$seqname); + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->tableName()] = array($usekey,false,$seqname); } @@ -2346,13 +2416,13 @@ class DB_DataObject extends DB_DataObject_Overload // it's not currently connected! // try and work out what to use for the dsn ! - $options= &$_DB_DATAOBJECT['CONFIG']; + $options= $_DB_DATAOBJECT['CONFIG']; // if the databse dsn dis defined in the object.. $dsn = isset($this->_database_dsn) ? $this->_database_dsn : null; if (!$dsn) { - if (!$this->_database) { - $this->_database = isset($options["table_{$this->__table}"]) ? $options["table_{$this->__table}"] : null; + if (!$this->_database && !empty($this->__table)) { + $this->_database = isset($options["table_{$this->tableName()}"]) ? $options["table_{$this->tableName()}"] : null; } if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug("Checking for database specific ini ('{$this->_database}') : database_{$this->_database} in options","CONNECT"); @@ -2421,11 +2491,11 @@ class DB_DataObject extends DB_DataObject_Overload $db_options = PEAR::getStaticProperty('DB','options'); require_once 'DB.php'; if ($db_options) { - $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &DB::connect($dsn,$db_options); + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = DB::connect($dsn,$db_options); } else { - $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &DB::connect($dsn); + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = DB::connect($dsn); } - + } else { /* assumption is MDB2 */ require_once 'MDB2.php'; @@ -2434,13 +2504,13 @@ class DB_DataObject extends DB_DataObject_Overload $db_options = is_array($db_options) ? $db_options : array(); $db_options['portability'] = isset($db_options['portability'] ) ? $db_options['portability'] : MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE; - $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &MDB2::connect($dsn,$db_options); + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = MDB2::connect($dsn,$db_options); } if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - $this->debug(serialize($_DB_DATAOBJECT['CONNECTIONS']), "CONNECT",5); + $this->debug(print_r($_DB_DATAOBJECT['CONNECTIONS'],true), "CONNECT",5); } if (PEAR::isError($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { $this->debug($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->toString(), "CONNECT FAILED",5); @@ -2467,7 +2537,6 @@ class DB_DataObject extends DB_DataObject_Overload } // Oracle need to optimize for portibility - not sure exactly what this does though :) - $c = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; return true; } @@ -2486,9 +2555,9 @@ class DB_DataObject extends DB_DataObject_Overload $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; - $options = &$_DB_DATAOBJECT['CONFIG']; + $options = $_DB_DATAOBJECT['CONFIG']; $_DB_driver = empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ? 'DB': $_DB_DATAOBJECT['CONFIG']['db_driver']; @@ -2498,15 +2567,19 @@ class DB_DataObject extends DB_DataObject_Overload } - if (strtoupper($string) == 'BEGIN') { + if ( + strtoupper($string) == 'BEGIN' || + strtoupper($string) == 'START TRANSACTION' + ) { if ($_DB_driver == 'DB') { $DB->autoCommit(false); + $DB->simpleQuery('BEGIN'); } else { $DB->beginTransaction(); } - // db backend adds begin anyway from now on.. return true; } + if (strtoupper($string) == 'COMMIT') { $res = $DB->commit(); if ($_DB_driver == 'DB') { @@ -2564,7 +2637,7 @@ class DB_DataObject extends DB_DataObject_Overload } // see if we got a failure.. - try again a few times.. - if (!is_a($result,'PEAR_Error')) { + if (!is_object($result) || !is_a($result,'PEAR_Error')) { break; } if ($result->getCode() != -14) { // *DB_ERROR_NODBSELECTED @@ -2575,10 +2648,11 @@ class DB_DataObject extends DB_DataObject_Overload } - if (is_a($result,'PEAR_Error')) { + if (is_object($result) && is_a($result,'PEAR_Error')) { if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug($result->toString(), "Query Error",1 ); } + $this->N = false; return $this->raiseError($result); } if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { @@ -2607,14 +2681,17 @@ class DB_DataObject extends DB_DataObject_Overload if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug(serialize($result), 'RESULT',5); } - if (method_exists($result, 'numrows')) { + if (method_exists($result, 'numRows')) { if ($_DB_driver == 'DB') { $DB->expectError(DB_ERROR_UNSUPPORTED); } else { $DB->expectError(MDB2_ERROR_UNSUPPORTED); } - $this->N = $result->numrows(); - if (is_a($this->N,'PEAR_Error')) { + + $this->N = $result->numRows(); + //var_dump($this->N); + + if (is_object($this->N) && is_a($this->N,'PEAR_Error')) { $this->N = true; } $DB->popExpect(); @@ -2634,7 +2711,7 @@ class DB_DataObject extends DB_DataObject_Overload { global $_DB_DATAOBJECT; $this->_connect(); - $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); $options = $_DB_DATAOBJECT['CONFIG']; @@ -2666,12 +2743,12 @@ class DB_DataObject extends DB_DataObject_Overload } $kSql = $quoteIdentifiers - ? ( $DB->quoteIdentifier($this->__table) . '.' . $DB->quoteIdentifier($k) ) - : "{$this->__table}.{$k}"; + ? ( $DB->quoteIdentifier($this->tableName()) . '.' . $DB->quoteIdentifier($k) ) + : "{$this->tableName()}.{$k}"; - if (is_a($this->$k,'DB_DataObject_Cast')) { + if (is_object($this->$k) && is_a($this->$k,'DB_DataObject_Cast')) { $dbtype = $DB->dsn["phptype"]; $value = $this->$k->toString($v,$DB); if (PEAR::isError($value)) { @@ -2711,28 +2788,6 @@ class DB_DataObject extends DB_DataObject_Overload } } - /** - * autoload Class relating to a table - * (depreciated - use ::factory) - * - * @param string $table table - * @access private - * @return string classname on Success - */ - function staticAutoloadTable($table) - { - global $_DB_DATAOBJECT; - if (empty($_DB_DATAOBJECT['CONFIG'])) { - DB_DataObject::_loadConfig(); - } - $p = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ? - $_DB_DATAOBJECT['CONFIG']['class_prefix'] : ''; - $class = $p . preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)); - - $ce = substr(phpversion(),0,1) > 4 ? class_exists($class,false) : class_exists($class); - $class = $ce ? $class : DB_DataObject::_autoloadClass($class); - return $class; - } /** @@ -2756,7 +2811,8 @@ class DB_DataObject extends DB_DataObject_Overload - function factory($table = '') { + static function factory($table = '') + { global $_DB_DATAOBJECT; @@ -2773,16 +2829,17 @@ class DB_DataObject extends DB_DataObject_Overload } // no configuration available for database if (!empty($database) && empty($_DB_DATAOBJECT['CONFIG']['database_'.$database])) { - return DB_DataObject::raiseError( + $do = new DB_DataObject(); + $do->raiseError( "unable to find database_{$database} in Configuration, It is required for factory with database" , 0, PEAR_ERROR_DIE ); } - + /* if ($table === '') { - if (is_a($this,'DB_DataObject') && strlen($this->__table)) { - $table = $this->__table; + if (is_a($this,'DB_DataObject') && strlen($this->tableName())) { + $table = $this->tableName(); } else { return DB_DataObject::raiseError( "factory did not recieve a table name", @@ -2790,10 +2847,13 @@ class DB_DataObject extends DB_DataObject_Overload } } + */ // does this need multi db support?? $cp = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ? explode(PATH_SEPARATOR, $_DB_DATAOBJECT['CONFIG']['class_prefix']) : ''; + //print_r($cp); + // multiprefix support. $tbl = preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)); if (is_array($cp)) { @@ -2813,7 +2873,6 @@ class DB_DataObject extends DB_DataObject_Overload $rclass = $ce ? $class : DB_DataObject::_autoloadClass($class, $table); - // proxy = full|light if (!$rclass && isset($_DB_DATAOBJECT['CONFIG']['proxy'])) { @@ -2828,7 +2887,9 @@ class DB_DataObject extends DB_DataObject_Overload $d = new DB_DataObject; $d->__table = $table; - if (is_a($ret = $d->_connect(), 'PEAR_Error')) { + + $ret = $d->_connect(); + if (is_object($ret) && is_a($ret, 'PEAR_Error')) { return $ret; } @@ -2836,14 +2897,17 @@ class DB_DataObject extends DB_DataObject_Overload return $x->$proxyMethod( $d->_database, $table); } - if (!$rclass) { - return DB_DataObject::raiseError( + if (!$rclass || !class_exists($rclass)) { + $dor = new DB_DataObject(); + return $dor->raiseError( "factory could not find class " . (is_array($class) ? implode(PATH_SEPARATOR, $class) : $class ). "from $table", DB_DATAOBJECT_ERROR_INVALIDCONFIG); } - $ret = new $rclass; + + $ret = new $rclass(); + if (!empty($database)) { DB_DataObject::debug("Setting database to $database","FACTORY",1); $ret->database($database); @@ -2920,7 +2984,8 @@ class DB_DataObject extends DB_DataObject_Overload } } if (!$found) { - DB_DataObject::raiseError( + $dor = new DB_DataObject(); + $dor->raiseError( "autoload:Could not find class " . implode(',', $cls) . " using class_location value :" . $search . " using include_path value :" . ini_get('include_path'), @@ -2941,7 +3006,8 @@ class DB_DataObject extends DB_DataObject_Overload } } if (!$ce) { - DB_DataObject::raiseError( + $dor = new DB_DataObject(); + $dor->raiseError( "autoload:Could not autoload " . implode(',', $cls) , DB_DATAOBJECT_ERROR_INVALIDCONFIG); return false; @@ -2968,11 +3034,14 @@ class DB_DataObject extends DB_DataObject_Overload * Should look a bit like * [local_col_name] => "related_tablename:related_col_name" * + * @param array $new_links optional - force update of the links for this table + * You probably want to restore it to it's original state after, + * as modifying here does it for the whole PHP request. * * @return array|null * array = if there are links defined for this table. * empty array - if there is a links.ini file, but no links on this table - * null - if no links.ini exists for this database (hence try auto_links). + * false - if no links.ini exists for this database (hence try auto_links). * @access public * @see DB_DataObject::getLinks(), DB_DataObject::getLink() */ @@ -2986,232 +3055,183 @@ class DB_DataObject extends DB_DataObject_Overload // have to connect.. -> otherwise things break later. $this->_connect(); - if (isset($_DB_DATAOBJECT['LINKS'][$this->_database][$this->__table])) { - return $_DB_DATAOBJECT['LINKS'][$this->_database][$this->__table]; + // alias for shorter code.. + $lcfg = &$_DB_DATAOBJECT['LINKS']; + $cfg = $_DB_DATAOBJECT['CONFIG']; + + if ($args = func_get_args()) { + // an associative array was specified, that updates the current + // schema... - be careful doing this + if (empty( $lcfg[$this->_database])) { + $lcfg[$this->_database] = array(); + } + $lcfg[$this->_database][$this->tableName()] = $args[0]; + + } + // loaded and available. + if (isset($lcfg[$this->_database][$this->tableName()])) { + return $lcfg[$this->_database][$this->tableName()]; + } + + // loaded + if (isset($lcfg[$this->_database])) { + // either no file, or empty.. + return $lcfg[$this->_database] === false ? null : array(); } - - - - - // attempt to load links file here.. - - if (!isset($_DB_DATAOBJECT['LINKS'][$this->_database])) { - $schemas = isset($_DB_DATAOBJECT['CONFIG']['schema_location']) ? - array("{$_DB_DATAOBJECT['CONFIG']['schema_location']}/{$this->_database}.ini") : - array() ; - - if (isset($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"])) { - $schemas = is_array($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]) ? - $_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"] : - explode(PATH_SEPARATOR,$_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]); + // links are same place as schema by default. + $schemas = isset($cfg['schema_location']) ? + array("{$cfg['schema_location']}/{$this->_database}.ini") : + array() ; + + // if ini_* is set look there instead. + // and support multiple locations. + if (isset($cfg["ini_{$this->_database}"])) { + $schemas = is_array($cfg["ini_{$this->_database}"]) ? + $cfg["ini_{$this->_database}"] : + explode(PATH_SEPARATOR,$cfg["ini_{$this->_database}"]); + } + + // default to not available. + $lcfg[$this->_database] = false; + + foreach ($schemas as $ini) { + + $links = isset($cfg["links_{$this->_database}"]) ? + $cfg["links_{$this->_database}"] : + str_replace('.ini','.links.ini',$ini); + + // file really exists.. + if (!file_exists($links) || !is_file($links)) { + if (!empty($cfg['debug'])) { + $this->debug("Missing links.ini file: $links","links",1); + } + continue; } + + // set to empty array - as we have at least one file now.. + $lcfg[$this->_database] = empty($lcfg[$this->_database]) ? array() : $lcfg[$this->_database]; + + // merge schema file into lcfg.. + $lcfg[$this->_database] = array_merge( + $lcfg[$this->_database], + parse_ini_file($links, true) + ); + + if (!empty($cfg['debug'])) { + $this->debug("Loaded links.ini file: $links","links",1); + } - $_DB_DATAOBJECT['LINKS'][$this->_database] = array(); - foreach ($schemas as $ini) { + } + + if (!empty($_DB_DATAOBJECT['CONFIG']['portability']) && $_DB_DATAOBJECT['CONFIG']['portability'] & 1) { + foreach($lcfg[$this->_database] as $k=>$v) { - $links = - isset($_DB_DATAOBJECT['CONFIG']["links_{$this->_database}"]) ? - $_DB_DATAOBJECT['CONFIG']["links_{$this->_database}"] : - str_replace('.ini','.links.ini',$ini); - - if (file_exists($links) && is_file($links)) { - /* not sure why $links = ... here - TODO check if that works */ - $_DB_DATAOBJECT['LINKS'][$this->_database] = array_merge( - $_DB_DATAOBJECT['LINKS'][$this->_database], - parse_ini_file($links, true) - ); - - if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - $this->debug("Loaded links.ini file: $links","links",1); - } - } else { - if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { - $this->debug("Missing links.ini file: $links","links",1); - } + $nk = strtolower($k); + // results in duplicate cols.. but not a big issue.. + $lcfg[$this->_database][$nk] = isset($lcfg[$this->_database][$nk]) + ? $lcfg[$this->_database][$nk] : array(); + + foreach($v as $kk =>$vv) { + //var_Dump($vv);exit; + $vv =explode(':', $vv); + $vv[0] = strtolower($vv[0]); + $lcfg[$this->_database][$nk][$kk] = implode(':', $vv); } + + } } - + //echo '
';print_r($lcfg);exit;
         
         // if there is no link data at all on the file!
         // we return null.
-        if (!isset($_DB_DATAOBJECT['LINKS'][$this->_database])) {
+        if ($lcfg[$this->_database] === false) {
             return null;
         }
         
-        if (isset($_DB_DATAOBJECT['LINKS'][$this->_database][$this->__table])) {
-            return $_DB_DATAOBJECT['LINKS'][$this->_database][$this->__table];
+        if (isset($lcfg[$this->_database][$this->tableName()])) {
+            return $lcfg[$this->_database][$this->tableName()];
         }
         
         return array();
     }
+    
+    
     /**
-     * load related objects
+     * generic getter/setter for links
      *
-     * There are two ways to use this, one is to set up a .links.ini file
-     * into a static property named .links and specifies the table joins,
-     * the other highly dependent on naming columns 'correctly' :)
-     * using colname = xxxxx_yyyyyy
-     * xxxxxx = related table; (yyyyy = user defined..)
-     * looks up table xxxxx, for value id=$this->xxxxx
-     * stores it in $this->_xxxxx_yyyyy
-     * you can change what object vars the links are stored in by 
-     * changeing the format parameter
+     * This is the new 'recommended' way to get get/set linked objects.
+     * must be used with links.ini
      *
+     * usage:
+     *  get:
+     *  $obj = $do->link('company_id');
+     *  $obj = $do->link(array('local_col', 'linktable:linked_col'));
+     *  
+     *  set:
+     *  $do->link('company_id',0);
+     *  $do->link('company_id',$obj);
+     *  $do->link('company_id', array($obj));
      *
-     * @param  string format (default _%s) where %s is the table name.
-     * @author Tim White 
-     * @access public
-     * @return boolean , true on success
-     */
-    function getLinks($format = '_%s')
+     *  example function
+     *
+     *  function company() {
+     *     $this->link(array('company_id','company:id'), func_get_args());
+     *   }
+     *
+     * 
+     *
+     * @param  mixed $link_spec              link specification (normally a string)
+     *                                       uses similar rules to  joinAdd() array argument.
+     * @param  mixed $set_value (optional)   int, DataObject, or array('set')
+     * @author Alan Knowles
+     * @access public
+     * @return mixed true or false on setting, object on getting
+     */
+    function link($field, $set_args = array())
     {
-         
-        // get table will load the options.
-        if ($this->_link_loaded) {
-            return true;
-        }
-        $this->_link_loaded = false;
-        $cols  = $this->table();
-        $links = $this->links();
-         
-        $loaded = array();
-        
-        if ($links) {   
-            foreach($links as $key => $match) {
-                list($table,$link) = explode(':', $match);
-                $k = sprintf($format, str_replace('.', '_', $key));
-                // makes sure that '.' is the end of the key;
-                if ($p = strpos($key,'.')) {
-                      $key = substr($key, 0, $p);
-                }
-                
-                $this->$k = $this->getLink($key, $table, $link);
-                
-                if (is_object($this->$k)) {
-                    $loaded[] = $k; 
-                }
-            }
-            $this->_link_loaded = $loaded;
-            return true;
-        }
-        // this is the autonaming stuff..
-        // it sends the column name down to getLink and lets that sort it out..
-        // if there is a links file then it is not used!
-        // IT IS DEPRECIATED!!!! - USE 
-        if (!is_null($links)) {    
-            return false;
-        }
-        
+        require_once 'DB/DataObject/Links.php';
+        $l = new DB_DataObject_Links($this);
+        return  $l->link($field,$set_args) ;
         
-        foreach (array_keys($cols) as $key) {
-            if (!($p = strpos($key, '_'))) {
-                continue;
-            }
-            // does the table exist.
-            $k =sprintf($format, $key);
-            $this->$k = $this->getLink($key);
-            if (is_object($this->$k)) {
-                $loaded[] = $k; 
-            }
-        }
-        $this->_link_loaded = $loaded;
-        return true;
     }
-
-    /**
-     * return name from related object
-     *
-     * There are two ways to use this, one is to set up a .links.ini file
-     * into a static property named .links and specifies the table joins,
-     * the other is highly dependant on naming columns 'correctly' :)
+    
+      /**
+     * load related objects
      *
-     * NOTE: the naming convention is depreciated!!! - use links.ini
+     * Generally not recommended to use this.
+     * The generator should support creating getter_setter methods which are better suited.
      *
-     * using colname = xxxxx_yyyyyy
-     * xxxxxx = related table; (yyyyy = user defined..)
-     * looks up table xxxxx, for value id=$this->xxxxx
-     * stores it in $this->_xxxxx_yyyyy
+     * Relies on  .links.ini
      *
-     * you can also use $this->getLink('thisColumnName','otherTable','otherTableColumnName')
+     * Sets properties on the calling dataobject  you can change what
+     * object vars the links are stored in by  changeing the format parameter
      *
      *
-     * @param string $row    either row or row.xxxxx
-     * @param string $table  name of table to look up value in
-     * @param string $link   name of column in other table to match
+     * @param  string format (default _%s) where %s is the table name.
      * @author Tim White 
      * @access public
-     * @return mixed object on success
+     * @return boolean , true on success
      */
-    function getLink($row, $table = null, $link = false)
+    function getLinks($format = '_%s')
     {
-        
-        
-        // GUESS THE LINKED TABLE.. (if found - recursevly call self)
-        
-        if ($table === null) {
-            $links = $this->links();
-            
-            if (is_array($links)) {
-            
-                if ($links[$row]) {
-                    list($table,$link) = explode(':', $links[$row]);
-                    if ($p = strpos($row,".")) {
-                        $row = substr($row,0,$p);
-                    }
-                    return $this->getLink($row,$table,$link);
-                    
-                } 
-                
-                $this->raiseError(
-                    "getLink: $row is not defined as a link (normally this is ok)", 
-                    DB_DATAOBJECT_ERROR_NODATA);
-                    
-                $r = false;
-                return $r;// technically a possible error condition?
-
-            }  
-            // use the old _ method - this shouldnt happen if called via getLinks()
-            if (!($p = strpos($row, '_'))) {
-                $r = null;
-                return $r; 
-            }
-            $table = substr($row, 0, $p);
-            return $this->getLink($row, $table);
-            
+        require_once 'DB/DataObject/Links.php';
+         $l = new DB_DataObject_Links($this);
+        return $l->applyLinks($format);
+           
+    }
 
-        }
-        
-        
-        
-        if (!isset($this->$row)) {
-            $this->raiseError("getLink: row not set $row", DB_DATAOBJECT_ERROR_NODATA);
-            return false;
-        }
-        
-        // check to see if we know anything about this table..
-        
-        $obj = $this->factory($table);
-        
-        if (!is_a($obj,'DB_DataObject')) {
-            $this->raiseError(
-                "getLink:Could not find class for row $row, table $table", 
-                DB_DATAOBJECT_ERROR_INVALIDCONFIG);
-            return false;
-        }
-        if ($link) {
-            if ($obj->get($link, $this->$row)) {
-                return $obj;
-            } 
-            return  false;
-        }
-        
-        if ($obj->get($this->$row)) {
-            return $obj;
-        }
-        return false;
+    /**
+     * deprecited : @use link() 
+     */
+    function getLink($row, $table = null, $link = false)
+    {
+        require_once 'DB/DataObject/Links.php';
+        $l = new DB_DataObject_Links($this);
+        return $l->getLink($row, $table === null ? false: $table, $link);
+         
         
     }
 
@@ -3242,47 +3262,12 @@ class DB_DataObject extends DB_DataObject_Overload
      * }
      * 
      */
-    function &getLinkArray($row, $table = null)
+    function getLinkArray($row, $table = null)
     {
-        
-        $ret = array();
-        if (!$table) {
-            $links = $this->links();
-            
-            if (is_array($links)) {
-                if (!isset($links[$row])) {
-                    // failed..
-                    return $ret;
-                }
-                list($table,$link) = explode(':',$links[$row]);
-            } else {
-                if (!($p = strpos($row,'_'))) {
-                    return $ret;
-                }
-                $table = substr($row,0,$p);
-            }
-        }
-        
-        $c  = $this->factory($table);
-        
-        if (!is_a($c,'DB_DataObject')) {
-            $this->raiseError(
-                "getLinkArray:Could not find class for row $row, table $table", 
-                DB_DATAOBJECT_ERROR_INVALIDCONFIG
-            );
-            return $ret;
-        }
-
-        // if the user defined method list exists - use it...
-        if (method_exists($c, 'listFind')) {
-            $c->listFind($this->id);
-        } else {
-            $c->find();
-        }
-        while ($c->fetch()) {
-            $ret[] = $c;
-        }
-        return $ret;
+        require_once 'DB/DataObject/Links.php';
+        $l = new DB_DataObject_Links($this);
+        return $l->getLinkArray($row, $table === null ? false: $table);
+     
     }
 
      /**
@@ -3362,9 +3347,10 @@ class DB_DataObject extends DB_DataObject_Overload
      * @param    optional $obj       object |array    the joining object (no value resets the join)
      *                                          If you use an array here it should be in the format:
      *                                          array('local_column','remotetable:remote_column');
-     *                                          if remotetable does not have a definition, you should
-     *                                          use @ to hide the include error message..
-     *                                      
+     *                                             if remotetable does not have a definition, you should
+     *                                             use @ to hide the include error message..
+     *                                          array('local_column',  $dataobject , 'remote_column');
+     *                                             if array has 3 args, then second is assumed to be the linked dataobject.
      *
      * @param    optional $joinType  string | array
      *                                          'LEFT'|'INNER'|'RIGHT'|'' Inner is default, '' indicates 
@@ -3424,14 +3410,21 @@ class DB_DataObject extends DB_DataObject_Overload
         $toTable = false;
         if (is_array($obj)) {
             $tfield = $obj[0];
-            list($toTable,$ofield) = explode(':',$obj[1]);
-            $obj = DB_DataObject::factory($toTable);
             
-            if (!$obj || is_a($obj,'PEAR_Error')) {
-                $obj = new DB_DataObject;
-                $obj->__table = $toTable;
+            if (count($obj) == 3) {
+                $ofield = $obj[2];
+                $obj = $obj[1];
+            } else {
+                list($toTable,$ofield) = explode(':',$obj[1]);
+            
+                $obj = is_string($toTable) ? DB_DataObject::factory($toTable) : $toTable;
+            
+                if (!$obj || !is_object($obj) || is_a($obj,'PEAR_Error')) {
+                    $obj = new DB_DataObject;
+                    $obj->__table = $toTable;
+                }
+                $obj->_connect();
             }
-            $obj->_connect();
             // set the table items to nothing.. - eg. do not try and match
             // things in the child table...???
             $items = array();
@@ -3442,7 +3435,7 @@ class DB_DataObject extends DB_DataObject_Overload
         }
         /*  make sure $this->_database is set.  */
         $this->_connect();
-        $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
+        $DB = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
        
 
         /// CHANGED 26 JUN 2009 - we prefer links from our local table over the remote one.
@@ -3473,7 +3466,7 @@ class DB_DataObject extends DB_DataObject_Overload
                         $ar[1] = explode(',', $ar[1]);
                     }
 
-                    if ($ar[0] != $obj->__table) {
+                    if ($ar[0] != $obj->tableName()) {
                         continue;
                     }
                     if ($joinCol !== false) {
@@ -3518,7 +3511,7 @@ class DB_DataObject extends DB_DataObject_Overload
                         $ar[1] = explode(',', $ar[1]);
                     }
                  
-                    if ($ar[0] != $this->__table) {
+                    if ($ar[0] != $this->tableName()) {
                         continue;
                     }
                     
@@ -3555,7 +3548,7 @@ class DB_DataObject extends DB_DataObject_Overload
 
         if ($ofield === false) {
             $this->raiseError(
-                "joinAdd: {$obj->__table} has no link with {$this->__table}",
+                "joinAdd: {$obj->tableName()} has no link with {$this->tableName()}",
                 DB_DATAOBJECT_ERROR_NODATA);
             return false;
         }
@@ -3564,7 +3557,7 @@ class DB_DataObject extends DB_DataObject_Overload
         // we default to joining as the same name (this is remvoed later..)
         
         if ($joinAs === false) {
-            $joinAs = $obj->__table;
+            $joinAs = $obj->tableName();
         }
         
         $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']);
@@ -3572,8 +3565,8 @@ class DB_DataObject extends DB_DataObject_Overload
         
         // not sure  how portable adding database prefixes is..
         $objTable = $quoteIdentifiers ? 
-                $DB->quoteIdentifier($obj->__table) : 
-                 $obj->__table ;
+                $DB->quoteIdentifier($obj->tableName()) : 
+                 $obj->tableName() ;
                 
         $dbPrefix  = '';
         if (strlen($obj->_database) && in_array($DB->dsn['phptype'],array('mysql','mysqli'))) {
@@ -3649,7 +3642,7 @@ class DB_DataObject extends DB_DataObject_Overload
                     continue;
                 }
                             
-                if (is_a($obj->$k,'DB_DataObject_Cast')) {
+                if (is_object($obj->$k) && is_a($obj->$k,'DB_DataObject_Cast')) {
                     $value = $obj->$k->toString($v,$DB);
                     if (PEAR::isError($value)) {
                         $this->raiseError($value->getMessage() ,DB_DATAOBJECT_ERROR_INVALIDARG);
@@ -3706,7 +3699,7 @@ class DB_DataObject extends DB_DataObject_Overload
                
         
         
-        $table = $this->__table;
+        $table = $this->tableName();
         
         if ($quoteIdentifiers) {
             $joinAs   = $DB->quoteIdentifier($joinAs);
@@ -3718,7 +3711,7 @@ class DB_DataObject extends DB_DataObject_Overload
        
         
         $fullJoinAs = '';
-        $addJoinAs  = ($quoteIdentifiers ? $DB->quoteIdentifier($obj->__table) : $obj->__table) != $joinAs;
+        $addJoinAs  = ($quoteIdentifiers ? $DB->quoteIdentifier($obj->tableName()) : $obj->tableName()) != $joinAs;
         if ($addJoinAs) {
             // join table a AS b - is only supported by a few databases and is probably not needed
             // , however since it makes the whole Statement alot clearer we are leaving it in
@@ -3767,6 +3760,246 @@ class DB_DataObject extends DB_DataObject_Overload
 
     }
 
+    /**
+     * autoJoin - using the links.ini file, it builds a query with all the joins 
+     * usage: 
+     * $x = DB_DataObject::factory('mytable');
+     * $x->autoJoin();
+     * $x->get(123); 
+     *   will result in all of the joined data being added to the fetched object..
+     * 
+     * $x = DB_DataObject::factory('mytable');
+     * $x->autoJoin();
+     * $ar = $x->fetchAll();
+     *   will result in an array containing all the data from the table, and any joined tables..
+     * 
+     * $x = DB_DataObject::factory('mytable');
+     * $jdata = $x->autoJoin();
+     * $x->selectAdd(); //reset..
+     * foreach($_REQUEST['requested_cols'] as $c) {
+     *    if (!isset($jdata[$c])) continue; // ignore columns not available..
+     *    $x->selectAdd( $jdata[$c] . ' as ' . $c);
+     * }
+     * $ar = $x->fetchAll(); 
+     *   will result in only the columns requested being fetched...
+     *
+     *
+     *
+     * @param     array     Configuration
+     *          exclude  Array of columns to exclude from results (eg. modified_by_id)
+     *          links    The equivilant links.ini data for this table eg.
+     *                    array( 'person_id' => 'person:id', .... )
+     *          include  Array of columns to include
+     *          distinct Array of distinct columns.
+     *          
+     * @return   array      info about joins
+     *                      cols => map of resulting {joined_tablename}.{joined_table_column_name}
+     *                      join_names => map of resulting {join_name_as}.{joined_table_column_name}
+     *                      count => the column to count on.
+     * @access   public
+     */
+    function autoJoin($cfg = array())
+    {
+        //var_Dump($cfg);exit;
+        $pre_links = $this->links();
+        if (!empty($cfg['links'])) {
+            $this->links(array_merge( $pre_links , $cfg['links']));
+        }
+        $map = $this->links( );
+        
+        
+        //print_r($map);
+        $tabdef = $this->table();
+         
+        // we need this as normally it's only cleared by an empty selectAs call.
+       
+        
+        $keys = array_keys($tabdef);
+        if (!empty($cfg['exclude'])) {
+            $keys = array_intersect($keys, array_diff($keys, $cfg['exclude'])); 
+        }
+        if (!empty($cfg['include'])) {
+            
+            $keys =  array_intersect($keys,  $cfg['include']); 
+        }
+        
+        $selectAs = array();
+        
+        if (!empty($keys)) {
+            $selectAs = array(array( $keys , '%s', false));
+        }
+        
+        $ret = array(
+            'cols' => array(),
+            'join_names' => array(),
+            'count' => false,
+        );
+        
+        
+        
+        $has_distinct = false;
+        if (!empty($cfg['distinct']) && $keys) {
+            
+            // reset the columsn?
+            $cols = array();
+            
+             //echo '
' ;print_r($xx);exit;
+            foreach($keys as $c) {
+                //var_dump($c);
+                
+                if (  $cfg['distinct'] == $c) {
+                    $has_distinct = 'DISTINCT( ' . $this->tableName() .'.'. $c .') as ' . $c;
+                    $ret['count'] =  'DISTINCT  ' . $this->tableName() .'.'. $c .'';
+                    continue;
+                }
+                // cols is in our filtered keys...
+                $cols = $c;
+                
+            }
+            // apply our filtered version, which excludes the distinct column.
+            
+            $selectAs = empty($cols) ?  array() : array(array(array(  $cols) , '%s', false)) ;
+            
+            
+            
+        } 
+                
+        foreach($keys as $k) {
+            $ret['cols'][$k] = $this->tableName(). '.' . $k;
+        }
+        
+         
+        
+        foreach($map as $ocl=>$info) {
+            
+            list($tab,$col) = explode(':', $info);
+            // what about multiple joins on the same table!!!
+            $xx = DB_DataObject::factory($tab);
+            if (!is_object($xx) || !is_a($xx, 'DB_DataObject')) {
+                continue;
+            }
+            // skip columns that are excluded.
+            
+            // we ignore include here... - as
+             
+            // this is borked ... for multiple jions..
+            $this->joinAdd($xx, 'LEFT', 'join_'.$ocl.'_'. $col, $ocl);
+            
+            if (!empty($cfg['exclude']) && in_array($ocl, $cfg['exclude'])) {
+                continue;
+            }
+            
+            $tabdef = $xx->table();
+            $table = $xx->tableName();
+            
+            $keys = array_keys($tabdef);
+            
+            
+            if (!empty($cfg['exclude'])) {
+                $keys = array_intersect($keys, array_diff($keys, $cfg['exclude']));
+                
+                foreach($keys as $k) {
+                    if (in_array($ocl.'_'.$k, $cfg['exclude'])) {
+                        $keys = array_diff($keys, $k); // removes the k..
+                    }
+                }
+                
+            }
+            
+            if (!empty($cfg['include'])) {
+                // include will basically be BASECOLNAME_joinedcolname
+                $nkeys = array();
+                foreach($keys as $k) {
+                    if (in_array( sprintf($ocl.'_%s', $k), $cfg['include'])) {
+                        $nkeys[] = $k;
+                    }
+                }
+                $keys = $nkeys;
+            }
+            
+            if (empty($keys)) {
+                continue;
+            }
+            // got distinct, and not yet found it..
+            if (!$has_distinct && !empty($cfg['distinct']))  {
+                $cols = array();
+                foreach($keys as $c) {
+                    $tn = sprintf($ocl.'_%s', $c);
+                      
+                    if ( $tn == $cfg['distinct']) {
+                        
+                        $has_distinct = 'DISTINCT( ' . 'join_'.$ocl.'_'.$col.'.'.$c .')  as ' . $tn ;
+                        $ret['count'] =  'DISTINCT  join_'.$ocl.'_'.$col.'.'.$c;
+                       // var_dump($this->countWhat );
+                        continue;
+                    }
+                    $cols[] = $c;
+                     
+                }
+                
+                if (!empty($cols)) {
+                    $selectAs[] = array($cols, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
+                }
+                
+            } else {
+                $selectAs[] = array($keys, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
+            }
+              
+            foreach($keys as $k) {
+                $ret['cols'][sprintf('%s_%s', $ocl, $k)] = $tab.'.'.$k;
+                $ret['join_names'][sprintf('%s_%s', $ocl, $k)] = sprintf('join_%s_%s.%s',$ocl, $col, $k);
+            }
+             
+        }
+        
+        // fill in the select details..
+        $this->selectAdd(); 
+        
+        if ($has_distinct) {
+            $this->selectAdd($has_distinct);
+        }
+       
+        foreach($selectAs as $ar) {            
+            $this->selectAs($ar[0], $ar[1], $ar[2]);
+        }
+        // restore links..
+        $this->links( $pre_links );
+        
+        return $ret;
+        
+    }
+    
+    /**
+     * Factory method for calling DB_DataObject_Cast
+     *
+     * if used with 1 argument DB_DataObject_Cast::sql($value) is called
+     * 
+     * if used with 2 arguments DB_DataObject_Cast::$value($callvalue) is called
+     * valid first arguments are: blob, string, date, sql
+     * 
+     * eg. $member->updated = $member->sqlValue('NOW()');
+     * 
+     * 
+     * might handle more arguments for escaping later...
+     * 
+     *
+     * @param string $value (or type if used with 2 arguments)
+     * @param string $callvalue (optional) used with date/null etc..
+     */
+    
+    function sqlValue($value)
+    {
+        $method = 'sql';
+        if (func_num_args() == 2) {
+            $method = $value;
+            $value = func_get_arg(1);
+        }
+        require_once 'DB/DataObject/Cast.php';
+        return call_user_func(array('DB_DataObject_Cast', $method), $value);
+        
+    }
+    
+    
     /**
      * Copies items that are in the table definitions from an
      * array or object into the current object
@@ -3784,9 +4017,10 @@ class DB_DataObject extends DB_DataObject_Overload
         global $_DB_DATAOBJECT;
         $keys  = $this->keys();
         $items = $this->table();
+        
         if (!$items) {
             $this->raiseError(
-                "setFrom:Could not find table definition for {$this->__table}", 
+                "setFrom:Could not find table definition for {$this->tableName()}", 
                 DB_DATAOBJECT_ERROR_INVALIDCONFIG);
             return;
         }
@@ -3838,13 +4072,15 @@ class DB_DataObject extends DB_DataObject_Overload
                 }
                 continue;
             }
-            if (is_object($from[sprintf($format,$k)])) {
+            $val = $from[sprintf($format,$k)];
+            if (is_a($val, 'DB_DataObject_Cast')) {
+                $this->$k = $val;
                 continue;
             }
-            if (is_array($from[sprintf($format,$k)])) {
+            if (is_object($val) || is_array($val)) {
                 continue;
             }
-            $ret = $this->fromValue($k,$from[sprintf($format,$k)]);
+            $ret = $this->fromValue($k,$val);
             if ($ret !== true)  {
                 $overload_return[$k] = 'Not A Valid Value';
             }
@@ -3867,7 +4103,9 @@ class DB_DataObject extends DB_DataObject_Overload
      * will also return links converted to arrays.
      *
      * @param   string  sprintf format for array
-     * @param   bool    empty only return elemnts that have a value set.
+     * @param   bool||number    [true = elemnts that have a value set],
+     *                          [false = table + returned colums] ,
+     *                          [0 = returned columsn only]
      *
      * @access   public
      * @return   array of key => value for row
@@ -3876,34 +4114,40 @@ class DB_DataObject extends DB_DataObject_Overload
     function toArray($format = '%s', $hideEmpty = false) 
     {
         global $_DB_DATAOBJECT;
+        
+        // we use false to ignore sprintf.. (speed up..)
+        $format = $format == '%s' ? false : $format;
+        
         $ret = array();
         $rf = ($this->_resultFields !== false) ? $this->_resultFields : 
-                (isset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]) ? $_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid] : false);
+                (isset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]) ?
+                 $_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid] : false);
+        
         $ar = ($rf !== false) ?
-            array_merge($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid],$this->table()) :
+            (($hideEmpty === 0) ? $rf : array_merge($rf, $this->table())) :
             $this->table();
 
         foreach($ar as $k=>$v) {
              
             if (!isset($this->$k)) {
                 if (!$hideEmpty) {
-                    $ret[sprintf($format,$k)] = '';
+                    $ret[$format === false ? $k : sprintf($format,$k)] = '';
                 }
                 continue;
             }
             // call the overloaded getXXXX() method. - except getLink and getLinks
             if (method_exists($this,'get'.$k) && !in_array(strtolower($k),array('links','link'))) {
-                $ret[sprintf($format,$k)] = $this->{'get'.$k}();
+                $ret[$format === false ? $k : sprintf($format,$k)] = $this->{'get'.$k}();
                 continue;
             }
             // should this call toValue() ???
-            $ret[sprintf($format,$k)] = $this->$k;
+            $ret[$format === false ? $k : sprintf($format,$k)] = $this->$k;
         }
         if (!$this->_link_loaded) {
             return $ret;
         }
         foreach($this->_link_loaded as $k) {
-            $ret[sprintf($format,$k)] = $this->$k->toArray();
+            $ret[$format === false ? $k : sprintf($format,$k)] = $this->$k->toArray();
         
         }
         
@@ -4018,7 +4262,7 @@ class DB_DataObject extends DB_DataObject_Overload
      * @access public
      * @return object The DB connection
      */
-    function &getDatabaseConnection()
+    function getDatabaseConnection()
     {
         global $_DB_DATAOBJECT;
 
@@ -4041,7 +4285,7 @@ class DB_DataObject extends DB_DataObject_Overload
      * @return object The DB result object
      */
      
-    function &getDatabaseResult()
+    function getDatabaseResult()
     {
         global $_DB_DATAOBJECT;
         $this->_connect();
@@ -4173,7 +4417,7 @@ class DB_DataObject extends DB_DataObject_Overload
         $options = $_DB_DATAOBJECT['CONFIG'];
         $cols = $this->table();
         // dont know anything about this col..
-        if (!isset($cols[$col])) {
+        if (!isset($cols[$col]) || is_a($value, 'DB_DataObject_Cast')) {
             $this->$col = $value;
             return true;
         }
@@ -4391,7 +4635,7 @@ class DB_DataObject extends DB_DataObject_Overload
      * @access  public
      * @return  none
      */
-    function debugLevel($v = null)
+    static function debugLevel($v = null)
     {
         global $_DB_DATAOBJECT;
         if (empty($_DB_DATAOBJECT['CONFIG'])) {
@@ -4408,7 +4652,7 @@ class DB_DataObject extends DB_DataObject_Overload
     /**
      * Last Error that has occured
      * - use $this->_lastError or
-     * $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
+     * $last_error = PEAR::getStaticProperty('DB_DataObject','lastError');
      *
      * @access  public
      * @var     object PEAR_Error (or false)
@@ -4433,17 +4677,10 @@ class DB_DataObject extends DB_DataObject_Overload
         if ($behaviour == PEAR_ERROR_DIE && !empty($_DB_DATAOBJECT['CONFIG']['dont_die'])) {
             $behaviour = null;
         }
+        
         $error = &PEAR::getStaticProperty('DB_DataObject','lastError');
         
-        // this will never work totally with PHP's object model.
-        // as this is passed on static calls (like staticGet in our case)
-
-        if (isset($this) && is_object($this) && is_subclass_of($this,'db_dataobject')) {
-            $this->_lastError = $error;
-        }
-
-        $_DB_DATAOBJECT['LASTERROR'] = $error;
-
+      
         // no checks for production here?....... - we log  errors before we throw them.
         DB_DataObject::debug($message,'ERROR',1);
         
@@ -4452,10 +4689,19 @@ class DB_DataObject extends DB_DataObject_Overload
             $error = $message;
         } else {
             require_once 'DB/DataObject/Error.php';
-            $error = PEAR::raiseError($message, $type, $behaviour,
+            $dor = new PEAR();
+            $error = $dor->raiseError($message, $type, $behaviour,
                             $opts=null, $userinfo=null, 'DB_DataObject_Error'
                         );
         }
+        // this will never work totally with PHP's object model.
+        // as this is passed on static calls (like staticGet in our case)
+ 
+        $_DB_DATAOBJECT['LASTERROR'] = $error;
+        
+        if (isset($this) && is_object($this) && is_subclass_of($this,'db_dataobject')) {
+            $this->_lastError = $error;
+        }
    
         return $error;
     }
@@ -4505,7 +4751,13 @@ class DB_DataObject extends DB_DataObject_Overload
 
         if (is_array($this->_link_loaded)) {
             foreach ($this->_link_loaded as $do) {
-                $do->free();
+                if (
+                        !empty($this->{$do}) &&
+                        is_object($this->{$do}) &&
+                        method_exists($this->{$do}, 'free')
+                    ) {
+                    $this->{$do}->free();
+                }
             }
         }
 
@@ -4562,6 +4814,78 @@ class DB_DataObject extends DB_DataObject_Overload
     	
     }
     
+    /**
+     * (deprecated - use ::get / and your own caching method)
+     */
+    static function staticGet($class, $k, $v = null)
+    {
+        $lclass = strtolower($class);
+        global $_DB_DATAOBJECT;
+        if (empty($_DB_DATAOBJECT['CONFIG'])) {
+            DB_DataObject::_loadConfig();
+        }
+
+        
+
+        $key = "$k:$v";
+        if ($v === null) {
+            $key = $k;
+        }
+        if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
+            DB_DataObject::debug("$class $key","STATIC GET - TRY CACHE");
+        }
+        if (!empty($_DB_DATAOBJECT['CACHE'][$lclass][$key])) {
+            return $_DB_DATAOBJECT['CACHE'][$lclass][$key];
+        }
+        if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
+            DB_DataObject::debug("$class $key","STATIC GET - NOT IN CACHE");
+        }
+
+        $obj = DB_DataObject::factory(substr($class,strlen($_DB_DATAOBJECT['CONFIG']['class_prefix'])));
+        if (PEAR::isError($obj)) {
+            $dor = new DB_DataObject();
+            $dor->raiseError("could not autoload $class", DB_DATAOBJECT_ERROR_NOCLASS);
+            $r = false;
+            return $r;
+        }
+        
+        if (!isset($_DB_DATAOBJECT['CACHE'][$lclass])) {
+            $_DB_DATAOBJECT['CACHE'][$lclass] = array();
+        }
+        if (!$obj->get($k,$v)) {
+            $dor = new DB_DataObject();
+            $dor->raiseError("No Data return from get $k $v", DB_DATAOBJECT_ERROR_NODATA);
+            
+            $r = false;
+            return $r;
+        }
+        $_DB_DATAOBJECT['CACHE'][$lclass][$key] = $obj;
+        return $_DB_DATAOBJECT['CACHE'][$lclass][$key];
+    }
+    
+    /**
+     * autoload Class relating to a table
+     * (deprecited - use ::factory)
+     *
+     * @param  string  $table  table
+     * @access private
+     * @return string classname on Success
+     */
+    function staticAutoloadTable($table)
+    {
+        global $_DB_DATAOBJECT;
+        if (empty($_DB_DATAOBJECT['CONFIG'])) {
+            DB_DataObject::_loadConfig();
+        }
+        $p = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ?
+            $_DB_DATAOBJECT['CONFIG']['class_prefix'] : '';
+        $class = $p . preg_replace('/[^A-Z0-9]/i','_',ucfirst($table));
+        
+        $ce = substr(phpversion(),0,1) > 4 ? class_exists($class,false) : class_exists($class);
+        $class = $ce ? $class  : DB_DataObject::_autoloadClass($class);
+        return $class;
+    }
+    
     /* ---- LEGACY BC METHODS - NOT DOCUMENTED - See Documentation on New Methods. ---*/
     
     function _get_table() { return $this->table(); }