mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 01:44:31 +02:00
Improved column type introspection, closes #92
This commit is contained in:
parent
43f1c6ab4e
commit
1125460497
2 changed files with 21 additions and 14 deletions
|
|
@ -33,3 +33,22 @@ def suggest_column_types(records):
|
|||
t = str
|
||||
column_types[key] = t
|
||||
return column_types
|
||||
|
||||
|
||||
def column_affinity(column_type):
|
||||
# Implementation of SQLite affinity rules from
|
||||
# https://www.sqlite.org/datatype3.html#determination_of_column_affinity
|
||||
assert isinstance(column_type, str)
|
||||
column_type = column_type.upper().strip()
|
||||
if column_type == "":
|
||||
return str # We differ from spec, which says it should be BLOB
|
||||
if "INT" in column_type:
|
||||
return int
|
||||
if "CHAR" in column_type or "CLOB" in column_type or "TEXT" in column_type:
|
||||
return str
|
||||
if "BLOB" in column_type:
|
||||
return bytes
|
||||
if "REAL" in column_type or "FLOA" in column_type or "DOUB" in column_type:
|
||||
return float
|
||||
# Default is 'NUMERIC', which we currently also treat as float
|
||||
return float
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue