1
1
import re
2
+ from collections import namedtuple
2
3
3
- from django .db .backends .base .introspection import (
4
- BaseDatabaseIntrospection ,
5
- FieldInfo ,
6
- TableInfo ,
7
- )
4
+ from django .db .backends .base .introspection import BaseDatabaseIntrospection
5
+ from django .db .backends .base .introspection import FieldInfo as BaseFieldInfo
6
+ from django .db .backends .base .introspection import TableInfo as BaseTableInfo
8
7
from django .utils .functional import cached_property
9
8
9
+ FieldInfo = namedtuple ("FieldInfo" , BaseFieldInfo ._fields + ("comment" ,))
10
+ TableInfo = namedtuple ("TableInfo" , BaseTableInfo ._fields + ("comment" ,))
11
+
10
12
constraint_pattern = re .compile (
11
13
r"CONSTRAINT (`)?((?(1)(?:[^\\`]|\\.)+|\S+))(?(1)`|) (CHECK .+?),?\n"
12
14
)
16
18
17
19
18
20
class DatabaseIntrospection (BaseDatabaseIntrospection ):
19
- ignored_tables = []
20
-
21
21
def get_field_type (self , data_type , description ):
22
22
if data_type .startswith ("LowCardinality" ): # LowCardinality(Int16)
23
23
data_type = data_type [15 :- 1 ]
24
24
if data_type .startswith ("Nullable" ): # Nullable(Int16)
25
25
data_type = data_type [9 :- 1 ]
26
26
if data_type .startswith ("FixedString" ): # FixedString(20)
27
27
return "FixedStringField"
28
- elif data_type .startswith ("DateTime64" ):
28
+ elif data_type .startswith ("DateTime64" ): # DateTime64(6, 'UTC')
29
29
return "DateTime64Field"
30
- elif data_type .startswith ("Decimal" ):
30
+ elif data_type .startswith ("Decimal" ): # Decimal(9, 3)
31
31
return "DecimalField"
32
32
elif data_type .startswith ("Enum8" ):
33
33
return "Enum8Field"
@@ -50,40 +50,34 @@ def get_table_list(self, cursor):
50
50
"""Return a list of table and view names in the current database."""
51
51
cursor .execute (
52
52
"""
53
- SELECT table_name,
54
- CASE table_type WHEN 2 THEN 'v' ELSE 't' END
55
- FROM INFORMATION_SCHEMA.TABLES
56
- WHERE table_catalog = currentDatabase()
57
- AND table_type IN (1, 2)
53
+ SELECT name,
54
+ if(engine LIKE '%%View', 'v', 't'),
55
+ comment
56
+ FROM system.tables
57
+ WHERE database = currentDatabase()
58
+ AND NOT is_temporary
59
+ AND engine NOT LIKE 'System%%'
60
+ AND has_own_data
58
61
"""
59
62
)
60
- return [
61
- TableInfo (* row )
62
- for row in cursor .fetchall ()
63
- if row [0 ] not in self .ignored_tables
64
- ]
63
+ return [TableInfo (* row ) for row in cursor .fetchall ()]
65
64
66
65
def get_table_description (self , cursor , table_name ):
67
- """
68
- Return a description of the table.
69
- """
70
- # Query the INFORMATION_SCHEMA.COLUMNS table.
66
+ """Return a description of the table."""
71
67
cursor .execute (
72
68
"""
73
- SELECT column_name, data_type, NULL, character_maximum_length ,
69
+ SELECT name, type, character_octet_length, character_octet_length ,
74
70
coalesce(numeric_precision, datetime_precision),
75
- numeric_scale, is_nullable::Bool, column_default , NULL
76
- FROM INFORMATION_SCHEMA.COLUMNS
77
- WHERE table_catalog = currentDatabase() AND table_name = %s
71
+ numeric_scale, type LIKE 'Nullable(%%)', default_expression , NULL, comment
72
+ FROM system.columns
73
+ WHERE database = currentDatabase() AND table = %s
78
74
""" ,
79
75
[table_name ],
80
76
)
81
77
return [FieldInfo (* line ) for line in cursor .fetchall ()]
82
78
83
79
def get_constraints (self , cursor , table_name ):
84
- """
85
- Retrieve any constraints and indexes.
86
- """
80
+ """Retrieve any constraints and indexes."""
87
81
constraints = {}
88
82
# No way to get structured data, parse from SHOW CREATE TABLE.
89
83
# https://clickhouse.com/docs/en/sql-reference/statements/show#show-create-table
0 commit comments