Skip to content

Commit e2a8afa

Browse files
committed
update for version 5
1 parent fb50a35 commit e2a8afa

File tree

7 files changed

+265
-177
lines changed

7 files changed

+265
-177
lines changed

django_iris/__init__.py

+32-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from django.db.models.functions.text import Chr, ConcatPair, StrIndex
55
from django.db.models.fields import TextField, CharField
66

7+
from django_iris.compiler import SQLCompiler
8+
79
fn_template = "{fn %(function)s(%(expressions)s)}"
810

911
as_fn = [
@@ -22,16 +24,20 @@
2224
"TAN",
2325
]
2426

27+
2528
def as_intersystems(cls):
2629
def inner(func):
2730
cls.as_intersystems = func
31+
2832
return inner
2933

34+
3035
class Log10(Func):
3136
function = "LOG10"
3237
arity = 1
3338
lookup_name = "log10"
3439

40+
3541
class Convert(Func):
3642
function = "CONVERT"
3743
lookup_name = "convert"
@@ -45,36 +51,45 @@ def as_sql(self, compiler, connection, **extra_context):
4551
extra_context["db_type"] = self.output_field.cast_db_type(connection)
4652
return super().as_sql(compiler, connection, **extra_context)
4753

54+
4855
def convert_streams(expressions):
4956
return [
50-
Convert(expression, CharField()) if isinstance(expression, Col) and isinstance(expression.target, TextField) else expression
57+
(
58+
Convert(expression, CharField())
59+
if isinstance(expression, Col) and isinstance(expression.target, TextField)
60+
else expression
61+
)
5162
for expression in expressions
5263
]
5364

65+
5466
@as_intersystems(Exists)
5567
def exists_as_intersystems(self, compiler, connection, template=None, **extra_context):
5668
template = "(SELECT COUNT(*) FROM (%(subquery)s))"
5769
return self.as_sql(compiler, connection, template, **extra_context)
5870

71+
5972
@as_intersystems(Chr)
6073
def chr_as_intersystems(self, compiler, connection, **extra_context):
6174
return self.as_sql(compiler, connection, function="CHAR", **extra_context)
6275

76+
6377
@as_intersystems(ConcatPair)
6478
def concat_as_intersystems(self, compiler, connection, **extra_context):
6579
copy = self.copy()
6680
expressions = convert_streams(copy.get_source_expressions())
6781
"""
6882
STRING in IRIS retuns NULL if all NULL arguments, so, just add empty string, to make it always non NULL
6983
"""
70-
copy.set_source_expressions([Value("")]+ expressions)
84+
copy.set_source_expressions([Value("")] + expressions)
7185
return super(ConcatPair, copy).as_sql(
7286
compiler,
7387
connection,
7488
function="STRING",
7589
**extra_context,
7690
)
7791

92+
7893
@as_intersystems(StrIndex)
7994
def instr_as_intersystems(self, compiler, connection, **extra_context):
8095
copy = self.copy()
@@ -86,23 +101,26 @@ def instr_as_intersystems(self, compiler, connection, **extra_context):
86101
**extra_context,
87102
)
88103

104+
89105
@as_intersystems(Random)
90106
def random_as_intersystems(self, compiler, connection, **extra_context):
91-
return self.as_sql(compiler, connection, template="%%TSQL.ZRAND(1e10)", **extra_context)
107+
return self.as_sql(
108+
compiler, connection, template="%%TSQL.ZRAND(1e10)", **extra_context
109+
)
110+
92111

93112
@as_intersystems(Ln)
94113
def ln_as_intersystems(self, compiler, connection, **extra_context):
95-
return self.as_sql(compiler, connection, function="LOG", template=fn_template, **extra_context)
114+
return self.as_sql(
115+
compiler, connection, function="LOG", template=fn_template, **extra_context
116+
)
96117

97118

98119
@as_intersystems(Log)
99120
def log_as_intersystems(self, compiler, connection, **extra_context):
100121
copy = self.copy()
101122
copy.set_source_expressions(
102-
[
103-
Log10(expression)
104-
for expression in copy.get_source_expressions()[::-1]
105-
]
123+
[Log10(expression) for expression in copy.get_source_expressions()[::-1]]
106124
)
107125
return super(Log, copy).as_sql(
108126
compiler,
@@ -112,22 +130,24 @@ def log_as_intersystems(self, compiler, connection, **extra_context):
112130
**extra_context,
113131
)
114132

133+
115134
@as_intersystems(Func)
116-
def func_as_intersystems(self, compiler, connection, **extra_context):
117-
if self.function in as_fn:
135+
def func_as_intersystems(self, compiler: SQLCompiler, connection, **extra_context):
136+
if self.function in as_fn:
118137
return self.as_sql(compiler, connection, template=fn_template, **extra_context)
119138
return self.as_sql(compiler, connection, **extra_context)
120139

140+
121141
@as_intersystems(Now)
122142
def now_as_intersystems(self, compiler, connection, **extra_context):
123143
return self.as_sql(
124144
compiler, connection, template="CURRENT_TIMESTAMP(6)", **extra_context
125145
)
126146

