1 | from ..adapters.sap import SAPDB |
---|
2 | from .base import SQLDialect |
---|
3 | from . import dialects, sqltype_for |
---|
4 | |
---|
5 | |
---|
6 | @dialects.register_for(SAPDB) |
---|
7 | class SAPDBDialect(SQLDialect): |
---|
8 | @sqltype_for("integer") |
---|
9 | def type_integer(self): |
---|
10 | return "INT" |
---|
11 | |
---|
12 | @sqltype_for("text") |
---|
13 | def type_text(self): |
---|
14 | return "LONG" |
---|
15 | |
---|
16 | @sqltype_for("bigint") |
---|
17 | def type_bigint(self): |
---|
18 | return "BIGINT" |
---|
19 | |
---|
20 | @sqltype_for("double") |
---|
21 | def type_double(self): |
---|
22 | return "DOUBLE PRECISION" |
---|
23 | |
---|
24 | @sqltype_for("decimal") |
---|
25 | def type_decimal(self): |
---|
26 | return "FIXED(%(precision)s,%(scale)s)" |
---|
27 | |
---|
28 | @sqltype_for("id") |
---|
29 | def type_id(self): |
---|
30 | return "INT PRIMARY KEY" |
---|
31 | |
---|
32 | @sqltype_for("big-id") |
---|
33 | def type_big_id(self): |
---|
34 | return "BIGINT PRIMARY KEY" |
---|
35 | |
---|
36 | @sqltype_for("reference") |
---|
37 | def type_reference(self): |
---|
38 | return ( |
---|
39 | "INT, 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, FOREIGN KEY (%(field_name)s) REFERENCES " |
---|
47 | + "%(foreign_key)s ON DELETE %(on_delete_action)s" |
---|
48 | ) |
---|
49 | |
---|
50 | def sequence_name(self, tablename): |
---|
51 | return self.quote("%s_id_Seq" % tablename) |
---|
52 | |
---|
53 | def select( |
---|
54 | self, |
---|
55 | fields, |
---|
56 | tables, |
---|
57 | where=None, |
---|
58 | groupby=None, |
---|
59 | having=None, |
---|
60 | orderby=None, |
---|
61 | limitby=None, |
---|
62 | distinct=False, |
---|
63 | for_update=False, |
---|
64 | ): |
---|
65 | dst, whr, grp, order, limit, offset, upd = "", "", "", "", "", "", "" |
---|
66 | if distinct is True: |
---|
67 | dst = " DISTINCT" |
---|
68 | elif distinct: |
---|
69 | dst = " DISTINCT ON (%s)" % distinct |
---|
70 | if where: |
---|
71 | whr = " %s" % self.where(where) |
---|
72 | if groupby: |
---|
73 | grp = " GROUP BY %s" % groupby |
---|
74 | if having: |
---|
75 | grp += " HAVING %s" % having |
---|
76 | if orderby: |
---|
77 | order = " ORDER BY %s" % orderby |
---|
78 | if limitby: |
---|
79 | (lmin, lmax) = limitby |
---|
80 | if whr: |
---|
81 | whr2 = whr + " AND w_row > %i" % lmin |
---|
82 | else: |
---|
83 | whr2 = self.where("w_row > %i" % lmin) |
---|
84 | return ( |
---|
85 | "SELECT%s %s FROM (SELECT w_tmp.*, ROWNO w_row FROM " |
---|
86 | + "(SELECT %s FROM %s%s%s%s) w_tmp WHERE ROWNO=%i) %s%s%s%s;" |
---|
87 | % ( |
---|
88 | dst, |
---|
89 | fields, |
---|
90 | fields, |
---|
91 | tables, |
---|
92 | whr, |
---|
93 | grp, |
---|
94 | order, |
---|
95 | lmax, |
---|
96 | tables, |
---|
97 | whr2, |
---|
98 | grp, |
---|
99 | order, |
---|
100 | ) |
---|
101 | ) |
---|
102 | if for_update: |
---|
103 | upd = " FOR UPDATE" |
---|
104 | return "SELECT%s%s%s %s FROM %s%s%s%s%s;" % ( |
---|
105 | dst, |
---|
106 | limit, |
---|
107 | offset, |
---|
108 | fields, |
---|
109 | tables, |
---|
110 | whr, |
---|
111 | grp, |
---|
112 | order, |
---|
113 | upd, |
---|
114 | ) |
---|