-
Notifications
You must be signed in to change notification settings - Fork 14.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New endpoint that receives SQL and returns data as JSON #842
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1214,6 +1214,7 @@ def select_star(self, database_id, table_name): | |
@log_this | ||
def runsql(self): | ||
"""Runs arbitrary sql and returns and html table""" | ||
# TODO deprecate in fabor on `sql_json` | ||
session = db.session() | ||
limit = 1000 | ||
data = json.loads(request.form.get('data')) | ||
|
@@ -1253,6 +1254,59 @@ def runsql(self): | |
session.commit() | ||
return content | ||
|
||
@has_access | ||
@expose("/sql_json/", methods=['POST', 'GET']) | ||
@log_this | ||
def sql_json(self): | ||
"""Runs arbitrary sql and returns and json""" | ||
session = db.session() | ||
limit = 1000 | ||
sql = request.form.get('sql') | ||
database_id = request.form.get('database_id') | ||
mydb = session.query(models.Database).filter_by(id=database_id).first() | ||
|
||
if ( | ||
not self.can_access( | ||
'all_datasource_access', 'all_datasource_access')): | ||
raise utils.CaravelSecurityException(_( | ||
"This view requires the `all_datasource_access` permission")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add sql text to the error message or maybe rephrase: |
||
|
||
error_msg = "" | ||
if not mydb: | ||
error_msg = "The database selected doesn't seem to exist" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do u think about adding database_id to the error message ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather not expose ids to users as it may be confusing. This error really shouldn't happen though. Maybe if someone deletes a DB while someone else is running queries. |
||
else: | ||
eng = mydb.get_sqla_engine() | ||
if limit: | ||
sql = sql.strip().strip(';') | ||
qry = ( | ||
select('*') | ||
.select_from(TextAsFrom(text(sql), ['*']).alias('inner_qry')) | ||
.limit(limit) | ||
) | ||
sql = str(qry.compile(eng, compile_kwargs={"literal_binds": True})) | ||
try: | ||
df = pd.read_sql_query(sql=sql, con=eng) | ||
df = df.fillna(0) # TODO make sure NULL | ||
except Exception as e: | ||
logging.exception(e) | ||
error_msg = utils.error_msg_from_exception(e) | ||
|
||
session.commit() | ||
if error_msg: | ||
return Response( | ||
json.dumps({ | ||
'error': error_msg, | ||
}), | ||
status=500, | ||
mimetype="application/json") | ||
else: | ||
data = { | ||
'columns': [c for c in df.columns], | ||
'data': df.to_dict(orient='records'), | ||
} | ||
return json.dumps(data, default=utils.json_int_dttm_ser, allow_nan=False) | ||
|
||
|
||
@has_access | ||
@expose("/refresh_datasources/") | ||
def refresh_datasources(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/fabor/favor