Skip to content

Commit

Permalink
Update adodb to V5.06a
Browse files Browse the repository at this point in the history
  • Loading branch information
mantis committed Dec 13, 2008
1 parent b03a6cf commit f3fd7b0
Show file tree
Hide file tree
Showing 109 changed files with 4,332 additions and 286 deletions.
218 changes: 210 additions & 8 deletions core/adodb/adodb-active-record.inc.php
@@ -1,7 +1,7 @@
<?php
/*
@version V5.05 11 July 2008 (c) 2000-2008 John Lim (jlim#natsoft.com). All rights reserved.
@version V5.06 16 Oct 2008 (c) 2000-2008 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.
Expand All @@ -10,12 +10,13 @@
Active Record implementation. Superset of Zend Framework's.
Version 0.08
Version 0.90
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
Expand All @@ -36,6 +37,8 @@ class ADODB_Active_Table {
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();
}

// returns index into $_ADODB_ACTIVE_DBS
Expand Down Expand Up @@ -63,6 +66,8 @@ function ADODB_SetDatabaseAdapter(&$db)


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 $_tableat; // associative index pointing to ADODB_Active_Table, eg $ADODB_Active_DBS[_dbat]->tables[$this->_tableat]
Expand All @@ -71,6 +76,8 @@ class ADODB_Active_Record {
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;
Expand Down Expand Up @@ -105,6 +112,7 @@ function __construct($table = false, $pkeyarr=false, $db=false)
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
Expand All @@ -115,6 +123,7 @@ function __construct($table = false, $pkeyarr=false, $db=false)

$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);
}

Expand All @@ -126,6 +135,8 @@ function __wakeup()

function _pluralize($table)
{
if (!ADODB_Active_Record::$_changeNames) return $table;

$ut = strtoupper($table);
$len = strlen($table);
$lastc = $ut[$len-1];
Expand All @@ -145,6 +156,120 @@ function _pluralize($table)
}
}

// 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)
{
$ar = new ADODB_Active_Record($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()
}

function belongsTo($foreignRef,$foreignKey=false, $parentKey='')
{
global $inflector;

$ar = new ADODB_Active_Record($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];
}

/**
* __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];
$objs = $obj->Find($obj->foreignKey.'='.$this->id. ' '.$whereOrderBy,false,false,$extras);
if (!$objs) $objs = array();
$this->$name = $objs;
return $objs;
}

return array();
}
//////////////////////////////////

// update metadata
Expand All @@ -169,7 +294,6 @@ function UpdateActiveTable($pkeys=false,$forceUpdate=false)
}
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)) {
Expand Down Expand Up @@ -270,6 +394,12 @@ function UpdateActiveTable($pkeys=false,$forceUpdate=false)
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;
}

Expand Down Expand Up @@ -338,7 +468,7 @@ function DB()
}

// retrieve ADODB_Active_Table
function TableInfo()
function &TableInfo()
{
global $_ADODB_ACTIVE_DBS;

Expand Down Expand Up @@ -391,6 +521,7 @@ function Set(&$row)
}
else
$keys = array_keys($row);

# <AP>
reset($keys);
$this->_original = array();
Expand All @@ -400,6 +531,7 @@ function Set(&$row)
$this->_original[] = $value;
next($keys);
}

# </AP>
return true;
}
Expand Down Expand Up @@ -427,6 +559,8 @@ function doquote(&$db, $val,$t)
case 'T':
if (empty($val)) return 'null';

case 'B':
case 'N':
case 'C':
case 'X':
if (is_null($val)) return 'null';
Expand Down Expand Up @@ -459,13 +593,20 @@ function GenWhere(&$db, &$table)

//------------------------------------------------------------ Public functions below

function Load($where,$bindarr=false)
function Load($where=null,$bindarr=false)
{
$db = $this->DB(); if (!$db) return false;
$this->_where = $where;

$save = $db->SetFetchMode(ADODB_FETCH_NUM);
$row = $db->GetRow("select * from ".$this->_table.' WHERE '.$where,$bindarr);
$qry = "select * from ".$this->_table;

if($where) {
$qry .= ' WHERE '.$where;
}
$row = $db->GetRow($qry,$bindarr);


$db->SetFetchMode($save);

return $this->Set($row);
Expand Down Expand Up @@ -544,10 +685,10 @@ function Delete()
}

// returns an array of active record objects
function Find($whereOrderBy,$bindarr=false,$pkeysArr=false)
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);
$arr = $db->GetActiveRecordsClass(get_class($this),$this->_table, $whereOrderBy,$bindarr,$pkeysArr,$extra);
return $arr;
}

Expand Down Expand Up @@ -677,4 +818,65 @@ function GetAttributeNames()

};

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']);
} else {
$rs = $db->SelectLimit($qry, $extra['limit']);
}
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;
}
?>

0 comments on commit f3fd7b0

Please sign in to comment.