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
# -*- coding: utf-8 -*-
import io, os, re, glob
 
base = r'C:\Users\jcxiong\Documents\Codex\MyProject\trafficAudit'
with io.open(os.path.join(base, 'docs', 'init.sql'), encoding='utf-8') as f:
    sql = f.read()
 
def snake(name):
    return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()
 
tables = {}
for m in re.finditer(r'CREATE TABLE\s+(\w+)\s*\((.*?)\)\s*ENGINE', sql, re.S):
    name, body = m.group(1), m.group(2)
    cols = set()
    for cm in re.finditer(r'^\s*(\w+)\s', body, re.M):
        cols.add(cm.group(1))
    tables[name] = cols
 
entity_files = glob.glob(os.path.join(base, 'traffic-audit-server', 'src', 'main', 'java', 'com', 'trafficaudit', '**', 'entity', '*.java'), recursive=True)
for f in entity_files:
    with io.open(f, encoding='utf-8') as fh:
        src = fh.read()
    tm = re.search(r'@TableName\("(\w+)"\)', src)
    if not tm:
        print(os.path.basename(f), '-> NO @TableName'); continue
    tname = tm.group(1)
    fields = re.findall(r'private\s+[\w<>]+\s+(\w+);', src)
    fields = [x for x in fields if x != 'serialVersionUID']
    col_fields = {snake(fld) for fld in fields}
    missing = sorted(col_fields - tables.get(tname, set()))
    extra = sorted(tables.get(tname, set()) - col_fields)
    status = 'OK' if not missing else 'MISSING: ' + ', '.join(missing)
    print(f'{os.path.basename(f)} -> {tname}: {status}')
    if extra:
        print('   (table-only cols):', ', '.join(extra))