Project

General

Profile

1
# SQL code generation
2

    
3
import copy
4
import operator
5
from ordereddict import OrderedDict
6
import re
7
import UserDict
8
import warnings
9

    
10
import dicts
11
import exc
12
import iters
13
import lists
14
import objects
15
import strings
16
import util
17

    
18
##### Names
19

    
20
identifier_max_len = 63 # works for both PostgreSQL and MySQL
21

    
22
def concat(str_, suffix):
23
    '''Preserves version so that it won't be truncated off the string, leading
24
    to collisions.'''
25
    # Preserve version
26
    match = re.match(r'^(.*?)((?:(?:#\d+)?\)?)*(?:\.\w+)?(?:::[\w ]+)*)$', str_)
27
    if match:
28
        str_, old_suffix = match.groups()
29
        suffix = old_suffix+suffix
30
    
31
    return strings.concat(str_, suffix, identifier_max_len)
32

    
33
def truncate(str_): return concat(str_, '')
34

    
35
def is_safe_name(name):
36
    '''A name is safe *and unambiguous* if it:
37
    * contains only *lowercase* word (\w) characters
38
    * doesn't start with a digit
39
    * contains "_", so that it's not a keyword
40
    '''
41
    return re.match(r'^(?=.*_)(?!\d)[^\WA-Z]+$', name)
42

    
43
def esc_name(name, quote='"'):
44
    return quote + name.replace(quote, quote+quote) + quote
45
        # doubling an embedded quote escapes it in both PostgreSQL and MySQL
46

    
47
def unesc_name(name, quote='"'):
48
    removed_ref = [False]
49
    name = strings.remove_prefix(quote, name, removed_ref)
50
    if removed_ref[0]:
51
        name = strings.remove_suffix(quote, name, removed_ref)
52
        assert removed_ref[0]
53
        name = name.replace(quote+quote, quote)
54
    return name
55

    
56
def clean_name(name): return name.replace('"', '').replace('`', '')
57

    
58
def esc_comment(comment): return '/*'+comment.replace('*/', '* /')+'*/'
59

    
60
def lstrip(str_):
61
    '''Also removes comments.'''
62
    if str_.startswith('/*'): comment, sep, str_ = str_.partition('*/')
63
    return str_.lstrip()
64

    
65
##### General SQL code objects
66

    
67
class MockDb:
68
    def esc_value(self, value): return strings.repr_no_u(value)
69
    
70
    def esc_name(self, name): return esc_name(name)
71
    
72
    def col_info(self, col):
73
        return TypedCol(col.name, '<type>', CustomCode('<default>'), True)
74

    
75
mockDb = MockDb()
76

    
77
class BasicObject(objects.BasicObject):
78
    def __str__(self): return clean_name(strings.repr_no_u(self))
79

    
80
##### Unparameterized code objects
81

    
82
class Code(BasicObject):
83
    def __init__(self, lang='sql'):
84
        self.lang = lang
85
    
86
    def to_str(self, db): raise NotImplementedError()
87
    
88
    def __repr__(self): return self.to_str(mockDb)
89

    
90
class CustomCode(Code):
91
    def __init__(self, str_):
92
        Code.__init__(self)
93
        
94
        self.str_ = str_
95
    
96
    def to_str(self, db): return self.str_
97

    
98
def as_Code(value, db=None):
99
    '''
100
    @param db If set, runs db.std_code() on the value.
101
    '''
102
    if isinstance(value, Code): return value
103
    
104
    if util.is_str(value):
105
        if db != None: value = db.std_code(value)
106
        return CustomCode(value)
107
    else: return Literal(value)
108

    
109
class Expr(Code):
110
    def __init__(self, expr):
111
        Code.__init__(self)
112
        
113
        self.expr = expr
114
    
115
    def to_str(self, db): return '('+self.expr.to_str(db)+')'
116

    
117
##### Names
118

    
119
class Name(Code):
120
    def __init__(self, name):
121
        Code.__init__(self)
122
        
123
        name = truncate(name)
124
        
125
        self.name = name
126
    
127
    def to_str(self, db): return db.esc_name(self.name)
