123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- <?php
- require_once 'DB.php';
- class DB_storage extends PEAR
- {
-
-
- public $_table = null;
-
- public $_keycolumn = null;
-
- public $_dbh = null;
-
- public $_properties = array();
-
- public $_changes = array();
-
- public $_readonly = false;
-
- public $_validator = null;
-
-
-
- public function __construct($table, $keycolumn, &$dbh, $validator = null)
- {
- $this->PEAR('DB_Error');
- $this->_table = $table;
- $this->_keycolumn = $keycolumn;
- $this->_dbh = $dbh;
- $this->_readonly = false;
- $this->_validator = $validator;
- }
-
-
-
- public function _makeWhere($keyval = null)
- {
- if (is_array($this->_keycolumn)) {
- if ($keyval === null) {
- for ($i = 0; $i < sizeof($this->_keycolumn); $i++) {
- $keyval[] = $this->{$this->_keycolumn[$i]};
- }
- }
- $whereclause = '';
- for ($i = 0; $i < sizeof($this->_keycolumn); $i++) {
- if ($i > 0) {
- $whereclause .= ' AND ';
- }
- $whereclause .= $this->_keycolumn[$i];
- if (is_null($keyval[$i])) {
-
-
- $whereclause .= ' IS NULL';
- } else {
- $whereclause .= ' = ' . $this->_dbh->quote($keyval[$i]);
- }
- }
- } else {
- if ($keyval === null) {
- $keyval = @$this->{$this->_keycolumn};
- }
- $whereclause = $this->_keycolumn;
- if (is_null($keyval)) {
-
-
- $whereclause .= ' IS NULL';
- } else {
- $whereclause .= ' = ' . $this->_dbh->quote($keyval);
- }
- }
- return $whereclause;
- }
-
-
-
- public function setup($keyval)
- {
- $whereclause = $this->_makeWhere($keyval);
- $query = 'SELECT * FROM ' . $this->_table . ' WHERE ' . $whereclause;
- $sth = $this->_dbh->query($query);
- if (DB::isError($sth)) {
- return $sth;
- }
- $row = $sth->fetchRow(DB_FETCHMODE_ASSOC);
- if (DB::isError($row)) {
- return $row;
- }
- if (!$row) {
- return $this->raiseError(
- null,
- DB_ERROR_NOT_FOUND,
- null,
- null,
- $query,
- null,
- true
- );
- }
- foreach ($row as $key => $value) {
- $this->_properties[$key] = true;
- $this->$key = $value;
- }
- return DB_OK;
- }
-
-
-
- public function insert($newpk)
- {
- if (is_array($this->_keycolumn)) {
- $primarykey = $this->_keycolumn;
- } else {
- $primarykey = array($this->_keycolumn);
- }
- settype($newpk, "array");
- for ($i = 0; $i < sizeof($primarykey); $i++) {
- $pkvals[] = $this->_dbh->quote($newpk[$i]);
- }
- $sth = $this->_dbh->query("INSERT INTO $this->_table (" .
- implode(",", $primarykey) . ") VALUES(" .
- implode(",", $pkvals) . ")");
- if (DB::isError($sth)) {
- return $sth;
- }
- if (sizeof($newpk) == 1) {
- $newpk = $newpk[0];
- }
- $this->setup($newpk);
- }
-
-
-
- public function toString()
- {
- $info = strtolower(get_class($this));
- $info .= " (table=";
- $info .= $this->_table;
- $info .= ", keycolumn=";
- if (is_array($this->_keycolumn)) {
- $info .= "(" . implode(",", $this->_keycolumn) . ")";
- } else {
- $info .= $this->_keycolumn;
- }
- $info .= ", dbh=";
- if (is_object($this->_dbh)) {
- $info .= $this->_dbh->toString();
- } else {
- $info .= "null";
- }
- $info .= ")";
- if (sizeof($this->_properties)) {
- $info .= " [loaded, key=";
- $keyname = $this->_keycolumn;
- if (is_array($keyname)) {
- $info .= "(";
- for ($i = 0; $i < sizeof($keyname); $i++) {
- if ($i > 0) {
- $info .= ",";
- }
- $info .= $this->$keyname[$i];
- }
- $info .= ")";
- } else {
- $info .= $this->$keyname;
- }
- $info .= "]";
- }
- if (sizeof($this->_changes)) {
- $info .= " [modified]";
- }
- return $info;
- }
-
-
-
- public function dump()
- {
- foreach ($this->_properties as $prop => $foo) {
- print "$prop = ";
- print htmlentities($this->$prop);
- print "<br />\n";
- }
- }
-
-
-
- public function &create($table, &$data)
- {
- $classname = strtolower(get_class($this));
- $obj = new $classname($table);
- foreach ($data as $name => $value) {
- $obj->_properties[$name] = true;
- $obj->$name = &$value;
- }
- return $obj;
- }
-
-
-
-
-
-
-
-
- public function set($property, $newvalue)
- {
-
-
- if ($this->_readonly) {
- return $this->raiseError(
- null,
- DB_WARNING_READ_ONLY,
- null,
- null,
- null,
- null,
- true
- );
- }
- if (@isset($this->_properties[$property])) {
- if (empty($this->_validator)) {
- $valid = true;
- } else {
- $valid = @call_user_func(
- $this->_validator,
- $this->_table,
- $property,
- $newvalue,
- $this->$property,
- $this
- );
- }
- if ($valid) {
- $this->$property = $newvalue;
- if (empty($this->_changes[$property])) {
- $this->_changes[$property] = 0;
- } else {
- $this->_changes[$property]++;
- }
- } else {
- return $this->raiseError(
- null,
- DB_ERROR_INVALID,
- null,
- null,
- "invalid field: $property",
- null,
- true
- );
- }
- return true;
- }
- return $this->raiseError(
- null,
- DB_ERROR_NOSUCHFIELD,
- null,
- null,
- "unknown field: $property",
- null,
- true
- );
- }
-
-
-
- public function &get($property)
- {
-
- if (isset($this->_properties[$property])) {
- return $this->$property;
- }
- $tmp = null;
- return $tmp;
- }
-
-
-
- public function _DB_storage()
- {
- if (sizeof($this->_changes)) {
- $this->store();
- }
- $this->_properties = array();
- $this->_changes = array();
- $this->_table = null;
- }
-
-
-
- public function store()
- {
- $params = array();
- $vars = array();
- foreach ($this->_changes as $name => $foo) {
- $params[] = &$this->$name;
- $vars[] = $name . ' = ?';
- }
- if ($vars) {
- $query = 'UPDATE ' . $this->_table . ' SET ' .
- implode(', ', $vars) . ' WHERE ' .
- $this->_makeWhere();
- $stmt = $this->_dbh->prepare($query);
- $res = $this->_dbh->execute($stmt, $params);
- if (DB::isError($res)) {
- return $res;
- }
- $this->_changes = array();
- }
- return DB_OK;
- }
-
-
-
- public function remove()
- {
- if ($this->_readonly) {
- return $this->raiseError(
- null,
- DB_WARNING_READ_ONLY,
- null,
- null,
- null,
- null,
- true
- );
- }
- $query = 'DELETE FROM ' . $this->_table .' WHERE '.
- $this->_makeWhere();
- $res = $this->_dbh->query($query);
- if (DB::isError($res)) {
- return $res;
- }
- foreach ($this->_properties as $prop => $foo) {
- unset($this->$prop);
- }
- $this->_properties = array();
- $this->_changes = array();
- return DB_OK;
- }
-
- }
|