Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  /**
   3   * Active Record implementation. Superset of Zend Framework's.
   4   *
   5   * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
   6   *
   7   * @package ADOdb
   8   * @link https://adodb.org Project's web site and documentation
   9   * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  10   *
  11   * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  12   * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  13   * any later version. This means you can use it in proprietary products.
  14   * See the LICENSE.md file distributed with this source code for details.
  15   * @license BSD-3-Clause
  16   * @license LGPL-2.1-or-later
  17   *
  18   * @copyright 2000-2013 John Lim
  19   * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  20   */
  21  
  22  include_once (ADODB_DIR.'/adodb-lib.inc.php');
  23  
  24  global $_ADODB_ACTIVE_DBS;
  25  global $ADODB_ACTIVE_CACHESECS; // set to true to enable caching of metadata such as field info
  26  global $ACTIVE_RECORD_SAFETY; // set to false to disable safety checks
  27  global $ADODB_ACTIVE_DEFVALS; // use default values of table definition when creating new active record.
  28  
  29  // array of ADODB_Active_DB's, indexed by ADODB_Active_Record->_dbat
  30  $_ADODB_ACTIVE_DBS = array();
  31  $ACTIVE_RECORD_SAFETY = true;
  32  $ADODB_ACTIVE_DEFVALS = false;
  33  $ADODB_ACTIVE_CACHESECS = 0;
  34  
  35  class ADODB_Active_DB {
  36  	 var $db; // ADOConnection
  37  	 var $tables; // assoc array of ADODB_Active_Table objects, indexed by tablename
  38  }
  39  
  40  class ADODB_Active_Table {
  41  	 var $name; // table name
  42  	 var $flds; // assoc array of adofieldobjs, indexed by fieldname
  43  	 var $keys; // assoc array of primary keys, indexed by fieldname
  44  	 var $_created; // only used when stored as a cached file
  45  	 var $_belongsTo = array();
  46  	 var $_hasMany = array();
  47  }
  48  
  49  // $db = database connection
  50  // $index = name of index - can be associative, for an example see
  51  //    PHPLens Issue No: 17790
  52  // returns index into $_ADODB_ACTIVE_DBS
  53  function ADODB_SetDatabaseAdapter(&$db, $index=false)
  54  {
  55  	 global $_ADODB_ACTIVE_DBS;
  56  
  57  	 foreach($_ADODB_ACTIVE_DBS as $k => $d) {
  58  	 	 if($d->db === $db) {
  59  	 	 	 return $k;
  60  	 	 }
  61  	 }
  62  
  63  	 $obj = new ADODB_Active_DB();
  64  	 $obj->db = $db;
  65  	 $obj->tables = array();
  66  
  67  	 if ($index == false) {
  68  	 	 $index = sizeof($_ADODB_ACTIVE_DBS);
  69  	 }
  70  
  71  	 $_ADODB_ACTIVE_DBS[$index] = $obj;
  72  
  73  	 return sizeof($_ADODB_ACTIVE_DBS)-1;
  74  }
  75  
  76  
  77  class ADODB_Active_Record {
  78  	 static $_changeNames = true; // dynamically pluralize table names
  79  
  80  	 /** @var bool|string Allows override of global $ADODB_QUOTE_FIELDNAMES */
  81  	 public $_quoteNames;
  82  
  83  	 static $_foreignSuffix = '_id'; //
  84  	 var $_dbat; // associative index pointing to ADODB_Active_DB eg. $ADODB_Active_DBS[_dbat]
  85  	 var $_table; // tablename, if set in class definition then use it as table name
  86  	 var $_tableat; // associative index pointing to ADODB_Active_Table, eg $ADODB_Active_DBS[_dbat]->tables[$this->_tableat]
  87  	 var $_where; // where clause set in Load()
  88  	 var $_saved = false; // indicates whether data is already inserted.
  89  	 var $_lasterr = false; // last error message
  90  	 var $_original = false; // the original values loaded or inserted, refreshed on update
  91  
  92  	 var $foreignName; // CFR: class name when in a relationship
  93  
  94  	 var $lockMode = ' for update '; // you might want to change to
  95  
  96  	static function UseDefaultValues($bool=null)
  97  	 {
  98  	 global $ADODB_ACTIVE_DEFVALS;
  99  	 	 if (isset($bool)) {
 100  	 	 	 $ADODB_ACTIVE_DEFVALS = $bool;
 101  	 	 }
 102  	 	 return $ADODB_ACTIVE_DEFVALS;
 103  	 }
 104  
 105  	 // should be static
 106  	static function SetDatabaseAdapter(&$db, $index=false)
 107  	 {
 108  	 	 return ADODB_SetDatabaseAdapter($db, $index);
 109  	 }
 110  
 111  
 112  	public function __set($name, $value)
 113  	 {
 114  	 	 $name = str_replace(' ', '_', $name);
 115  	 	 $this->$name = $value;
 116  	 }
 117  
 118  	 // php5 constructor
 119  	function __construct($table = false, $pkeyarr=false, $db=false)
 120  	 {
 121  	 	 global $_ADODB_ACTIVE_DBS, $ADODB_QUOTE_FIELDNAMES;
 122  
 123  	 	 // Set the local override for field quoting, only if not defined yet
 124  	 	 if (!isset($this->_quoteNames)) {
 125  	 	 	 $this->_quoteNames = $ADODB_QUOTE_FIELDNAMES;
 126  	 	 }
 127  
 128  	 	 if ($db == false && is_object($pkeyarr)) {
 129  	 	 	 $db = $pkeyarr;
 130  	 	 	 $pkeyarr = false;
 131  	 	 }
 132  
 133  	 	 if (!$table) {
 134  	 	 	 if (!empty($this->_table)) {
 135  	 	 	 	 $table = $this->_table;
 136  	 	 	 }
 137  	 	 	 else $table = $this->_pluralize(get_class($this));
 138  	 	 }
 139  	 	 $this->foreignName = strtolower(get_class($this)); // CFR: default foreign name
 140  	 	 if ($db) {
 141  	 	 	 $this->_dbat = ADODB_Active_Record::SetDatabaseAdapter($db);
 142  	 	 } else if (!isset($this->_dbat)) {
 143  	 	 	 if (sizeof($_ADODB_ACTIVE_DBS) == 0) {
 144  	 	 	 	 $this->Error(
 145  	 	 	 	 	 "No database connection set; use ADOdb_Active_Record::SetDatabaseAdapter(\$db)",
 146  	 	 	 	 	 'ADODB_Active_Record::__constructor'
 147  	 	 	 	 );
 148  	 	 	 }
 149  	 	 	 end($_ADODB_ACTIVE_DBS);
 150  	 	 	 $this->_dbat = key($_ADODB_ACTIVE_DBS);
 151  	 	 }
 152  
 153  	 	 $this->_table = $table;
 154  	 	 $this->_tableat = $table; # reserved for setting the assoc value to a non-table name, eg. the sql string in future
 155  
 156  	 	 $this->UpdateActiveTable($pkeyarr);
 157  	 }
 158  
 159  	function __wakeup()
 160  	 {
 161    	 	 $class = get_class($this);
 162    	 	 new $class;
 163  	 }
 164  
 165  	function _pluralize($table)
 166  	 {
 167  	 	 if (!ADODB_Active_Record::$_changeNames) {
 168  	 	 	 return $table;
 169  	 	 }
 170  
 171  	 	 $ut = strtoupper($table);
 172  	 	 $len = strlen($table);
 173  	 	 $lastc = $ut[$len-1];
 174  	 	 $lastc2 = substr($ut,$len-2);
 175  	 	 switch ($lastc) {
 176  	 	 case 'S':
 177  	 	 	 return $table.'es';
 178  	 	 case 'Y':
 179  	 	 	 return substr($table,0,$len-1).'ies';
 180  	 	 case 'X':
 181  	 	 	 return $table.'es';
 182  	 	 case 'H':
 183  	 	 	 if ($lastc2 == 'CH' || $lastc2 == 'SH') {
 184  	 	 	 	 return $table.'es';
 185  	 	 	 }
 186  	 	 default:
 187  	 	 	 return $table.'s';
 188  	 	 }
 189  	 }
 190  
 191  	 // CFR Lamest singular inflector ever - @todo Make it real!
 192  	 // Note: There is an assumption here...and it is that the argument's length >= 4
 193  	function _singularize($tables)
 194  	 {
 195  
 196  	 	 if (!ADODB_Active_Record::$_changeNames) {
 197  	 	 	 return $table;
 198  	 	 }
 199  
 200  	 	 $ut = strtoupper($tables);
 201  	 	 $len = strlen($tables);
 202  	 	 if($ut[$len-1] != 'S') {
 203  	 	 	 return $tables; // I know...forget oxen
 204  	 	 }
 205  	 	 if($ut[$len-2] != 'E') {
 206  	 	 	 return substr($tables, 0, $len-1);
 207  	 	 }
 208  	 	 switch($ut[$len-3]) {
 209  	 	 	 case 'S':
 210  	 	 	 case 'X':
 211  	 	 	 	 return substr($tables, 0, $len-2);
 212  	 	 	 case 'I':
 213  	 	 	 	 return substr($tables, 0, $len-3) . 'y';
 214  	 	 	 case 'H';
 215  	 	 	 	 if($ut[$len-4] == 'C' || $ut[$len-4] == 'S') {
 216  	 	 	 	 	 return substr($tables, 0, $len-2);
 217  	 	 	 	 }
 218  	 	 	 default:
 219  	 	 	 	 return substr($tables, 0, $len-1); // ?
 220  	 	 }
 221  	 }
 222  
 223  	function hasMany($foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
 224  	 {
 225  	 	 $ar = new $foreignClass($foreignRef);
 226  	 	 $ar->foreignName = $foreignRef;
 227  	 	 $ar->UpdateActiveTable();
 228  	 	 $ar->foreignKey = ($foreignKey) ? $foreignKey : $foreignRef.ADODB_Active_Record::$_foreignSuffix;
 229  	 	 $table =& $this->TableInfo();
 230  	 	 $table->_hasMany[$foreignRef] = $ar;
 231  	 #	 $this->$foreignRef = $this->_hasMany[$foreignRef]; // WATCHME Removed assignment by ref. to please __get()
 232  	 }
 233  
 234  	 // use when you don't want ADOdb to auto-pluralize tablename
 235  	static function TableHasMany($table, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
 236  	 {
 237  	 	 $ar = new ADODB_Active_Record($table);
 238  	 	 $ar->hasMany($foreignRef, $foreignKey, $foreignClass);
 239  	 }
 240  
 241  	 // use when you don't want ADOdb to auto-pluralize tablename
 242  	static function TableKeyHasMany($table, $tablePKey, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
 243  	 {
 244  	 	 if (!is_array($tablePKey)) {
 245  	 	 	 $tablePKey = array($tablePKey);
 246  	 	 }
 247  	 	 $ar = new ADODB_Active_Record($table,$tablePKey);
 248  	 	 $ar->hasMany($foreignRef, $foreignKey, $foreignClass);
 249  	 }
 250  
 251  
 252  	 // use when you want ADOdb to auto-pluralize tablename for you. Note that the class must already be defined.
 253  	 // e.g. class Person will generate relationship for table Persons
 254  	static function ClassHasMany($parentclass, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
 255  	 {
 256  	 	 $ar = new $parentclass();
 257  	 	 $ar->hasMany($foreignRef, $foreignKey, $foreignClass);
 258  	 }
 259  
 260  
 261  	function belongsTo($foreignRef,$foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
 262  	 {
 263  	 	 global $inflector;
 264  
 265  	 	 $ar = new $parentClass($this->_pluralize($foreignRef));
 266  	 	 $ar->foreignName = $foreignRef;
 267  	 	 $ar->parentKey = $parentKey;
 268  	 	 $ar->UpdateActiveTable();
 269  	 	 $ar->foreignKey = ($foreignKey) ? $foreignKey : $foreignRef.ADODB_Active_Record::$_foreignSuffix;
 270  
 271  	 	 $table =& $this->TableInfo();
 272  	 	 $table->_belongsTo[$foreignRef] = $ar;
 273  	 #	 $this->$foreignRef = $this->_belongsTo[$foreignRef];
 274  	 }
 275  
 276  	static function ClassBelongsTo($class, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
 277  	 {
 278  	 	 $ar = new $class();
 279  	 	 $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass);
 280  	 }
 281  
 282  	static function TableBelongsTo($table, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
 283  	 {
 284  	 	 $ar = new ADODB_Active_Record($table);
 285  	 	 $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass);
 286  	 }
 287  
 288  	static function TableKeyBelongsTo($table, $tablePKey, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
 289  	 {
 290  	 	 if (!is_array($tablePKey)) {
 291  	 	 	 $tablePKey = array($tablePKey);
 292  	 	 }
 293  	 	 $ar = new ADODB_Active_Record($table, $tablePKey);
 294  	 	 $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass);
 295  	 }
 296  
 297  
 298  	 /**
 299  	  * __get Access properties - used for lazy loading
 300  	  *
 301  	  * @param mixed $name
 302  	  * @access protected
 303  	  * @return mixed
 304  	  */
 305  	 function __get($name)
 306  	 {
 307  	 	 return $this->LoadRelations($name, '', -1, -1);
 308  	 }
 309  
 310  	 /**
 311  	  * @param string $name
 312  	  * @param string $whereOrderBy : eg. ' AND field1 = value ORDER BY field2'
 313  	  * @param offset
 314  	  * @param limit
 315  	  * @return mixed
 316  	  */
 317  	function LoadRelations($name, $whereOrderBy='', $offset=-1,$limit=-1)
 318  	 {
 319  	 	 $extras = array();
 320  	 	 $table = $this->TableInfo();
 321  	 	 if ($limit >= 0) {
 322  	 	 	 $extras['limit'] = $limit;
 323  	 	 }
 324  	 	 if ($offset >= 0) {
 325  	 	 	 $extras['offset'] = $offset;
 326  	 	 }
 327  
 328  	 	 if (strlen($whereOrderBy)) {
 329  	 	 	 if (!preg_match('/^[ \n\r]*AND/i', $whereOrderBy)) {
 330  	 	 	 	 if (!preg_match('/^[ \n\r]*ORDER[ \n\r]/i', $whereOrderBy)) {
 331  	 	 	 	 	 $whereOrderBy = 'AND ' . $whereOrderBy;
 332  	 	 	 	 }
 333  	 	 	 }
 334  	 	 }
 335  
 336  	 	 if(!empty($table->_belongsTo[$name])) {
 337  	 	 	 $obj = $table->_belongsTo[$name];
 338  	 	 	 $columnName = $obj->foreignKey;
 339  	 	 	 if(empty($this->$columnName)) {
 340  	 	 	 	 $this->$name = null;
 341  	 	 	 }
 342  	 	 	 else {
 343  	 	 	 	 if ($obj->parentKey) {
 344  	 	 	 	 	 $key = $obj->parentKey;
 345  	 	 	 	 }
 346  	 	 	 	 else {
 347  	 	 	 	 	 $key = reset($table->keys);
 348  	 	 	 	 }
 349  
 350  	 	 	 	 $arrayOfOne = $obj->Find($key.'='.$this->$columnName.' '.$whereOrderBy,false,false,$extras);
 351  	 	 	 	 if ($arrayOfOne) {
 352  	 	 	 	 	 $this->$name = $arrayOfOne[0];
 353  	 	 	 	 	 return $arrayOfOne[0];
 354  	 	 	 	 }
 355  	 	 	 }
 356  	 	 }
 357  	 	 if(!empty($table->_hasMany[$name])) {
 358  	 	 	 $obj = $table->_hasMany[$name];
 359  	 	 	 $key = reset($table->keys);
 360  	 	 	 $id = @$this->$key;
 361  	 	 	 if (!is_numeric($id)) {
 362  	 	 	 	 $db = $this->DB();
 363  	 	 	 	 $id = $db->qstr($id);
 364  	 	 	 }
 365  	 	 	 $objs = $obj->Find($obj->foreignKey.'='.$id. ' '.$whereOrderBy,false,false,$extras);
 366  	 	 	 if (!$objs) {
 367  	 	 	 	 $objs = array();
 368  	 	 	 }
 369  	 	 	 $this->$name = $objs;
 370  	 	 	 return $objs;
 371  	 	 }
 372  
 373  	 	 return array();
 374  	 }
 375  	 //////////////////////////////////
 376  
 377  	 // update metadata
 378  	function UpdateActiveTable($pkeys=false,$forceUpdate=false)
 379  	 {
 380  	 global $_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS;
 381  	 global $ADODB_ACTIVE_DEFVALS,$ADODB_FETCH_MODE;
 382  
 383  	 	 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat];
 384  
 385  	 	 $table = $this->_table;
 386  	 	 $tables = $activedb->tables;
 387  	 	 $tableat = $this->_tableat;
 388  	 	 if (!$forceUpdate && !empty($tables[$tableat])) {
 389  
 390  	 	 	 $acttab = $tables[$tableat];
 391  	 	 	 foreach($acttab->flds as $name => $fld) {
 392  	 	 	 	 if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) {
 393  	 	 	 	 	 $this->$name = $fld->default_value;
 394  	 	 	 	 }
 395  	 	 	 	 else {
 396  	 	 	 	 	 $this->$name = null;
 397  	 	 	 	 }
 398  	 	 	 }
 399  	 	 	 return;
 400  	 	 }
 401  	 	 $db = $activedb->db;
 402  	 	 $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType . '_active_'. $table . '.cache';
 403  	 	 if (!$forceUpdate && $ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR && file_exists($fname)) {
 404  	 	 	 $fp = fopen($fname,'r');
 405  	 	 	 @flock($fp, LOCK_SH);
 406  	 	 	 $acttab = unserialize(fread($fp,100000));
 407  	 	 	 fclose($fp);
 408  	 	 	 if ($acttab->_created + $ADODB_ACTIVE_CACHESECS - (abs(rand()) % 16) > time()) {
 409  	 	 	 	 // abs(rand()) randomizes deletion, reducing contention to delete/refresh file
 410  	 	 	 	 // ideally, you should cache at least 32 secs
 411  
 412  	 	 	 	 foreach($acttab->flds as $name => $fld) {
 413  	 	 	 	 	 if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) {
 414  	 	 	 	 	 	 $this->$name = $fld->default_value;
 415  	 	 	 	 	 }
 416  	 	 	 	 	 else {
 417  	 	 	 	 	 	 $this->$name = null;
 418  	 	 	 	 	 }
 419  	 	 	 	 }
 420  
 421  	 	 	 	 $activedb->tables[$table] = $acttab;
 422  
 423  	 	 	 	 //if ($db->debug) ADOConnection::outp("Reading cached active record file: $fname");
 424  	 	 	   	 return;
 425  	 	 	 } else if ($db->debug) {
 426  	 	 	 	 ADOConnection::outp("Refreshing cached active record file: $fname");
 427  	 	 	 }
 428  	 	 }
 429  	 	 $activetab = new ADODB_Active_Table();
 430  	 	 $activetab->name = $table;
 431  
 432  	 	 $save = $ADODB_FETCH_MODE;
 433  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
 434  	 	 if ($db->fetchMode !== false) {
 435  	 	 	 $savem = $db->SetFetchMode(false);
 436  	 	 }
 437  
 438  	 	 $cols = $db->MetaColumns($table);
 439  
 440  	 	 if (isset($savem)) {
 441  	 	 	 $db->SetFetchMode($savem);
 442  	 	 }
 443  	 	 $ADODB_FETCH_MODE = $save;
 444  
 445  	 	 if (!$cols) {
 446  	 	 	 $this->Error("Invalid table name: $table",'UpdateActiveTable');
 447  	 	 	 return false;
 448  	 	 }
 449  	 	 $fld = reset($cols);
 450  	 	 if (!$pkeys) {
 451  	 	 	 if (isset($fld->primary_key)) {
 452  	 	 	 	 $pkeys = array();
 453  	 	 	 	 foreach($cols as $name => $fld) {
 454  	 	 	 	 	 if (!empty($fld->primary_key)) {
 455  	 	 	 	 	 	 $pkeys[] = $name;
 456  	 	 	 	 	 }
 457  	 	 	 	 }
 458  	 	 	 } else
 459  	 	 	 	 $pkeys = $this->GetPrimaryKeys($db, $table);
 460  	 	 }
 461  	 	 if (empty($pkeys)) {
 462  	 	 	 $this->Error("No primary key found for table $table",'UpdateActiveTable');
 463  	 	 	 return false;
 464  	 	 }
 465  
 466  	 	 $attr = array();
 467  	 	 $keys = array();
 468  
 469  	 	 switch (ADODB_ASSOC_CASE) {
 470  	 	 case ADODB_ASSOC_CASE_LOWER:
 471  	 	 	 foreach($cols as $name => $fldobj) {
 472  	 	 	 	 $name = strtolower($name);
 473  	 	 	 	 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) {
 474  	 	 	 	 	 $this->$name = $fldobj->default_value;
 475  	 	 	 	 }
 476  	 	 	 	 else {
 477  	 	 	 	 	 $this->$name = null;
 478  	 	 	 	 }
 479  	 	 	 	 $attr[$name] = $fldobj;
 480  	 	 	 }
 481  	 	 	 foreach($pkeys as $k => $name) {
 482  	 	 	 	 $keys[strtolower($name)] = strtolower($name);
 483  	 	 	 }
 484  	 	 	 break;
 485  
 486  	 	 case ADODB_ASSOC_CASE_UPPER:
 487  	 	 	 foreach($cols as $name => $fldobj) {
 488  	 	 	 	 $name = strtoupper($name);
 489  
 490  	 	 	 	 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) {
 491  	 	 	 	 	 $this->$name = $fldobj->default_value;
 492  	 	 	 	 }
 493  	 	 	 	 else {
 494  	 	 	 	 	 $this->$name = null;
 495  	 	 	 	 }
 496  	 	 	 	 $attr[$name] = $fldobj;
 497  	 	 	 }
 498  
 499  	 	 	 foreach($pkeys as $k => $name) {
 500  	 	 	 	 $keys[strtoupper($name)] = strtoupper($name);
 501  	 	 	 }
 502  	 	 	 break;
 503  	 	 default:
 504  	 	 	 foreach($cols as $name => $fldobj) {
 505  
 506  	 	 	 	 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) {
 507  	 	 	 	 	 $this->$name = $fldobj->default_value;
 508  	 	 	 	 }
 509  	 	 	 	 else {
 510  	 	 	 	 	 $this->$name = null;
 511  	 	 	 	 }
 512  	 	 	 	 $attr[$name] = $fldobj;
 513  	 	 	 }
 514  	 	 	 foreach($pkeys as $k => $name) {
 515  	 	 	 	 $keys[$name] = $cols[strtoupper($name)]->name;
 516  	 	 	 }
 517  	 	 	 break;
 518  	 	 }
 519  
 520  	 	 $activetab->keys = $keys;
 521  	 	 $activetab->flds = $attr;
 522  
 523  	 	 if ($ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR) {
 524  	 	 	 $activetab->_created = time();
 525  	 	 	 $s = serialize($activetab);
 526  	 	 	 if (!function_exists('adodb_write_file')) {
 527  	 	 	 	 include_once (ADODB_DIR.'/adodb-csvlib.inc.php');
 528  	 	 	 }
 529  	 	 	 adodb_write_file($fname,$s);
 530  	 	 }
 531  	 	 if (isset($activedb->tables[$table])) {
 532  	 	 	 $oldtab = $activedb->tables[$table];
 533  
 534  	 	 	 if ($oldtab) {
 535  	 	 	 	 $activetab->_belongsTo = $oldtab->_belongsTo;
 536  	 	 	 	 $activetab->_hasMany = $oldtab->_hasMany;
 537  	 	 	 }
 538  	 	 }
 539  	 	 $activedb->tables[$table] = $activetab;
 540  	 }
 541  
 542  	function GetPrimaryKeys(&$db, $table)
 543  	 {
 544  	 	 return $db->MetaPrimaryKeys($table);
 545  	 }
 546  
 547  	 // error handler for both PHP4+5.
 548  	function Error($err,$fn)
 549  	 {
 550  	 global $_ADODB_ACTIVE_DBS;
 551  
 552  	 	 $fn = get_class($this).'::'.$fn;
 553  	 	 $this->_lasterr = $fn.': '.$err;
 554  
 555  	 	 if ($this->_dbat < 0) {
 556  	 	 	 $db = false;
 557  	 	 }
 558  	 	 else {
 559  	 	 	 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat];
 560  	 	 	 $db = $activedb->db;
 561  	 	 }
 562  
 563  	 	 if (function_exists('adodb_throw')) {
 564  	 	 	 if (!$db) {
 565  	 	 	 	 adodb_throw('ADOdb_Active_Record', $fn, -1, $err, 0, 0, false);
 566  	 	 	 }
 567  	 	 	 else {
 568  	 	 	 	 adodb_throw($db->databaseType, $fn, -1, $err, 0, 0, $db);
 569  	 	 	 }
 570  	 	 } else {
 571  	 	 	 if (!$db || $db->debug) {
 572  	 	 	 	 ADOConnection::outp($this->_lasterr);
 573  	 	 	 }
 574  	 	 }
 575  
 576  	 }
 577  
 578  	 // return last error message
 579  	function ErrorMsg()
 580  	 {
 581  	 	 if (!function_exists('adodb_throw')) {
 582  	 	 	 if ($this->_dbat < 0) {
 583  	 	 	 	 $db = false;
 584  	 	 	 }
 585  	 	 	 else {
 586  	 	 	 	 $db = $this->DB();
 587  	 	 	 }
 588  
 589  	 	 	 // last error could be database error too
 590  	 	 	 if ($db && $db->ErrorMsg()) {
 591  	 	 	 	 return $db->ErrorMsg();
 592  	 	 	 }
 593  	 	 }
 594  	 	 return $this->_lasterr;
 595  	 }
 596  
 597  	function ErrorNo()
 598  	 {
 599  	 	 if ($this->_dbat < 0) {
 600  	 	 	 return -9999; // no database connection...
 601  	 	 }
 602  	 	 $db = $this->DB();
 603  
 604  	 	 return (int) $db->ErrorNo();
 605  	 }
 606  
 607  
 608  	 // retrieve ADOConnection from _ADODB_Active_DBs
 609  	 function DB()
 610  	 {
 611  	 global $_ADODB_ACTIVE_DBS;
 612  
 613  	 	 if ($this->_dbat < 0) {
 614  	 	 	 $false = false;
 615  	 	 	 $this->Error("No database connection set: use ADOdb_Active_Record::SetDatabaseAdaptor(\$db)", "DB");
 616  	 	 	 return $false;
 617  	 	 }
 618  	 	 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat];
 619  	 	 $db = $activedb->db;
 620  	 	 return $db;
 621  	 }
 622  
 623  	 // retrieve ADODB_Active_Table
 624  	 function &TableInfo()
 625  	 {
 626  	 global $_ADODB_ACTIVE_DBS;
 627  	 	 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat];
 628  	 	 $table = $activedb->tables[$this->_tableat];
 629  	 	 return $table;
 630  	 }
 631  
 632  
 633  	 // I have an ON INSERT trigger on a table that sets other columns in the table.
 634  	 // So, I find that for myTable, I want to reload an active record after saving it. -- Malcolm Cook
 635  	function Reload()
 636  	 {
 637  	 	 $db = $this->DB();
 638  	 	 if (!$db) {
 639  	 	 	 return false;
 640  	 	 }
 641  	 	 $table = $this->TableInfo();
 642  	 	 $where = $this->GenWhere($db, $table);
 643  	 	 return($this->Load($where));
 644  	 }
 645  
 646  
 647  	 // set a numeric array (using natural table field ordering) as object properties
 648  	function Set(&$row)
 649  	 {
 650  	 global $ACTIVE_RECORD_SAFETY;
 651  
 652  	 	 $db = $this->DB();
 653  
 654  	 	 if (!$row) {
 655  	 	 	 $this->_saved = false;
 656  	 	 	 return false;
 657  	 	 }
 658  
 659  	 	 $this->_saved = true;
 660  
 661  	 	 $table = $this->TableInfo();
 662  	 	 if ($ACTIVE_RECORD_SAFETY && sizeof($table->flds) != sizeof($row)) {
 663  	 	 	 # <AP>
 664  	 	 	 $bad_size = TRUE;
 665  	 	 	 if (sizeof($row) == 2 * sizeof($table->flds)) {
 666  	 	 	 	 // Only keep string keys
 667  	 	 	 	 $keys = array_filter(array_keys($row), 'is_string');
 668  	 	 	 	 if (sizeof($keys) == sizeof($table->flds)) {
 669  	 	 	 	 	 $bad_size = FALSE;
 670  	 	 	 	 }
 671  	 	 	 }
 672  	 	 	 if ($bad_size) {
 673  	 	 	 $this->Error("Table structure of $this->_table has changed","Load");
 674  	 	 	 return false;
 675  	 	 }
 676  	 	 	 # </AP>
 677  	 	 }
 678  	 	 else
 679  	 	 	 $keys = array_keys($row);
 680  
 681  	 	 # <AP>
 682  	 	 reset($keys);
 683  	 	 $this->_original = array();
 684  	 	 foreach($table->flds as $name=>$fld) {
 685  	 	 	 $value = $row[current($keys)];
 686  	 	 	 $this->$name = $value;
 687  	 	 	 $this->_original[] = $value;
 688  	 	 	 next($keys);
 689  	 	 }
 690  
 691  	 	 # </AP>
 692  	 	 return true;
 693  	 }
 694  
 695  	 // get last inserted id for INSERT
 696  	function LastInsertID(&$db,$fieldname)
 697  	 {
 698  	 	 if ($db->hasInsertID) {
 699  	 	 	 $val = $db->Insert_ID($this->_table,$fieldname);
 700  	 	 }
 701  	 	 else {
 702  	 	 	 $val = false;
 703  	 	 }
 704  
 705  	 	 if (is_null($val) || $val === false)
 706  	 	 {
 707  	 	 	 $SQL = sprintf("SELECT MAX(%s) FROM %s",
 708  	 	 	 	 	 	    $this->nameQuoter($db,$fieldname),
 709  	 	 	 	 	 	    $this->nameQuoter($db,$this->_table)
 710  	 	 	 	 	 	    );
 711  	 	 	 // this might not work reliably in multi-user environment
 712  	 	 	 return $db->GetOne($SQL);
 713  	 	 }
 714  	 	 return $val;
 715  	 }
 716  
 717  	 // quote data in where clause
 718  	function doquote(&$db, $val,$t)
 719  	 {
 720  	 	 switch($t) {
 721  	 	 case 'L':
 722  	 	 	 if (strpos($db->databaseType,'postgres') !== false) {
 723  	 	 	 	 return $db->qstr($val);
 724  	 	 	 }
 725  	 	 case 'D':
 726  	 	 case 'T':
 727  	 	 	 if (empty($val)) {
 728  	 	 	 	 return 'null';
 729  	 	 	 }
 730  	 	 case 'B':
 731  	 	 case 'N':
 732  	 	 case 'C':
 733  	 	 case 'X':
 734  	 	 	 if (is_null($val)) {
 735  	 	 	 	 return 'null';
 736  	 	 	 }
 737  
 738  	 	 	 if (strlen($val)>0 &&
 739  	 	 	 	 (strncmp($val,"'",1) != 0 || substr($val,strlen($val)-1,1) != "'")
 740  	 	 	 ) {
 741  	 	 	 	 return $db->qstr($val);
 742  	 	 	 	 break;
 743  	 	 	 }
 744  	 	 default:
 745  	 	 	 return $val;
 746  	 	 	 break;
 747  	 	 }
 748  	 }
 749  
 750  	 // generate where clause for an UPDATE/SELECT
 751  	function GenWhere(&$db, &$table)
 752  	 {
 753  	 	 $keys = $table->keys;
 754  	 	 $parr = array();
 755  
 756  	 	 foreach($keys as $k) {
 757  	 	 	 $f = $table->flds[$k];
 758  	 	 	 if ($f) {
 759  	 	 	 	 $columnName = $this->nameQuoter($db,$k);
 760  	 	 	 	 $parr[] = $columnName.' = '.$this->doquote($db,$this->$k,$db->MetaType($f->type));
 761  	 	 	 }
 762  	 	 }
 763  	 	 return implode(' AND ', $parr);
 764  	 }
 765  
 766  
 767  	function _QName($n,$db=false)
 768  	 {
 769  	 	 if (!ADODB_Active_Record::$_quoteNames) {
 770  	 	 	 return $n;
 771  	 	 }
 772  	 	 if (!$db) {
 773  	 	 	 $db = $this->DB();
 774  	 	 	 if (!$db) {
 775  	 	 	 	 return false;
 776  	 	 	 }
 777  	 	 }
 778  	 	 return $db->nameQuote.$n.$db->nameQuote;
 779  	 }
 780  
 781  	 //------------------------------------------------------------ Public functions below
 782  
 783  	function Load($where=null,$bindarr=false, $lock = false)
 784  	 {
 785  	 	 global $ADODB_FETCH_MODE;
 786  
 787  	 	 $db = $this->DB();
 788  	 	 if (!$db) {
 789  	 	 	 return false;
 790  	 	 }
 791  	 	 $this->_where = $where;
 792  
 793  	 	 $save = $ADODB_FETCH_MODE;
 794  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 795  	 	 if ($db->fetchMode !== false) {
 796  	 	 	 $savem = $db->SetFetchMode(false);
 797  	 	 }
 798  
 799  	 	 $qry = sprintf("SELECT * FROM %s",
 800  	 	 	 	 	    $this->nameQuoter($db,$this->_table)
 801  	 	 	 	 	    );
 802  
 803  	 	 if($where) {
 804  	 	 	 $qry .= ' WHERE '.$where;
 805  	 	 }
 806  	 	 if ($lock) {
 807  	 	 	 $qry .= $this->lockMode;
 808  	 	 }
 809  
 810  	 	 $row = $db->GetRow($qry,$bindarr);
 811  
 812  	 	 if (isset($savem)) {
 813  	 	 	 $db->SetFetchMode($savem);
 814  	 	 }
 815  	 	 $ADODB_FETCH_MODE = $save;
 816  
 817  	 	 return $this->Set($row);
 818  	 }
 819  
 820  	function LoadLocked($where=null, $bindarr=false)
 821  	 {
 822  	 	 $this->Load($where,$bindarr,true);
 823  	 }
 824  
 825  	 # useful for multiple record inserts
 826  	 # see PHPLens Issue No: 17795
 827  	function Reset()
 828  	 {
 829  	 	 $this->_where=null;
 830  	 	 $this->_saved = false;
 831  	 	 $this->_lasterr = false;
 832  	 	 $this->_original = false;
 833  	 	 $vars=get_object_vars($this);
 834  	 	 foreach($vars as $k=>$v){
 835  	 	 	 if(substr($k,0,1)!=='_'){
 836  	 	 	 	 $this->{$k}=null;
 837  	 	 	 }
 838  	 	 }
 839  	 	 $this->foreignName=strtolower(get_class($this));
 840  	 	 return true;
 841  	 }
 842  
 843  	 // false on error
 844  	function Save()
 845  	 {
 846  	 	 if ($this->_saved) {
 847  	 	 	 $ok = $this->Update();
 848  	 	 }
 849  	 	 else {
 850  	 	 	 $ok = $this->Insert();
 851  	 	 }
 852  
 853  	 	 return $ok;
 854  	 }
 855  
 856  
 857  	 // false on error
 858  	function Insert()
 859  	 {
 860  	 	 $db = $this->DB();
 861  	 	 if (!$db) {
 862  	 	 	 return false;
 863  	 	 }
 864  	 	 $cnt = 0;
 865  	 	 $table = $this->TableInfo();
 866  
 867  	 	 $valarr = array();
 868  	 	 $names = array();
 869  	 	 $valstr = array();
 870  
 871  	 	 foreach($table->flds as $name=>$fld) {
 872  	 	 	 $val = $this->$name;
 873  	 	 	 if(!is_array($val) || !is_null($val) || !array_key_exists($name, $table->keys)) {
 874  	 	 	 	 $valarr[] = $val;
 875  	 	 	 	 $names[] = $this->nameQuoter($db,$name);
 876  	 	 	 	 $valstr[] = $db->Param($cnt);
 877  	 	 	 	 $cnt += 1;
 878  	 	 	 }
 879  	 	 }
 880  
 881  	 	 if (empty($names)){
 882  	 	 	 foreach($table->flds as $name=>$fld) {
 883  	 	 	 	 $valarr[] = null;
 884  	 	 	 	 $names[] = $this->nameQuoter($db,$name);
 885  	 	 	 	 $valstr[] = $db->Param($cnt);
 886  	 	 	 	 $cnt += 1;
 887  	 	 	 }
 888  	 	 }
 889  
 890  	 	 $tableName = $this->nameQuoter($db,$this->_table);
 891  	 	 $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
 892  	 	 	 	 	    $tableName,
 893  	 	 	 	 	    implode(',',$names),
 894  	 	 	 	 	    implode(',',$valstr)
 895  	 	 	 	 	    );
 896  	 	 $ok = $db->Execute($sql,$valarr);
 897  
 898  	 	 if ($ok) {
 899  	 	 	 $this->_saved = true;
 900  	 	 	 $autoinc = false;
 901  	 	 	 foreach($table->keys as $k) {
 902  	 	 	 	 if (is_null($this->$k)) {
 903  	 	 	 	 	 $autoinc = true;
 904  	 	 	 	 	 break;
 905  	 	 	 	 }
 906  	 	 	 }
 907  	 	 	 if ($autoinc && sizeof($table->keys) == 1) {
 908  	 	 	 	 $k = reset($table->keys);
 909  	 	 	 	 $this->$k = $this->LastInsertID($db,$k);
 910  	 	 	 }
 911  	 	 }
 912  
 913  	 	 $this->_original = $valarr;
 914  	 	 return !empty($ok);
 915  	 }
 916  
 917  	function Delete()
 918  	 {
 919  	 	 $db = $this->DB();
 920  	 	 if (!$db) {
 921  	 	 	 return false;
 922  	 	 }
 923  	 	 $table = $this->TableInfo();
 924  
 925  	 	 $where = $this->GenWhere($db,$table);
 926  
 927  	 	 $tableName = $this->nameQuoter($db,$this->_table);
 928  
 929  	 	 $sql = sprintf('DELETE FROM %s WHERE %s',
 930  	 	 	 	 	    $tableName,
 931  	 	 	 	 	    $where
 932  	 	 	 	 	    );
 933  
 934  	 	 $ok = $db->Execute($sql);
 935  
 936  	 	 return $ok ? true : false;
 937  	 }
 938  
 939  	 // returns an array of active record objects
 940  	function Find($whereOrderBy,$bindarr=false,$pkeysArr=false,$extra=array())
 941  	 {
 942  	 	 $db = $this->DB();
 943  	 	 if (!$db || empty($this->_table)) {
 944  	 	 	 return false;
 945  	 	 }
 946  	 	 $arr = $db->GetActiveRecordsClass(get_class($this),$this->_table, $whereOrderBy,$bindarr,$pkeysArr,$extra);
 947  	 	 return $arr;
 948  	 }
 949  
 950  	 // returns 0 on error, 1 on update, 2 on insert
 951  	function Replace()
 952  	 {
 953  	 	 $db = $this->DB();
 954  	 	 if (!$db) {
 955  	 	 	 return false;
 956  	 	 }
 957  	 	 $table = $this->TableInfo();
 958  
 959  	 	 $pkey = $table->keys;
 960  
 961  	 	 foreach($table->flds as $name=>$fld) {
 962  	 	 	 $val = $this->$name;
 963  	 	 	 /*
 964  	 	 	 if (is_null($val)) {
 965  	 	 	 	 if (isset($fld->not_null) && $fld->not_null) {
 966  	 	 	 	 	 if (isset($fld->default_value) && strlen($fld->default_value)) {
 967  	 	 	 	 	 	 continue;
 968  	 	 	 	 	 }
 969  	 	 	 	 	 else {
 970  	 	 	 	 	 	 $this->Error("Cannot update null into $name","Replace");
 971  	 	 	 	 	 	 return false;
 972  	 	 	 	 	 }
 973  	 	 	 	 }
 974  	 	 	 }*/
 975  	 	 	 if (is_null($val) && !empty($fld->auto_increment)) {
 976  	 	 	 	 continue;
 977  	 	 	 }
 978  
 979  	 	 	 if (is_array($val)) {
 980  	 	 	 	 continue;
 981  	 	 	 }
 982  
 983  	 	 	 $t = $db->MetaType($fld->type);
 984  	 	 	 $arr[$name] = $this->doquote($db,$val,$t);
 985  	 	 	 $valarr[] = $val;
 986  	 	 }
 987  
 988  	 	 if (!is_array($pkey)) {
 989  	 	 	 $pkey = array($pkey);
 990  	 	 }
 991  
 992  	 	 switch (ADODB_ASSOC_CASE) {
 993  	 	 	 case ADODB_ASSOC_CASE_LOWER:
 994  	 	 	 	 foreach ($pkey as $k => $v) {
 995  	 	 	 	 	 $pkey[$k] = strtolower($v);
 996  	 	 	 	 }
 997  	 	 	 	 break;
 998  	 	 	 case ADODB_ASSOC_CASE_UPPER:
 999  	 	 	 	 foreach ($pkey as $k => $v) {
1000  	 	 	 	 	 $pkey[$k] = strtoupper($v);
1001  	 	 	 	 }
1002  	 	 	 	 break;
1003  	 	 }
1004  
1005  	 	 $newArr = array();
1006  	 	 foreach($arr as $k=>$v)
1007  	 	 	 $newArr[$this->nameQuoter($db,$k)] = $v;
1008  	 	 $arr = $newArr;
1009  
1010  	 	 $newPkey = array();
1011  	 	 foreach($pkey as $k=>$v)
1012  	 	 	 $newPkey[$k] = $this->nameQuoter($db,$v);
1013  	 	 $pkey = $newPkey;
1014  
1015  	 	 $tableName = $this->nameQuoter($db,$this->_table);
1016  
1017  	 	 $ok = $db->Replace($tableName,$arr,$pkey);
1018  	 	 if ($ok) {
1019  	 	 	 $this->_saved = true; // 1= update 2=insert
1020  	 	 	 if ($ok == 2) {
1021  	 	 	 	 $autoinc = false;
1022  	 	 	 	 foreach($table->keys as $k) {
1023  	 	 	 	 	 if (is_null($this->$k)) {
1024  	 	 	 	 	 	 $autoinc = true;
1025  	 	 	 	 	 	 break;
1026  	 	 	 	 	 }
1027  	 	 	 	 }
1028  	 	 	 	 if ($autoinc && sizeof($table->keys) == 1) {
1029  	 	 	 	 	 $k = reset($table->keys);
1030  	 	 	 	 	 $this->$k = $this->LastInsertID($db,$k);
1031  	 	 	 	 }
1032  	 	 	 }
1033  
1034  	 	 	 $this->_original = $valarr;
1035  	 	 }
1036  	 	 return $ok;
1037  	 }
1038  
1039  	 // returns 0 on error, 1 on update, -1 if no change in data (no update)
1040  	function Update()
1041  	 {
1042  	 	 $db = $this->DB();
1043  	 	 if (!$db) {
1044  	 	 	 return false;
1045  	 	 }
1046  	 	 $table = $this->TableInfo();
1047  
1048  	 	 $where = $this->GenWhere($db, $table);
1049  
1050  	 	 if (!$where) {
1051  	 	 	 $this->error("Where missing for table $table", "Update");
1052  	 	 	 return false;
1053  	 	 }
1054  	 	 $valarr = array();
1055  	 	 $neworig = array();
1056  	 	 $pairs = array();
1057  	 	 $i = -1;
1058  	 	 $cnt = 0;
1059  	 	 foreach($table->flds as $name=>$fld) {
1060  	 	 	 $i += 1;
1061  	 	 	 $val = $this->$name;
1062  	 	 	 $neworig[] = $val;
1063  
1064  	 	 	 if (isset($table->keys[$name]) || is_array($val)) {
1065  	 	 	 	 continue;
1066  	 	 	 }
1067  
1068  	 	 	 if (is_null($val)) {
1069  	 	 	 	 if (isset($fld->not_null) && $fld->not_null) {
1070  	 	 	 	 	 if (isset($fld->default_value) && strlen($fld->default_value)) {
1071  	 	 	 	 	 	 continue;
1072  	 	 	 	 	 }
1073  	 	 	 	 	 else {
1074  	 	 	 	 	 	 $this->Error("Cannot set field $name to NULL","Update");
1075  	 	 	 	 	 	 return false;
1076  	 	 	 	 	 }
1077  	 	 	 	 }
1078  	 	 	 }
1079  
1080  	 	 	 if (isset($this->_original[$i]) && strcmp($val,$this->_original[$i]) == 0) {
1081  	 	 	 	 continue;
1082  	 	 	 }
1083  
1084  	 	 	 if (is_null($this->_original[$i]) && is_null($val)) {
1085  	 	 	 	 continue;
1086  	 	 	 }
1087  
1088  	 	 	 $valarr[] = $val;
1089  	 	 	 $pairs[] = $this->nameQuoter($db,$name).'='.$db->Param($cnt);
1090  	 	 	 $cnt += 1;
1091  	 	 }
1092  
1093  
1094  	 	 if (!$cnt) {
1095  	 	 	 return -1;
1096  	 	 }
1097  
1098  	 	 $tableName = $this->nameQuoter($db,$this->_table);
1099  
1100  	 	 $sql = sprintf('UPDATE %s SET %s WHERE %s',
1101  	 	 	 	 	    $tableName,
1102  	 	 	 	 	    implode(',',$pairs),
1103  	 	 	 	 	    $where);
1104  
1105  	 	 $ok = $db->Execute($sql,$valarr);
1106  	 	 if ($ok) {
1107  	 	 	 $this->_original = $neworig;
1108  	 	 	 return 1;
1109  	 	 }
1110  	 	 return 0;
1111  	 }
1112  
1113  	function GetAttributeNames()
1114  	 {
1115  	 	 $table = $this->TableInfo();
1116  	 	 if (!$table) {
1117  	 	 	 return false;
1118  	 	 }
1119  	 	 return array_keys($table->flds);
1120  	 }
1121  
1122  	 /**
1123  	  * Quotes the table, column and field names.
1124  	  *
1125  	  * This honours the internal {@see $_quoteNames} property, which overrides
1126  	  * the global $ADODB_QUOTE_FIELDNAMES directive.
1127  	  *
1128  	  * @param ADOConnection $db   The database connection
1129  	  * @param string        $name The table or column name to quote
1130  	  *
1131  	  * @return string The quoted name
1132  	  */
1133  	private function nameQuoter($db, $name)
1134  	 {
1135  	 	 global $ADODB_QUOTE_FIELDNAMES;
1136  
1137  	 	 $save = $ADODB_QUOTE_FIELDNAMES;
1138  	 	 $ADODB_QUOTE_FIELDNAMES = $this->_quoteNames;
1139  
1140  	 	 $string = _adodb_quote_fieldname($db, $name);
1141  
1142  	 	 $ADODB_QUOTE_FIELDNAMES = $save;
1143  
1144  	 	 return $string;
1145  	 }
1146  
1147  };
1148  
1149  function adodb_GetActiveRecordsClass(&$db, $class, $table,$whereOrderBy,$bindarr, $primkeyArr,
1150  	 	 	 $extra)
1151  {
1152  global $_ADODB_ACTIVE_DBS;
1153  
1154  
1155  	 $save = $db->SetFetchMode(ADODB_FETCH_NUM);
1156  
1157  	 $qry = "select * from ".$table;
1158  
1159  	 if (!empty($whereOrderBy)) {
1160  	 	 $qry .= ' WHERE '.$whereOrderBy;
1161  	 }
1162  	 if(isset($extra['limit'])) {
1163  	 	 $rows = false;
1164  	 	 if(isset($extra['offset'])) {
1165  	 	 	 $rs = $db->SelectLimit($qry, $extra['limit'], $extra['offset'],$bindarr);
1166  	 	 } else {
1167  	 	 	 $rs = $db->SelectLimit($qry, $extra['limit'],-1,$bindarr);
1168  	 	 }
1169  	 	 if ($rs) {
1170  	 	 	 while (!$rs->EOF) {
1171  	 	 	 	 $rows[] = $rs->fields;
1172  	 	 	 	 $rs->MoveNext();
1173  	 	 	 }
1174  	 	 }
1175  	 } else
1176  	 	 $rows = $db->GetAll($qry,$bindarr);
1177  
1178  	 $db->SetFetchMode($save);
1179  
1180  	 $false = false;
1181  
1182  	 if ($rows === false) {
1183  	 	 return $false;
1184  	 }
1185  
1186  
1187  	 if (!class_exists($class)) {
1188  	 	 $db->outp_throw("Unknown class $class in GetActiveRecordsClass()",'GetActiveRecordsClass');
1189  	 	 return $false;
1190  	 }
1191  	 $arr = array();
1192  	 // arrRef will be the structure that knows about our objects.
1193  	 // It is an associative array.
1194  	 // We will, however, return arr, preserving regular 0.. order so that
1195  	 // obj[0] can be used by app developers.
1196  	 $arrRef = array();
1197  	 $bTos = array(); // Will store belongTo's indices if any
1198  	 foreach($rows as $row) {
1199  
1200  	 	 $obj = new $class($table,$primkeyArr,$db);
1201  	 	 if ($obj->ErrorNo()){
1202  	 	 	 $db->_errorMsg = $obj->ErrorMsg();
1203  	 	 	 return $false;
1204  	 	 }
1205  	 	 $obj->Set($row);
1206  	 	 $arr[] = $obj;
1207  	 } // foreach($rows as $row)
1208  
1209  	 return $arr;
1210  }