128

    
129
def as_Name(value):
130
    if isinstance(value, Code): return value
131
    else: return Name(value)
132

    
133
##### Literal values
134

    
135
class Literal(Code):
136
    def __init__(self, value):
137
        Code.__init__(self)
138
        
139
        self.value = value
140
    
141
    def to_str(self, db): return db.esc_value(self.value)
142

    
143
def as_Value(value):
144
    if isinstance(value, Code): return value
145
    else: return Literal(value)
146

    
147
def is_literal(value): return isinstance(value, Literal)
148

    
149
def is_null(value): return is_literal(value) and value.value == None
150

    
151
##### Derived elements
152

    
153
src_self = object() # tells Col that it is its own source column
154

    
155
class Derived(Code):
156
    def __init__(self, srcs):
157
        '''An element which was derived from some other element(s).
158
        @param srcs See self.set_srcs()
159
        '''
160
        Code.__init__(self)
161
        
162
        self.set_srcs(srcs)
163
    
164
    def set_srcs(self, srcs, overwrite=True):
165
        '''
166
        @param srcs (self_type...)|src_self The element(s) this is derived from
167
        '''
168
        if not overwrite and self.srcs != (): return # already set
169
        
170
        if srcs == src_self: srcs = (self,)
171
        srcs = tuple(srcs) # make Col hashable
172
        self.srcs = srcs
173
    
174
    def _compare_on(self):
175
        compare_on = self.__dict__.copy()
176
        del compare_on['srcs'] # ignore
177
        return compare_on
178

    
179
def cols_srcs(cols): return lists.uniqify(iters.flatten((v.srcs for v in cols)))
180

    
181
##### Tables
182

    
183
class Table(Derived):
184
    def __init__(self, name, schema=None, srcs=(), is_temp=False):
185
        '''
186
        @param schema str|None (for no schema)
187
        @param srcs (Table...)|src_self See Derived.set_srcs()
188
        '''
189
        Derived.__init__(self, srcs)
190
        
191
        if util.is_str(name): name = truncate(name)
192
        
193
        self.name = name
194
        self.schema = schema
195
        self.is_temp = is_temp
196
        self.index_cols = {}
197
    
198
    def to_str(self, db):
199
        str_ = ''
200
        if self.schema != None: str_ += as_Name(self.schema).to_str(db)+'.'
201
        str_ += as_Name(self.name).to_str(db)
202
        return str_
203
    
204
    def to_Table(self): return self
205
    
206
    def _compare_on(self):
207
        compare_on = Derived._compare_on(self)
208
        del compare_on['index_cols'] # ignore
209
        return compare_on
210

    
211
def is_underlying_table(table):
212
    return isinstance(table, Table) and table.to_Table() is table
213

    
214
class NoUnderlyingTableException(Exception): pass
215

    
216
def underlying_table(table):
217
    table = remove_table_rename(table)
218
    if not is_underlying_table(table): raise NoUnderlyingTableException
219
    return table
220

    
221
def as_Table(table, schema=None):
222
    if table == None or isinstance(table, Code): return table
223
    else: return Table(table, schema)
224

    
225
def suffixed_table(table, suffix):
226
    table = copy.copy(table) # don't modify input!
227
    table.name = concat(table.name, suffix)
228
    return table
229

    
230
class NamedTable(Table):
231
    def __init__(self, name, code, cols=None):
232
        Table.__init__(self, name)
233
        
234
        code = as_Table(code)
235
        if not isinstance(code, (Table, FunctionCall, Expr)): code = Expr(code)
236
        if cols != None: cols = [to_name_only_col(c).to_Col() for c in cols]
237
        
238
        self.code = code
239
        self.cols = cols
240
    
241
    def to_str(self, db):
242
        str_ = self.code.to_str(db)
243
        if str_.find('\n') >= 0: whitespace = '\n'
244
        else: whitespace = ' '
245
        str_ += whitespace+'AS '+Table.to_str(self, db)
246
        if self.cols != None:
247
            str_ += ' ('+(', '.join((c.to_str(db) for c in self.cols)))+')'
248
        return str_
249
    
250
    def to_Table(self): return Table(self.name)
