From 64c14c627859aaf02f0fea8b254a012e944527c3 Mon Sep 17 00:00:00 2001 From: Harris Minhas Date: Tue, 1 Aug 2023 16:54:59 +0500 Subject: [PATCH 1/2] BooleanField code --- examples/cpu_usage/collect.py | 2 +- examples/cpu_usage/models.py | 3 ++- src/infi/clickhouse_orm/fields.py | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/examples/cpu_usage/collect.py b/examples/cpu_usage/collect.py index 34ee5b4c..f2c4b385 100644 --- a/examples/cpu_usage/collect.py +++ b/examples/cpu_usage/collect.py @@ -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) ]) diff --git a/examples/cpu_usage/models.py b/examples/cpu_usage/models.py index c19007a6..a233a215 100644 --- a/examples/cpu_usage/models.py +++ b/examples/cpu_usage/models.py @@ -1,4 +1,4 @@ -from infi.clickhouse_orm import Model, DateTimeField, UInt16Field, Float32Field, Memory +from infi.clickhouse_orm import Model, DateTimeField, UInt16Field, Float32Field, Memory, BooleanField class CPUStats(Model): @@ -6,6 +6,7 @@ class CPUStats(Model): timestamp = DateTimeField() cpu_id = UInt16Field() cpu_percent = Float32Field() + working_status = BooleanField() engine = Memory() diff --git a/src/infi/clickhouse_orm/fields.py b/src/infi/clickhouse_orm/fields.py index 6e73e3fe..e326a4df 100644 --- a/src/infi/clickhouse_orm/fields.py +++ b/src/infi/clickhouse_orm/fields.py @@ -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 = 'UInt8' + # 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) From 5028769c3ba378cc142e373f42d59a88212f20a8 Mon Sep 17 00:00:00 2001 From: Harris Minhas Date: Wed, 2 Aug 2023 09:19:42 +0500 Subject: [PATCH 2/2] db type for BooleanField --- src/infi/clickhouse_orm/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/infi/clickhouse_orm/fields.py b/src/infi/clickhouse_orm/fields.py index e326a4df..3c4f584b 100644 --- a/src/infi/clickhouse_orm/fields.py +++ b/src/infi/clickhouse_orm/fields.py @@ -164,7 +164,7 @@ def validate(self, value): class BooleanField(Field): # The ClickHouse column type to use - db_type = 'UInt8' + db_type = 'Bool' # The default value class_default = False