Path: blob/master/src/infrastructure/storage/connection/AphrontIsolatedDatabaseConnection.php
12241 views
<?php12final class AphrontIsolatedDatabaseConnection3extends AphrontDatabaseConnection {45private $configuration;6private static $nextInsertID;7private $insertID;89private $transcript = array();1011private $allResults;12private $affectedRows;1314public function __construct(array $configuration) {15$this->configuration = $configuration;1617if (self::$nextInsertID === null) {18// Generate test IDs into a distant ID space to reduce the risk of19// collisions and make them distinctive.20self::$nextInsertID = 55555000000 + mt_rand(0, 1000);21}22}2324public function openConnection() {25return;26}2728public function close() {29return;30}3132public function escapeUTF8String($string) {33return '<S>';34}3536public function escapeBinaryString($string) {37return '<B>';38}3940public function escapeColumnName($name) {41return '<C>';42}4344public function escapeMultilineComment($comment) {45return '<K>';46}4748public function escapeStringForLikeClause($value) {49return '<L>';50}5152private function getConfiguration($key, $default = null) {53return idx($this->configuration, $key, $default);54}5556public function getInsertID() {57return $this->insertID;58}5960public function getAffectedRows() {61return $this->affectedRows;62}6364public function selectAllResults() {65return $this->allResults;66}6768public function executeQuery(PhutilQueryString $query) {6970// NOTE: "[\s<>K]*" allows any number of (properly escaped) comments to71// appear prior to the allowed keyword, since this connection escapes72// them as "<K>" (above).7374$display_query = $query->getMaskedString();75$raw_query = $query->getUnmaskedString();7677$keywords = array(78'INSERT',79'UPDATE',80'DELETE',81'START',82'SAVEPOINT',83'COMMIT',84'ROLLBACK',85);86$preg_keywords = array();87foreach ($keywords as $key => $word) {88$preg_keywords[] = preg_quote($word, '/');89}90$preg_keywords = implode('|', $preg_keywords);9192if (!preg_match('/^[\s<>K]*('.$preg_keywords.')\s*/i', $raw_query)) {93throw new AphrontNotSupportedQueryException(94pht(95"Database isolation currently only supports some queries. You are ".96"trying to issue a query which does not begin with an allowed ".97"keyword (%s): '%s'.",98implode(', ', $keywords),99$display_query));100}101102$this->transcript[] = $display_query;103104// NOTE: This method is intentionally simplified for now, since we're only105// using it to stub out inserts/updates. In the future it will probably need106// to grow more powerful.107108$this->allResults = array();109110// NOTE: We jitter the insert IDs to keep tests honest; a test should cover111// the relationship between objects, not their exact insertion order. This112// guarantees that IDs are unique but makes it impossible to hard-code tests113// against this specific implementation detail.114self::$nextInsertID += mt_rand(1, 10);115$this->insertID = self::$nextInsertID;116$this->affectedRows = 1;117}118119public function executeRawQueries(array $raw_queries) {120$results = array();121foreach ($raw_queries as $id => $raw_query) {122$results[$id] = array();123}124return $results;125}126127public function getQueryTranscript() {128return $this->transcript;129}130131}132133134