251

    
252
def remove_table_rename(table):
253
    if isinstance(table, NamedTable): table = table.code
254
    return table
255

    
256
##### Columns
257

    
258
class Col(Derived):
259
    def __init__(self, name, table=None, srcs=()):
260
        '''
261
        @param table Table|None (for no table)
262
        @param srcs (Col...)|src_self See Derived.set_srcs()
263
        '''
264
        Derived.__init__(self, srcs)
265
        
266
        if util.is_str(name): name = truncate(name)
267
        if util.is_str(table): table = Table(table)
268
        assert table == None or isinstance(table, Table)
269
        
270
        self.name = name
271
        self.table = table
272
    
273
    def to_str(self, db, for_str=False):
274
        str_ = as_Name(self.name).to_str(db)
275
        if for_str: str_ = clean_name(str_)
276
        if self.table != None:
277
            table = self.table.to_Table()
278
            if for_str: str_ = concat(str(table), '.'+str_)
279
            else: str_ = table.to_str(db)+'.'+str_
280
        return str_
281
    
282
    def __str__(self): return self.to_str(mockDb, for_str=True)
283
    
284
    def to_Col(self): return self
285

    
286
def is_table_col(col): return isinstance(col, Col) and col.table != None
287

    
288
def index_col(col):
289
    if not is_table_col(col): return None
290
    
291
    table = col.table
292
    try: name = table.index_cols[col.name]
293
    except KeyError: return None
294
    else: return Col(name, table, col.srcs)
295

    
296
def is_temp_col(col): return is_table_col(col) and col.table.is_temp
297

    
298
def as_Col(col, table=None, name=None):
299
    '''
300
    @param name If not None, any non-Col input will be renamed using NamedCol.
301
    '''
302
    if name != None:
303
        col = as_Value(col)
304
        if not isinstance(col, Col): col = NamedCol(name, col)
305
    
306
    if isinstance(col, Code): return col
307
    else: return Col(col, table)
308

    
309
def with_table(col, table):
310
    if isinstance(col, NamedCol): pass # doesn't take a table
311
    elif isinstance(col, FunctionCall):
312
        col = copy.deepcopy(col) # don't modify input!
313
        col.args[0].table = table
314
    else:
315
        col = copy.copy(col) # don't modify input!
316
        col.table = table
317
    return col
318

    
319
def with_default_table(col, table):
320
    col = as_Col(col)
321
    if col.table == None: col = with_table(col, table)
322
    return col
323

    
324
def set_cols_table(table, cols):
325
    table = as_Table(table)
326
    
327
    for i, col in enumerate(cols):
328
        col = cols[i] = as_Col(col)
329
        col.table = table
330

    
331
def to_name_only_col(col, check_table=None):
332
    col = as_Col(col)
333
    if not is_table_col(col): return col
334
    
335
    if check_table != None:
336
        table = col.table
337
        assert table == None or table == check_table
338
    return Col(col.name)
339

    
340
def suffixed_col(col, suffix):
341
    return Col(concat(col.name, suffix), col.table, col.srcs)
342

    
343
class NamedCol(Col):
344
    def __init__(self, name, code):
345
        Col.__init__(self, name)
346
        
347
        code = as_Value(code)
348
        
349
        self.code = code
350
    
351
    def to_str(self, db):
352
        return self.code.to_str(db)+' AS '+Col.to_str(self, db)
353
    
354
    def to_Col(self): return Col(self.name)
355

    
356
def remove_col_rename(col):
357
    if isinstance(col, NamedCol): col = col.code
358
    return col
359

    
360
def underlying_col(col):
361
    col = remove_col_rename(col)
362
    if not isinstance(col, Col): raise NoUnderlyingTableException
363
    
364
    return Col(col.name, underlying_table(col.table), col.srcs)
365

    
366
def wrap(wrap_func, value):
367
    '''Wraps a value, propagating any column renaming to the returned value.'''
368
    if isinstance(value, NamedCol):
369
        return NamedCol(value.name, wrap_func(value.code))
370
    else: return wrap_func(value)
371

    
372
class ColDict(dicts.DictProxy):
373
    '''A dict that automatically makes inserted entries Col objects'''
