1 | from ..adapters.mysql import MySQL |
---|
2 | from ..helpers.methods import varquote_aux |
---|
3 | from .base import SQLDialect |
---|
4 | from . import dialects, sqltype_for |
---|
5 | |
---|
6 | |
---|
7 | @dialects.register_for(MySQL) |
---|
8 | class MySQLDialect(SQLDialect): |
---|
9 | quote_template = "`%s`" |
---|
10 | |
---|
11 | @sqltype_for("datetime") |
---|
12 | def type_datetime(self): |
---|
13 | return "DATETIME" |
---|
14 | |
---|
15 | @sqltype_for("text") |
---|
16 | def type_text(self): |
---|
17 | return "LONGTEXT" |
---|
18 | |
---|
19 | @sqltype_for("blob") |
---|
20 | def type_blob(self): |
---|
21 | return "LONGBLOB" |
---|
22 | |
---|
23 | @sqltype_for("bigint") |
---|
24 | def type_bigint(self): |
---|
25 | return "BIGINT" |
---|
26 | |
---|
27 | @sqltype_for("id") |
---|
28 | def type_id(self): |
---|
29 | return "INT AUTO_INCREMENT NOT NULL" |
---|
30 | |
---|
31 | @sqltype_for("big-id") |
---|
32 | def type_big_id(self): |
---|
33 | return "BIGINT AUTO_INCREMENT NOT NULL" |
---|
34 | |
---|
35 | @sqltype_for("reference") |
---|
36 | def type_reference(self): |
---|
37 | return ( |
---|
38 | "INT %(null)s %(unique)s, INDEX %(index_name)s " |
---|
39 | + "(%(field_name)s), FOREIGN KEY (%(field_name)s) REFERENCES " |
---|
40 | + "%(foreign_key)s ON DELETE %(on_delete_action)s" |
---|
41 | ) |
---|
42 | |
---|
43 | @sqltype_for("big-reference") |
---|
44 | def type_big_reference(self): |
---|
45 | return ( |
---|
46 | "BIGINT %(null)s %(unique)s, INDEX %(index_name)s " |
---|
47 | + "(%(field_name)s), FOREIGN KEY (%(field_name)s) REFERENCES " |
---|
48 | + "%(foreign_key)s ON DELETE %(on_delete_action)s" |
---|
49 | ) |
---|
50 | |
---|
51 | @sqltype_for("reference FK") |
---|
52 | def type_reference_fk(self): |
---|
53 | return ( |
---|
54 | ", CONSTRAINT `FK_%(constraint_name)s` FOREIGN KEY " |
---|
55 | + "(%(field_name)s) REFERENCES %(foreign_key)s ON DELETE " |
---|
56 | + "%(on_delete_action)s" |
---|
57 | ) |
---|
58 | |
---|
59 | def varquote(self, val): |
---|
60 | return varquote_aux(val, "`%s`") |
---|
61 | |
---|
62 | def insert_empty(self, table): |
---|
63 | return "INSERT INTO %s VALUES (DEFAULT);" % table |
---|
64 | |
---|
65 | def delete(self, table, where=None): |
---|
66 | tablename = self.writing_alias(table) |
---|
67 | whr = "" |
---|
68 | if where: |
---|
69 | whr = " %s" % self.where(where) |
---|
70 | return "DELETE %s FROM %s%s;" % (table.sql_shortref, tablename, whr) |
---|
71 | |
---|
72 | @property |
---|
73 | def random(self): |
---|
74 | return "RAND()" |
---|
75 | |
---|
76 | def substring(self, field, parameters, query_env={}): |
---|
77 | return "SUBSTRING(%s,%s,%s)" % ( |
---|
78 | self.expand(field, query_env=query_env), |
---|
79 | parameters[0], |
---|
80 | parameters[1], |
---|
81 | ) |
---|
82 | |
---|
83 | def epoch(self, first, query_env={}): |
---|
84 | return "UNIX_TIMESTAMP(%s)" % self.expand(first, query_env=query_env) |
---|
85 | |
---|
86 | def concat(self, *items, **kwargs): |
---|
87 | query_env = kwargs.get("query_env", {}) |
---|
88 | tmp = (self.expand(x, "string", query_env=query_env) for x in items) |
---|
89 | return "CONCAT(%s)" % ",".join(tmp) |
---|
90 | |
---|
91 | def regexp(self, first, second, query_env={}): |
---|
92 | return "(%s REGEXP %s)" % ( |
---|
93 | self.expand(first, query_env=query_env), |
---|
94 | self.expand(second, "string", query_env=query_env), |
---|
95 | ) |
---|
96 | |
---|
97 | def cast(self, first, second, query_env={}): |
---|
98 | if second == "LONGTEXT": |
---|
99 | second = "CHAR" |
---|
100 | return "CAST(%s AS %s)" % (first, second) |
---|
101 | |
---|
102 | def drop_table(self, table, mode): |
---|
103 | # breaks db integrity but without this mysql does not drop table |
---|
104 | return [ |
---|
105 | "SET FOREIGN_KEY_CHECKS=0;", |
---|
106 | "DROP TABLE %s;" % table._rname, |
---|
107 | "SET FOREIGN_KEY_CHECKS=1;", |
---|
108 | ] |
---|
109 | |
---|
110 | def drop_index(self, name, table): |
---|
111 | return "DROP INDEX %s ON %s;" % (self.quote(name), table._rname) |
---|