Skip to content

Commit 27a942b

Browse files
committed
prototype for array field
1 parent c755075 commit 27a942b

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

piccolo/columns/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,11 @@ def get_sql_value(self, value: t.Any) -> t.Any:
475475
output = f"'{value.isoformat().replace('T', '')}'"
476476
elif isinstance(value, bytes):
477477
output = f"'{value.hex()}'"
478+
elif isinstance(value, list):
479+
# Convert to the array syntax.
480+
output = (
481+
"'{" + ", ".join([self.get_sql_value(i) for i in value]) + "}'"
482+
)
478483
else:
479484
output = value
480485

piccolo/columns/column_types.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def get_querystring(
5252
value: t.Union[str, Varchar, Text],
5353
engine_type: str,
5454
reverse=False,
55-
):
55+
) -> QueryString:
5656
Concat = ConcatPostgres if engine_type == "postgres" else ConcatSQLite
5757

5858
if isinstance(value, (Varchar, Text)):
@@ -106,7 +106,7 @@ def get_querystring(
106106
operator: str,
107107
value: t.Union[int, float, Integer],
108108
reverse=False,
109-
):
109+
) -> QueryString:
110110
if isinstance(value, Integer):
111111
column: Integer = value
112112
if len(column._meta.call_chain) > 0:
@@ -1343,3 +1343,27 @@ class Blob(Bytea):
13431343
"""
13441344

13451345
pass
1346+
1347+
1348+
###############################################################################
1349+
1350+
1351+
class Array(Column):
1352+
value_type = list
1353+
1354+
def __init__(
1355+
self,
1356+
base_column: Column,
1357+
default: t.Union[t.List, t.Callable[[], t.List], None] = list,
1358+
**kwargs,
1359+
) -> None:
1360+
self._validate_default(default, (list, None))
1361+
1362+
self.base_column = base_column
1363+
self.default = default
1364+
kwargs.update({"base_column": base_column, "default": default})
1365+
super().__init__(**kwargs)
1366+
1367+
@property
1368+
def column_type(self):
1369+
return f"{self.base_column.column_type}[]"

tests/columns/test_array.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from unittest import TestCase
2+
3+
from piccolo.table import Table
4+
from piccolo.columns.column_types import Array, Integer
5+
6+
from ..base import postgres_only
7+
8+
9+
class MyTable(Table):
10+
value = Array(base_column=Integer())
11+
12+
13+
@postgres_only
14+
class TestArrayPostgres(TestCase):
15+
"""
16+
Make sure an Array column can be created.
17+
"""
18+
19+
def setUp(self):
20+
MyTable.create_table().run_sync()
21+
22+
def tearDown(self):
23+
MyTable.alter().drop_table().run_sync()
24+
25+
def test_storage(self):
26+
"""
27+
Make sure data can be stored and retrieved.
28+
"""
29+
MyTable(value=[1, 2, 3]).save().run_sync()
30+
31+
row = MyTable.objects().first().run_sync()
32+
self.assertEqual(row.value, [1, 2, 3])

0 commit comments

Comments
 (0)