Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b68178e

Browse files
committed
Kotlin: Handle enums better when generating dbscheme
1 parent 5f99165 commit b68178e

2 files changed

Lines changed: 61 additions & 41 deletions

File tree

java/kotlin-extractor/generate_dbscheme.py

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,54 @@ def upperFirst(string):
1313
dbscheme = re.sub(r'/\*.*?\*/', '', dbscheme, flags=re.DOTALL)
1414
dbscheme = re.sub(r'//[^\r\n]*/', '', dbscheme)
1515

16+
enums = {}
1617
type_aliases = {}
1718
type_hierarchy = {}
1819

20+
def genTable(kt, relname, body, enum = None, kind = None, num = None, typ = None):
21+
kt.write('fun TrapWriter.write' + upperFirst(relname))
22+
if kind is not None:
23+
kt.write('_' + typ)
24+
kt.write('(')
25+
for colname, db_type in re.findall('(\S+)\s*:\s*([^\s,]+)', body):
26+
if colname != kind:
27+
kt.write(colname + ': ')
28+
if db_type == 'int':
29+
# TODO: Do something better if the column is a 'case'
30+
kt.write('Int')
31+
elif db_type == 'float':
32+
kt.write('Double')
33+
elif db_type == 'string':
34+
kt.write('String')
35+
elif db_type == 'date':
36+
kt.write('String')
37+
elif db_type == 'boolean':
38+
kt.write('Boolean')
39+
elif db_type[0] == '@':
40+
label = db_type[1:]
41+
if label == enum:
42+
label = typ
43+
kt.write('Label<out Db' + upperFirst(label) + '>')
44+
else:
45+
raise Exception('Bad db_type: ' + db_type)
46+
kt.write(', ')
47+
kt.write(') {\n')
48+
kt.write(' this.writeTrap("' + relname + '(')
49+
comma = ''
50+
for colname, db_type in re.findall('(\S+)\s*:\s*([^\s,]+)', body):
51+
kt.write(comma)
52+
if colname == kind:
53+
kt.write(str(num))
54+
elif db_type == 'string' or db_type == 'date':
55+
kt.write('\\"$' + colname + '\\"') # TODO: Escaping
56+
else:
57+
# TODO: Any reformatting or escaping necessary?
58+
# e.g. float formats?
59+
kt.write('$' + colname)
60+
comma = ', '
61+
kt.write(')\\n")\n')
62+
kt.write('}\n')
63+
1964
with open('src/main/kotlin/KotlinExtractorDbScheme.kt', 'w') as kt:
2065
kt.write('/* Generated by ' + sys.argv[0] + ': Do not edit manually. */\n')
2166
kt.write('package com.github.codeql\n')
@@ -29,10 +74,13 @@ def upperFirst(string):
2974
for name, kind, body in re.findall(r'case\s+@([^.\s]*)\.([^.\s]*)\s+of\b(.*?);',
3075
dbscheme,
3176
flags=re.DOTALL):
77+
mapping = []
3278
for num, typ in re.findall(r'(\d+)\s*=\s*@(\S+)', body):
3379
s = type_hierarchy.get(typ, set())
3480
s.add(name)
3581
type_hierarchy[typ] = s
82+
mapping.append((int(num), typ))
83+
enums[name] = (kind, mapping)
3684

