Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: BooleanField for ORM #206

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/cpu_usage/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
timestamp = datetime.datetime.now()
print(timestamp)
db.insert([
CPUStats(timestamp=timestamp, cpu_id=cpu_id, cpu_percent=cpu_percent)
CPUStats(timestamp=timestamp, cpu_id=cpu_id, cpu_percent=cpu_percent, working_status=True)
for cpu_id, cpu_percent in enumerate(stats)
])
3 changes: 2 additions & 1 deletion examples/cpu_usage/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from infi.clickhouse_orm import Model, DateTimeField, UInt16Field, Float32Field, Memory
from infi.clickhouse_orm import Model, DateTimeField, UInt16Field, Float32Field, Memory, BooleanField


class CPUStats(Model):

timestamp = DateTimeField()
cpu_id = UInt16Field()
cpu_percent = Float32Field()
working_status = BooleanField()

engine = Memory()

18 changes: 18 additions & 0 deletions src/infi/clickhouse_orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,25 @@ def validate(self, value):
if len(value) > self._length:
raise ValueError('Value of %d bytes is too long for FixedStringField(%d)' % (len(value), self._length))

class BooleanField(Field):
# The ClickHouse column type to use
db_type = 'Bool'
# The default value
class_default = False

def to_python(self, value, timezone_in_use):
# Convert valid values to bool
if value in (1, '1', True):
return True
elif value in (0, '0', False):
return False
else:
raise ValueError('Invalid value for BooleanField: %r' % value)

def to_db_string(self, value, quote=True):
# The value was already converted by to_python, so it's a bool
return '1' if value else '0'

class DateField(Field):

min_value = datetime.date(1970, 1, 1)
Expand Down