Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/storage/lisk/LiskDAO.php
Show First 20 Lines • Show All 163 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
abstract class LiskDAO { | abstract class LiskDAO { | ||||
const CONFIG_IDS = 'id-mechanism'; | const CONFIG_IDS = 'id-mechanism'; | ||||
const CONFIG_TIMESTAMPS = 'timestamps'; | const CONFIG_TIMESTAMPS = 'timestamps'; | ||||
const CONFIG_AUX_PHID = 'auxiliary-phid'; | const CONFIG_AUX_PHID = 'auxiliary-phid'; | ||||
const CONFIG_SERIALIZATION = 'col-serialization'; | const CONFIG_SERIALIZATION = 'col-serialization'; | ||||
const CONFIG_BINARY = 'binary'; | const CONFIG_BINARY = 'binary'; | ||||
const CONFIG_COLUMN_SCHEMA = 'col-schema'; | |||||
const SERIALIZATION_NONE = 'id'; | const SERIALIZATION_NONE = 'id'; | ||||
const SERIALIZATION_JSON = 'json'; | const SERIALIZATION_JSON = 'json'; | ||||
const SERIALIZATION_PHP = 'php'; | const SERIALIZATION_PHP = 'php'; | ||||
const IDS_AUTOINCREMENT = 'ids-auto'; | const IDS_AUTOINCREMENT = 'ids-auto'; | ||||
const IDS_COUNTER = 'ids-counter'; | const IDS_COUNTER = 'ids-counter'; | ||||
const IDS_MANUAL = 'ids-manual'; | const IDS_MANUAL = 'ids-manual'; | ||||
▲ Show 20 Lines • Show All 158 Lines • ▼ Show 20 Lines | /* -( Configuring Lisk )--------------------------------------------------- */ | ||||
* This will cause Lisk to JSON-serialize the 'complex' field before it is | * This will cause Lisk to JSON-serialize the 'complex' field before it is | ||||
* written, and unserialize it when it is read. | * written, and unserialize it when it is read. | ||||
* | * | ||||
* CONFIG_BINARY | * CONFIG_BINARY | ||||
* You can optionally provide a map of columns to a flag indicating that | * You can optionally provide a map of columns to a flag indicating that | ||||
* they store binary data. These columns will not raise an error when | * they store binary data. These columns will not raise an error when | ||||
* handling binary writes. | * handling binary writes. | ||||
* | * | ||||
* CONFIG_COLUMN_SCHEMA | |||||
* Provide a map of columns to schema column types. | |||||
* | |||||
* @return dictionary Map of configuration options to values. | * @return dictionary Map of configuration options to values. | ||||
* | * | ||||
* @task config | * @task config | ||||
*/ | */ | ||||
protected function getConfiguration() { | protected function getConfiguration() { | ||||
return array( | return array( | ||||
self::CONFIG_IDS => self::IDS_AUTOINCREMENT, | self::CONFIG_IDS => self::IDS_AUTOINCREMENT, | ||||
self::CONFIG_TIMESTAMPS => true, | self::CONFIG_TIMESTAMPS => true, | ||||
▲ Show 20 Lines • Show All 1,353 Lines • ▼ Show 20 Lines | public static function loadNextCounterID( | ||||
return $conn_w->getInsertID(); | return $conn_w->getInsertID(); | ||||
} | } | ||||
private function getBinaryColumns() { | private function getBinaryColumns() { | ||||
return $this->getConfigOption(self::CONFIG_BINARY); | return $this->getConfigOption(self::CONFIG_BINARY); | ||||
} | } | ||||
public function getSchemaColumns() { | |||||
$custom_map = $this->getConfigOption(self::CONFIG_COLUMN_SCHEMA); | |||||
if (!$custom_map) { | |||||
$custom_map = array(); | |||||
} | |||||
$serialization = $this->getConfigOption(self::CONFIG_SERIALIZATION); | |||||
if (!$serialization) { | |||||
$serialization = array(); | |||||
} | |||||
$serialization_map = array( | |||||
self::SERIALIZATION_JSON => 'text', | |||||
self::SERIALIZATION_PHP => 'blob', | |||||
); | |||||
$builtin = array( | |||||
'id' => 'id', | |||||
'phid' => 'phid', | |||||
'dateCreated' => 'epoch', | |||||
'dateModified' => 'epoch', | |||||
); | |||||
$map = array(); | |||||
foreach ($this->getAllLiskProperties() as $property) { | |||||
// First, use types specified explicitly in the table configuration. | |||||
$type = idx($custom_map, $property); | |||||
if ($type) { | |||||
$map[$property] = $type; | |||||
continue; | |||||
} | |||||
// If we don't have an explicit type, try a builtin type for the | |||||
// column. | |||||
$type = idx($builtin, $property); | |||||
if ($type) { | |||||
$map[$property] = $type; | |||||
continue; | |||||
} | |||||
// If the column has serialization, we can infer the column type. | |||||
if (isset($serialization[$property])) { | |||||
$type = idx($serialization_map, $serialization[$property]); | |||||
if ($type) { | |||||
$map[$property] = $type; | |||||
continue; | |||||
} | |||||
} | |||||
// If the column is named `somethingPHID`, infer it is a PHID. | |||||
if (preg_match('/[a-z]PHID$/', $property)) { | |||||
$map[$property] = 'phid'; | |||||
continue; | |||||
} | |||||
// If the column is named `somethingID`, infer it is an ID. | |||||
if (preg_match('/[a-z]ID$/', $property)) { | |||||
$map[$property] = 'id'; | |||||
continue; | |||||
} | |||||
// We don't know the type of this column. | |||||
$map[$property] = null; | |||||
} | |||||
return $map; | |||||
} | |||||
} | } |