-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialect.py
More file actions
186 lines (149 loc) · 6.55 KB
/
dialect.py
File metadata and controls
186 lines (149 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from sqlalchemy import exc
from sqlalchemy.connectors.mysqldb import MySQLDBConnector
from sqlalchemy.engine.default import DefaultDialect
from sqlalchemy.sql import compiler, expression, elements, functions
class match(functions.ReturnTypeFromArgs):
name = 'MATCH'
class SQLCompiler(compiler.SQLCompiler):
def visit_column(self, column, result_map=None, **kwargs):
name = column.name
if name is None:
raise exc.CompileError(
'Cannot compile Column object until it\'s "name" is assigned.'
)
is_literal = column.is_literal
if not is_literal and isinstance(name, elements._truncated_label):
name = self._truncated_identifier("colident", name)
#if result_map is not None:
# result_map[name.lower()] = (name, (column, ), column.type)
if is_literal:
name = self.escape_literal_column(name)
else:
name = self.preparer.quote(name, column.name)
return name
def visit_select(self, select,
asfrom=False,
parens=True,
iswrapper=False,
fromhints=None,
compound_index=1,
force_result_map=False,
positional_name=None, **kwargs):
entry = self.stack and self.stack[-1] or {}
existingfroms = entry.get('from', None)
froms = select._get_display_froms(existingfroms)
correlate_froms = set(expression._from_objects(*froms))
# TODO: might want to propagate existing froms for
# select(select(select)) where innermost select should correlate
# to outermost if existingfroms: correlate_froms =
# correlate_froms.union(existingfroms)
self.stack.append({'from': correlate_froms,
'iswrapper': iswrapper})
if compound_index == 1 and not entry or entry.get('iswrapper', False):
column_clause_args = {'result_map': self.result_map}
else:
column_clause_args = {}
populate_result_map = force_result_map or (
compound_index == 0 and (not entry or entry.get('iswrapper', False)))
# the actual list of columns to print in the SELECT column list.
inner_columns = [
c for c in [
self._label_select_column(select,
column,
populate_result_map,
asfrom,
column_clause_args,
name=name)
for name, column in select._columns_plus_names]
if c is not None
]
text = 'SELECT '
if select._hints:
byfrom = dict([
(from_, hinttext % {
'name':from_._compiler_dispatch(
self, ashint=True)
})
for (from_, dialect), hinttext in
select._hints.iteritems()
if dialect in ('*', self.dialect.name)
])
hint_text = self.get_select_hint_text(byfrom)
if hint_text:
text += hint_text + ' '
if select._prefixes:
text += ' '.join(
x._compiler_dispatch(self, **kwargs)
for x in select._prefixes) + " "
text += self.get_select_precolumns(select)
text += ', '.join(inner_columns)
if froms:
text += ' \nFROM '
if select._hints:
text += ', '.join([f._compiler_dispatch(self,
asfrom=True, fromhints=byfrom,
**kwargs)
for f in froms])
else:
text += ', '.join([f._compiler_dispatch(self,
asfrom=True, **kwargs)
for f in froms])
else:
text += self.default_from()
if select._whereclause is not None:
t = select._whereclause._compiler_dispatch(self, **kwargs)
if t:
text += ' \nWHERE ' + t
if select._group_by_clause.clauses:
group_by = select._group_by_clause._compiler_dispatch(
self, **kwargs)
if group_by:
text += ' GROUP BY ' + group_by
if select._having is not None:
t = select._having._compiler_dispatch(self, **kwargs)
if t:
text += ' \nHAVING ' + t
if select._order_by_clause.clauses:
text += self.order_by_clause(select, **kwargs)
if getattr(select, '_within_group_order_by_clause', None) is not None:
if select._within_group_order_by_clause.clauses:
text += self.within_group_order_by_clause(select, **kwargs)
if select._limit is not None:
text += self.limit_clause(select)
if getattr(select, '_options', None) is not None:
if select._options.options:
text += self.options_clause(select, **kwargs)
if select.for_update:
text += self.for_update_clause(select)
self.stack.pop(-1)
if asfrom and parens:
return "(" + text + ")"
else:
return text
def limit_clause(self, select):
limit, offset = select._limit, select._offset
if limit:
if offset:
return ' \nLIMIT %s, %s' % (
self.process(expression.literal(offset)),
self.process(expression.literal(limit)))
return ' \nLIMIT %s' % self.process(expression.literal(limit))
return ''
def visit_multi(self, element, **kwargs):
return '(%s)' % self.visit_clauselist(element, **kwargs)
def visit_match(self, element, **kwargs):
return 'match(%s)' % self.process(expression.literal(element.value))
class Dialect(DefaultDialect, MySQLDBConnector):
name = 'sphinxql'
default_paramstyle = 'format'
positional = True
ddl_compiler = None
statement_compiler = SQLCompiler
supports_unicode_statements = True
supports_multivalues_insert = True
supports_right_nested_joins = False
supports_alter = False
supports_views = False
description_encoding = None
def _check_unicode_returns(self, connection, additional_tests=None):
return True