147+
127148
@as_intersystems(OrderBy)
128149
def orderby_as_intersystems(self, compiler, connection, **extra_context):
129150
copy = self.copy()
130151
# IRIS does not support order NULL
131-
copy.nulls_first = copy.nulls_last = False
152+
copy.nulls_first = copy.nulls_last = False
132153
return copy.as_sql(compiler, connection, **extra_context)
133-

django_iris/base.py

+73-76
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,56 @@ def ignore(*args, **kwargs):
2424
class DatabaseClient(BaseDatabaseClient):
2525
runshell = ignore
2626

27+
2728
class DatabaseWrapper(BaseDatabaseWrapper):
28-
vendor = 'intersystems'
29-
display_name = 'InterSystems IRIS'
29+
vendor = "intersystems"
30+
display_name = "InterSystems IRIS"
3031

3132
data_types = {
32-
'AutoField': 'IDENTITY',
33-
'BigAutoField': 'IDENTITY',
34-
'BinaryField': 'LONG BINARY',
35-
'BooleanField': 'BIT',
36-
'CharField': 'VARCHAR(%(max_length)s)',
37-
'DateField': 'DATE',
38-
'DateTimeField': 'TIMESTAMP',
39-
'DecimalField': 'NUMERIC(%(max_digits)s, %(decimal_places)s)',
40-
'DurationField': 'BIGINT',
41-
'FileField': 'VARCHAR(%(max_length)s)',
42-
'FilePathField': 'VARCHAR(%(max_length)s)',
43-
'FloatField': 'DOUBLE PRECISION',
44-
'IntegerField': 'INTEGER',
45-
'BigIntegerField': 'BIGINT',
46-
'IPAddressField': 'CHAR(15)',
47-
'GenericIPAddressField': 'CHAR(39)',
48-
'JSONField': 'VARCHAR(32768)',
49-
'OneToOneField': 'INTEGER',
50-
'PositiveBigIntegerField': 'BIGINT',
51-
'PositiveIntegerField': 'INTEGER',
52-
'PositiveSmallIntegerField': 'SMALLINT',
53-
'SlugField': 'VARCHAR(%(max_length)s)',
54-
'SmallAutoField': 'IDENTITY',
55-
'SmallIntegerField': 'SMALLINT',
56-
'TextField': 'TEXT',
57-
'TimeField': 'TIME(6)',
58-
'UUIDField': 'CHAR(32)',
33+
"AutoField": "IDENTITY",
34+
"BigAutoField": "IDENTITY",
35+
"BinaryField": "LONG BINARY",
36+
"BooleanField": "BIT",
37+
"CharField": "VARCHAR(%(max_length)s)",
38+
"DateField": "DATE",
39+
"DateTimeField": "TIMESTAMP",
40+
"DecimalField": "NUMERIC(%(max_digits)s, %(decimal_places)s)",
41+
"DurationField": "BIGINT",
42+
"FileField": "VARCHAR(%(max_length)s)",
43+
"FilePathField": "VARCHAR(%(max_length)s)",
44+
"FloatField": "DOUBLE PRECISION",
45+
"IntegerField": "INTEGER",
46+
"BigIntegerField": "BIGINT",
47+
"IPAddressField": "CHAR(15)",
48+
"GenericIPAddressField": "CHAR(39)",
49+
"JSONField": "VARCHAR(32768)",
50+
"OneToOneField": "INTEGER",
51+
"PositiveBigIntegerField": "BIGINT",
52+
"PositiveIntegerField": "INTEGER",
53+
"PositiveSmallIntegerField": "SMALLINT",
54+
"SlugField": "VARCHAR(%(max_length)s)",
55+
"SmallAutoField": "IDENTITY",
56+
"SmallIntegerField": "SMALLINT",
57+
"TextField": "TEXT",
58+
"TimeField": "TIME(6)",
59+
"UUIDField": "UNIQUEIDENTIFIER",
5960
}
6061

6162
operators = {
62-
'exact': '= %s',
63-
'iexact': "LIKE %s ESCAPE '\\'",
64-
'contains': "LIKE %s ESCAPE '\\'",
65-
'icontains': "LIKE %s ESCAPE '\\'",
63+
"exact": "= %s",
64+
"iexact": "LIKE %s ESCAPE '\\'",
65+
"contains": "LIKE %s ESCAPE '\\'",
66+
"icontains": "LIKE %s ESCAPE '\\'",
6667
# 'regex': "%%%%MATCHES %s ESCAPE '\\'",
6768
# 'iregex': "%%%%MATCHES %s ESCAPE '\\'",
68-
'gt': '> %s',
69-
'gte': '>= %s',
70-
'lt': '< %s',
71-
'lte': '<= %s',
72-
'startswith': "LIKE %s ESCAPE '\\'",
73-
'endswith': "LIKE %s ESCAPE '\\'",
74-
'istartswith': "LIKE %s ESCAPE '\\'",
75-
'iendswith': "LIKE %s ESCAPE '\\'",
69+
"gt": "> %s",
70+
"gte": ">= %s",
71+
"lt": "< %s",
72+
"lte": "<= %s",
73+
"startswith": "LIKE %s ESCAPE '\\'",
74+
"endswith": "LIKE %s ESCAPE '\\'",
75+
"istartswith": "LIKE %s ESCAPE '\\'",
76+
"iendswith": "LIKE %s ESCAPE '\\'",
7677
}
7778

