8.16.6. Composite Type Input and Output Syntax

8.16.6. Composite Type Input and Output Syntax
8.16.6.複合類型的輸入及輸出語法
The external text representation of a composite value consists of items that are interpreted according to the I/O conversion rules for the individual field types, plus decoration that indicates the composite structure. The decoration consists of parentheses (( and )) around the whole value, plus commas (,between adjacent items. Whitespace outside the parentheses is ignored, but within the parentheses it is considered part of the field value, and might or might not be significant depending on the input conversion rules for the field data type. For example, in:
複合值的外部文本表示由根據各個字段類型的I/O轉換規則解釋的條目以及表示複合結構的修飾組成。修飾由圍繞整個值的圓括號((和)),以及相鄰項目之間的逗號(,)組成。 括號外的空格將被忽略,但括號內的空格將被視爲字段值的一部分,並且轉換規則取決於輸入字段的數據類型,該空格可能有效也可能無效。 例如,在:
 
'( 42)'
 
the whitespace will be ignored if the field type is integer, but not if it is text.
如果該類型爲整數,則會省略空格;如果類型爲文本,則不會省略。
 
As shown previously, when writing a composite value you can write double quotes around any individual field value. You must do so if the field value would otherwise confuse the composite-value parser. In particular, fields containing parentheses, commas, double quotes, or backslashes must be double-quoted. To put a double quote or backslash in a quoted composite field value, precede it with a backslash. (Also, a pair of double quotes within a double-quoted field value is taken to represent a double quote character, analogously to the rules for single quotes in SQL literal strings.) Alternatively,you can avoid quoting and use backslash-escaping to protect all data characters that would otherwise be taken as composite syntax.
如前所示,編寫複合值時,可以在任何單個字段值周圍寫雙引號。 如果字段值會使複合值解析器混亂,則必須這樣做。 特別是,包含括號,逗號,雙引號或反斜槓的字段必須用雙引號引起來。要將雙引號或反斜槓放在帶引號的複合字段值中,請在其前面加上反斜槓。(此外,雙引號字段值內的一對雙引號也代表一個雙引號字符,類似於SQL文字字符串中單引號的規則。)或者,可以避免使用引號並使用反斜槓轉義來保護所有可能被視爲複合語法的數據字符。
 
A completely empty field value (no characters at all between the commas or parentheses) represents a NULL. To write a value that is an empty string rather than NULL, write "".
完全爲空的字段值(逗號或括號之間完全沒有字符)表示NULL。 要寫一個空字符串而不是NULL的值,寫“”。
 
The composite output routine will put double quotes around field values if they are empty strings or contain parentheses, commas, double quotes, backslashes, or white space. (Doing so for white space is not essential, but aids legibility.) Double quotes and backslashes embedded in field values will be doubled.
 
如果輸出複合類型中爲空字符串或包含括號,逗號,雙引號,反斜槓或空格,則輸出時將在字段值兩邊加上雙引號。 (這樣做對空格不是必需的,但有助於提高可讀性。)字段值中嵌入的雙引號和反斜槓將加倍。
 
Note
Remember that what you write in an SQL command will first be interpreted as a string literal, and then as a composite. This doubles the number of backslashes you need (assuming escape string syntax is used). For example, to insert a text field containinga double quote and a backslash in a composite value, you'd need to write:
請記住,在SQL命令中編寫的內容將首先解釋爲字符串文字,然後再解釋爲組合。 這會使所需的反斜槓數量加倍(假設使用了轉義字符串語法)。 例如,要在複合值中插入包含雙引號和反斜槓的文本字段,則需要編寫爲:
 
INSERT ... VALUES ('("\"\\")');
 
The string-literal processor removes one level of backslashes, so that what arrives at the composite-value parser looks like ("\"\\"). In turn, the string fed to the text data type's input routine becomes "\. (If we were working with a data type whose input routine also treated backslashes specially, bytea for example, we might need as many as eight backslashes in the command to get one backslash into the stored composite field.) Dollar quoting (see Section 4.1.2.4) can be used to avoid the need to double backslashes.
字符串字面量處理器除去一級反斜槓,使得到達複合值解析器的內容看起來像(“ \” \\“)。反過來,饋送到文本數據類型的輸入例程的字符串變爲” \“。 (如果使用的數據類型的輸入例程也對反斜槓進行了特殊處理,例如bytea,則在命令中可能需要多達8個反斜槓才能使一個反斜槓進入存儲的複合字段。)美元引號(請參閱第4.1.2.4節)可以避免使用雙反斜槓。
 
Tip
小貼士
The ROW constructor syntax is usually easier to work with than the composite-literal syntax when writing composite values in SQL commands. In ROW, individual field values are written the same way they would be written when not members of a composite.
在SQL命令中編寫複合值時,ROW構造函數語法通常比複合文字語法更易於使用。 在ROW中,單個字段值的寫入方式與非組合成員時的寫入方式相同。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章