classStrings
final class Strings
Decodes and spells the quoted string tokens of MySQL under a fixed escape setting.
Rule: MYSQL-STRING-DECODE-001. Scope: the terminals TEXT_STRING and NCHAR_STRING. The text between the quotes is read byte by byte. A doubled quote character is one quote. Unless NO_BACKSLASH_ESCAPES is set, a backslash and the byte after it are one escape: \0 is NUL, \b backspace, \n newline, \r carriage return, \t tab, \Z byte 0x1A, \% and \_ keep the backslash, and before any other byte the backslash is dropped. Under NO_BACKSLASH_ESCAPES a backslash is an ordinary byte. The spelling writes single quotes, doubles each single quote and, when backslash is an escape, doubles each backslash; that spelling decodes to the same bytes. Assumption: the client character set does not use the bytes of a backslash or a quote inside a multi-byte character (true for utf8mb4, utf8mb3, latin1 and ASCII; not for sjis, big5, gbk, gb18030, cp932). Terminates: one pass over the text. Source: https://dev.mysql.com/doc/refman/8.4/en/string-literals.html. Status: Implemented.
Methods§
public function decode(string $text, bool $backslashEscapes): stringDecodes a quoted string token, with or without the N prefix, to its bytes.
Parameters
$text | string | |
$backslashEscapes | bool |
Returns
stringTest cases 7
StringsTest::testDecodeAcceptsTheNationalPrefixcallsStringsTest::testDecodeKeepsBytesAboveAsciiAsWrittencallsStringsTest::testDecodeKeepsTheBackslashUnderNoBackslashEscapescallsStringsTest::testDecodeReadsADoubledQuoteAsOnecallsStringsTest::testDecodeReadsEveryEscapeLetterWhenBackslashEscapescallsStringsTest::testEncodeRoundTripsThroughDecodeUnderBothSettingscallsStringsTest::testEncodeSpellsControlBytesAsEscapesWhenBackslashEscapescalls
Called from 4
- method-call SqlSemantics\Platform\MySql\Lowering\Leaf\LiteralRule::decode() packages/sql-semantics-mysql/src/Lowering/Leaf/LiteralRule.php:216
- method-call SqlSemantics\Platform\MySql\Rules\LeafKeys::name() packages/sql-semantics-mysql/src/Rules/LeafKeys.php:99
- method-call SqlSemantics\Platform\MySql\Rules\LeafKeys::terminal() packages/sql-semantics-mysql/src/Rules/LeafKeys.php:112
- method-call SqlSemantics\Platform\MySql\Rules\LeafKeys::terminal() packages/sql-semantics-mysql/src/Rules/LeafKeys.php:113
Calls 2
- function-call
strlenline 47 - class-const Strings::ESCAPES() line 53
public function encode(string $value, bool $backslashEscapes): stringSpells bytes as a single-quoted string that decodes to the same bytes.
Parameters
$value | string | |
$backslashEscapes | bool |
Returns
stringTest cases 3
Called from 3
- method-call SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral::render() packages/sql-semantics-mysql/src/Statement/Literal/StringLiteral.php:104
- method-call SqlSemantics\Platform\MySql\Statement\Literal\TemporalLiteral::render() packages/sql-semantics-mysql/src/Statement/Literal/TemporalLiteral.php:80
- method-call SqlSemantics\Platform\MySql\Statement\Literal\Text::render() packages/sql-semantics-mysql/src/Statement/Literal/Text.php:50
Calls 3
- function-call
strtrline 70 - class-const Strings::SPELLINGS() line 70
- function-call
str_replaceline 72
Private surface 2§
Implementation details, listed for orientation only.
private const ESCAPES = ['0' => "\x00", 'b' => "\x08", 'n' => "\n", 'r' => "\r", 't' => "\t", 'Z' => "\…private const SPELLINGS = ['\\' => '\\\\', "\x00" => '\0', "\n" => '\n', "\r" => '\r', "\x1a" => '\Z']Test cases 8§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 8
StringsTest::testDecodeAcceptsTheNationalPrefixcallsStringsTest::testDecodeKeepsBytesAboveAsciiAsWrittencallsStringsTest::testDecodeKeepsTheBackslashUnderNoBackslashEscapescallsStringsTest::testDecodeReadsADoubledQuoteAsOnecallsStringsTest::testDecodeReadsEveryEscapeLetterWhenBackslashEscapescallsStringsTest::testEncodeRoundTripsThroughDecodeUnderBothSettingscallsStringsTest::testEncodeSpellsControlBytesAsEscapesWhenBackslashEscapescallsStringsTest::testEncodeWritesSingleQuotesAndDoublesWhatWouldEndThemcalls
Relations§
Instantiated in 7
- new SqlSemantics\Platform\MySql\Lowering\Leaf\LiteralRule::decode() packages/sql-semantics-mysql/src/Lowering/Leaf/LiteralRule.php:216
- new SqlSemantics\Platform\MySql\Rules\LeafKeys::name() packages/sql-semantics-mysql/src/Rules/LeafKeys.php:99
- new SqlSemantics\Platform\MySql\Rules\LeafKeys::terminal() packages/sql-semantics-mysql/src/Rules/LeafKeys.php:112
- new SqlSemantics\Platform\MySql\Rules\LeafKeys::terminal() packages/sql-semantics-mysql/src/Rules/LeafKeys.php:113
- new SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral::render() packages/sql-semantics-mysql/src/Statement/Literal/StringLiteral.php:99
- new SqlSemantics\Platform\MySql\Statement\Literal\TemporalLiteral::render() packages/sql-semantics-mysql/src/Statement/Literal/TemporalLiteral.php:80
- new SqlSemantics\Platform\MySql\Statement\Literal\Text::render() packages/sql-semantics-mysql/src/Statement/Literal/Text.php:50
Method calls 7
- method-call SqlSemantics\Platform\MySql\Lowering\Leaf\LiteralRule::decode() packages/sql-semantics-mysql/src/Lowering/Leaf/LiteralRule.php:216
- method-call SqlSemantics\Platform\MySql\Rules\LeafKeys::name() packages/sql-semantics-mysql/src/Rules/LeafKeys.php:99
- method-call SqlSemantics\Platform\MySql\Rules\LeafKeys::terminal() packages/sql-semantics-mysql/src/Rules/LeafKeys.php:112
- method-call SqlSemantics\Platform\MySql\Rules\LeafKeys::terminal() packages/sql-semantics-mysql/src/Rules/LeafKeys.php:113
- method-call SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral::render() packages/sql-semantics-mysql/src/Statement/Literal/StringLiteral.php:104
- method-call SqlSemantics\Platform\MySql\Statement\Literal\TemporalLiteral::render() packages/sql-semantics-mysql/src/Statement/Literal/TemporalLiteral.php:80
- method-call SqlSemantics\Platform\MySql\Statement\Literal\Text::render() packages/sql-semantics-mysql/src/Statement/Literal/Text.php:50