123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
- abstract class Managed_DataObject extends Memcached_DataObject
- {
-
- public static function schemaDef()
- {
- throw new MethodNotImplementedException(__METHOD__);
- }
-
- static function getKV($k,$v=NULL)
- {
- return parent::getClassKV(get_called_class(), $k, $v);
- }
-
- static function pkeyGet(array $kv)
- {
- return parent::pkeyGetClass(get_called_class(), $kv);
- }
-
- static function multiGet($keyCol, array $keyVals, $skipNulls=true)
- {
- return parent::multiGetClass(get_called_class(), $keyCol, $keyVals, $skipNulls);
- }
-
- static function pivotGet($keyCol, array $keyVals, array $otherCols=array())
- {
- return parent::pivotGetClass(get_called_class(), $keyCol, $keyVals, $otherCols);
- }
-
- static function listFind($keyCol, array $keyVals)
- {
- return parent::listFindClass(get_called_class(), $keyCol, $keyVals);
- }
-
- static function listGet($keyCol, array $keyVals)
- {
- return parent::listGetClass(get_called_class(), $keyCol, $keyVals);
- }
-
- public function table()
- {
- $table = static::schemaDef();
- return array_map(array($this, 'columnBitmap'), $table['fields']);
- }
-
- function keys()
- {
- return array_keys($this->keyTypes());
- }
-
- function sequenceKey()
- {
- $table = static::schemaDef();
- foreach ($table['fields'] as $name => $column) {
- if ($column['type'] == 'serial') {
-
-
- return array($name, true, false);
- }
- }
-
- return array(false, false, false);
- }
-
- function keyTypes()
- {
- $table = static::schemaDef();
- $keys = array();
- if (!empty($table['unique keys'])) {
- foreach ($table['unique keys'] as $idx => $fields) {
- foreach ($fields as $name) {
- $keys[$name] = 'U';
- }
- }
- }
- if (!empty($table['primary key'])) {
- foreach ($table['primary key'] as $name) {
- $keys[$name] = 'K';
- }
- }
- return $keys;
- }
-
- function columnBitmap($column)
- {
- $type = $column['type'];
-
- $intTypes = array('int',
- 'integer',
- 'float',
- 'serial',
- 'numeric');
- if (in_array($type, $intTypes)) {
- $style = DB_DATAOBJECT_INT;
- } else {
- $style = DB_DATAOBJECT_STR;
- }
-
- $formatStyles = array('blob' => DB_DATAOBJECT_BLOB,
- 'text' => DB_DATAOBJECT_TXT,
- 'date' => DB_DATAOBJECT_DATE,
- 'time' => DB_DATAOBJECT_TIME,
- 'datetime' => DB_DATAOBJECT_DATE | DB_DATAOBJECT_TIME,
- 'timestamp' => DB_DATAOBJECT_MYSQLTIMESTAMP);
- if (isset($formatStyles[$type])) {
- $style |= $formatStyles[$type];
- }
-
- if (!empty($column['not null'])) {
- $style |= DB_DATAOBJECT_NOTNULL;
- }
- return $style;
- }
- function links()
- {
- $links = array();
- $table = static::schemaDef();
- foreach ($table['foreign keys'] as $keyname => $keydef) {
- if (count($keydef) == 2 && is_string($keydef[0]) && is_array($keydef[1]) && count($keydef[1]) == 1) {
- if (isset($keydef[1][0])) {
- $links[$keydef[1][0]] = $keydef[0].':'.$keydef[1][1];
- }
- }
- }
- return $links;
- }
-
- function _allCacheKeys()
- {
- $table = static::schemaDef();
- $ckeys = array();
- if (!empty($table['unique keys'])) {
- $keyNames = $table['unique keys'];
- foreach ($keyNames as $idx => $fields) {
- $val = array();
- foreach ($fields as $name) {
- $val[$name] = self::valueString($this->$name);
- }
- $ckeys[] = self::multicacheKey($this->tableName(), $val);
- }
- }
- if (!empty($table['primary key'])) {
- $fields = $table['primary key'];
- $val = array();
- foreach ($fields as $name) {
- $val[$name] = self::valueString($this->$name);
- }
- $ckeys[] = self::multicacheKey($this->tableName(), $val);
- }
- return $ckeys;
- }
-
- public function getID()
- {
-
- if (!isset($this->id)) {
- throw new Exception('No ID set.');
- } elseif (empty($this->id)) {
- throw new Exception('Empty ID for object! (not inserted yet?).');
- }
-
- return $this->id;
- }
-
-
-
-
- public function updateWithKeys(&$orig, $pid='id')
- {
- if (!$orig instanceof $this) {
- throw new ServerException('Tried updating a DataObject with a different class than itself.');
- }
-
- $this->query('BEGIN');
- $parts = array();
- foreach ($this->keys() as $k) {
- if (strcmp($this->$k, $orig->$k) != 0) {
- $parts[] = $k . ' = ' . $this->_quote($this->$k);
- }
- }
- if (count($parts) == 0) {
-
- if ($this->update($orig) === false) {
- common_log_db_error($this, 'UPDATE', __FILE__);
-
- $this->query('ROLLBACK');
- throw new ServerException("Could not UPDATE non-keys for {$this->__table}");
- }
- $orig->decache();
- $this->encache();
-
- $this->query('COMMIT');
-
- return true;
- }
- $qry = sprintf('UPDATE %1$s SET %2$s WHERE %3$s = %4$s',
- common_database_tablename($this->tableName()),
- implode(', ', $parts),
- $pid,
- $this->_quote($this->$pid));
- $result = $this->query($qry);
- if ($result === false) {
- common_log_db_error($this, 'UPDATE', __FILE__);
-
- $this->query('ROLLBACK');
- throw new ServerException("Could not UPDATE key fields for {$this->__table}");
- }
-
-
-
- if ($this->update($orig) === false) {
- common_log_db_error($this, 'UPDATE', __FILE__);
-
- $this->query('ROLLBACK');
- throw new ServerException("Could not UPDATE non-keys for {$this->__table}");
- }
- $orig->decache();
- $this->encache();
-
- $this->query('COMMIT');
-
- return $result;
- }
- }
|