1 | from ..adapters.firebird import FireBird |
---|
2 | from ..objects import Expression |
---|
3 | from .base import SQLDialect |
---|
4 | from . import dialects, sqltype_for |
---|
5 | |
---|
6 | |
---|
7 | @dialects.register_for(FireBird) |
---|
8 | class FireBirdDialect(SQLDialect): |
---|
9 | @sqltype_for("text") |
---|
10 | def type_text(self): |
---|
11 | return "BLOB SUB_TYPE 1" |
---|
12 | |
---|
13 | @sqltype_for("bigint") |
---|
14 | def type_bigint(self): |
---|
15 | return "BIGINT" |
---|
16 | |
---|
17 | @sqltype_for("double") |
---|
18 | def type_double(self): |
---|
19 | return "DOUBLE PRECISION" |
---|
20 | |
---|
21 | @sqltype_for("decimal") |
---|
22 | def type_decimal(self): |
---|
23 | return "DECIMAL(%(precision)s,%(scale)s)" |
---|
24 | |
---|
25 | @sqltype_for("blob") |
---|
26 | def type_blob(self): |
---|
27 | return "BLOB SUB_TYPE 0" |
---|
28 | |
---|
29 | @sqltype_for("id") |
---|
30 | def type_id(self): |
---|
31 | return "INTEGER PRIMARY KEY" |
---|
32 | |
---|
33 | @sqltype_for("big-id") |
---|
34 | def type_big_id(self): |
---|
35 | return "BIGINT PRIMARY KEY" |
---|
36 | |
---|
37 | @sqltype_for("reference") |
---|
38 | def type_reference(self): |
---|
39 | return "INTEGER REFERENCES %(foreign_key)s " + "ON DELETE %(on_delete_action)s" |
---|
40 | |
---|
41 | @sqltype_for("big-reference") |
---|
42 | def type_big_reference(self): |
---|
43 | return "BIGINT REFERENCES %(foreign_key)s " + "ON DELETE %(on_delete_action)s" |
---|
44 | |
---|
45 | def sequence_name(self, tablename): |
---|
46 | return self.quote("genid_%s" % tablename) |
---|
47 | |
---|
48 | def trigger_name(self, tablename): |
---|
49 | return "trg_id_%s" % tablename |
---|
50 | |
---|
51 | @property |
---|
52 | def random(self): |
---|
53 | return "RAND()" |
---|
54 | |
---|
55 | def not_null(self, default, field_type): |
---|
56 | return "DEFAULT %s NOT NULL" % self.adapter.represent(default, field_type) |
---|
57 | |
---|
58 | def epoch(self, val, query_env={}): |
---|
59 | return "DATEDIFF(second, '1970-01-01 00:00:00', %s)" % self.expand( |
---|
60 | val, query_env=query_env |
---|
61 | ) |
---|
62 | |
---|
63 | def substring(self, field, parameters, query_env={}): |
---|
64 | return "SUBSTRING(%s from %s for %s)" % ( |
---|
65 | self.expand(field, query_env=query_env), |
---|
66 | parameters[0], |
---|
67 | parameters[1], |
---|
68 | ) |
---|
69 | |
---|
70 | def length(self, val, query_env={}): |
---|
71 | return "CHAR_LENGTH(%s)" % self.expand(val, query_env=query_env) |
---|
72 | |
---|
73 | def contains(self, first, second, case_sensitive=True, query_env={}): |
---|
74 | if first.type.startswith("list:"): |
---|
75 | second = Expression( |
---|
76 | None, |
---|
77 | self.concat( |
---|
78 | "|", |
---|
79 | Expression(None, self.replace(second, ("|", "||"), query_env)), |
---|
80 | "|", |
---|
81 | ), |
---|
82 | ) |
---|
83 | return "(%s CONTAINING %s)" % ( |
---|
84 | self.expand(first, query_env=query_env), |
---|
85 | self.expand(second, "string", query_env=query_env), |
---|
86 | ) |
---|
87 | |
---|
88 | def select( |
---|
89 | self, |
---|
90 | fields, |
---|
91 | tables, |
---|
92 | where=None, |
---|
93 | groupby=None, |
---|
94 | having=None, |
---|
95 | orderby=None, |
---|
96 | limitby=None, |
---|
97 | distinct=False, |
---|
98 | for_update=False, |
---|
99 | ): |
---|
100 | dst, whr, grp, order, limit, offset, upd = "", "", "", "", "", "", "" |
---|
101 | if distinct is True: |
---|
102 | dst = " DISTINCT" |
---|
103 | elif distinct: |
---|
104 | dst = " DISTINCT ON (%s)" % distinct |
---|
105 | if where: |
---|
106 | whr = " %s" % self.where(where) |
---|
107 | if groupby: |
---|
108 | grp = " GROUP BY %s" % groupby |
---|
109 | if having: |
---|
110 | grp += " HAVING %s" % having |
---|
111 | if orderby: |
---|
112 | order = " ORDER BY %s" % orderby |
---|
113 | if limitby: |
---|
114 | (lmin, lmax) = limitby |
---|
115 | limit = " FIRST %i" % (lmax - lmin) |
---|
116 | offset = " SKIP %i" % lmin |
---|
117 | if for_update: |
---|
118 | upd = " FOR UPDATE" |
---|
119 | return "SELECT%s%s%s %s FROM %s%s%s%s%s;" % ( |
---|
120 | dst, |
---|
121 | limit, |
---|
122 | offset, |
---|
123 | fields, |
---|
124 | tables, |
---|
125 | whr, |
---|
126 | grp, |
---|
127 | order, |
---|
128 | upd, |
---|
129 | ) |
---|
130 | |
---|
131 | def drop_table(self, table, mode): |
---|
132 | sequence_name = table._sequence_name |
---|
133 | return [ |
---|
134 | "DROP TABLE %s %s;" % (table._rname, mode), |
---|
135 | "DROP GENERATOR %s;" % sequence_name, |
---|
136 | ] |
---|
137 | |
---|
138 | def truncate(self, table, mode=""): |
---|
139 | return [ |
---|
140 | "DELETE FROM %s;" % table._rname, |
---|
141 | "SET GENERATOR %s TO 0;" % table._sequence_name, |
---|
142 | ] |
---|