Source

htsql-charset-option / src / htsql / tr / serializer.py

Full commit
  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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#
# Copyright (c) 2006-2010, Prometheus Research, LLC
# Authors: Clark C. Evans <cce@clarkevans.com>,
#          Kirill Simonov <xi@resolvent.net>
#


"""
:mod:`htsql.tr.serializer`
==========================

This module implements the SQL serializer.
"""


from ..adapter import Adapter, Utility, adapts, find_adapters
from ..domain import (Domain, BooleanDomain, NumberDomain, StringDomain,
                      DateDomain)
from .frame import (Clause, Frame, LeafFrame, ScalarFrame,
                    BranchFrame, CorrelatedFrame, SegmentFrame,
                    QueryFrame, Phrase, EqualityPhrase, InequalityPhrase,
                    ConjunctionPhrase, DisjunctionPhrase, NegationPhrase,
                    LiteralPhrase, LeafReferencePhrase, BranchReferencePhrase,
                    CorrelatedFramePhrase)
from .plan import Plan
import decimal


class Serializer(object):

    def __init__(self):
        self.format = Format()
        self.alias_by_clause = {}

    def add_alias(self, clause, alias):
        self.alias_by_clause[clause] = alias

    def get_alias(self, clause):
        return self.alias_by_clause[clause]

    def clear_aliases(self):
        self.alias_by_clause = {}

    def serialize(self, clause):
        serialize = Serialize(clause, self)
        return serialize.serialize()

    def serialize_aliases(self, frame, parents=[]):
        serialize = Serialize(frame, self)
        return serialize.serialize_aliases(parents)

    def call(self, clause):
        serialize = Serialize(clause, self)
        return serialize.call()


class Format(Utility):

    def name(self, name):
        return "\"%s\"" % name.replace("\"", "\"\"")

    def alias(self, name, index=None):
        if index is None:
            return name
        return "%s_%s" % (name, index)

    def attr(self, parent, child):
        return "%s.%s" % (parent, child)

    def as_op(self, value, alias):
        return "%s AS %s" % (value, alias)

    def parens(self, value):
        return "(%s)" % value

    def list(self, values):
        return ", ".join(values)

    def null(self):
        return "NULL"

    def true(self):
        return "TRUE"

    def false(self):
        return "FALSE"

    def number(self, value):
        if isinstance(value, (int, float)):
            return repr(value)
        if isinstance(value, (long, decimal.Decimal)):
            return str(value)

    def string(self, value):
        return "'%s'" % value.replace("'", "''")

    def date(self, value):
        return "CAST('%s' AS DATE)" % value

    def or_op(self, values):
        if not values:
            return self.false()
        return "(%s)" % " OR ".join(values)

    def and_op(self, values):
        if not values:
            return self.true()
        return "(%s)" % " AND ".join(values)

    def not_op(self, value):
        return "(NOT %s)" % value

    def unary_op(self, op, right):
        return "(%s %s)" % (op, right)

    def binary_op(self, left, op, right):
        return "(%s %s %s)" % (left, op, right)

    def equal_op(self, left, right, is_negative=False):
        op = "="
        if is_negative:
            op = "!="
        return self.binary_op(left, op, right)

    def order(self, value, dir):
        if dir == +1:
            op = "ASC"
        elif dir == -1:
            op = "DESC"
        return "%s %s" % (value, op)

    def join(self, base=None, target=None,
             condition=None, is_inner=True):
        assert target is not None
        if base is None:
            assert condition is None and is_inner
            return target
        if condition is None:
            if is_inner:
                return "%s CROSS JOIN %s" % (base, target)
            else:
                condition = self.true()
        if is_inner:
            op = "INNER JOIN"
        else:
            op = "LEFT OUTER JOIN"
        return "%s %s %s ON (%s)" % (base, op, target, condition)

    def select(self, select_clause, from_clause=None,
               where_clause=None, group_clause=None,
               having_clause=None, order_clause=None,
               limit=None, offset=None):
        clauses = []
        clauses.append("SELECT %s" % select_clause)
        if from_clause is not None:
            clauses.append(" FROM %s" % from_clause)
        if where_clause is not None:
            clauses.append(" WHERE %s" % where_clause)
        if group_clause is not None:
            clauses.append(" GROUP BY %s" % group_clause)
        if having_clause is not None:
            clauses.append(" HAVING %s" % having_clause)
        if order_clause is not None:
            clauses.append(" ORDER BY %s" % order_clause)
        if limit is not None:
            clauses.append(" LIMIT %s" % limit)
        if offset is not None:
            clauses.append(" OFFSET %s" % offset)
        return "".join(clauses)

    def scalar_select(self):
        return "(SELECT 1)"


class Serialize(Adapter):

    adapts(Clause, Serializer)

    def serialize(self):
        raise NotImplementedError()

    def call(self):
        return "!"