374
    
375
    def __init__(self, db, keys_table, dict_={}):
376
        dicts.DictProxy.__init__(self, OrderedDict())
377
        
378
        keys_table = as_Table(keys_table)
379
        
380
        self.db = db
381
        self.table = keys_table
382
        self.update(dict_) # after setting vars because __setitem__() needs them
383
    
384
    def copy(self): return ColDict(self.db, self.table, self.inner.copy())
385
    
386
    def __getitem__(self, key):
387
        return dicts.DictProxy.__getitem__(self, self._key(key))
388
    
389
    def __setitem__(self, key, value):
390
        key = self._key(key)
391
        if value == None: value = self.db.col_info(key).default
392
        dicts.DictProxy.__setitem__(self, key, as_Col(value, name=key.name))
393
    
394
    def _key(self, key): return as_Col(key, self.table)
395

    
396
##### Functions
397

    
398
Function = Table
399
as_Function = as_Table
400

    
401
class InternalFunction(CustomCode): pass
402

    
403
#### Calls
404

    
405
class NamedArg(NamedCol):
406
    def __init__(self, name, value):
407
        NamedCol.__init__(self, name, value)
408
    
409
    def to_str(self, db):
410
        return Col.to_str(self, db)+' := '+self.code.to_str(db)
411

    
412
class FunctionCall(Code):
413
    def __init__(self, function, *args, **kw_args):
414
        '''
415
        @param args [Code|literal-value...] The function's arguments
416
        '''
417
        Code.__init__(self)
418
        
419
        function = as_Function(function)
420
        def filter_(arg): return remove_col_rename(as_Value(arg))
421
        args = map(filter_, args)
422
        args += [NamedArg(k, filter_(v)) for k, v in kw_args.iteritems()]
423
        
424
        self.function = function
425
        self.args = args
426
    
427
    def to_str(self, db):
428
        args_str = ', '.join((v.to_str(db) for v in self.args))
429
        return self.function.to_str(db)+'('+args_str+')'
430

    
431
def wrap_in_func(function, value):
432
    '''Wraps a value inside a function call.
433
    Propagates any column renaming to the returned value.
434
    '''
435
    return wrap(lambda v: FunctionCall(function, v), value)
436

    
437
def unwrap_func_call(func_call, check_name=None):
438
    '''Unwraps any function call to its first argument.
439
    Also removes any column renaming.
440
    '''
441
    func_call = remove_col_rename(func_call)
442
    if not isinstance(func_call, FunctionCall): return func_call
443
    
444
    if check_name != None:
445
        name = func_call.function.name
446
        assert name == None or name == check_name
447
    return func_call.args[0]
448

    
449
#### Definitions
450

    
451
class FunctionDef(Code):
452
    def __init__(self, function, return_type, body):
453
        Code.__init__(self)
454
        
455
        body = as_Code(body)
456
        
457
        self.function = function
458
        self.return_type = return_type
459
        self.body = body
460
    
461
    def to_str(self, db):
462
        str_ = '''\
463
CREATE FUNCTION '''+self.function.to_str(db)+'''()
464
RETURNS '''+self.return_type+'''
465
LANGUAGE '''+self.body.lang+'''
466
AS $$
467
'''+self.body.to_str(db)+'''
468
$$;
469
'''
470
        return str_
471

    
472
class RowExcIgnore(Code):
473
    def __init__(self, row_type, select_query, with_row, cols=None,
474
        exc='unique_violation'):
475
        Code.__init__(self, lang='plpgsql')
476
        
477
        select_query = as_Code(select_query)
478
        with_row = as_Code(with_row)
479
        
480
        self.row_type = row_type
481
        self.select_query = select_query
482
        self.with_row = with_row
483
        self.cols = cols
484
        self.exc = exc
485
    
486
    def to_str(self, db):
487
        if self.cols == None: row_vars = [Table('row')]
488
        else: row_vars = [Col(c.name, 'row') for c in self.cols]
489
        
