Skip to content

Commit fe49cd1

Browse files
committed
Add more types to the Example model
1 parent 1e098f8 commit fe49cd1

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

python/jupyter_notebook/cs_models.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from sqlalchemy.orm import DeclarativeBase, mapped_column, Mapped, sessionmaker
2-
from sqlalchemy.types import TypeDecorator, String
2+
from sqlalchemy.types import TypeDecorator, String, Integer, Date, Boolean, Float
33
from sqlalchemy import create_engine, select, text
44
from sqlalchemy.exc import IntegrityError
55
import json
@@ -19,7 +19,7 @@ def process_bind_param(self, value, dialect):
1919
if value is not None:
2020
value_dict = {
2121
"k": "pt",
22-
"p": value,
22+
"p": str(value),
2323
"i": {
2424
"t": self.table_name,
2525
"c": self.column_name
@@ -34,13 +34,40 @@ def process_result_value(self, value, dialect):
3434
return None
3535
return value['p']
3636

37+
class EncryptedInt(CsTypeDecorator):
38+
impl = String
3739

38-
class EncryptedUtf8Str(CsTypeDecorator):
40+
def __init__(self, *args, **kwargs):
41+
super().__init__(*args, **kwargs)
42+
43+
class EncryptedBoolean(CsTypeDecorator):
44+
impl = String
45+
46+
def __init__(self, *args, **kwargs):
47+
super().__init__(*args, **kwargs)
48+
49+
def process_bind_param(self, value, dialect):
50+
if value is not None:
51+
value = str(value).lower()
52+
return super().process_bind_param(value, dialect)
53+
54+
class EncryptedDate(CsTypeDecorator):
55+
impl = String
56+
57+
def __init__(self, *args, **kwargs):
58+
super().__init__(*args, **kwargs)
59+
60+
class EncryptedFloat(CsTypeDecorator):
3961
impl = String
4062

4163
def __init__(self, *args, **kwargs):
4264
super().__init__(*args, **kwargs)
4365

66+
class EncryptedUtf8Str(CsTypeDecorator):
67+
impl = String
68+
69+
def __init__(self, *args, **kwargs):
70+
super().__init__(*args, **kwargs)
4471

4572
class EncryptedJsonb(CsTypeDecorator):
4673
impl = String
@@ -55,12 +82,20 @@ class Example(BaseModel):
5582
__tablename__ = "examples"
5683

5784
id: Mapped[int] = mapped_column(primary_key=True)
58-
encrypted_utf8_str = mapped_column(EncryptedUtf8Str("examples", "encrypted_utf8_str"))
59-
encrypted_jsonb = mapped_column(EncryptedJsonb("examples", "encrypted_jsonb"))
60-
61-
def __init__(self, utf8_str=None, jsonb=None):
62-
self.encrypted_utf8_str = utf8_str
63-
self.encrypted_jsonb = jsonb
85+
encrypted_int = mapped_column(EncryptedInt(__tablename__, "encrypted_int"))
86+
encrypted_boolean = mapped_column(EncryptedBoolean(__tablename__, "encrypted_boolean"))
87+
encrypted_date = mapped_column(EncryptedDate(__tablename__, "encrypted_date"))
88+
encrypted_float = mapped_column(EncryptedFloat(__tablename__, "encrypted_float"))
89+
encrypted_utf8_str = mapped_column(EncryptedUtf8Str(__tablename__, "encrypted_utf8_str"))
90+
encrypted_jsonb = mapped_column(EncryptedJsonb(__tablename__, "encrypted_jsonb"))
91+
92+
def __init__(self, e_utf8_str=None, e_jsonb=None, e_int=None, e_float=None, e_date=None, e_bool=None):
93+
self.encrypted_utf8_str = e_utf8_str
94+
self.encrypted_jsonb = e_jsonb
95+
self.encrypted_int = e_int
96+
self.encrypted_float = e_float
97+
self.encrypted_date = e_date
98+
self.encrypted_boolean = e_bool
6499

65100
def __repr__(self):
66-
return f"<Example(id={self.id}, encrypted_utf8_str={self.encrypted_utf8_str}, encrypted_jsonb={self.encrypted_jsonb})>"
101+
return f"<Example(id={self.id}, encrypted_utf8_str={self.encrypted_utf8_str}, encrypted_jsonb={self.encrypted_jsonb}, encrypted_int={self.encrypted_int}, encrypted_float={self.encrypted_float}, encrypted_date={self.encrypted_date}, encrypted_boolean={self.encrypted_boolean})>"

python/jupyter_notebook/cs_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class CsRow:
9191
'encrypted_boolean': CsBool.from_parsed_json,
9292
'encrypted_date': CsDate.from_parsed_json,
9393
'encrypted_float': CsFloat.from_parsed_json,
94-
'encrypted_utf8_str': CsText.from_parsed_json
94+
'encrypted_utf8_str': CsText.from_parsed_json,
95+
'encrypted_jsonb': CsText.from_parsed_json
9596
}
9697

9798
def __init__(self, row):

0 commit comments

Comments
 (0)