-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplpgsql.py
205 lines (157 loc) · 6.44 KB
/
plpgsql.py
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
import os
import jinja2
from jinja2 import FileSystemLoader
e = jinja2.Environment(loader=FileSystemLoader('tpls'))
def getPGTypeFieldName(colName):
return colName[colName.find('_') + 1:]
def getSProcName(schema, table):
return table
def getTypeName(table):
return "t_" + table.name
def getFieldNameForTable(table):
return table.name
def create_pg_type(table):
t = e.get_template('sql/type_definition.sql')
colSource = []
for f in table.fields:
type = f.type
if f.maxLength is not None and f.maxLength > 0:
type += "(" + str(f.maxLength) + ")"
colSource.append(""" """ + getPGTypeFieldName(f.name) + """ """ + type)
for a in table.associations:
colSource.append(""" """ + getFieldNameForTable(a.tableTo) + """ """ + getTypeName(a.tableTo))
return t.render(name=getTypeName(table), columns=",\n".join(colSource))
def create_insert(table):
t = e.get_template('sql/insert.sql')
cols = []
ins = []
rets = []
for f in table.fields:
rets.append(" " + f.name)
if not f.isSerial:
cols.append(" " + f.name)
ins.append(" p_in." + getPGTypeFieldName(f.name))
columns = ",\n".join(cols)
insertValues = ",\n".join(ins)
returnColumns = ",\n".join(rets)
return t.render(sprocName=getSProcName(table.schema, table.name),
returnType=getTypeName(table),
schema=table.schema,
tableName=table.name,
columns=columns,
insertValues=insertValues,
returnColumns=returnColumns)
def create_update(table):
t = e.get_template('sql/update.sql')
cols = []
wheres = []
rets = []
for f in table.fields:
rets.append(" " + f.name)
if f.isSerial:
cols.append(" " + f.name + " = COALESCE ( p_in." + getPGTypeFieldName(f.name) + ", " + f.name + " )")
wheres.append(" " + f.name + " = p_in." + getPGTypeFieldName(f.name))
updateColumns = ",\n".join(cols)
whereColumns = ",\n".join(wheres)
returnColumns = ",\n".join(rets)
return t.render(sprocName=getSProcName(table.schema, table.name),
returnType=getTypeName(table),
schema=table.schema,
tableName=table.name,
updateColumns=updateColumns,
whereColumns=whereColumns,
returnColumns=returnColumns)
def create_delete(table):
t = e.get_template('sql/delete.sql')
cols = []
rets = []
for f in table.fields:
rets.append(" " + f.name)
if f.isSerial:
cols.append(" " + f.name + " = p_in." + getPGTypeFieldName(f.name))
whereColumns = ",\n".join(cols)
returnColumns = ",\n".join(rets)
return t.render(sprocName=getSProcName(table.schema, table.name),
returnType=getTypeName(table),
schema=table.schema,
tableName=table.name,
whereColumns=whereColumns,
returnColumns=returnColumns)
def create_select_pk(table):
t = e.get_template('sql/select_using_uk.sql')
cols = []
wheres = []
keys = []
for f in table.fields:
cols.append(" " + f.name)
if f.isPk:
wheres.append(" " + f.name + " = p_" + getPGTypeFieldName(f.name))
keys.append("p_" + getPGTypeFieldName(f.name) + " " + f.type)
for a in table.associations:
if a.doFollow and a.tableFrom == table:
cols.append(""" CASE WHEN (""" + a.getSourceTuple() + """) IS NOT NULL THEN
(SELECT """ + a.tableTo.getSelectFieldListForType() + """
FROM """ + a.tableTo.schema + "." + a.tableTo.name + """
WHERE (""" + a.getSourceTuple() + ") = (" + a.getTargetTuple() + ")) ELSE NULL END")
elif a.doFollow:
cols.append(
""" ARRAY ( SELECT """ + a.tableFrom.getSelectFieldListForType() + """ FROM """ + a.tableFrom.schema + "." + a.tableFrom.name + """ WHERE (""" + a.getSourceTuple() + """) = (""" + a.getTargetTuple() + """) ) """)
selectColumns = ",\n".join(cols)
whereColumns = ",\n".join(wheres)
return t.render(sprocName=getSProcName(table.schema, table.name),
keyColumns=", ".join(keys),
returnType=getTypeName(table),
schema=table.schema,
tableName=table.name,
whereColumns=whereColumns,
selectColumns=selectColumns)
def generate_selects(sp, table):
t = e.get_template('sql/select_using_uk.sql')
file_name = getTypeName(table)
cols = []
for f in table.fields:
cols.append(" " + f.name)
selectColumns = ",\n".join(cols)
for ui in table.indexes:
keys = []
wheres = []
names = []
for i in ui:
f = table.fields[int(i) - 1]
wheres.append(" " + f.name + " = p_" + getPGTypeFieldName(f.name))
keys.append("p_" + getPGTypeFieldName(f.name) + " " + f.type)
names.append(getPGTypeFieldName(f.name))
proc_name = "_".join(names)
src = t.render(sprocName=getSProcName(table.schema, table.name),
keyColumns=", ".join(keys),
uniqueKeyName=proc_name,
returnType=getTypeName(table),
schema=table.schema,
tableName=table.name,
whereColumns=",\n".join(wheres),
selectColumns=selectColumns)
save_file(sp, file_name + '_get_by_' + proc_name, src)
def create_sprocs(table):
ret = create_insert(table)
ret += create_delete(table)
ret += create_update(table)
ret += create_select_pk(table)
return ret
def save_file(path, file_name, data):
if not os.path.isdir(path):
os.makedirs(path, 0755)
fd = open(path + os.sep + "20_" + file_name + ".sql", "w")
fd.write(data)
fd.close()
def generate_code(table, path):
src = create_pg_type(table)
file_name = getTypeName(table)
save_file(path + os.sep + '03_types', file_name, src)
sp = path + os.sep + '05_stored_procedures'
src = create_insert(table)
save_file(sp, file_name + '_create', src)
src = create_update(table)
save_file(sp, file_name + '_update', src)
src = create_delete(table)
save_file(sp, file_name + '_delete', src)
generate_selects(sp, table)