490
        str_ = '''\
491
DECLARE
492
    row '''+self.row_type+''';
493
BEGIN
494
    /* Need an EXCEPTION block for each individual row because "When an error is
495
    caught by an EXCEPTION clause, [...] all changes to persistent database
496
    state within the block are rolled back."
497
    This is unfortunate because "A block containing an EXCEPTION clause is
498
    significantly more expensive to enter and exit than a block without one."
499
    (http://www.postgresql.org/docs/8.3/static/plpgsql-control-structures.html\
500
#PLPGSQL-ERROR-TRAPPING)
501
    */
502
    FOR '''+(', '.join((v.to_str(db) for v in row_vars)))+''' IN
503
'''+self.select_query.to_str(db)+'''
504
    LOOP
505
        BEGIN
506
            RETURN QUERY
507
'''+self.with_row.to_str(db)+'''
508
;
509
        EXCEPTION
510
            WHEN '''+self.exc+''' THEN NULL; -- continue to next row
511
        END;
512
    END LOOP;
513
END;\
514
'''
515
        return str_
516

    
517
##### Casts
518

    
519
class Cast(FunctionCall):
520
    def __init__(self, type_, value):
521
        value = as_Value(value)
522
        
523
        self.type_ = type_
524
        self.value = value
525
    
526
    def to_str(self, db):
527
        return 'CAST('+self.value.to_str(db)+' AS '+self.type_+')'
528

    
529
def cast_literal(value):
530
    if not is_literal(value): return value
531
    
532
    if util.is_str(value.value): value = Cast('text', value)
533
    return value
534

    
535
##### Conditions
536

    
537
class NotCond(Code):
538
    def __init__(self, cond):
539
        Code.__init__(self)
540
        
541
        self.cond = cond
542
    
543
    def to_str(self, db): return 'NOT '+self.cond.to_str(db)
544

    
545
class ColValueCond(Code):
546
    def __init__(self, col, value):
547
        Code.__init__(self)
548
        
549
        value = as_ValueCond(value)
550
        
551
        self.col = col
552
        self.value = value
553
    
554
    def to_str(self, db): return self.value.to_str(db, self.col)
555

    
556
def combine_conds(conds, keyword=None):
557
    '''
558
    @param keyword The keyword to add before the conditions, if any
559
    '''
560
    str_ = ''
561
    if keyword != None:
562
        if conds == []: whitespace = ''
563
        elif len(conds) == 1: whitespace = ' '
564
        else: whitespace = '\n'
565
        str_ += keyword+whitespace
566
    
567
    str_ += '\nAND '.join(conds)
568
    return str_
569

    
570
##### Condition column comparisons
571

    
572
class ValueCond(BasicObject):
573
    def __init__(self, value):
574
        value = remove_col_rename(as_Value(value))
575
        
576
        self.value = value
577
    
578
    def to_str(self, db, left_value):
579
        '''
580
        @param left_value The Code object that the condition is being applied on
581
        '''
582
        raise NotImplemented()
583
    
584
    def __repr__(self): return self.to_str(mockDb, '<left_value>')
585

    
586
class CompareCond(ValueCond):
587
    def __init__(self, value, operator='='):
588
        '''
589
        @param operator By default, compares NULL values literally. Use '~=' or
590
            '~!=' to pass NULLs through.
591
        '''
592
        ValueCond.__init__(self, value)
593
        self.operator = operator
594
    
595
    def to_str(self, db, left_value):
596
        left_value = remove_col_rename(as_Col(left_value))
597
        
598
        right_value = self.value
599
        
600
        # Parse operator
601
        operator = self.operator
602
        passthru_null_ref = [False]
603
        operator = strings.remove_prefix('~', operator, passthru_null_ref)
604
        neg_ref = [False]
605
        operator = strings.remove_prefix('!', operator, neg_ref)
606
        equals = operator.endswith('=') # also includes <=, >=
607
        
608
        # Handle nullable columns
609
        check_null = False
610
        if not passthru_null_ref[0]: # NULLs compare equal
611
            try: left_value = ensure_not_null(db, left_value)
612
            except ensure_not_null_excs: # fall back to alternate method
613
                check_null = equals and isinstance(right_value, Col)
614
            else:
615
                if isinstance(left_value, EnsureNotNull):
616
                    right_value = ensure_not_null(db, right_value,
617
                        left_value.type) # apply same function to both sides
