FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlmodel/sqlmodel/sql/sqltypes.py at main · codassassin/sqlmodel · GitHub
codassassin
sqlmodel
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlmodel
/
sqlmodel
/
sql
/
sqltypes.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (47 loc) · 1.79 KB
Breadcrumbs
sqlmodel
/
sqlmodel
/
sql
/
sqltypes.py
Copy path
File metadata and controls
60 lines (47 loc) · 1.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import
uuid
from
typing
import
Any
,
cast
from
sqlalchemy
import
types
from
sqlalchemy
.
dialects
.
postgresql
import
UUID
from
sqlalchemy
.
engine
.
interfaces
import
Dialect
from
sqlalchemy
.
types
import
CHAR
,
TypeDecorator
class
AutoString
(
types
.
TypeDecorator
):
impl
=
types
.
String
cache_ok
=
True
mysql_default_length
=
255
def
load_dialect_impl
(
self
,
dialect
:
Dialect
)
->
"types.TypeEngine[Any]"
:
impl
=
cast
(
types
.
String
,
self
.
impl
)
if
impl
.
length
is
None
and
dialect
.
name
==
"mysql"
:
return
dialect
.
type_descriptor
(
types
.
String
(
self
.
mysql_default_length
))
# type: ignore
return
super
().
load_dialect_impl
(
dialect
)
# Reference form SQLAlchemy docs: https://docs.sqlalchemy.org/en/14/core/custom_types.html#backend-agnostic-guid-type
# with small modifications
class
GUID
(
TypeDecorator
):
"""Platform-independent GUID type.
Uses PostgreSQL's UUID type, otherwise uses
CHAR(32), storing as stringified hex values.
"""
impl
=
CHAR
cache_ok
=
True
def
load_dialect_impl
(
self
,
dialect
):
if
dialect
.
name
==
"postgresql"
:
return
dialect
.
type_descriptor
(
UUID
())
else
:
return
dialect
.
type_descriptor
(
CHAR
(
32
))
def
process_bind_param
(
self
,
value
,
dialect
):
if
value
is
None
:
return
value
elif
dialect
.
name
==
"postgresql"
:
return
str
(
value
)
else
:
if
not
isinstance
(
value
,
uuid
.
UUID
):
return
f"
{
uuid
.
UUID
(
value
).
int
:x
}
"
else
:
# hexstring
return
f"
{
value
.
int
:x
}
"
def
process_result_value
(
self
,
value
,
dialect
):
if
value
is
None
:
return
value
else
:
if
not
isinstance
(
value
,
uuid
.
UUID
):
value
=
uuid
.
UUID
(
value
)
return
value
Back
|
FazBrowse Home
|
New Git URL