Upgrade to v26 (#172)
* A bunch of new updates from icehrm pro * Push changes to frontend
This commit is contained in:
@@ -3,16 +3,16 @@
|
||||
|
||||
@version V5.11 5 May 2010 (c) 2000-2010 John Lim (jlim#natsoft.com). All rights reserved.
|
||||
Latest version is available at http://adodb.sourceforge.net
|
||||
|
||||
Released under both BSD license and Lesser GPL library license.
|
||||
Whenever there is any discrepancy between the two licenses,
|
||||
|
||||
Released under both BSD license and Lesser GPL library license.
|
||||
Whenever there is any discrepancy between the two licenses,
|
||||
the BSD license will take precedence.
|
||||
|
||||
|
||||
Active Record implementation. Superset of Zend Framework's.
|
||||
|
||||
|
||||
Version 0.92
|
||||
|
||||
See http://www-128.ibm.com/developerworks/java/library/j-cb03076/?ca=dgr-lnxw01ActiveRecord
|
||||
|
||||
See http://www-128.ibm.com/developerworks/java/library/j-cb03076/?ca=dgr-lnxw01ActiveRecord
|
||||
for info on Ruby on Rails Active Record implementation
|
||||
*/
|
||||
|
||||
@@ -44,7 +44,7 @@ class ADODB_Active_Table {
|
||||
|
||||
// $db = database connection
|
||||
// $index = name of index - can be associative, for an example see
|
||||
// http://phplens.com/lens/lensforum/msgs.php?id=17790
|
||||
// http://phplens.com/lens/lensforum/msgs.php?id=17790
|
||||
// returns index into $_ADODB_ACTIVE_DBS
|
||||
function ADODB_SetDatabaseAdapter(&$db, $index=false)
|
||||
{
|
||||
@@ -58,23 +58,23 @@ function ADODB_SetDatabaseAdapter(&$db, $index=false)
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if ($d->db->_connectionID === $db->_connectionID && $db->database == $d->db->database) {
|
||||
if ($d->db->_connectionID === $db->_connectionID && $db->database == $d->db->database) {
|
||||
$obj = $d;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($index == false) $index = sizeof($_ADODB_ACTIVE_DBS);
|
||||
|
||||
|
||||
if(!isset($obj)) {
|
||||
$obj = new ADODB_Active_DB();
|
||||
$obj->db = $db;
|
||||
$obj->tables = array();
|
||||
$obj->tables = array();
|
||||
}
|
||||
|
||||
|
||||
$_ADODB_ACTIVE_DBS[$index] = $obj;
|
||||
|
||||
|
||||
return $index;
|
||||
}
|
||||
|
||||
@@ -82,8 +82,8 @@ function ADODB_SetDatabaseAdapter(&$db, $index=false)
|
||||
class ADODB_Active_Record {
|
||||
static $_changeNames = true; // dynamically pluralize table names
|
||||
static $_quoteNames = false;
|
||||
|
||||
static $_foreignSuffix = '_id'; //
|
||||
|
||||
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]
|
||||
@@ -103,7 +103,7 @@ class ADODB_Active_Record {
|
||||
}
|
||||
|
||||
// should be static
|
||||
static function SetDatabaseAdapter(&$db, $index=false)
|
||||
static function SetDatabaseAdapter(&$db, $index=false)
|
||||
{
|
||||
//error_log("Coming into ".self::_pluralize(get_called_class())."'s SetDatabaseAdapter where ".get_class());
|
||||
if(!$index || !isset($index)) {
|
||||
@@ -111,19 +111,19 @@ class ADODB_Active_Record {
|
||||
}
|
||||
return ADODB_SetDatabaseAdapter($db, $index);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$name = str_replace(' ', '_', $name);
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
|
||||
// php5 constructor
|
||||
function __construct($table = false, $pkeyarr=false, $db=false)
|
||||
{
|
||||
global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS;
|
||||
|
||||
|
||||
if ($db == false && is_object($pkeyarr)) {
|
||||
$db = $pkeyarr;
|
||||
$pkeyarr = false;
|
||||
@@ -143,9 +143,9 @@ class ADODB_Active_Record {
|
||||
if(isset($_ADODB_ACTIVE_DBS[self::_pluralize(get_called_class())])) {
|
||||
$this->_dbat = self::_pluralize(get_called_class());
|
||||
} else {
|
||||
$this->_dbat = key($_ADODB_ACTIVE_DBS);
|
||||
$this->_dbat = key($_ADODB_ACTIVE_DBS);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
$this->_table = $table;
|
||||
@@ -153,13 +153,13 @@ class ADODB_Active_Record {
|
||||
|
||||
$this->UpdateActiveTable($pkeyarr);
|
||||
}
|
||||
|
||||
|
||||
function __wakeup()
|
||||
{
|
||||
$class = get_class($this);
|
||||
new $class;
|
||||
}
|
||||
|
||||
|
||||
static function _pluralize($table)
|
||||
{
|
||||
if (!ADODB_Active_Record::$_changeNames) return $table;
|
||||
@@ -170,26 +170,26 @@ class ADODB_Active_Record {
|
||||
$lastc2 = substr($ut,$len-2);
|
||||
switch ($lastc) {
|
||||
case 'S':
|
||||
return $table.'es';
|
||||
return $table.'es';
|
||||
case 'Y':
|
||||
return substr($table,0,$len-1).'ies';
|
||||
case 'X':
|
||||
case 'X':
|
||||
return $table.'es';
|
||||
case 'H':
|
||||
case 'H':
|
||||
if ($lastc2 == 'CH' || $lastc2 == 'SH')
|
||||
return $table.'es';
|
||||
default:
|
||||
return $table.'s';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// CFR Lamest singular inflector ever - @todo Make it real!
|
||||
// Note: There is an assumption here...and it is that the argument's length >= 4
|
||||
function _singularize($tables)
|
||||
{
|
||||
|
||||
|
||||
if (!ADODB_Active_Record::$_changeNames) return $table;
|
||||
|
||||
|
||||
$ut = strtoupper($tables);
|
||||
$len = strlen($tables);
|
||||
if($ut[$len-1] != 'S')
|
||||
@@ -221,14 +221,14 @@ class ADODB_Active_Record {
|
||||
$table->_hasMany[$foreignRef] = $ar;
|
||||
# $this->$foreignRef = $this->_hasMany[$foreignRef]; // WATCHME Removed assignment by ref. to please __get()
|
||||
}
|
||||
|
||||
|
||||
// use when you don't want ADOdb to auto-pluralize tablename
|
||||
static function TableHasMany($table, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
|
||||
{
|
||||
$ar = new ADODB_Active_Record($table);
|
||||
$ar->hasMany($foreignRef, $foreignKey, $foreignClass);
|
||||
}
|
||||
|
||||
|
||||
// use when you don't want ADOdb to auto-pluralize tablename
|
||||
static function TableKeyHasMany($table, $tablePKey, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
|
||||
{
|
||||
@@ -236,8 +236,8 @@ class ADODB_Active_Record {
|
||||
$ar = new ADODB_Active_Record($table,$tablePKey);
|
||||
$ar->hasMany($foreignRef, $foreignKey, $foreignClass);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// use when you want ADOdb to auto-pluralize tablename for you. Note that the class must already be defined.
|
||||
// e.g. class Person will generate relationship for table Persons
|
||||
static function ClassHasMany($parentclass, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
|
||||
@@ -245,7 +245,7 @@ class ADODB_Active_Record {
|
||||
$ar = new $parentclass();
|
||||
$ar->hasMany($foreignRef, $foreignKey, $foreignClass);
|
||||
}
|
||||
|
||||
|
||||
|
||||
function belongsTo($foreignRef,$foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
|
||||
{
|
||||
@@ -256,24 +256,24 @@ class ADODB_Active_Record {
|
||||
$ar->parentKey = $parentKey;
|
||||
$ar->UpdateActiveTable();
|
||||
$ar->foreignKey = ($foreignKey) ? $foreignKey : $foreignRef.ADODB_Active_Record::$_foreignSuffix;
|
||||
|
||||
|
||||
$table =& $this->TableInfo();
|
||||
$table->_belongsTo[$foreignRef] = $ar;
|
||||
# $this->$foreignRef = $this->_belongsTo[$foreignRef];
|
||||
}
|
||||
|
||||
|
||||
static function ClassBelongsTo($class, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
|
||||
{
|
||||
$ar = new $class();
|
||||
$ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass);
|
||||
}
|
||||
|
||||
|
||||
static function TableBelongsTo($table, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
|
||||
{
|
||||
$ar = new ADOdb_Active_Record($table);
|
||||
$ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass);
|
||||
}
|
||||
|
||||
|
||||
static function TableKeyBelongsTo($table, $tablePKey, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
|
||||
{
|
||||
if (!is_array($tablePKey)) $tablePKey = array($tablePKey);
|
||||
@@ -284,8 +284,8 @@ class ADODB_Active_Record {
|
||||
|
||||
/**
|
||||
* __get Access properties - used for lazy loading
|
||||
*
|
||||
* @param mixed $name
|
||||
*
|
||||
* @param mixed $name
|
||||
* @access protected
|
||||
* @return mixed
|
||||
*/
|
||||
@@ -293,9 +293,9 @@ class ADODB_Active_Record {
|
||||
{
|
||||
return $this->LoadRelations($name, '', -1, -1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $name
|
||||
* @param string $whereOrderBy : eg. ' AND field1 = value ORDER BY field2'
|
||||
* @param offset
|
||||
* @param limit
|
||||
@@ -307,12 +307,12 @@ class ADODB_Active_Record {
|
||||
$table = $this->TableInfo();
|
||||
if ($limit >= 0) $extras['limit'] = $limit;
|
||||
if ($offset >= 0) $extras['offset'] = $offset;
|
||||
|
||||
if (strlen($whereOrderBy))
|
||||
|
||||
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];
|
||||
@@ -323,7 +323,7 @@ class ADODB_Active_Record {
|
||||
{
|
||||
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];
|
||||
@@ -332,7 +332,7 @@ class ADODB_Active_Record {
|
||||
}
|
||||
}
|
||||
if(!empty($table->_hasMany[$name]))
|
||||
{
|
||||
{
|
||||
$obj = $table->_hasMany[$name];
|
||||
$key = reset($table->keys);
|
||||
$id = @$this->$key;
|
||||
@@ -345,11 +345,11 @@ class ADODB_Active_Record {
|
||||
$this->$name = $objs;
|
||||
return $objs;
|
||||
}
|
||||
|
||||
|
||||
return array();
|
||||
}
|
||||
//////////////////////////////////
|
||||
|
||||
|
||||
// update metadata
|
||||
function UpdateActiveTable($pkeys=false,$forceUpdate=false)
|
||||
{
|
||||
@@ -365,7 +365,7 @@ class ADODB_Active_Record {
|
||||
|
||||
$acttab = $tables[$tableat];
|
||||
foreach($acttab->flds as $name => $fld) {
|
||||
if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value))
|
||||
if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value))
|
||||
$this->$name = $fld->default_value;
|
||||
else
|
||||
$this->$name = null;
|
||||
@@ -379,19 +379,19 @@ class ADODB_Active_Record {
|
||||
@flock($fp, LOCK_SH);
|
||||
$acttab = unserialize(fread($fp,100000));
|
||||
fclose($fp);
|
||||
if ($acttab->_created + $ADODB_ACTIVE_CACHESECS - (abs(rand()) % 16) > time()) {
|
||||
if ($acttab->_created + $ADODB_ACTIVE_CACHESECS - (abs(rand()) % 16) > time()) {
|
||||
// abs(rand()) randomizes deletion, reducing contention to delete/refresh file
|
||||
// ideally, you should cache at least 32 secs
|
||||
|
||||
|
||||
foreach($acttab->flds as $name => $fld) {
|
||||
if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value))
|
||||
if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value))
|
||||
$this->$name = $fld->default_value;
|
||||
else
|
||||
$this->$name = null;
|
||||
}
|
||||
|
||||
|
||||
$activedb->tables[$table] = $acttab;
|
||||
|
||||
|
||||
//if ($db->debug) ADOConnection::outp("Reading cached active record file: $fname");
|
||||
return;
|
||||
} else if ($db->debug) {
|
||||
@@ -400,18 +400,18 @@ class ADODB_Active_Record {
|
||||
}
|
||||
$activetab = new ADODB_Active_Table();
|
||||
$activetab->name = $table;
|
||||
|
||||
|
||||
$save = $ADODB_FETCH_MODE;
|
||||
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
|
||||
if ($db->fetchMode !== false) $savem = $db->SetFetchMode(false);
|
||||
|
||||
|
||||
$cols = $db->MetaColumns($table);
|
||||
|
||||
|
||||
if (isset($savem)) $db->SetFetchMode($savem);
|
||||
$ADODB_FETCH_MODE = $save;
|
||||
|
||||
|
||||
if (!$cols) {
|
||||
$this->Error("Invalid table name: $table",'UpdateActiveTable');
|
||||
$this->Error("Invalid table name: $table",'UpdateActiveTable');
|
||||
return false;
|
||||
}
|
||||
$fld = reset($cols);
|
||||
@@ -421,14 +421,14 @@ class ADODB_Active_Record {
|
||||
foreach($cols as $name => $fld) {
|
||||
if (!empty($fld->primary_key)) $pkeys[] = $name;
|
||||
}
|
||||
} else
|
||||
} else
|
||||
$pkeys = $this->GetPrimaryKeys($db, $table);
|
||||
}
|
||||
if (empty($pkeys)) {
|
||||
$this->Error("No primary key found for table $table",'UpdateActiveTable');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
$attr = array();
|
||||
$keys = array();
|
||||
$ADODB_ASSOC_CASE = 2;
|
||||
@@ -446,18 +446,18 @@ class ADODB_Active_Record {
|
||||
$keys[strtolower($name)] = strtolower($name);
|
||||
}
|
||||
break;
|
||||
|
||||
case 1:
|
||||
|
||||
case 1:
|
||||
foreach($cols as $name => $fldobj) {
|
||||
$name = strtoupper($name);
|
||||
|
||||
|
||||
if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value))
|
||||
$this->$name = $fldobj->default_value;
|
||||
else
|
||||
$this->$name = null;
|
||||
$attr[$name] = $fldobj;
|
||||
}
|
||||
|
||||
|
||||
foreach($pkeys as $k => $name) {
|
||||
$keys[strtoupper($name)] = strtoupper($name);
|
||||
}
|
||||
@@ -465,7 +465,7 @@ class ADODB_Active_Record {
|
||||
default:
|
||||
foreach($cols as $name => $fldobj) {
|
||||
$name = ($fldobj->name);
|
||||
|
||||
|
||||
if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value))
|
||||
$this->$name = $fldobj->default_value;
|
||||
else
|
||||
@@ -477,7 +477,7 @@ class ADODB_Active_Record {
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
$activetab->keys = $keys;
|
||||
$activetab->flds = $attr;
|
||||
|
||||
@@ -489,58 +489,58 @@ class ADODB_Active_Record {
|
||||
}
|
||||
if (isset($activedb->tables[$table])) {
|
||||
$oldtab = $activedb->tables[$table];
|
||||
|
||||
|
||||
if ($oldtab) $activetab->_belongsTo = $oldtab->_belongsTo;
|
||||
if ($oldtab) $activetab->_hasMany = $oldtab->_hasMany;
|
||||
}
|
||||
$activedb->tables[$table] = $activetab;
|
||||
}
|
||||
|
||||
|
||||
function GetPrimaryKeys(&$db, $table)
|
||||
{
|
||||
return $db->MetaPrimaryKeys($table);
|
||||
}
|
||||
|
||||
// error handler for both PHP4+5.
|
||||
|
||||
// error handler for both PHP4+5.
|
||||
function Error($err,$fn)
|
||||
{
|
||||
global $_ADODB_ACTIVE_DBS;
|
||||
|
||||
|
||||
$fn = get_class($this).'::'.$fn;
|
||||
$this->_lasterr = $fn.': '.$err;
|
||||
|
||||
|
||||
if ($this->_dbat < 0) $db = false;
|
||||
else {
|
||||
$activedb = $_ADODB_ACTIVE_DBS[$this->_dbat];
|
||||
$db = $activedb->db;
|
||||
}
|
||||
|
||||
if (function_exists('adodb_throw')) {
|
||||
|
||||
if (function_exists('adodb_throw')) {
|
||||
if (!$db) adodb_throw('ADOdb_Active_Record', $fn, -1, $err, 0, 0, false);
|
||||
else adodb_throw($db->databaseType, $fn, -1, $err, 0, 0, $db);
|
||||
} else
|
||||
if (!$db || $db->debug) ADOConnection::outp($this->_lasterr);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// return last error message
|
||||
function ErrorMsg()
|
||||
{
|
||||
if (!function_exists('adodb_throw')) {
|
||||
if ($this->_dbat < 0) $db = false;
|
||||
else $db = $this->DB();
|
||||
|
||||
|
||||
// last error could be database error too
|
||||
if ($db && $db->ErrorMsg()) return $db->ErrorMsg();
|
||||
}
|
||||
return $this->_lasterr;
|
||||
}
|
||||
|
||||
function ErrorNo()
|
||||
|
||||
function ErrorNo()
|
||||
{
|
||||
if ($this->_dbat < 0) return -9999; // no database connection...
|
||||
$db = $this->DB();
|
||||
|
||||
|
||||
return (int) $db->ErrorNo();
|
||||
}
|
||||
|
||||
@@ -549,7 +549,7 @@ class ADODB_Active_Record {
|
||||
function DB()
|
||||
{
|
||||
global $_ADODB_ACTIVE_DBS;
|
||||
|
||||
|
||||
if ($this->_dbat < 0) {
|
||||
$false = false;
|
||||
$this->Error("No database connection set: use ADOdb_Active_Record::SetDatabaseAdaptor(\$db)", "DB");
|
||||
@@ -559,7 +559,7 @@ class ADODB_Active_Record {
|
||||
$db = $activedb->db;
|
||||
return $db;
|
||||
}
|
||||
|
||||
|
||||
// retrieve ADODB_Active_Table
|
||||
function &TableInfo()
|
||||
{
|
||||
@@ -568,8 +568,8 @@ class ADODB_Active_Record {
|
||||
$table = $activedb->tables[$this->_tableat];
|
||||
return $table;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// I have an ON INSERT trigger on a table that sets other columns in the table.
|
||||
// So, I find that for myTable, I want to reload an active record after saving it. -- Malcolm Cook
|
||||
function Reload()
|
||||
@@ -580,21 +580,21 @@ class ADODB_Active_Record {
|
||||
return($this->Load($where));
|
||||
}
|
||||
|
||||
|
||||
|
||||
// set a numeric array (using natural table field ordering) as object properties
|
||||
function Set(&$row)
|
||||
{
|
||||
global $ACTIVE_RECORD_SAFETY;
|
||||
|
||||
|
||||
$db = $this->DB();
|
||||
|
||||
|
||||
if (!$row) {
|
||||
$this->_saved = false;
|
||||
$this->_saved = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
$this->_saved = true;
|
||||
|
||||
|
||||
$table = $this->TableInfo();
|
||||
if ($ACTIVE_RECORD_SAFETY && sizeof($table->flds) != sizeof($row)) {
|
||||
# <AP>
|
||||
@@ -613,7 +613,7 @@ class ADODB_Active_Record {
|
||||
}
|
||||
else
|
||||
$keys = array_keys($row);
|
||||
|
||||
|
||||
# <AP>
|
||||
reset($keys);
|
||||
$this->_original = array();
|
||||
@@ -627,7 +627,7 @@ class ADODB_Active_Record {
|
||||
# </AP>
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// get last inserted id for INSERT
|
||||
function LastInsertID(&$db,$fieldname)
|
||||
{
|
||||
@@ -635,32 +635,32 @@ class ADODB_Active_Record {
|
||||
$val = $db->Insert_ID($this->_table,$fieldname);
|
||||
else
|
||||
$val = false;
|
||||
|
||||
|
||||
if (is_null($val) || $val === false) {
|
||||
// this might not work reliably in multi-user environment
|
||||
return $db->GetOne("select max(".$fieldname.") from ".$this->_table);
|
||||
}
|
||||
return $val;
|
||||
}
|
||||
|
||||
|
||||
// quote data in where clause
|
||||
function doquote(&$db, $val,$t)
|
||||
{
|
||||
switch($t) {
|
||||
case 'L':
|
||||
if (strpos($db->databaseType,'postgres') !== false) return $db->qstr($val);
|
||||
case 'D':
|
||||
case 'D':
|
||||
case 'T':
|
||||
if (empty($val)) return 'null';
|
||||
|
||||
case 'B':
|
||||
|
||||
case 'B':
|
||||
case 'N':
|
||||
case 'C':
|
||||
case 'X':
|
||||
if (is_null($val)) return 'null';
|
||||
|
||||
if (strlen($val)>1 &&
|
||||
(strncmp($val,"'",1) != 0 || substr($val,strlen($val)-1,1) != "'")) {
|
||||
|
||||
if (strlen($val)>1 &&
|
||||
(strncmp($val,"'",1) != 0 || substr($val,strlen($val)-1,1) != "'")) {
|
||||
return $db->qstr($val);
|
||||
break;
|
||||
}
|
||||
@@ -669,13 +669,13 @@ class ADODB_Active_Record {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// generate where clause for an UPDATE/SELECT
|
||||
function GenWhere(&$db, &$table)
|
||||
{
|
||||
$keys = $table->keys;
|
||||
$parr = array();
|
||||
|
||||
|
||||
foreach($keys as $k) {
|
||||
$f = $table->flds[$k];
|
||||
if ($f) {
|
||||
@@ -684,17 +684,17 @@ class ADODB_Active_Record {
|
||||
}
|
||||
return implode(' and ', $parr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
function _QName($n,$db=false)
|
||||
{
|
||||
if (!ADODB_Active_Record::$_quoteNames) return $n;
|
||||
if (!$db) $db = $this->DB(); if (!$db) return false;
|
||||
return $db->nameQuote.$n.$db->nameQuote;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------ Public functions below
|
||||
|
||||
|
||||
function Load($where=null,$bindarr=false)
|
||||
{
|
||||
$this->_where = $where;
|
||||
@@ -705,31 +705,31 @@ class ADODB_Active_Record {
|
||||
}
|
||||
return $this->LoadFromRawQuery($qry, $bindarr);
|
||||
}
|
||||
|
||||
|
||||
function LoadFromRawQuery($qry, $bindarr=false) {
|
||||
global $ADODB_FETCH_MODE;
|
||||
|
||||
|
||||
$db = $this->DB(); if (!$db) return false;
|
||||
|
||||
|
||||
$save = $ADODB_FETCH_MODE;
|
||||
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
|
||||
if ($db->fetchMode !== false) $savem = $db->SetFetchMode(false);
|
||||
|
||||
|
||||
$row = $db->GetRow($qry,$bindarr);
|
||||
|
||||
|
||||
if (isset($savem)) $db->SetFetchMode($savem);
|
||||
$ADODB_FETCH_MODE = $save;
|
||||
|
||||
|
||||
return $this->Set($row);
|
||||
}
|
||||
|
||||
|
||||
# useful for multiple record inserts
|
||||
# see http://phplens.com/lens/lensforum/msgs.php?id=17795
|
||||
function Reset()
|
||||
{
|
||||
$this->_where=null;
|
||||
$this->_saved = false;
|
||||
$this->_lasterr = false;
|
||||
$this->_saved = false;
|
||||
$this->_lasterr = false;
|
||||
$this->_original = false;
|
||||
$vars=get_object_vars($this);
|
||||
foreach($vars as $k=>$v){
|
||||
@@ -740,24 +740,24 @@ class ADODB_Active_Record {
|
||||
$this->foreignName=strtolower(get_class($this));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// false on error
|
||||
function Save()
|
||||
{
|
||||
if ($this->_saved) $ok = $this->Update();
|
||||
else $ok = $this->Insert();
|
||||
|
||||
|
||||
return $ok;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// false on error
|
||||
function Insert()
|
||||
{
|
||||
$db = $this->DB(); if (!$db) return false;
|
||||
$cnt = 0;
|
||||
$table = $this->TableInfo();
|
||||
|
||||
|
||||
$valarr = array();
|
||||
$names = array();
|
||||
$valstr = array();
|
||||
@@ -771,7 +771,7 @@ class ADODB_Active_Record {
|
||||
$cnt += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (empty($names)){
|
||||
foreach($table->flds as $name=>$fld) {
|
||||
$valarr[] = null;
|
||||
@@ -782,7 +782,7 @@ class ADODB_Active_Record {
|
||||
}
|
||||
$sql = 'INSERT INTO '.$this->_table."(".implode(',',$names).') VALUES ('.implode(',',$valstr).')';
|
||||
$ok = $db->Execute($sql,$valarr);
|
||||
|
||||
|
||||
if ($ok) {
|
||||
$this->_saved = true;
|
||||
$autoinc = false;
|
||||
@@ -797,32 +797,32 @@ class ADODB_Active_Record {
|
||||
$this->$k = $this->LastInsertID($db,$k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$this->_original = $valarr;
|
||||
return !empty($ok);
|
||||
}
|
||||
|
||||
|
||||
function Delete()
|
||||
{
|
||||
$db = $this->DB(); if (!$db) return false;
|
||||
$table = $this->TableInfo();
|
||||
|
||||
|
||||
$where = $this->GenWhere($db,$table);
|
||||
$sql = 'DELETE FROM '.$this->_table.' WHERE '.$where;
|
||||
$ok = $db->Execute($sql);
|
||||
|
||||
|
||||
return $ok ? true : false;
|
||||
}
|
||||
|
||||
|
||||
protected function intify($ret) {
|
||||
if(is_numeric($ret)) {
|
||||
return intval($ret);
|
||||
} else {
|
||||
return $ret;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function Aggregate($function, $column, $whereGroupBy, $bindarr=false) {
|
||||
if(!in_array($function, ADODB_Active_Record::$_supportedAggregateFunctions)) {
|
||||
throw new InvalidArgumentException("Unknown Aggregate Function $function");
|
||||
@@ -833,11 +833,11 @@ class ADODB_Active_Record {
|
||||
$db = $this->DB(); if (!$db || empty($this->_table)) return false;
|
||||
return $db->GetOne("select $function($column) from ".$this->_table." where ". $whereGroupBy, $bindarr);
|
||||
}
|
||||
|
||||
|
||||
function Count($whereGroupBy, $bindarr=false) {
|
||||
return $this->intify( $this->Aggregate("count", "*", $whereGroupBy, $bindarr) );
|
||||
}
|
||||
|
||||
|
||||
function CountDistinct($column, $whereGroupBy, $bindarr=false) {
|
||||
if(!in_array($column, $this->GetAttributeNames())) {
|
||||
throw new InvalidArgumentException("Unknown Column for CountDistinct $column");
|
||||
@@ -845,7 +845,7 @@ class ADODB_Active_Record {
|
||||
$db = $this->DB(); if (!$db || empty($this->_table)) return false;
|
||||
return $this->intify( $db->GetOne("select count(distinct($column)) from ".$this->_table." where ". $whereGroupBy, $bindarr) );
|
||||
}
|
||||
|
||||
|
||||
// returns an array of active record objects
|
||||
function Find($whereOrderBy,$bindarr=false,$pkeysArr=false,$extra=array())
|
||||
{
|
||||
@@ -853,7 +853,7 @@ class ADODB_Active_Record {
|
||||
$arr = $db->GetActiveRecordsClass(get_class($this),$this->_table, $whereOrderBy,$bindarr,$pkeysArr,$extra);
|
||||
return $arr;
|
||||
}
|
||||
|
||||
|
||||
// returns an array of active record objects
|
||||
function FindFromRawQuery($query,$bindarr=false,$pkeysArr=false,$extra=array())
|
||||
{
|
||||
@@ -861,17 +861,17 @@ class ADODB_Active_Record {
|
||||
$arr = $db->GetActiveRecordsClass2(get_class($this),$this->_table, $query,$bindarr,$pkeysArr,$extra);
|
||||
return $arr;
|
||||
}
|
||||
|
||||
|
||||
// returns 0 on error, 1 on update, 2 on insert
|
||||
function Replace()
|
||||
{
|
||||
global $ADODB_ASSOC_CASE;
|
||||
|
||||
|
||||
$db = $this->DB(); if (!$db) return false;
|
||||
$table = $this->TableInfo();
|
||||
|
||||
|
||||
$pkey = $table->keys;
|
||||
|
||||
|
||||
foreach($table->flds as $name=>$fld) {
|
||||
$val = $this->$name;
|
||||
/*
|
||||
@@ -887,24 +887,24 @@ class ADODB_Active_Record {
|
||||
if (is_null($val) && !empty($fld->auto_increment)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (is_array($val)) continue;
|
||||
|
||||
|
||||
$t = $db->MetaType($fld->type);
|
||||
$arr[$name] = $this->doquote($db,$val,$t);
|
||||
$valarr[] = $val;
|
||||
}
|
||||
|
||||
|
||||
if (!is_array($pkey)) $pkey = array($pkey);
|
||||
|
||||
|
||||
if ($ADODB_ASSOC_CASE == 0)
|
||||
|
||||
|
||||
if ($ADODB_ASSOC_CASE == 0)
|
||||
foreach($pkey as $k => $v)
|
||||
$pkey[$k] = strtolower($v);
|
||||
elseif ($ADODB_ASSOC_CASE == 1)
|
||||
elseif ($ADODB_ASSOC_CASE == 1)
|
||||
foreach($pkey as $k => $v)
|
||||
$pkey[$k] = strtoupper($v);
|
||||
|
||||
|
||||
$ok = $db->Replace($this->_table,$arr,$pkey);
|
||||
if ($ok) {
|
||||
$this->_saved = true; // 1= update 2=insert
|
||||
@@ -921,9 +921,9 @@ class ADODB_Active_Record {
|
||||
$this->$k = $this->LastInsertID($db,$k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$this->_original = $valarr;
|
||||
}
|
||||
}
|
||||
return $ok;
|
||||
}
|
||||
|
||||
@@ -932,14 +932,14 @@ class ADODB_Active_Record {
|
||||
{
|
||||
$db = $this->DB(); if (!$db) return false;
|
||||
$table = $this->TableInfo();
|
||||
|
||||
|
||||
$where = $this->GenWhere($db, $table);
|
||||
|
||||
|
||||
if (!$where) {
|
||||
$this->error("Where missing for table $table", "Update");
|
||||
return false;
|
||||
}
|
||||
$valarr = array();
|
||||
$valarr = array();
|
||||
$neworig = array();
|
||||
$pairs = array();
|
||||
$i = -1;
|
||||
@@ -948,10 +948,10 @@ class ADODB_Active_Record {
|
||||
$i += 1;
|
||||
$val = $this->$name;
|
||||
$neworig[] = $val;
|
||||
|
||||
if (isset($table->keys[$name]) || is_array($val))
|
||||
|
||||
if (isset($table->keys[$name]) || is_array($val))
|
||||
continue;
|
||||
|
||||
|
||||
if (is_null($val)) {
|
||||
if (isset($fld->not_null) && $fld->not_null) {
|
||||
if (isset($fld->default_value) && strlen($fld->default_value)) continue;
|
||||
@@ -969,8 +969,8 @@ class ADODB_Active_Record {
|
||||
$pairs[] = $this->_QName($name,$db).'='.$db->Param($cnt);
|
||||
$cnt += 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (!$cnt) return -1;
|
||||
$sql = 'UPDATE '.$this->_table." SET ".implode(",",$pairs)." WHERE ".$where;
|
||||
$ok = $db->Execute($sql,$valarr);
|
||||
@@ -980,21 +980,21 @@ class ADODB_Active_Record {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
function GetAttributeNames()
|
||||
{
|
||||
$table = $this->TableInfo();
|
||||
if (!$table) return false;
|
||||
return array_keys($table->flds);
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
function adodb_GetActiveRecordsClass(&$db, $class, $table,$whereOrderBy,$bindarr, $primkeyArr,
|
||||
$extra)
|
||||
{
|
||||
$qry = "select * from ".$table;
|
||||
|
||||
|
||||
if (!empty($whereOrderBy)) {
|
||||
$qry .= ' WHERE '.$whereOrderBy;
|
||||
}
|
||||
@@ -1006,7 +1006,7 @@ function adodb_GetActiveRecordsClass2(&$db, $class, $table, $qry, $bindarr, $pri
|
||||
{
|
||||
global $_ADODB_ACTIVE_DBS;
|
||||
|
||||
|
||||
|
||||
$save = $db->SetFetchMode(ADODB_FETCH_NUM);
|
||||
if(isset($extra['limit']))
|
||||
{
|
||||
@@ -1027,13 +1027,13 @@ function adodb_GetActiveRecordsClass2(&$db, $class, $table, $qry, $bindarr, $pri
|
||||
}
|
||||
|
||||
$db->SetFetchMode($save);
|
||||
|
||||
|
||||
$false = false;
|
||||
|
||||
if ($rows === false) {
|
||||
|
||||
if ($rows === false) {
|
||||
return $false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!class_exists($class)) {
|
||||
$db->outp_throw("Unknown class $class in GetActiveRecordsClass()",'GetActiveRecordsClass');
|
||||
@@ -1047,7 +1047,7 @@ function adodb_GetActiveRecordsClass2(&$db, $class, $table, $qry, $bindarr, $pri
|
||||
$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();
|
||||
@@ -1055,7 +1055,7 @@ function adodb_GetActiveRecordsClass2(&$db, $class, $table, $qry, $bindarr, $pri
|
||||
}
|
||||
$obj->Set($row);
|
||||
$arr[] = $obj;
|
||||
} // foreach($rows as $row)
|
||||
} // foreach($rows as $row)
|
||||
|
||||
return $arr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user