1 |
2211
|
aaronmk
|
# 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()
|