618
        
619
        if equals and is_null(right_value): operator = 'IS'
620
        
621
        left = left_value.to_str(db)
622
        right = right_value.to_str(db)
623
        
624
        # Create str
625
        str_ = left+' '+operator+' '+right
626
        if check_null:
627
            str_ = '('+str_+' OR ('+left+' IS NULL AND '+right+' IS NULL))'
628
        if neg_ref[0]: str_ = 'NOT '+str_
629
        return str_
630

    
631
# Tells as_ValueCond() to assume a non-ValueCond is a literal value
632
assume_literal = object()
633

    
634
def as_ValueCond(value, default_table=assume_literal):
635
    if not isinstance(value, ValueCond):
636
        if default_table is not assume_literal:
637
            value = with_default_table(value, default_table)
638
        return CompareCond(value)
639
    else: return value
640

    
641
##### Joins
642

    
643
join_same = object() # tells Join the left and right columns have the same name
644

    
645
# Tells Join the left and right columns have the same name and are never NULL
646
join_same_not_null = object()
647

    
648
filter_out = object() # tells Join to filter out rows that match the join
649

    
650
class Join(BasicObject):
651
    def __init__(self, table, mapping={}, type_=None):
652
        '''
653
        @param mapping dict(right_table_col=left_table_col, ...)
654
            * if left_table_col is join_same: left_table_col = right_table_col
655
              * Note that right_table_col must be a string
656
            * if left_table_col is join_same_not_null:
657
              left_table_col = right_table_col and both have NOT NULL constraint
658
              * Note that right_table_col must be a string
659
        @param type_ None (for plain join)|str (e.g. 'LEFT')|filter_out
660
            * filter_out: equivalent to 'LEFT' with the query filtered by
661
              `table_pkey IS NULL` (indicating no match)
662
        '''
663
        if util.is_str(table): table = Table(table)
664
        assert type_ == None or util.is_str(type_) or type_ is filter_out
665
        
666
        self.table = table
667
        self.mapping = mapping
668
        self.type_ = type_
669
    
670
    def to_str(self, db, left_table_):
671
        def join(entry):
672
            '''Parses non-USING joins'''
673
            right_table_col, left_table_col = entry
674
            
675
            # Switch order (right_table_col is on the left in the comparison)
676
            left = right_table_col
677
            right = left_table_col
678
            left_table = self.table
679
            right_table = left_table_
680
            
681
            # Parse left side
682
            left = with_default_table(left, left_table)
683
            
684
            # Parse special values
685
            left_on_right = Col(left.name, right_table)
686
            if right is join_same: right = left_on_right
687
            elif right is join_same_not_null:
688
                right = CompareCond(left_on_right, '~=')
689
            
690
            # Parse right side
691
            right = as_ValueCond(right, right_table)
692
            
693
            return right.to_str(db, left)
694
        
695
        # Create join condition
696
        type_ = self.type_
697
        joins = self.mapping
698
        if joins == {}: join_cond = None
699
        elif type_ is not filter_out and reduce(operator.and_,
700
            (v is join_same_not_null for v in joins.itervalues())):
701
            # all cols w/ USING, so can use simpler USING syntax
702
            cols = map(to_name_only_col, joins.iterkeys())
703
            join_cond = 'USING ('+(', '.join((c.to_str(db) for c in cols)))+')'
704
        else: join_cond = combine_conds(map(join, joins.iteritems()), 'ON')
705
        
706
        if isinstance(self.table, NamedTable): whitespace = '\n'
707
        else: whitespace = ' '
708
        
709
        # Create join
710
        if type_ is filter_out: type_ = 'LEFT'
711
        str_ = ''
712
        if type_ != None: str_ += type_+' '
713
        str_ += 'JOIN'+whitespace+self.table.to_str(db)
714
        if join_cond != None: str_ += whitespace+join_cond
715
        return str_
716
    
717
    def __repr__(self): return self.to_str(mockDb, '<left_table>')
