mirror of
https://github.com/Infinidat/infi.clickhouse_orm.git
synced 2025-02-22 03:00:33 +03:00
Added documentation about custom fields
This commit is contained in:
parent
b976899f75
commit
0f9487f589
|
@ -180,8 +180,8 @@ Here's another example - a field for storing UUIDs in the database as 16-byte st
|
||||||
```python
|
```python
|
||||||
from infi.clickhouse_orm.fields import Field
|
from infi.clickhouse_orm.fields import Field
|
||||||
from infi.clickhouse_orm.utils import escape
|
from infi.clickhouse_orm.utils import escape
|
||||||
from six import string_types
|
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
import six
|
||||||
|
|
||||||
class UUIDField(Field):
|
class UUIDField(Field):
|
||||||
|
|
||||||
|
@ -195,9 +195,9 @@ Here's another example - a field for storing UUIDs in the database as 16-byte st
|
||||||
# Convert valid values to UUID instance
|
# Convert valid values to UUID instance
|
||||||
if isinstance(value, UUID):
|
if isinstance(value, UUID):
|
||||||
return value
|
return value
|
||||||
elif isinstance(value, string_types):
|
elif isinstance(value, six.string_types):
|
||||||
return UUID(bytes=value) if len(value) == 16 else UUID(value)
|
return UUID(bytes=value.encode('latin1')) if len(value) == 16 else UUID(value)
|
||||||
elif isinstance(value, (int, long)):
|
elif isinstance(value, six.integer_types):
|
||||||
return UUID(int=value)
|
return UUID(int=value)
|
||||||
elif isinstance(value, tuple):
|
elif isinstance(value, tuple):
|
||||||
return UUID(fields=value)
|
return UUID(fields=value)
|
||||||
|
@ -206,9 +206,15 @@ Here's another example - a field for storing UUIDs in the database as 16-byte st
|
||||||
|
|
||||||
def to_db_string(self, value, quote=True):
|
def to_db_string(self, value, quote=True):
|
||||||
# The value was already converted by to_python, so it's a UUID instance
|
# The value was already converted by to_python, so it's a UUID instance
|
||||||
return escape(value.bytes, quote)
|
val = value.bytes
|
||||||
|
if six.PY3:
|
||||||
|
val = str(val, 'latin1')
|
||||||
|
return escape(val, quote)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note that the latin-1 encoding is used as an identity encoding for converting between raw bytes and strings. This is required in Python 3, where `str` and `bytes` are different types.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
[<< Querysets](querysets.md) | [Table of Contents](toc.md) | [Table Engines >>](table_engines.md)
|
[<< Querysets](querysets.md) | [Table of Contents](toc.md) | [Table Engines >>](table_engines.md)
|
|
@ -1,6 +1,6 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import unittest
|
import unittest
|
||||||
from six import string_types
|
import six
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from infi.clickhouse_orm.database import Database
|
from infi.clickhouse_orm.database import Database
|
||||||
from infi.clickhouse_orm.fields import Field, Int16Field
|
from infi.clickhouse_orm.fields import Field, Int16Field
|
||||||
|
@ -101,9 +101,9 @@ class UUIDField(Field):
|
||||||
# Convert valid values to UUID instance
|
# Convert valid values to UUID instance
|
||||||
if isinstance(value, UUID):
|
if isinstance(value, UUID):
|
||||||
return value
|
return value
|
||||||
elif isinstance(value, string_types):
|
elif isinstance(value, six.string_types):
|
||||||
return UUID(bytes=value) if len(value) == 16 else UUID(value)
|
return UUID(bytes=value.encode('latin1')) if len(value) == 16 else UUID(value)
|
||||||
elif isinstance(value, (int, long)):
|
elif isinstance(value, six.integer_types):
|
||||||
return UUID(int=value)
|
return UUID(int=value)
|
||||||
elif isinstance(value, tuple):
|
elif isinstance(value, tuple):
|
||||||
return UUID(fields=value)
|
return UUID(fields=value)
|
||||||
|
@ -112,4 +112,8 @@ class UUIDField(Field):
|
||||||
|
|
||||||
def to_db_string(self, value, quote=True):
|
def to_db_string(self, value, quote=True):
|
||||||
# The value was already converted by to_python, so it's a UUID instance
|
# The value was already converted by to_python, so it's a UUID instance
|
||||||
return escape(value.bytes, quote)
|
val = value.bytes
|
||||||
|
if six.PY3:
|
||||||
|
val = str(val, 'latin1')
|
||||||
|
return escape(val, quote)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user