A PostgreSQL ARRAY literal. This is used to produce ARRAY literals in SQL expressions, e.g.:: from sqlalchemy.dialects.postgresql import array from sqlalchemy.dialects import postgresql from sqlalchemy import select, func stmt = select(array([1, 2]) + array([3,
| 74 | |
| 75 | |
| 76 | class array(expression.ExpressionClauseList[_T]): |
| 77 | """A PostgreSQL ARRAY literal. |
| 78 | |
| 79 | This is used to produce ARRAY literals in SQL expressions, e.g.:: |
| 80 | |
| 81 | from sqlalchemy.dialects.postgresql import array |
| 82 | from sqlalchemy.dialects import postgresql |
| 83 | from sqlalchemy import select, func |
| 84 | |
| 85 | stmt = select(array([1, 2]) + array([3, 4, 5])) |
| 86 | |
| 87 | print(stmt.compile(dialect=postgresql.dialect())) |
| 88 | |
| 89 | Produces the SQL: |
| 90 | |
| 91 | .. sourcecode:: sql |
| 92 | |
| 93 | SELECT ARRAY[%(param_1)s, %(param_2)s] || |
| 94 | ARRAY[%(param_3)s, %(param_4)s, %(param_5)s]) AS anon_1 |
| 95 | |
| 96 | An instance of :class:`.array` will always have the datatype |
| 97 | :class:`_types.ARRAY`. The "inner" type of the array is inferred from the |
| 98 | values present, unless the :paramref:`_postgresql.array.type_` keyword |
| 99 | argument is passed:: |
| 100 | |
| 101 | array(["foo", "bar"], type_=CHAR) |
| 102 | |
| 103 | When constructing an empty array, the :paramref:`_postgresql.array.type_` |
| 104 | argument is particularly important as PostgreSQL server typically requires |
| 105 | a cast to be rendered for the inner type in order to render an empty array. |
| 106 | SQLAlchemy's compilation for the empty array will produce this cast so |
| 107 | that:: |
| 108 | |
| 109 | stmt = array([], type_=Integer) |
| 110 | print(stmt.compile(dialect=postgresql.dialect())) |
| 111 | |
| 112 | Produces: |
| 113 | |
| 114 | .. sourcecode:: sql |
| 115 | |
| 116 | ARRAY[]::INTEGER[] |
| 117 | |
| 118 | As required by PostgreSQL for empty arrays. |
| 119 | |
| 120 | .. versionadded:: 2.0.40 added support to render empty PostgreSQL array |
| 121 | literals with a required cast. |
| 122 | |
| 123 | Multidimensional arrays are produced by nesting :class:`.array` constructs. |
| 124 | The dimensionality of the final :class:`_types.ARRAY` |
| 125 | type is calculated by |
| 126 | recursively adding the dimensions of the inner :class:`_types.ARRAY` |
| 127 | type:: |
| 128 | |
| 129 | stmt = select( |
| 130 | array( |
| 131 | [array([1, 2]), array([3, 4]), array([column("q"), column("x")])] |
| 132 | ) |
| 133 | ) |
no outgoing calls