3785
# unions
3886
for name, unions in re.findall(r'@(\w+)\s*=\s*(@\w+(?:\s*\|\s*@\w+)*)',
@@ -52,41 +100,17 @@ def upperFirst(string):
52100
for relname, body in re.findall('\n([\w_]+)(\([^)]*\))',
53101
dbscheme,
54102
flags=re.DOTALL):
103+
enum = None
55104
for db_type in re.findall(':\s*@([^\s,]+)\s*(?:,|$)', body):
56105
type_hierarchy[db_type] = type_hierarchy.get(db_type, set())
57-
kt.write('fun TrapWriter.write' + upperFirst(relname) + '(')
58-
for colname, db_type in re.findall('(\S+)\s*:\s*([^\s,]+)', body):
59-
kt.write(colname + ': ')
60-
if db_type == 'int':
61-
# TODO: Do something better if the column is a 'case'
62-
kt.write('Int')
63-
elif db_type == 'float':
64-
kt.write('Double')
65-
elif db_type == 'string':
66-
kt.write('String')
67-
elif db_type == 'date':
68-
kt.write('String')
69-
elif db_type == 'boolean':
70-
kt.write('Boolean')
71-
elif db_type[0] == '@':
72-
kt.write('Label<out Db' + upperFirst(db_type[1:]) + '>')
73-
else:
74-
raise Exception('Bad db_type: ' + db_type)
75-
kt.write(', ')
76-
kt.write(') {\n')
77-
kt.write(' this.writeTrap("' + relname + '(')
78-
comma = ''
79-
for colname, db_type in re.findall('(\S+)\s*:\s*([^\s,]+)', body):
80-
kt.write(comma)
81-
if db_type == 'string' or db_type == 'date':
82-
kt.write('\\"$' + colname + '\\"') # TODO: Escaping
83-
else:
84-
# TODO: Any reformatting or escaping necessary?
85-
# e.g. float formats?
86-
kt.write('$' + colname)
87-
comma = ', '
88-
kt.write(')\\n")\n')
89-
kt.write('}\n')
106+
if db_type in enums:
107+
enum = db_type
108+
if enum is None:
109+
genTable(kt, relname, body)
110+
else:
111+
(kind, mapping) = enums[enum]
112+
for num, typ in mapping:
113+
genTable(kt, relname, body, enum, kind, num, typ)
90114

91115
for typ in sorted(type_hierarchy):
92116
if typ in type_aliases:

java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ class KotlinFileExtractor(val tw: TrapWriter) {
223223
fun extractBlockBody(b: IrBlockBody, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
224224
val id = tw.getFreshIdLabel<DbBlock>()
225225
val locId = tw.getLocation(b.startOffset, b.endOffset)
226-
val kind = 0 // TODO: stmt kind for block from generated module
227-
tw.writeStmts(id, kind, parent, idx, callable)
226+
tw.writeStmts_block(id, parent, idx, callable)
228227
tw.writeHasLocation(id, locId)
229228
for((sIdx, stmt) in b.statements.withIndex()) {
230229
extractStatement(stmt, callable, id, sIdx)
@@ -236,8 +235,7 @@ class KotlinFileExtractor(val tw: TrapWriter) {
236235
is IrReturn -> {
237236
val id = tw.getFreshIdLabel<DbReturnstmt>()
238237
val locId = tw.getLocation(s.startOffset, s.endOffset)
239-
val kind = 9 // TODO: stmt kind for return from generated module
240-
tw.writeStmts(id, kind, parent, idx, callable)
238+
tw.writeStmts_returnstmt(id, parent, idx, callable)
241239
tw.writeHasLocation(id, locId)
242240
extractExpression(s.value, id, 0)
243241
}
@@ -252,11 +250,10 @@ class KotlinFileExtractor(val tw: TrapWriter) {
252250
val left = e.dispatchReceiver
253251
val right = e.getValueArgument(0)
254252
if(left != null && right != null) {
255-
val kind = 27 // TODO: expr kind for addexpr from generated module
256253
val id = tw.getFreshIdLabel<DbAddexpr>()
257254
val typeId = useType(e.type)
258255
val locId = tw.getLocation(e.startOffset, e.endOffset)
259-
tw.writeExprs(id, kind, typeId, parent, idx)
256+
tw.writeExprs_addexpr(id, typeId, parent, idx)
260257
tw.writeHasLocation(id, locId)
261258
extractExpression(left, id, 0)
262259
extractExpression(right, id, 1)
@@ -269,11 +266,10 @@ class KotlinFileExtractor(val tw: TrapWriter) {
269266
}
270267
is IrConst<*> -> {
271268
val v = e.value as Int
272-
val kind = 17 // TODO: expr kind for integerliteral from generated module
273269
val id = tw.getFreshIdLabel<DbIntegerliteral>()
274270
val typeId = useType(e.type)
275271
val locId = tw.getLocation(e.startOffset, e.endOffset)
276-
tw.writeExprs(id, kind, typeId, parent, idx)
272+
tw.writeExprs_integerliteral(id, typeId, parent, idx)
277273
tw.writeHasLocation(id, locId)
278274
tw.writeNamestrings(v.toString(), v.toString(), id)
279275
}

0 commit comments

Comments
 (0)