7879
pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')"
@@ -83,7 +84,6 @@ class DatabaseWrapper(BaseDatabaseWrapper):
8384
"istartswith": "LIKE UPPER({}) || '%%'",
8485
"endswith": "LIKE '%%' || {}",
8586
"iendswith": "LIKE '%%' || UPPER({})",
86-
8787
}
8888

8989
Database = Database
@@ -110,62 +110,59 @@ def get_connection_params(self):
110110
settings_dict = self.settings_dict
111111

112112
conn_params = {
113-
'username': None,
114-
'password': None,
115-
'timeout': 30,
113+
"username": None,
114+
"password": None,
115+
"timeout": 30,
116116
}
117-
if settings_dict['USER']:
118-
conn_params['username'] = settings_dict['USER']
119-
if settings_dict['PASSWORD']:
120-
conn_params['password'] = settings_dict['PASSWORD']
121-
if 'TIMEOUT' in settings_dict:
122-
conn_params['timeout'] = settings_dict['TIMEOUT']
123-
if 'EMBEDDED' in settings_dict:
124-
conn_params['embedded'] = settings_dict['EMBEDDED']
125-
if 'CONNECTION_STRING' in settings_dict:
126-
conn_params['connectionstr'] = settings_dict['CONNECTION_STRING']
117+
if settings_dict["USER"]:
118+
conn_params["username"] = settings_dict["USER"]
119+
if settings_dict["PASSWORD"]:
120+
conn_params["password"] = settings_dict["PASSWORD"]
121+
if "TIMEOUT" in settings_dict:
122+
conn_params["timeout"] = settings_dict["TIMEOUT"]
123+
if "EMBEDDED" in settings_dict:
124+
conn_params["embedded"] = settings_dict["EMBEDDED"]
125+
if "CONNECTION_STRING" in settings_dict:
126+
conn_params["connectionstr"] = settings_dict["CONNECTION_STRING"]
127127
else:
128128
conn_params = {
129-
'hostname': 'localhost',
130-
'port': 1972,
131-
'namespace': 'USER',
129+
"hostname": "localhost",
130+
"port": 1972,
131+
"namespace": "USER",
132132
**conn_params,
133133
}
134-
if settings_dict['HOST']:
135-
conn_params['hostname'] = settings_dict['HOST']
136-
if settings_dict['PORT']:
137-
conn_params['port'] = settings_dict['PORT']
138-
if 'NAMESPACE' in settings_dict:
139-
conn_params['namespace'] = settings_dict['NAMESPACE']
140-
if settings_dict['NAME']:
141-
conn_params['namespace'] = settings_dict['NAME']
134+
if settings_dict["HOST"]:
135+
conn_params["hostname"] = settings_dict["HOST"]
136+
if settings_dict["PORT"]:
137+
conn_params["port"] = settings_dict["PORT"]
138+
if "NAMESPACE" in settings_dict:
139+
conn_params["namespace"] = settings_dict["NAMESPACE"]
140+
if settings_dict["NAME"]:
141+
conn_params["namespace"] = settings_dict["NAME"]
142142

143143
if (
144-
not conn_params['hostname'] or
145-
not conn_params['port'] or
146-
not conn_params['namespace']
144+
not conn_params["hostname"]
145+
or not conn_params["port"]
146+
or not conn_params["namespace"]
147147
):
148148
raise ImproperlyConfigured(
149149
"settings.DATABASES is improperly configured. "
150150
"Please supply the HOST, PORT and NAME"
151151
)
152152

153-
if (
154-
not conn_params['username'] or
155-
not conn_params['password']
156-
):
153+
if not conn_params["username"] or not conn_params["password"]:
157154
raise ImproperlyConfigured(
158155
"settings.DATABASES is improperly configured. "
159156
"Please supply the USER and PASSWORD"
160157
)
161158

162-
conn_params['application_name'] = 'django'
159+
conn_params["application_name"] = "django"
163160
conn_params["autoCommit"] = self.autocommit
164161
return conn_params
165162

166163
def init_connection_state(self):
167164
pass
168-
165+
169166
@async_unsafe
170167
def get_new_connection(self, conn_params):
171168
return Database.connect(**conn_params)
@@ -186,7 +183,7 @@ def create_cursor(self, name=None):
186183
def is_usable(self):
187184
try:
188185
with self.connection.cursor() as cursor:
189-
cursor.execute('SELECT 1')
186+
cursor.execute("SELECT 1")
190187
except:
191188
return False
192189
else:

0 commit comments

Comments
 (0)