103 lines
3.2 KiB
Python
103 lines
3.2 KiB
Python
from flask import Flask, render_template, request, jsonify, send_file, send_from_directory
|
||
from utils.sql_parse import parse_create_table_sql
|
||
from utils.log import Log
|
||
|
||
app = Flask(__name__)
|
||
log = Log().getlog()
|
||
|
||
|
||
@app.route('/css/<path:filename>')
|
||
def css_files(filename):
|
||
return send_from_directory('templates/css', filename)
|
||
|
||
|
||
@app.route('/')
|
||
def index():
|
||
return send_file('templates/index.html')
|
||
|
||
|
||
@app.route('/a')
|
||
def index_a():
|
||
return render_template('a.html')
|
||
|
||
|
||
@app.route('/b')
|
||
def index_b():
|
||
return render_template('b.html')
|
||
|
||
|
||
@app.route('/convert', methods=['POST'])
|
||
def convert_sql():
|
||
sql_input = request.form['sql']
|
||
if sql_input.strip() == '':
|
||
return jsonify({
|
||
'parsed_result': "请在上面输入框输入hologres建表语句",
|
||
'message': 'SQL processed.'
|
||
})
|
||
|
||
log.info("SQL Input: %s", sql_input)
|
||
try:
|
||
parsed_result = parse_create_table_sql(sql_input, '')
|
||
print(parsed_result)
|
||
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)
|
||
return jsonify(result)
|
||
|
||
|
||
@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)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
# 指定host和port,这里使用0.0.0.0可以让服务器被外部访问
|
||
app.run(host='0.0.0.0', port=8778, debug=True)
|