class SerializeFrame(Serialize):

    adapts(Frame, Serializer)

    def __init__(self, frame, serializer):
        self.frame = frame
        self.serializer = serializer
        self.format = serializer.format

    def serialize_aliases(self, parents):
        pass


class SerializeLeaf(SerializeFrame):

    adapts(LeafFrame, Serializer)

    def serialize(self):
        parent = self.format.name(self.frame.table.schema_name)
        child = self.format.name(self.frame.table.name)
        return self.format.attr(parent, child)

    def call(self):
        return self.frame.table.name


class SerializeScalar(SerializeFrame):

    adapts(ScalarFrame, Serializer)

    def serialize(self):
        return self.format.scalar_select()


class SerializeBranch(SerializeFrame):

    adapts(BranchFrame, Serializer)

    with_aliases = True

    def serialize(self):
        select = self.serialize_select()
        return self.format.parens(select)

    def serialize_select(self):
        select_clause = self.serialize_select_clause()
        from_clause = self.serialize_from_clause()
        where_clause = self.serialize_where_clause()
        group_clause = self.serialize_group_clause()
        having_clause = self.serialize_having_clause()
        order_clause = self.serialize_order_clause()
        limit = self.frame.limit
        offset = self.frame.offset
        select = self.format.select(select_clause=select_clause,
                                    from_clause=from_clause,
                                    where_clause=where_clause,
                                    group_clause=group_clause,
                                    having_clause=having_clause,
                                    order_clause=order_clause,
                                    limit=limit, offset=offset)
        return select

    def serialize_select_clause(self):
        select_clause = []
        for phrase in self.frame.select:
            value = self.serializer.serialize(phrase)
            if self.with_aliases:
                alias = self.serializer.get_alias(phrase)
                inherited_alias = None
                if isinstance(phrase, LeafReferencePhrase):
                    inherited_alias = phrase.column.name
                elif isinstance(phrase, BranchReferencePhrase):
                    inherited_alias = self.serializer.get_alias(phrase.phrase)
                if alias != inherited_alias:
                    alias = self.format.name(alias)
                    value = self.format.as_op(value, alias)
            select_clause.append(value)
        return self.format.list(select_clause)

    def serialize_from_clause(self):
        from_clause = None
        for link in self.frame.linkage:
            target = self.serializer.serialize(link.frame)
            alias = self.serializer.get_alias(link.frame)
            alias = self.format.name(alias)
            target = self.format.as_op(target, alias)
            condition = None
            if link.condition is not None:
                condition = self.serializer.serialize(link.condition)
            from_clause = self.format.join(from_clause, target,
                                           condition, link.is_inner)
        return from_clause

    def serialize_where_clause(self):
        where_clause = None
        if self.frame.filter is not None:
            where_clause = self.serializer.serialize(self.frame.filter)
        return where_clause

    def serialize_group_clause(self):
        if not self.frame.group:
            return None
        group_clause = []
        position_by_phrase = {}
        for idx, phrase in enumerate(self.frame.select):
            position_by_phrase[phrase] = idx+1
        for phrase in self.frame.group:
            if phrase in position_by_phrase:
                position = position_by_phrase[phrase]
                value = self.format.number(position)
            else:
                value = self.serializer.serialize(phrase)
            group_clause.append(value)
        return self.format.list(group_clause)

    def serialize_having_clause(self):
        having_clause = None
        if self.frame.group_filter is not None:
            having_clause = self.serializer.serialize(self.frame.group_filter)
        return having_clause

    def serialize_order_clause(self):
        if not self.frame.order:
            return None
        order_clause = []
        position_by_phrase = {}
        for idx, phrase in enumerate(self.frame.select):
            position_by_phrase[phrase] = idx+1
        for phrase, dir in self.frame.order:
            if phrase in position_by_phrase:
                position = position_by_phrase[phrase]
                value = self.format.number(position)
            else:
                value = self.serializer.serialize(phrase)
            value = self.format.order(value, dir)
            order_clause.append(value)
        return self.format.list(order_clause)

    def serialize_aliases(self, parents):
        taken_aliases = set()
        for parent in parents:
            for link in parent.linkage:
                alias = self.serializer.get_alias(link.frame)
                taken_aliases.add(alias)
        self.serialize_alias_collection(self.frame.select, taken_aliases)
        collection = []
        for link in self.frame.linkage:
            self.serializer.serialize_aliases(link.frame)
            collection.append(link.frame)
        self.serialize_alias_collection(collection, taken_aliases)

    def serialize_alias_collection(self, collection, taken_aliases):
        clauses_by_name = {}
        for clause in collection:
            if clause in self.serializer.alias_by_clause:
                continue
            name = self.serializer.call(clause)
            clauses_by_name.setdefault(name, []).append(clause)
        for name in sorted(clauses_by_name):
            clauses = clauses_by_name[name]
            for clause in clauses:
                index = None
                if len(clauses) > 1:
                    index = 1
                alias = self.format.alias(name, index)
                while alias in taken_aliases:
                    if index is None:
                        index = 1
                    index += 1
                    alias = self.format.alias(name, index)
                self.serializer.add_alias(clause, alias)
                taken_aliases.add(alias)

    def call(self):
        if self.frame.linkage:
            child = None
            for link in self.frame.linkage:
                if link.is_inner:
                    child = link.frame
            return self.serializer.call(child)
        return super(SerializeBranch, self).call()


