📄 adodb-active-record.inc.php
字号:
function &TableInfo() { global $_ADODB_ACTIVE_DBS; $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; $table =& $activedb->tables[$this->_tableat]; return $table; } // set a numeric array (using natural table field ordering) as object properties function Set(&$row) { global $ACTIVE_RECORD_SAFETY; $db =& $this->DB(); if (!$row) { $this->_saved = false; return false; } $this->_saved = true; $table =& $this->TableInfo(); if ($ACTIVE_RECORD_SAFETY && sizeof($table->flds) != sizeof($row)) { $this->Error("Table structure of $this->_table has changed","Load"); return false; } $cnt = 0; foreach($table->flds as $name=>$fld) { $this->$name = $row[$cnt]; $cnt += 1; } $this->_original = $row; return true; } // get last inserted id for INSERT function LastInsertID(&$db,$fieldname) { if ($db->hasInsertID) $val = $db->Insert_ID($this->_table,$fieldname); else $val = false; if (is_null($val) || $val === false) { // this might not work reliably in multi-user environment return $db->GetOne("select max(".$fieldname.") from ".$this->_table); } return $val; } // quote data in where clause function doquote(&$db, $val,$t) { switch($t) { case 'D': case 'T': if (empty($val)) return 'null'; case 'C': case 'X': if (is_null($val)) return 'null'; if (strncmp($val,"'",1) != 0 && substr($val,strlen($val)-1,1) != "'") { return $db->qstr($val); break; } default: return $val; break; } } // generate where clause for an UPDATE/SELECT function GenWhere(&$db, &$table) { $keys = $table->keys; $parr = array(); foreach($keys as $k) { $f = $table->flds[$k]; if ($f) { $parr[] = $k.' = '.$this->doquote($db,$this->$k,$db->MetaType($f->type)); } } return implode(' and ', $parr); } //------------------------------------------------------------ Public functions below function Load($where,$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); $db->SetFetchMode($save); return $this->Set($row); } // false on error function Save() { if ($this->_saved) $ok = $this->Update(); else $ok = $this->Insert(); return $ok; } // false on error function Insert() { $db =& $this->DB(); if (!$db) return false; $cnt = 0; $table =& $this->TableInfo(); $valarr = array(); $names = array(); $valstr = array(); foreach($table->flds as $name=>$fld) { $val = $this->$name; if(!is_null($val) || !array_key_exists($name, $table->keys)) { $valarr[] = $val; $names[] = $name; $valstr[] = $db->Param($cnt); $cnt += 1; } } if (empty($names)){ foreach($table->flds as $name=>$fld) { $valarr[] = null; $names[] = $name; $valstr[] = $db->Param($cnt); $cnt += 1; } } $sql = 'INSERT INTO '.$this->_table."(".implode(',',$names).') VALUES ('.implode(',',$valstr).')'; $ok = $db->Execute($sql,$valarr); if ($ok) { $this->_saved = true; $autoinc = false; foreach($table->keys as $k) { if (is_null($this->$k)) { $autoinc = true; break; } } if ($autoinc && sizeof($table->keys) == 1) { $k = reset($table->keys); $this->$k = $this->LastInsertID($db,$k); } } $this->_original = $valarr; return !empty($ok); } function Delete() { $db =& $this->DB(); if (!$db) return false; $table =& $this->TableInfo(); $where = $this->GenWhere($db,$table); $sql = 'DELETE FROM '.$this->_table.' WHERE '.$where; $ok = $db->Execute($sql); return $ok ? true : false; } // returns an array of active record objects function &Find($whereOrderBy,$bindarr=false,$pkeysArr=false) { $db =& $this->DB(); if (!$db || empty($this->_table)) return false; $arr =& $db->GetActiveRecordsClass(get_class($this),$this->_table, $whereOrderBy,$bindarr,$pkeysArr); return $arr; } // returns 0 on error, 1 on update, 2 on insert function Replace() { global $ADODB_ASSOC_CASE; $db =& $this->DB(); if (!$db) return false; $table =& $this->TableInfo(); $pkey = $table->keys; foreach($table->flds as $name=>$fld) { $val = $this->$name; /* if (is_null($val)) { if (isset($fld->not_null) && $fld->not_null) { if (isset($fld->default_value) && strlen($fld->default_value)) continue; else { $this->Error("Cannot update null into $name","Replace"); return false; } } }*/ if (is_null($val) && !empty($fld->auto_increment)) { continue; } $t = $db->MetaType($fld->type); $arr[$name] = $this->doquote($db,$val,$t); $valarr[] = $val; } if (!is_array($pkey)) $pkey = array($pkey); if ($ADODB_ASSOC_CASE == 0) foreach($pkey as $k => $v) $pkey[$k] = strtolower($v); elseif ($ADODB_ASSOC_CASE == 0) foreach($pkey as $k => $v) $pkey[$k] = strtoupper($v); $ok = $db->Replace($this->_table,$arr,$pkey); if ($ok) { $this->_saved = true; // 1= update 2=insert if ($ok == 2) { $autoinc = false; foreach($table->keys as $k) { if (is_null($this->$k)) { $autoinc = true; break; } } if ($autoinc && sizeof($table->keys) == 1) { $k = reset($table->keys); $this->$k = $this->LastInsertID($db,$k); } } $this->_original =& $valarr; } return $ok; } // returns 0 on error, 1 on update, -1 if no change in data (no update) function Update() { $db =& $this->DB(); if (!$db) return false; $table =& $this->TableInfo(); $where = $this->GenWhere($db, $table); if (!$where) { $this->error("Where missing for table $table", "Update"); return false; } $valarr = array(); $neworig = array(); $pairs = array(); $i = -1; $cnt = 0; foreach($table->flds as $name=>$fld) { $i += 1; $val = $this->$name; $neworig[] = $val; if (isset($table->keys[$name])) { continue; } if (is_null($val)) { if (isset($fld->not_null) && $fld->not_null) { if (isset($fld->default_value) && strlen($fld->default_value)) continue; else { $this->Error("Cannot set field $name to NULL","Update"); return false; } } } if (isset($this->_original[$i]) && $val == $this->_original[$i]) { continue; } $valarr[] = $val; $pairs[] = $name.'='.$db->Param($cnt); $cnt += 1; } if (!$cnt) return -1; $sql = 'UPDATE '.$this->_table." SET ".implode(",",$pairs)." WHERE ".$where; $ok = $db->Execute($sql,$valarr); if ($ok) { $this->_original =& $neworig; return 1; } return 0; } function GetAttributeNames() { $table =& $this->TableInfo(); if (!$table) return false; return array_keys($table->flds); } };?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -