sqllineage/sqllineage.py

103 lines
3.2 KiB
Python
Raw Normal View History

2025-08-20 17:59:26 +08:00
from flask import Flask, render_template, request, jsonify, send_file, send_from_directory
2025-02-05 14:18:02 +08:00
from utils.sql_parse import parse_create_table_sql
from utils.log import Log
app = Flask(__name__)
2025-02-05 16:16:44 +08:00
log = Log().getlog()
2025-02-05 14:18:02 +08:00
2025-08-20 17:59:26 +08:00
@app.route('/css/<path:filename>')
def css_files(filename):
return send_from_directory('templates/css', filename)
2025-02-05 14:18:02 +08:00
@app.route('/')
def index():
2025-02-27 19:05:15 +08:00
return send_file('templates/index.html')
2025-02-27 17:36:27 +08:00
2025-08-20 17:59:26 +08:00
2025-02-27 17:36:27 +08:00
@app.route('/a')
def index_a():
return render_template('a.html')
2025-08-20 17:59:26 +08:00
2025-02-27 17:36:27 +08:00
@app.route('/b')
def index_b():
return render_template('b.html')
2025-08-20 17:59:26 +08:00
2025-02-05 14:18:02 +08:00
@app.route('/convert', methods=['POST'])
def convert_sql():
sql_input = request.form['sql']
2025-02-27 17:36:27 +08:00
if sql_input.strip() == '':
2025-02-07 17:09:31 +08:00
return jsonify({
'parsed_result': "请在上面输入框输入hologres建表语句",
'message': 'SQL processed.'
})
2025-02-05 14:18:02 +08:00
log.info("SQL Input: %s", sql_input)
try:
2025-02-27 17:36:27 +08:00
parsed_result = parse_create_table_sql(sql_input, '')
2025-02-20 11:39:04 +08:00
print(parsed_result)
2025-02-05 14:18:02 +08:00
result = {
2025-02-05 15:57:40 +08:00
'parsed_result': parsed_result,
2025-02-05 14:18:02 +08:00
'message': 'SQL processed successfully.'
}
except Exception as e:
2025-02-05 15:57:40 +08:00
result = {'error': f'请检查输入sql:\n\n {str(e)}'}
2025-02-05 14:18:02 +08:00
log.info("SQL result: %s", result)
return jsonify(result)
2025-08-20 17:59:26 +08:00
@app.route('/convert1', methods=['POST'])
def convert_sql1():
createTableDDL = request.form['sql']
if createTableDDL.strip() == '':
return jsonify({
'parsed_result': "请在上面输入框输入hologres建表语句",
'message': 'SQL processed.'
})
log.info("SQL Input: %s", createTableDDL)
try:
parsed_result = ''
createTableDDL = createTableDDL.split('CREATE')[1].split('with (')[0]
createTableDDL = 'CREATE' + createTableDDL
print(createTableDDL)
createTableDDL = createTableDDL.replace('public.', '').replace('integer', 'int')
createTableDDL = createTableDDL.replace('timestamp with time zone', 'DATETIME').replace('text', 'VARCHAR(64)')
createTableDDL = createTableDDL.replace(',PRIMARY KEY', 'UNIQUE KEY')
createTableDDL = createTableDDL.replace('numeric(10,2)', 'DECIMAL(10,2)')
createTableDDL = createTableDDL.replace('numeric(10,4)', 'DECIMAL(10,4)')
createTableDDL = createTableDDL.split('\n')
createTableDDL = [(i + '\n').strip() for i in createTableDDL]
UNIQUE_str = ''
UNIQUE_key = ''
for i in createTableDDL:
if i.startswith('UNIQUE'):
# 获取字符()里面的字符串
UNIQUE_key = i.replace('UNIQUE KEY (', '').replace(")", "")
UNIQUE_str += i
else:
if i:
parsed_result += i + '\n'
UNIQUE_str += f' DISTRIBUTED BY HASH ({UNIQUE_key}) BUCKETS 32;'
parsed_result += UNIQUE_str
result = {
'parsed_result': parsed_result,
'message': 'SQL processed successfully.'
}
except Exception as e:
result = {'error': f'请检查输入sql:\n\n {str(e)}'}
log.info("SQL result: %s", result)
print(result)
return jsonify(result)
2025-02-05 14:18:02 +08:00
if __name__ == '__main__':
# 指定host和port这里使用0.0.0.0可以让服务器被外部访问
2025-02-27 17:36:27 +08:00
app.run(host='0.0.0.0', port=8778, debug=True)