class SerializeCorrelated(SerializeFrame):

    adapts(CorrelatedFrame, Serializer)

    with_aliases = False


class SerializeSegment(SerializeFrame):

    adapts(SegmentFrame, Serializer)

    with_aliases = False

    def serialize(self):
        self.serializer.serialize_aliases(self.frame)
        select = self.serialize_select()
        self.serializer.clear_aliases()
        return select


class SerializeQuery(SerializeFrame):

    adapts(QueryFrame, Serializer)

    def serialize(self):
        sql = None
        if self.frame.segment is not None:
            sql = self.serializer.serialize(self.frame.segment)
        return Plan(self.frame, sql, self.frame.mark)


class SerializePhrase(Serialize):

    adapts(Phrase, Serializer)

    def __init__(self, phrase, serializer):
        self.phrase = phrase
        self.serializer = serializer
        self.format = serializer.format


class SerializeEquality(SerializePhrase):

    adapts(EqualityPhrase, Serializer)

    def serialize(self):
        left = self.serializer.serialize(self.phrase.left)
        right = self.serializer.serialize(self.phrase.right)
        return self.format.equal_op(left, right)


class SerializeInequality(SerializePhrase):

    adapts(InequalityPhrase, Serializer)

    def serialize(self):
        left = self.serializer.serialize(self.phrase.left)
        right = self.serializer.serialize(self.phrase.right)
        return self.format.equal_op(left, right, is_negative=True)


class SerializeConjunction(SerializePhrase):

    adapts(ConjunctionPhrase, Serializer)

    def serialize(self):
        values = [self.serializer.serialize(term)
                  for term in self.phrase.terms]
        return self.format.and_op(values)


class SerializeDisjunction(SerializePhrase):

    adapts(DisjunctionPhrase, Serializer)

    def serialize(self):
        values = [self.serializer.serialize(term)
                  for term in self.phrase.terms]
        return self.format.or_op(values)


class SerializeNegation(SerializePhrase):

    adapts(NegationPhrase, Serializer)

    def serialize(self):
        value = self.serializer.serialize(self.phrase.term)
        return self.format.not_op(value)


class SerializeLiteral(SerializePhrase):

    adapts(LiteralPhrase, Serializer)

    def serialize(self):
        serialize_constant = SerializeConstant(self.phrase.domain,
                                               self.serializer)
        return serialize_constant.serialize(self.phrase.value)


class SerializeConstant(Adapter):

    adapts(Domain, Serializer)

    def __init__(self, domain, serializer):
        self.domain = domain
        self.serializer = serializer
        self.format = serializer.format

    def serialize(self, value):
        raise NotImplementedError()


class SerializeBooleanConstant(SerializeConstant):

    adapts(BooleanDomain, Serializer)

    def serialize(self, value):
        if value is None:
            return self.format.null()
        if value is True:
            return self.format.true()
        if value is False:
            return self.format.false()


class SerializeNumberConstant(SerializeConstant):

    adapts(NumberDomain, Serializer)

    def serialize(self, value):
        if value is None:
            return self.format.none()
        return self.format.number(value)


class SerializeStringConstant(SerializeConstant):

    adapts(StringDomain, Serializer)

    def serialize(self, value):
        if value is None:
            return self.format.none()
        return self.format.string(value)


class SerializeDateConstant(SerializeConstant):

    adapts(DateDomain, Serializer)

    def serialize(self, value):
        if value is None:
            return self.format.none()
        return self.format.date(value)


class SerializeLeafReference(SerializePhrase):

    adapts(LeafReferencePhrase, Serializer)

    def serialize(self):
        parent = self.serializer.get_alias(self.phrase.frame)
        parent = self.format.name(parent)
        child = self.format.name(self.phrase.column.name)
        return self.format.attr(parent, child)


class SerializeBranchReference(SerializePhrase):

    adapts(BranchReferencePhrase, Serializer)

    def serialize(self):
        parent = self.serializer.get_alias(self.phrase.frame)
        parent = self.format.name(parent)
        child = self.serializer.get_alias(self.phrase.phrase)
        child = self.format.name(child)
        return self.format.attr(parent, child)


class SerializeCorrelatedFrame(SerializePhrase):

    adapts(CorrelatedFramePhrase, Serializer)


serialize_adapters = find_adapters()