Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/storage/lisk/LiskDAO.php
Show First 20 Lines • Show All 165 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_PARTIAL_OBJECTS = 'partial-objects'; | const CONFIG_PARTIAL_OBJECTS = 'partial-objects'; | ||||
const CONFIG_BINARY = 'binary'; | |||||
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 169 Lines • ▼ Show 20 Lines | /* -( Configuring Lisk )--------------------------------------------------- */ | ||||
* when you are loading all objects of a class, but only care about a few | * when you are loading all objects of a class, but only care about a few | ||||
* fields). Turning on this option (by setting it to a truthy value) allows | * fields). Turning on this option (by setting it to a truthy value) allows | ||||
* users of the class to create/use partial objects, but it comes with some | * users of the class to create/use partial objects, but it comes with some | ||||
* side effects: your class cannot override the setters and getters provided | * side effects: your class cannot override the setters and getters provided | ||||
* by Lisk (use readField and writeField instead), and you should not | * by Lisk (use readField and writeField instead), and you should not | ||||
* directly access or assign protected members of your class (use the getters | * directly access or assign protected members of your class (use the getters | ||||
* and setters). | * and setters). | ||||
* | * | ||||
* CONFIG_BINARY | |||||
* 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 | |||||
* handling binary writes. | |||||
* | |||||
* @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 776 Lines • ▼ Show 20 Lines | public function update() { | ||||
$this->willWriteData($data); | $this->willWriteData($data); | ||||
$map = array(); | $map = array(); | ||||
foreach ($data as $k => $v) { | foreach ($data as $k => $v) { | ||||
$map[$k] = $v; | $map[$k] = $v; | ||||
} | } | ||||
$conn = $this->establishConnection('w'); | $conn = $this->establishConnection('w'); | ||||
$binary = $this->getBinaryColumns(); | |||||
foreach ($map as $key => $value) { | foreach ($map as $key => $value) { | ||||
if (!empty($binary[$key])) { | |||||
$map[$key] = qsprintf($conn, '%C = %nB', $key, $value); | |||||
} else { | |||||
$map[$key] = qsprintf($conn, '%C = %ns', $key, $value); | $map[$key] = qsprintf($conn, '%C = %ns', $key, $value); | ||||
} | } | ||||
} | |||||
$map = implode(', ', $map); | $map = implode(', ', $map); | ||||
$id = $this->getID(); | $id = $this->getID(); | ||||
$conn->query( | $conn->query( | ||||
'UPDATE %T SET %Q WHERE %C = '.(is_int($id) ? '%d' : '%s'), | 'UPDATE %T SET %Q WHERE %C = '.(is_int($id) ? '%d' : '%s'), | ||||
$this->getTableName(), | $this->getTableName(), | ||||
$map, | $map, | ||||
$this->getIDKeyForUse(), | $this->getIDKeyForUse(), | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | switch ($id_mechanism) { | ||||
break; | break; | ||||
default: | default: | ||||
throw new Exception('Unknown CONFIG_IDs mechanism!'); | throw new Exception('Unknown CONFIG_IDs mechanism!'); | ||||
} | } | ||||
$this->willWriteData($data); | $this->willWriteData($data); | ||||
$columns = array_keys($data); | $columns = array_keys($data); | ||||
$binary = $this->getBinaryColumns(); | |||||
foreach ($data as $key => $value) { | foreach ($data as $key => $value) { | ||||
try { | try { | ||||
if (!empty($binary[$key])) { | |||||
$data[$key] = qsprintf($conn, '%nB', $value); | |||||
} else { | |||||
$data[$key] = qsprintf($conn, '%ns', $value); | $data[$key] = qsprintf($conn, '%ns', $value); | ||||
} | |||||
} catch (AphrontQueryParameterException $parameter_exception) { | } catch (AphrontQueryParameterException $parameter_exception) { | ||||
throw new PhutilProxyException( | throw new PhutilProxyException( | ||||
pht( | pht( | ||||
"Unable to insert or update object of class %s, field '%s' ". | "Unable to insert or update object of class %s, field '%s' ". | ||||
"has a nonscalar value.", | "has a nonscalar value.", | ||||
get_class($this), | get_class($this), | ||||
$key), | $key), | ||||
$parameter_exception); | $parameter_exception); | ||||
▲ Show 20 Lines • Show All 541 Lines • ▼ Show 20 Lines | queryfx( | ||||
ON DUPLICATE KEY UPDATE | ON DUPLICATE KEY UPDATE | ||||
counterValue = LAST_INSERT_ID(counterValue + 1)', | counterValue = LAST_INSERT_ID(counterValue + 1)', | ||||
self::COUNTER_TABLE_NAME, | self::COUNTER_TABLE_NAME, | ||||
$counter_name); | $counter_name); | ||||
return $conn_w->getInsertID(); | return $conn_w->getInsertID(); | ||||
} | } | ||||
private function getBinaryColumns() { | |||||
return $this->getConfigOption(self::CONFIG_BINARY); | |||||
} | |||||
} | } |