diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index c4bc3e10e4..e29082ea42 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1079,13 +1079,10 @@ phutil_register_library_map(array( 'PhrictionEditController' => 'applications/phriction/controller/PhrictionEditController.php', 'PhrictionHistoryController' => 'applications/phriction/controller/PhrictionHistoryController.php', 'PhrictionListController' => 'applications/phriction/controller/PhrictionListController.php', - 'QueryFormattingTestCase' => 'infrastructure/storage/query/__tests__/QueryFormattingTestCase.php', ), 'function' => array( '_phabricator_date_format' => 'view/viewutils.php', - '_qsprintf_check_scalar_type' => 'infrastructure/storage/query/qsprintf.php', - '_qsprintf_check_type' => 'infrastructure/storage/query/qsprintf.php', 'celerity_generate_unique_node_id' => 'infrastructure/celerity/api.php', 'celerity_get_resource_uri' => 'infrastructure/celerity/api.php', 'celerity_register_resource_map' => 'infrastructure/celerity/map.php', @@ -1104,15 +1101,7 @@ phutil_register_library_map(array( 'phabricator_time' => 'view/viewutils.php', 'phid_get_type' => 'applications/phid/utils.php', 'phid_group_by_type' => 'applications/phid/utils.php', - 'qsprintf' => 'infrastructure/storage/query/qsprintf.php', - 'queryfx' => 'infrastructure/storage/query/queryfx.php', - 'queryfx_all' => 'infrastructure/storage/query/queryfx.php', - 'queryfx_one' => 'infrastructure/storage/query/queryfx.php', 'require_celerity_resource' => 'infrastructure/celerity/api.php', - 'vqsprintf' => 'infrastructure/storage/query/qsprintf.php', - 'vqueryfx' => 'infrastructure/storage/query/queryfx.php', - 'vqueryfx_all' => 'infrastructure/storage/query/queryfx.php', - 'xsprintf_query' => 'infrastructure/storage/query/qsprintf.php', ), 'xmap' => array( @@ -2058,6 +2047,5 @@ phutil_register_library_map(array( 'PhrictionEditController' => 'PhrictionController', 'PhrictionHistoryController' => 'PhrictionController', 'PhrictionListController' => 'PhrictionController', - 'QueryFormattingTestCase' => 'PhabricatorTestCase', ), )); diff --git a/src/infrastructure/storage/query/__tests__/QueryFormattingTestCase.php b/src/infrastructure/storage/query/__tests__/QueryFormattingTestCase.php deleted file mode 100644 index 2d015948fc..0000000000 --- a/src/infrastructure/storage/query/__tests__/QueryFormattingTestCase.php +++ /dev/null @@ -1,56 +0,0 @@ -establishConnection('r'); - - $this->assertEqual( - 'NULL', - qsprintf($conn_r, '%nd', null)); - - $this->assertEqual( - '0', - qsprintf($conn_r, '%nd', 0)); - - $this->assertEqual( - '0', - qsprintf($conn_r, '%d', 0)); - - $raised = null; - try { - qsprintf($conn_r, '%d', 'derp'); - } catch (Exception $ex) { - $raised = $ex; - } - $this->assertEqual( - (bool)$raised, - true, - 'qsprintf should raise exception for invalid %d conversion.'); - - $this->assertEqual( - "''", - qsprintf($conn_r, '%s', null)); - - $this->assertEqual( - 'NULL', - qsprintf($conn_r, '%ns', null)); - } - -} diff --git a/src/infrastructure/storage/query/qsprintf.php b/src/infrastructure/storage/query/qsprintf.php deleted file mode 100644 index 0af243e3d6..0000000000 --- a/src/infrastructure/storage/query/qsprintf.php +++ /dev/null @@ -1,314 +0,0 @@ - and %<. - * - * %> ("Prefix") - * Escapes a prefix query for a LIKE clause. For example: - * - * // Find all rows where `name` starts with $prefix. - * qsprintf($conn, 'WHERE name LIKE %>', $prefix); - * - * %< ("Suffix") - * Escapes a suffix query for a LIKE clause. For example: - * - * // Find all rows where `name` ends with $suffix. - * qsprintf($conn, 'WHERE name LIKE %<', $suffix); - * - * @group storage - */ -function qsprintf(AphrontDatabaseConnection $conn, $pattern/*, ... */) { - $args = func_get_args(); - array_shift($args); - return xsprintf('xsprintf_query', $conn, $args); -} - -/** - * @group storage - */ -function vqsprintf(AphrontDatabaseConnection $conn, $pattern, array $argv) { - array_unshift($argv, $pattern); - return xsprintf('xsprintf_query', $conn, $argv); -} - - -/** - * xsprintf() callback for encoding SQL queries. See qsprintf(). - * @group storage - */ -function xsprintf_query($userdata, &$pattern, &$pos, &$value, &$length) { - $type = $pattern[$pos]; - $conn = $userdata; - $next = (strlen($pattern) > $pos + 1) ? $pattern[$pos + 1] : null; - - $nullable = false; - $done = false; - - $prefix = ''; - - if (!($conn instanceof AphrontDatabaseConnection)) { - throw new Exception("Invalid database connection!"); - } - - switch ($type) { - case '=': // Nullable test - switch ($next) { - case 'd': - case 'f': - case 's': - $pattern = substr_replace($pattern, '', $pos, 1); - $length = strlen($pattern); - $type = 's'; - if ($value === null) { - $value = 'IS NULL'; - $done = true; - } else { - $prefix = '= '; - $type = $next; - } - break; - default: - throw new Exception('Unknown conversion, try %=d, %=s, or %=f.'); - } - break; - - case 'n': // Nullable... - switch ($next) { - case 'd': // ...integer. - case 'f': // ...float. - case 's': // ...string. - $pattern = substr_replace($pattern, '', $pos, 1); - $length = strlen($pattern); - $type = $next; - $nullable = true; - break; - default: - throw new Exception('Unknown conversion, try %nd or %ns.'); - } - break; - - case 'L': // List of.. - _qsprintf_check_type($value, "L{$next}", $pattern); - $pattern = substr_replace($pattern, '', $pos, 1); - $length = strlen($pattern); - $type = 's'; - $done = true; - - switch ($next) { - case 'd': // ...integers. - $value = implode(', ', array_map('intval', $value)); - break; - case 's': // ...strings. - foreach ($value as $k => $v) { - $value[$k] = "'".$conn->escapeString($v)."'"; - } - $value = implode(', ', $value); - break; - case 'C': // ...columns. - foreach ($value as $k => $v) { - $value[$k] = $conn->escapeColumnName($v); - } - $value = implode(', ', $value); - break; - default: - throw new Exception("Unknown conversion %L{$next}."); - } - break; - } - - if (!$done) { - _qsprintf_check_type($value, $type, $pattern); - switch ($type) { - case 's': // String - if ($nullable && $value === null) { - $value = 'NULL'; - } else { - $value = "'".$conn->escapeString($value)."'"; - } - $type = 's'; - break; - - case 'Q': // Query Fragment - $type = 's'; - break; - - case '~': // Like Substring - case '>': // Like Prefix - case '<': // Like Suffix - $value = $conn->escapeStringForLikeClause($value); - switch ($type) { - case '~': $value = "'%".$value."%'"; break; - case '>': $value = "'" .$value."%'"; break; - case '<': $value = "'%".$value. "'"; break; - } - $type = 's'; - break; - - case 'f': // Float - if ($nullable && $value === null) { - $value = 'NULL'; - } else { - $value = (float)$value; - } - $type = 's'; - break; - - case 'd': // Integer - if ($nullable && $value === null) { - $value = 'NULL'; - } else { - $value = (int)$value; - } - $type = 's'; - break; - - case 'T': // Table - case 'C': // Column - $value = $conn->escapeColumnName($value); - $type = 's'; - break; - - case 'K': // Komment - $value = $conn->escapeMultilineComment($value); - $type = 's'; - break; - - default: - throw new Exception("Unknown conversion '%{$type}'."); - - } - } - - if ($prefix) { - $value = $prefix.$value; - } - $pattern[$pos] = $type; -} - - -/** - * @group storage - */ -function _qsprintf_check_type($value, $type, $query) { - switch ($type) { - case 'Ld': case 'Ls': case 'LC': case 'LA': case 'LO': - if (!is_array($value)) { - throw new AphrontQueryParameterException( - $query, - "Expected array argument for %{$type} conversion."); - } - if (empty($value)) { - throw new AphrontQueryParameterException( - $query, - "Array for %{$type} conversion is empty."); - } - - foreach ($value as $scalar) { - _qsprintf_check_scalar_type($scalar, $type, $query); - } - break; - default: - _qsprintf_check_scalar_type($value, $type, $query); - break; - } -} - - -/** - * @group storage - */ -function _qsprintf_check_scalar_type($value, $type, $query) { - switch ($type) { - case 'Q': case 'LC': case 'T': case 'C': - if (!is_string($value)) { - throw new AphrontQueryParameterException( - $query, - "Expected a string for %{$type} conversion."); - } - break; - - case 'Ld': case 'd': case 'f': - if (!is_null($value) && !is_numeric($value)) { - throw new AphrontQueryParameterException( - $query, - "Expected a numeric scalar or null for %{$type} conversion."); - } - break; - - case 'Ls': case 's': - case '~': case '>': case '<': case 'K': - if (!is_null($value) && !is_scalar($value)) { - throw new AphrontQueryParameterException( - $query, - "Expected a scalar or null for %{$type} conversion."); - } - break; - - case 'LA': case 'LO': - if (!is_null($value) && !is_scalar($value) && - !(is_array($value) && !empty($value))) { - throw new AphrontQueryParameterException( - $query, - "Expected a scalar or null or non-empty array for ". - "%{$type} conversion."); - } - break; - default: - throw new Exception("Unknown conversion '{$type}'."); - } -} diff --git a/src/infrastructure/storage/query/queryfx.php b/src/infrastructure/storage/query/queryfx.php deleted file mode 100644 index 9e70accb45..0000000000 --- a/src/infrastructure/storage/query/queryfx.php +++ /dev/null @@ -1,67 +0,0 @@ -executeRawQuery($query); -} - -/** - * @group storage - */ -function vqueryfx(AphrontDatabaseConnection $conn, $sql, array $argv) { - array_unshift($argv, $conn, $sql); - call_user_func_array('queryfx', $argv); -} - -/** - * @group storage - */ -function queryfx_all(AphrontDatabaseConnection $conn, $sql/*, ... */) { - $argv = func_get_args(); - call_user_func_array('queryfx', $argv); - return $conn->selectAllResults(); -} - -/** - * @group storage - */ -function queryfx_one(AphrontDatabaseConnection $conn, $sql/*, ... */) { - $argv = func_get_args(); - $ret = call_user_func_array('queryfx_all', $argv); - if (count($ret) > 1) { - throw new AphrontQueryCountException( - 'Query returned more than one row.'); - } else if (count($ret)) { - return reset($ret); - } - return null; -} - -/** - * @group storage - */ -function vqueryfx_all(AphrontDatabaseConnection $conn, $sql, array $argv) { - array_unshift($argv, $conn, $sql); - call_user_func_array('queryfx', $argv); - return $conn->selectAllResults(); -}