1
1
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
3
3
from sqlalchemy import create_engine , select , text
4
4
from sqlalchemy .exc import IntegrityError
5
5
import json
@@ -19,7 +19,7 @@ def process_bind_param(self, value, dialect):
19
19
if value is not None :
20
20
value_dict = {
21
21
"k" : "pt" ,
22
- "p" : value ,
22
+ "p" : str ( value ) ,
23
23
"i" : {
24
24
"t" : self .table_name ,
25
25
"c" : self .column_name
@@ -34,13 +34,40 @@ def process_result_value(self, value, dialect):
34
34
return None
35
35
return value ['p' ]
36
36
37
+ class EncryptedInt (CsTypeDecorator ):
38
+ impl = String
37
39
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 ):
39
61
impl = String
40
62
41
63
def __init__ (self , * args , ** kwargs ):
42
64
super ().__init__ (* args , ** kwargs )
43
65
66
+ class EncryptedUtf8Str (CsTypeDecorator ):
67
+ impl = String
68
+
69
+ def __init__ (self , * args , ** kwargs ):
70
+ super ().__init__ (* args , ** kwargs )
44
71
45
72
class EncryptedJsonb (CsTypeDecorator ):
46
73
impl = String
@@ -55,12 +82,20 @@ class Example(BaseModel):
55
82
__tablename__ = "examples"
56
83
57
84
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
64
99
65
100
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 } )>"
0 commit comments