Project

General

Profile

« Previous | Next » 

Revision 2211

Moved SQL code generation classes from sql.py to new sql_gen.py. sql_gen.py: Added Code, Literal, ValueCond, and CompareCond. sql.py: Removed Query because we will use a different approach.

View differences:

lib/sql_gen.py
1
# SQL code generation
2

  
3
import sql
4

  
5
class Code:
6
    def to_str(self, db): raise NotImplemented()
7

  
8
class Literal:
9
    def __init__(self, value): self.value = value
10

  
11
class Table(Code):
12
    def __init__(self, name, schema=None):
13
        '''
14
        @param schema str|None (for no schema)
15
        '''
16
        self.name = name
17
        self.schema = schema
18
    
19
    def to_str(self, db): return sql.qual_name(db, self.schema, self.name)
20

  
21
class Col(Code):
22
    def __init__(self, name, table=None):
23
        '''
24
        @param table Table|None (for no table)
25
        '''
26
        assert table == None or isinstance(table, Table)
27
        
28
        self.name = name
29
        self.table = table
30
    
31
    def to_str(self, db):
32
        str_ = ''
33
        if self.table != None: str_ += self.table.to_str(db)+'.'
34
        str_ += sql.esc_name(db, self.name)
35
        return str_
36

  
37
class ValueCond(Code):
38
    def __init__(self, value): self.value = value
39

  
40
class CompareCond(ValueCond):
41
    def __init__(self, value, operator='='):
42
        ValueCond.__init__(self, value)
43
        self.operator = operator
44
    
45
    def to_str(self, db):
46
        raise NotImplemented()
lib/sql.py
117 117
    if schema != None: return esc_name_(schema)+'.'+table
118 118
    else: return table
119 119

  
120
##### Queries
121

  
122
class Query:
123
    def __init__(self, str='', values=None):
124
        if str == None: str = []
125
        
126
        self.str = str
127
        self.values = values
128

  
129 120
##### Database connections
130 121

  
131 122
db_config_names = ['engine', 'host', 'user', 'password', 'database', 'schemas']
......
390 381
                into_ref[0] = next_version(into_ref[0])
391 382
                # try again with next version of name
392 383

  
393
class Table:
394
    def __init__(self, name, schema=None):
395
        '''
396
        @param schema str|None (for no schema)
397
        '''
398
        self.name = name
399
        self.schema = schema
400
    
401
    def to_str(self, db): return qual_name(db, self.schema, self.name)
402

  
403
class Col:
404
    def __init__(self, name, table=None):
405
        '''
406
        @param table Table|None (for no table)
407
        '''
408
        assert table == None or isinstance(table, Table)
409
        
410
        self.name = name
411
        self.table = table
412
    
413
    def to_str(self, db):
414
        str_ = ''
415
        if self.table != None: str_ += self.table.to_str(db)+'.'
416
        str_ += esc_name(db, self.name)
417
        return str_
418

  
419 384
order_by_pkey = object() # tells mk_select() to order by the pkey
420 385

  
421 386
join_using = object() # tells mk_select() to join the column with USING

Also available in: Unified diff