8.15.6. Array Input and Output Syntax

8.15.6. Array Input and Output Syntax
8.15.6.數組輸入和輸出語法
The external text representation of an array value consists of items that are interpreted according to the I/O conversion rules for the array's element type, plus decoration that indicates the array structure.The decoration consists of curly braces ({ and }) around the array value plus delimiter characters between adjacent items. The delimiter character is usually a comma (,) but can be something else: it is determined by the typdelim setting for the array's element type. Among the standard data types provided in the PostgreSQL distribution, all use a comma, except for type box, which uses a semicolon (;). In a multidimensional array, each dimension (row, plane, cube, etc.) gets its own level of curly braces, and delimiters must be written between adjacent curly-braced entities of the same level.
數組值的外部文本表示形式包括根據數組元素類型的I/O轉換規則解釋的項目以及表示數組結構的修飾。修飾由數組值周圍的花括號({和})加上相鄰項目之間的分隔符組成。分隔符通常是逗號(,),但也可以是其他字符:它由數組元素類型的typdelim設置確定。在PostgreSQL發行版中提供的標準數據類型中,除了box類型使用分號(;)之外,所有數據均使用逗號。在多維數組中,每個維度(行,平面,立方體等)都有其自己的花括號級別,並且必須在同一級別的相鄰花括號實體之間編寫分隔符。
 
The array output routine will put double quotes around element values if they are empty strings, contain curly braces, delimiter characters, double quotes, backslashes, or white space, or match the word NULL. Double quotes and backslashes embedded in element values will be backslash-escaped. For numeric data types it is safe to assume that double quotes will never appear, but for textual data types one should be prepared to cope with either the presence or absence of quotes.
如果數組值是空字符串,包含大括號,定界符,雙引號,反斜槓或空格,或者匹配單詞NULL,則數組輸出時將在元素值兩邊加上雙引號。 元素值中嵌入的雙引號和反斜槓將被反斜槓轉義。 對於數字數據類型,可以安全地假定雙引號將永遠不會出現,但是對於文本數據類型,應準備使用雙引號來應對是否存在引號的情形。
 
By default, the lower bound index value of an array's dimensions is set to one. To represent arrays with other lower bounds, the array subscript ranges can be specified explicitly before writing the array contents. This decoration consists of square brackets ([]) around each array dimension's lower and upper bounds, with a colon (:) delimiter character in between. The array dimension decoration is followed by an equal sign (=). For example:
默認情況下,數組維的下界索引值設置爲1。爲了表示具有其他下限的數組,可以在寫入數組內容之前顯式指定數組下標範圍。此修飾由圍繞每個數組維的上下邊界的方括號([])組成,中間是一個冒號(:)分隔符。 數組維修飾後跟一個等號(=)。 例如:
 
SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
FROM (SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[] AS f1)
AS ss;
e1 | e2
----+----
1 | 6
(1 row)
 
The array output routine will include explicit dimensions in its result only when there are one or more lower bounds different from one.
僅當存在一個或多個與下界一不同的下限時,數組輸出例程纔會在其結果中顯式包括維度。
 
If the value written for an element is NULL (in any case variant), the element is taken to be NULL.The presence of any quotes or backslashes disables this and allows the literal string value “NULL” to be entered. Also, for backward compatibility with pre-8.2 versions of PostgreSQL, the array_nulls configuration parameter can be turned off to suppress recognition of NULL as a NULL.
如果爲元素寫入的值是NULL(在任何情況下都是變體),則該元素被視爲NULL。任何引號或反斜槓的出現都會禁用該值,並允許輸入文字字符串值“ NULL”。另外,爲了與PostgreSQL 8.2之前的版本向後兼容,可以關閉array_nulls配置參數以禁止將NULL識別爲NULL。
 
As shown previously, when writing an array value you can use double quotes around any individual array element. You must do so if the element value would otherwise confuse the array-value parser. For example, elements containing curly braces, commas (or the data type's delimiter character), double quotes, backslashes, or leading or trailing whitespace must be double-quoted. Empty strings and strings matching the word NULL must be quoted, too. To put a double quote or backslash in a quoted array element value, precede it with a backslash. Alternatively, you can avoid quotes and use backslash-escaping to protect all data characters that would otherwise be taken as array syntax.
如前所示,在編寫數組值時,可以在任何單個數組元素周圍使用雙引號。如果元素值會使數組值解析器混亂的話,則必須這樣做。例如,包含大括號,逗號(或數據類型的定界符),雙引號,反斜槓或前導或尾隨空格的元素必須用雙引號引起來。空字符串和與單詞NULL匹配的字符串也必須加引號。要將雙引號或反斜槓放在帶引號的數組元素值中,請在其前面加上反斜槓。另外,您可以避免使用引號,並使用反斜槓轉義來保護所有數據字符,否則這些數據字符將被視爲數組語法。
 
You can add whitespace before a left brace or after a right brace. You can also add whitespace before or after any individual item string. In all of these cases the whitespace will be ignored. However,whitespace within double-quoted elements, or surrounded on both sides by non-whitespace characters of an element, is not ignored.
可以在左括號前或右括號後添加空格。 還可以在任何單個項目字符串之前或之後添加空格。 在所有這些情況下,空格都將被忽略。 但是,不能忽略雙引號內的空格,或者在元素的兩側都用非空格字符包圍的空格。
 
Tip
小貼士
The ARRAY constructor syntax (see Section 4.2.12) is often easier to work with than the array-literal syntax when writing array values in SQL commands. In ARRAY, individual element values are written the same way they would be written when not members of an array.
在SQL命令中寫入數組值時,ARRAY構造函數語法(請參見第4.2.12節)通常比數組文字語法更易於使用。 在ARRAY中,單個元素值的寫入方式與非數組成員時的寫入方式相同。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章