718

    
719
##### Value exprs
720

    
721
all_cols = CustomCode('*')
722

    
723
default = CustomCode('DEFAULT')
724

    
725
row_count = FunctionCall(InternalFunction('COUNT'), all_cols)
726

    
727
class Coalesce(FunctionCall):
728
    def __init__(self, *args):
729
        FunctionCall.__init__(self, InternalFunction('COALESCE'), *args)
730

    
731
class Nullif(FunctionCall):
732
    def __init__(self, *args):
733
        FunctionCall.__init__(self, InternalFunction('NULLIF'), *args)
734

    
735
# See <http://www.postgresql.org/docs/8.3/static/datatype-numeric.html>
736
null_sentinels = {
737
    'character varying': r'\N',
738
    'double precision': 'NaN',
739
    'integer': 2147483647,
740
    'text': r'\N',
741
    'timestamp with time zone': 'infinity'
742
}
743

    
744
class EnsureNotNull(Coalesce):
745
    def __init__(self, value, type_):
746
        Coalesce.__init__(self, as_Col(value),
747
            Cast(type_, null_sentinels[type_]))
748
        
749
        self.type = type_
750
    
751
    def to_str(self, db):
752
        col = self.args[0]
753
        index_col_ = index_col(col)
754
        if index_col_ != None: return index_col_.to_str(db)
755
        return Coalesce.to_str(self, db)
756

    
757
##### Table exprs
758

    
759
class Values(Code):
760
    def __init__(self, values):
761
        '''
762
        @param values [...]|[[...], ...] Can be one or multiple rows.
763
        '''
764
        Code.__init__(self)
765
        
766
        rows = values
767
        if len(values) >= 1 and not lists.is_seq(values[0]): # only one row
768
            rows = [values]
769
        for i, row in enumerate(rows):
770
            rows[i] = map(remove_col_rename, map(as_Value, row))
771
        
772
        self.rows = rows
773
    
774
    def to_str(self, db):
775
        def row_str(row):
776
            return '('+(', '.join((v.to_str(db) for v in row)))+')'
777
        return 'VALUES '+(', '.join(map(row_str, self.rows)))
778

    
779
def NamedValues(name, cols, values):
780
    '''
781
    @param cols None|[...]
782
    @post `cols` will be changed to Col objects with the table set to `name`.
783
    '''
784
    table = NamedTable(name, Values(values), cols)
785
    if cols != None: set_cols_table(table, cols)
786
    return table
787

    
788
##### Database structure
789

    
790
class TypedCol(Col):
791
    def __init__(self, name, type_, default=None, nullable=True,
792
        constraints=None):
793
        assert default == None or isinstance(default, Code)
794
        
795
        Col.__init__(self, name)
796
        
797
        self.type = type_
798
        self.default = default
799
        self.nullable = nullable
800
        self.constraints = constraints
801
    
802
    def to_str(self, db):
803
        str_ = Col.to_str(self, db)+' '+self.type
804
        if not self.nullable: str_ += ' NOT NULL'
805
        if self.default != None: str_ += ' DEFAULT '+self.default.to_str(db)
806
        if self.constraints != None: str_ += ' '+self.constraints
807
        return str_
808
    
809
    def to_Col(self): return Col(self.name)
810

    
811
ensure_not_null_excs = (NoUnderlyingTableException, KeyError)
812

    
813
def ensure_not_null(db, col, type_=None):
814
    '''
815
    @param col If type_ is not set, must have an underlying column.
816
    @param type_ If set, overrides the underlying column's type.
817
    @return EnsureNotNull|Col
818
    @throws ensure_not_null_excs
819
    '''
820
    nullable = True
821
    try: typed_col = db.col_info(underlying_col(col))
822
    except NoUnderlyingTableException:
823
        col = remove_col_rename(col)
824
        if is_literal(col) and not is_null(col): nullable = False
825
        elif type_ == None: raise
826
    else:
827
        if type_ == None: type_ = typed_col.type
828
        nullable = typed_col.nullable
829
    
830
    if nullable:
831
        try: col = EnsureNotNull(col, type_)
832
        except KeyError, e:
833
            # Warn of no null sentinel for type, even if caller catches error
834
            warnings.warn(UserWarning(exc.str_(e)))
835
            raise
836
    
837
    return col
(25-25/37)