Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/storage/lisk/LiskDAO.php
| Show First 20 Lines • Show All 191 Lines • ▼ Show 20 Lines | abstract class LiskDAO { | ||||
| private $inSet = null; | private $inSet = null; | ||||
| protected $id; | protected $id; | ||||
| protected $phid; | protected $phid; | ||||
| protected $dateCreated; | protected $dateCreated; | ||||
| protected $dateModified; | protected $dateModified; | ||||
| private $dangerousOverrideID; | |||||
| private $dangerousOverrideTimeStamp; | |||||
| /** | /** | ||||
| * Build an empty object. | * Build an empty object. | ||||
| * | * | ||||
| * @return obj Empty object. | * @return obj Empty object. | ||||
| */ | */ | ||||
| public function __construct() { | public function __construct() { | ||||
| $id_key = $this->getIDKey(); | $id_key = $this->getIDKey(); | ||||
| if ($id_key) { | if ($id_key) { | ||||
| $this->$id_key = null; | $this->$id_key = null; | ||||
| } | } | ||||
| } | } | ||||
| /* -( Shooting Yourself in the Foot )------------------------------------- */ | |||||
| /* | |||||
| * Both of these "dangerous" methods are for overriding when objects | |||||
| * are created/changed and what their IDs are. This is intended for | |||||
| * (A) writing import scripts from other systems (B) setting up test | |||||
| * scenarios. | |||||
| * | |||||
| * They are dangerous and there is little safety to keep you from | |||||
| * wrecking an install. There is no guarantee that future versions | |||||
| * of Phabricator will not place logical constraints on the ordering | |||||
| * of timestamps and bad things may happen if you create impossible scenarios. | |||||
| */ | |||||
| public function setDangerousOverrideID($id) { | |||||
| $this->dangerousOverrideID = $id; | |||||
| return $this; | |||||
| } | |||||
| public function setDangerousOverrideTimeStamp($ts) { | |||||
| $this->dangerousOverrideTimeStamp = $ts; | |||||
| return $this; | |||||
| } | |||||
| /* -( Managing Connections )----------------------------------------------- */ | /* -( Managing Connections )----------------------------------------------- */ | ||||
| /** | /** | ||||
| * Establish a live connection to a database service. This method should | * Establish a live connection to a database service. This method should | ||||
| * return a new connection. Lisk handles connection caching and management; | * return a new connection. Lisk handles connection caching and management; | ||||
| * do not perform caching deeper in the stack. | * do not perform caching deeper in the stack. | ||||
| * | * | ||||
| ▲ Show 20 Lines • Show All 929 Lines • ▼ Show 20 Lines | /* -( Writing Objects )---------------------------------------------------- */ | ||||
| * | * | ||||
| * @task save | * @task save | ||||
| */ | */ | ||||
| protected function insertRecordIntoDatabase($mode) { | protected function insertRecordIntoDatabase($mode) { | ||||
| $this->willSaveObject(); | $this->willSaveObject(); | ||||
| $data = $this->getAllLiskPropertyValues(); | $data = $this->getAllLiskPropertyValues(); | ||||
| $conn = $this->establishConnection('w'); | $conn = $this->establishConnection('w'); | ||||
| $id_mechanism = $this->getConfigOption(self::CONFIG_IDS); | $id_mechanism = $this->getConfigOption(self::CONFIG_IDS); | ||||
| switch ($id_mechanism) { | switch ($id_mechanism) { | ||||
| case self::IDS_AUTOINCREMENT: | case self::IDS_AUTOINCREMENT: | ||||
| // If we are using autoincrement IDs, let MySQL assign the value for the | // If we are using autoincrement IDs, let MySQL assign the value for the | ||||
| // ID column, if it is empty. If the caller has explicitly provided a | // ID column, if it is empty. If the caller has explicitly provided a | ||||
| // value, use it. | // value, use it. | ||||
| $id_key = $this->getIDKeyForUse(); | $id_key = $this->getIDKeyForUse(); | ||||
| if (empty($data[$id_key])) { | if (isset($this->dangerousOverrideID)) { | ||||
| $this->setID($this->dangerousOverrideID); | |||||
| $data[$id_key] = $this->dangerousOverrideID; | |||||
| } else if (empty($data[$id_key])) { | |||||
| unset($data[$id_key]); | unset($data[$id_key]); | ||||
| } | } | ||||
| break; | break; | ||||
| case self::IDS_COUNTER: | case self::IDS_COUNTER: | ||||
| // If we are using counter IDs, assign a new ID if we don't already have | // If we are using counter IDs, assign a new ID if we don't already have | ||||
| // one. | // one. | ||||
| $id_key = $this->getIDKeyForUse(); | $id_key = $this->getIDKeyForUse(); | ||||
| if (empty($data[$id_key])) { | if (isset($this->dangerousOverrideID)) { | ||||
| $this->setID($this->dangerousOverrideID); | |||||
| $data[$id_key] = $this->dangerousOverrideID; | |||||
| } else if (empty($data[$id_key])) { | |||||
| $counter_name = $this->getTableName(); | $counter_name = $this->getTableName(); | ||||
| $id = self::loadNextCounterID($conn, $counter_name); | $id = self::loadNextCounterID($conn, $counter_name); | ||||
| $this->setID($id); | $this->setID($id); | ||||
| $data[$id_key] = $id; | $data[$id_key] = $id; | ||||
| } | } | ||||
| break; | break; | ||||
| case self::IDS_MANUAL: | case self::IDS_MANUAL: | ||||
| break; | break; | ||||
| ▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | /* -( Hooks and Callbacks )------------------------------------------------ */ | ||||
| * UPDATE. | * UPDATE. | ||||
| * | * | ||||
| * @task hook | * @task hook | ||||
| */ | */ | ||||
| protected function willSaveObject() { | protected function willSaveObject() { | ||||
| $use_timestamps = $this->getConfigOption(self::CONFIG_TIMESTAMPS); | $use_timestamps = $this->getConfigOption(self::CONFIG_TIMESTAMPS); | ||||
| if ($use_timestamps) { | if ($use_timestamps) { | ||||
| if (isset($this->dangerousOverrideTimeStamp)) { | |||||
| $this->setDateCreated($this->dangerousOverrideTimeStamp); | |||||
| $this->setDateModified($this->dangerousOverrideTimeStamp); | |||||
| } else { | |||||
| if (!$this->getDateCreated()) { | if (!$this->getDateCreated()) { | ||||
| $this->setDateCreated(time()); | $this->setDateCreated(time()); | ||||
| } | } | ||||
| $this->setDateModified(time()); | $this->setDateModified(time()); | ||||
| } | } | ||||
| } | |||||
| if ($this->getConfigOption(self::CONFIG_AUX_PHID) && !$this->getPHID()) { | if ($this->getConfigOption(self::CONFIG_AUX_PHID) && !$this->getPHID()) { | ||||
| $this->setPHID($this->generatePHID()); | $this->setPHID($this->generatePHID()); | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| ▲ Show 20 Lines • Show All 498 Lines • Show Last 20 Lines | |||||