1 | from ..adapters.sqlite import SQLite, Spatialite |
---|
2 | from .base import SQLDialect |
---|
3 | from . import dialects, sqltype_for |
---|
4 | |
---|
5 | |
---|
6 | @dialects.register_for(SQLite) |
---|
7 | class SQLiteDialect(SQLDialect): |
---|
8 | @sqltype_for("string") |
---|
9 | def type_string(self): |
---|
10 | return "CHAR(%(length)s)" |
---|
11 | |
---|
12 | @sqltype_for("float") |
---|
13 | def type_float(self): |
---|
14 | return "DOUBLE" |
---|
15 | |
---|
16 | @sqltype_for("double") |
---|
17 | def type_double(self): |
---|
18 | return self.types["float"] |
---|
19 | |
---|
20 | @sqltype_for("decimal") |
---|
21 | def type_decimal(self): |
---|
22 | return self.types["float"] |
---|
23 | |
---|
24 | def extract(self, field, what, query_env={}): |
---|
25 | return "web2py_extract('%s', %s)" % ( |
---|
26 | what, |
---|
27 | self.expand(field, query_env=query_env), |
---|
28 | ) |
---|
29 | |
---|
30 | def regexp(self, first, second, query_env={}): |
---|
31 | return "(%s REGEXP %s)" % ( |
---|
32 | self.expand(first, query_env=query_env), |
---|
33 | self.expand(second, "string", query_env=query_env), |
---|
34 | ) |
---|
35 | |
---|
36 | def select( |
---|
37 | self, |
---|
38 | fields, |
---|
39 | tables, |
---|
40 | where=None, |
---|
41 | groupby=None, |
---|
42 | having=None, |
---|
43 | orderby=None, |
---|
44 | limitby=None, |
---|
45 | distinct=False, |
---|
46 | for_update=False, |
---|
47 | ): |
---|
48 | if distinct and distinct is not True: |
---|
49 | raise SyntaxError("DISTINCT ON is not supported by SQLite") |
---|
50 | return super(SQLiteDialect, self).select( |
---|
51 | fields, |
---|
52 | tables, |
---|
53 | where, |
---|
54 | groupby, |
---|
55 | having, |
---|
56 | orderby, |
---|
57 | limitby, |
---|
58 | distinct, |
---|
59 | for_update, |
---|
60 | ) |
---|
61 | |
---|
62 | def truncate(self, table, mode=""): |
---|
63 | tablename = self.adapter.expand(table._raw_rname, "string") |
---|
64 | return [ |
---|
65 | self.delete(table), |
---|
66 | "DELETE FROM sqlite_sequence WHERE name=%s" % tablename, |
---|
67 | ] |
---|
68 | |
---|
69 | def writing_alias(self, table): |
---|
70 | if table._dalname != table._tablename: |
---|
71 | raise SyntaxError("SQLite does not support UPDATE/DELETE on aliased table") |
---|
72 | return table._rname |
---|
73 | |
---|
74 | |
---|
75 | @dialects.register_for(Spatialite) |
---|
76 | class SpatialiteDialect(SQLiteDialect): |
---|
77 | @sqltype_for("geometry") |
---|
78 | def type_geometry(self): |
---|
79 | return "GEOMETRY" |
---|
80 | |
---|
81 | def st_asgeojson(self, first, second, query_env={}): |
---|
82 | return "AsGeoJSON(%s,%s,%s)" % ( |
---|
83 | self.expand(first, query_env=query_env), |
---|
84 | second["precision"], |
---|
85 | second["options"], |
---|
86 | ) |
---|
87 | |
---|
88 | def st_astext(self, first, query_env={}): |
---|
89 | return "AsText(%s)" % self.expand(first, query_env=query_env) |
---|
90 | |
---|
91 | def st_contains(self, first, second, query_env={}): |
---|
92 | return "Contains(%s,%s)" % ( |
---|
93 | self.expand(first, query_env=query_env), |
---|
94 | self.expand(second, first.type, query_env=query_env), |
---|
95 | ) |
---|
96 | |
---|
97 | def st_distance(self, first, second, query_env={}): |
---|
98 | return "Distance(%s,%s)" % ( |
---|
99 | self.expand(first, query_env=query_env), |
---|
100 | self.expand(second, first.type, query_env=query_env), |
---|
101 | ) |
---|
102 | |
---|
103 | def st_equals(self, first, second, query_env={}): |
---|
104 | return "Equals(%s,%s)" % ( |
---|
105 | self.expand(first, query_env=query_env), |
---|
106 | self.expand(second, first.type, query_env=query_env), |
---|
107 | ) |
---|
108 | |
---|
109 | def st_intersects(self, first, second, query_env={}): |
---|
110 | return "Intersects(%s,%s)" % ( |
---|
111 | self.expand(first, query_env=query_env), |
---|
112 | self.expand(second, first.type, query_env=query_env), |
---|
113 | ) |
---|
114 | |
---|
115 | def st_overlaps(self, first, second, query_env={}): |
---|
116 | return "Overlaps(%s,%s)" % ( |
---|
117 | self.expand(first, query_env=query_env), |
---|
118 | self.expand(second, first.type, query_env=query_env), |
---|
119 | ) |
---|
120 | |
---|
121 | def st_simplify(self, first, second, query_env={}): |
---|
122 | return "Simplify(%s,%s)" % ( |
---|
123 | self.expand(first, query_env=query_env), |
---|
124 | self.expand(second, "double", query_env=query_env), |
---|
125 | ) |
---|
126 | |
---|
127 | def st_touches(self, first, second, query_env={}): |
---|
128 | return "Touches(%s,%s)" % ( |
---|
129 | self.expand(first, query_env=query_env), |
---|
130 | self.expand(second, first.type, query_env=query_env), |
---|
131 | ) |
---|
132 | |
---|
133 | def st_within(self, first, second, query_env={}): |
---|
134 | return "Within(%s,%s)" % ( |
---|
135 | self.expand(first, query_env=query_env), |
---|
136 | self.expand(second, first.type, query_env=query_env), |
---|
137 | ) |
---|