字串
字串字面值可以使用任何匹配的奇數個單引號或雙引號:
PRQL
from artists
derive {
single = 'hello world',
double = "hello world",
double_triple = """hello world""",
}
SQL
SELECT
*,
'hello world' AS single,
'hello world' AS double,
'hello world' AS double_triple
FROM
artists
引用和轉義字元
To quote a string containing quote characters, use the “other” type of quote, or use the escape character \, or use more quotes.
PRQL
from artists
select {
other = '"hello world"',
escaped = "\"hello world\"",
triple = """I said "hello world"!""",
}
SQL
SELECT
'"hello world"' AS other,
'"hello world"' AS escaped,
'I said "hello world"!' AS triple
FROM
artists
字串可以包含 JSON 標準定義的任何轉義字元序列。
PRQL
from artists
derive escapes = "\tXYZ\n \\ " # tab (\t), "XYZ", newline (\n), " ", \, " "
derive world = "\u{0048}\u{0065}\u{006C}\u{006C}\u{006F}" # "Hello"
derive hex = "\x48\x65\x6C\x6C\x6F" # "Hello"
derive turtle = "\u{01F422}" # "🐢"
SQL
SELECT
*,
' XYZ
\ ' AS escapes,
'Hello' AS world,
'Hello' AS hex,
'🐢' AS turtle
FROM
artists
其他字串格式
- F-字串 - 從一組欄位或值建立新字串。
- R-字串 - 不含任何形式轉義的字串的原始字元。
- S-strings - Insert SQL statements directly into the query. Use when PRQL doesn’t have an equivalent facility.
[!WARNING] 目前 PRQL 允許使用單一字元或多個字元引號的多行字串。在未來版本中,使用單一字元引號的字串可能會改變。
[!NOTE] These escape rules specify how PRQL interprets escape characters when compiling strings to SQL, not necessarily how the database will interpret the string. Dialects interpret escape characters differently, and PRQL doesn’t currently account for these differences. Please open issues with any difficulties in the current implementation.
轉義序列
除非存在 r 前綴,否則字串字面值中的轉義序列將按照類似於標準 C 所使用的規則進行解釋。公認的轉義序列是:
| 轉義序列 | 意義 |
|---|---|
\\ | 反斜線() |
\' | Single quote (’) |
\" | Double quote (“) |
\b | 退格 |
\f | 分頁符 |
\n | ASCII 換行符 (LF) |
\r | ASCII 回車符 (CR) |
\t | ASCII 水平製表符 (TAB) |
\xhh | 十六進位值為 hh 的字元 |
\u{xxxx} | 十六進位值為 xxxx 的字元 |