-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
369 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "scyllapy" | ||
version = "1.2.1" | ||
version = "1.3.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,48 @@ | ||
import dataclasses | ||
from typing import Any, List | ||
|
||
from ._internal.extra_types import BigInt, Counter, Double, SmallInt, TinyInt, Unset | ||
|
||
__all__ = ("BigInt", "Counter", "Double", "SmallInt", "TinyInt", "Unset") | ||
try: | ||
import pydantic | ||
except ImportError: | ||
pydantic = None | ||
|
||
|
||
class ScyllaPyUDT: | ||
""" | ||
Class for declaring UDT models. | ||
This class is a mixin for models like dataclasses and pydantic models, | ||
or classes that have `__slots__` attribute. | ||
It can be further extended to support other model types. | ||
""" | ||
|
||
def __dump_udt__(self) -> List[Any]: | ||
""" | ||
Method to dump UDT models to a dict. | ||
This method returns a list of values in the order of the UDT fields. | ||
Because in the protocol, UDT fields should be sent in the same order as | ||
they were declared. | ||
""" | ||
if dataclasses.is_dataclass(self): | ||
values = [] | ||
for field in dataclasses.fields(self): | ||
values.append(getattr(self, field.name)) | ||
return values | ||
if pydantic is not None and isinstance(self, pydantic.BaseModel): | ||
values = [] | ||
for param in self.__class__.__signature__.parameters: | ||
values.append(getattr(self, param)) | ||
return values | ||
if hasattr(self, "__slots__"): | ||
values = [] | ||
for slot in self.__slots__: | ||
values.append(getattr(self, slot)) | ||
return values | ||
raise ValueError("Unsupported model type") | ||
|
||
|
||
__all__ = ("BigInt", "Counter", "Double", "SmallInt", "TinyInt", "Unset", "ScyllaPyUDT") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytest | ||
from tests.utils import random_string | ||
|
||
from scyllapy import Scylla | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_udt_parsing(scylla: Scylla) -> None: | ||
table_name = random_string(4) | ||
await scylla.execute(f"CREATE TYPE test_udt{table_name} (id int, name text)") | ||
await scylla.execute( | ||
f"CREATE TABLE {table_name} " | ||
f"(id int PRIMARY KEY, udt_col frozen<test_udt{table_name}>)", | ||
) | ||
await scylla.execute( | ||
f"INSERT INTO {table_name} (id, udt_col) VALUES (1, {{id: 1, name: 'test'}})", | ||
) | ||
res = await scylla.execute(f"SELECT * FROM {table_name}") | ||
assert res.all() == [{"id": 1, "udt_col": {"id": 1, "name": "test"}}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.