Delete pubchemlite_invitrodb_to_dify_en.py
Browse files
pubchemlite_invitrodb_to_dify_en.py
DELETED
|
@@ -1,151 +0,0 @@
|
|
| 1 |
-
# Import dependency libraries
|
| 2 |
-
from flask import Flask, request, jsonify
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import sqlalchemy
|
| 5 |
-
from sqlalchemy.exc import SQLAlchemyError
|
| 6 |
-
|
| 7 |
-
# Create Flask application
|
| 8 |
-
app = Flask(__name__)
|
| 9 |
-
|
| 10 |
-
# Configure connection information for two databases (please modify username/password/address according to actual environment)
|
| 11 |
-
DB_CONFIGS = {
|
| 12 |
-
"pubchemlite": {
|
| 13 |
-
"uri": "mysql+pymysql://root:mysql1086@localhost:3306/pubchemlite"
|
| 14 |
-
},
|
| 15 |
-
"invitrodb_v4_3": {
|
| 16 |
-
"uri": "mysql+pymysql://root:mysql1086@localhost:3306/invitrodb_v4_3" # Please confirm database name and password
|
| 17 |
-
}
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
# List of allowed database identifiers
|
| 21 |
-
ALLOWED_DB_IDENTIFIERS = list(DB_CONFIGS.keys())
|
| 22 |
-
# Only allow SELECT operations (security restriction)
|
| 23 |
-
ALLOWED_SQL_OPERATIONS = ["select"]
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Core interface: /execute_sql (supports POST method, GET only returns description)
|
| 27 |
-
@app.route('/execute_sql', methods=['GET', 'POST'])
|
| 28 |
-
def execute_sql():
|
| 29 |
-
# GET request returns interface description
|
| 30 |
-
if request.method == 'GET':
|
| 31 |
-
return jsonify({
|
| 32 |
-
"status": "info",
|
| 33 |
-
"message": "Please use POST method to call, parameters include db_identifier (pubchemlite/invitrodb_v4_3) and sql (SELECT only)"
|
| 34 |
-
})
|
| 35 |
-
|
| 36 |
-
try:
|
| 37 |
-
# 1. Get and validate request parameters
|
| 38 |
-
data = request.json
|
| 39 |
-
if not data:
|
| 40 |
-
return jsonify({
|
| 41 |
-
"status": "error",
|
| 42 |
-
"message": "Request body cannot be empty, must contain db_identifier and sql parameters"
|
| 43 |
-
}), 400
|
| 44 |
-
|
| 45 |
-
# Get database identifier and validate
|
| 46 |
-
db_identifier = data.get("db_identifier")
|
| 47 |
-
if not db_identifier:
|
| 48 |
-
return jsonify({
|
| 49 |
-
"status": "error",
|
| 50 |
-
"message": "Missing required parameter: db_identifier (optional values: pubchemlite/invitrodb_v4_3)"
|
| 51 |
-
}), 400
|
| 52 |
-
|
| 53 |
-
if db_identifier not in ALLOWED_DB_IDENTIFIERS:
|
| 54 |
-
return jsonify({
|
| 55 |
-
"status": "error",
|
| 56 |
-
"message": f"Invalid db_identifier: {db_identifier}, only supports: {','.join(ALLOWED_DB_IDENTIFIERS)}"
|
| 57 |
-
}), 400
|
| 58 |
-
|
| 59 |
-
# Get SQL and validate
|
| 60 |
-
sql = data.get("sql")
|
| 61 |
-
if not sql:
|
| 62 |
-
return jsonify({
|
| 63 |
-
"status": "error",
|
| 64 |
-
"message": "Missing required parameter: sql (SELECT query statements only)"
|
| 65 |
-
}), 400
|
| 66 |
-
|
| 67 |
-
# Security check: only allow SELECT operations
|
| 68 |
-
sql_lower = sql.strip().lower()
|
| 69 |
-
if not sql_lower.startswith(tuple(ALLOWED_SQL_OPERATIONS)):
|
| 70 |
-
return jsonify({
|
| 71 |
-
"status": "error",
|
| 72 |
-
"message": "Only SELECT query operations are supported, dangerous operations like INSERT/UPDATE/DELETE/DROP are prohibited"
|
| 73 |
-
}), 400
|
| 74 |
-
|
| 75 |
-
# 2. Create connection engine for the corresponding database
|
| 76 |
-
db_uri = DB_CONFIGS[db_identifier]["uri"]
|
| 77 |
-
engine = sqlalchemy.create_engine(
|
| 78 |
-
db_uri,
|
| 79 |
-
pool_pre_ping=True, # Check connection validity
|
| 80 |
-
pool_recycle=3600 # Recycle connections every 1 hour to prevent timeouts
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
# 3. Execute SQL query
|
| 84 |
-
with engine.connect() as conn:
|
| 85 |
-
df = pd.read_sql(sql, conn)
|
| 86 |
-
|
| 87 |
-
# 4. Return successful result
|
| 88 |
-
return jsonify({
|
| 89 |
-
"status": "success",
|
| 90 |
-
"data": df.to_dict(orient="records"),
|
| 91 |
-
"message": ""
|
| 92 |
-
})
|
| 93 |
-
|
| 94 |
-
# Database execution error (table/field does not exist, etc.)
|
| 95 |
-
except SQLAlchemyError as e:
|
| 96 |
-
error_msg = str(e)
|
| 97 |
-
# Refine error messages to fit the two database scenarios
|
| 98 |
-
if "pubchemlite_ccs" in error_msg and db_identifier == "invitrodb_v4_3":
|
| 99 |
-
error_msg = f"pubchemlite_ccs table does not exist in invitrodb_v4_3 database (this table is only supported in pubchemlite): {error_msg}"
|
| 100 |
-
elif "CompoundName" in error_msg and db_identifier == "invitrodb_v4_3":
|
| 101 |
-
error_msg = f"CompoundName field does not exist in invitrodb_v4_3 database (this field is only supported in pubchemlite_ccs table of pubchemlite): {error_msg}"
|
| 102 |
-
elif "assay" in error_msg and db_identifier == "pubchemlite":
|
| 103 |
-
error_msg = f"assay table does not exist in pubchemlite database (this table is only supported in invitrodb_v4_3): {error_msg}"
|
| 104 |
-
return jsonify({
|
| 105 |
-
"status": "error",
|
| 106 |
-
"message": f"Database execution error: {error_msg}"
|
| 107 |
-
}), 500
|
| 108 |
-
|
| 109 |
-
# Client issues such as parameter/format errors
|
| 110 |
-
except Exception as e:
|
| 111 |
-
return jsonify({
|
| 112 |
-
"status": "error",
|
| 113 |
-
"message": f"Request processing failed: {str(e)}"
|
| 114 |
-
}), 400
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
# Root directory route (provides interface usage instructions)
|
| 118 |
-
@app.route('/')
|
| 119 |
-
def home():
|
| 120 |
-
return """
|
| 121 |
-
<h1>SQL Execution API (Supports Dual Databases)</h1>
|
| 122 |
-
<p>Please send POST requests to <code>/execute_sql</code> to execute SQL queries.</p>
|
| 123 |
-
<p>Request parameters:</p>
|
| 124 |
-
<ul>
|
| 125 |
-
<li>db_identifier: Target database (required, optional values: pubchemlite/invitrodb_v4_3)</li>
|
| 126 |
-
<li>sql: SELECT query statement (required, SELECT operations only)</li>
|
| 127 |
-
</ul>
|
| 128 |
-
<p>Example 1 (query pubchemlite):</p>
|
| 129 |
-
<pre>
|
| 130 |
-
curl -X POST http://127.0.0.1:5000/execute_sql \\
|
| 131 |
-
-H "Content-Type: application/json" \\
|
| 132 |
-
-d '{
|
| 133 |
-
"db_identifier": "pubchemlite",
|
| 134 |
-
"sql": "SELECT Identifier, CompoundName, MolecularFormula FROM pubchemlite_ccs WHERE PubMed_Count > 5 LIMIT 10"
|
| 135 |
-
}'
|
| 136 |
-
</pre>
|
| 137 |
-
<p>Example 2 (query invitrodb_v4_3):</p>
|
| 138 |
-
<pre>
|
| 139 |
-
curl -X POST http://127.0.0.1:5000/execute_sql \\
|
| 140 |
-
-H "Content-Type: application/json" \\
|
| 141 |
-
-d '{
|
| 142 |
-
"db_identifier": "invitrodb_v4_3",
|
| 143 |
-
"sql": "SELECT aid, assay_name, organism FROM assay WHERE ncbi_taxon_id = 9606 LIMIT 10"
|
| 144 |
-
}'
|
| 145 |
-
</pre>
|
| 146 |
-
"""
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
# Start the application (listen on all network interfaces, port 5000, debug mode can be turned off)
|
| 150 |
-
if __name__ == '__main__':
|
| 151 |
-
app.run(host='0.0.0.0', port=5000, debug=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|