8.4.2. bytea Escape Format

丹心明月發表於2021-01-04
8.4.2. bytea Escape Format
8.4.2.bytea escape格式
The “escape” format is the traditional PostgreSQL format for the bytea type. It takes the approach of  representing a binary string as a sequence of ASCII characters, while converting those bytes that cannot  be represented as an ASCII character into special escape sequences. If, from the point of view of the  application, representing bytes as characters makes sense, then this representation can be convenient.  But in practice it is usually confusing because it fuzzes up the distinction between binary strings and  character strings, and also the particular escape mechanism that was chosen is somewhat unwieldy. Therefore, this format should probably be avoided for most new applications.
“escape”格式是 PostgreSQL中 bytea型別的傳統格式。它採用將二進位制字串表示為ASCII字元序列的方法,同時將那些無法表示為ASCII字元的位元組轉換為特殊的轉義序列。如果從應用程式的角度來看,將位元組表示為字元是有意義的,那麼這種表示方式將很方便。但實際上,它通常令人困惑,因為它模糊了二進位制字串和字串之間的區別,並且所選擇的特殊轉義機制有些笨拙,因此,對於大多數新應用程式,應避免使用這種格式。
 
When entering bytea values in escape format, octets of certain values must be escaped, while all  octet values can be escaped. In general, to escape an octet, convert it into its three-digit octal value and  precede it by a backslash. Backslash itself (octet decimal value 92) can alternatively be represented  by double backslashes. Table 8.7 shows the characters that must be escaped, and gives the alternative  escape sequences where applicable.
在以escape格式輸入bytea值時,必須對某些值的八位位元組進行轉義,也可對所有八位位元組值都進行轉義。通常,要轉義八位位元組,需先將其轉換為三位數的八進位制值,並在其前加反斜槓。 反斜槓本身(八位位元組的十進位制值92)也可以用雙反斜槓表示。 表8.7顯示了必須轉義的字元,並給出了相適應的替代轉義序列。
 
The requirement to escape non-printable octets varies depending on locale settings. In some instances  you can get away with leaving them unescaped.
對不可列印的八位位元組進行轉義的要求取決於語言環境設定。在某些情況下,可以無需轉義。
 
The reason that single quotes must be doubled, as shown in Table 8.7, is that this is true for any string  literal in a SQL command. The generic string-literal parser consumes the outermost single quotes and  reduces any pair of single quotes to one data character. What the bytea input function sees is just  one single quote, which it treats as a plain data character. However, the bytea input function treats  backslashes as special, and the other behaviors shown in Table 8.7 are implemented by that function.
如表8.7所示,單引號必須寫兩次。通用字串文字解析器使用最外面的單引號,並將任何一對單引號都簡化為一個資料字元。bytea輸入函式看到的只是一個單引號,它將其視為純資料字元。 但是,bytea輸入函式將反斜槓視為特殊字元,表8.7中所示的其他行為由該函式實現。
 
In some contexts, backslashes must be doubled compared to what is shown above, because the generic  string-literal parser will also reduce pairs of backslashes to one data character; see Section 4.1.2.1.
在某些情況下,與上面顯示的相比,反斜槓必須加倍,因為通用字串文字解析器還將把反斜槓對減少為一個資料字元。請參閱 第4.1.2.1節
 
Bytea octets are output in hex format by default. If you change bytea_output to escape , “nonprintable”  octets are converted to their equivalent three-digit octal value and preceded by one backslash. Most “printable” octets are output by their standard representation in the client character set, e.g.:
預設,bytea位元組以十六進位制格式輸出。如果將bytea_output更改為escape,則``不可列印的''八位位組將轉換為等效的三位數八進位制值並以一個反斜槓開頭。大多數``可列印的''八位位組均以其在客戶端字符集中的標準表示形式輸出,例如:
 
SET bytea_output = 'escape';
SELECT 'abc \153\154\155 \052\251\124'::bytea;
bytea
----------------
abc klm *\251T
 
 
The octet with decimal value 92 (backslash) is doubled in the output. Details are in Table 8.8.
十進位制值92(反斜槓)的八位位元組 在輸出中為兩個反斜槓 。 詳情參見表8.8。
 
Depending on the front end to PostgreSQL you use, you might have additional work to do in terms of  escaping and unescaping bytea strings. For example, you might also have to escape line feeds and  carriage returns if your interface automatically translates these. 
根據所使用的PostgreSQL的前端,在轉義和非轉義bytea字串方面,您可能還需要做其他工作。例如,如果您的介面自動翻譯了換行符和回車符,則可能還必須轉義它們。
 

相關文章