8.4.2. bytea Escape Format

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.

“转义”格式是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.

在以转义格式输入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所示,单引号必须写两次的原因是,这对于SQL命令中的任何字符串文字都是正确的。通用字符串文字解析器使用最外面的单引号,并将任何一对单引号都简化为一个数据字符。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字符串方面,您可能还需要做其他工作。例如,如果您的界面自动翻译了换行符和回车符,则可能还必须转义它们。

发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104631415