sunyongmofang closed pull request #4614: Add sql editor
URL: https://github.com/apache/incubator-superset/pull/4614
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/superset/assets/javascripts/explore/stores/controls.jsx
b/superset/assets/javascripts/explore/stores/controls.jsx
index ec4522be88..13e0d07ecb 100644
--- a/superset/assets/javascripts/explore/stores/controls.jsx
+++ b/superset/assets/javascripts/explore/stores/controls.jsx
@@ -1197,6 +1197,16 @@ export const controls = {
description: t('What should be shown on the label?'),
},
+ sql: {
+ type: 'TextAreaControl',
+ label: t('Code'),
+ description: t('Put your sql here. This feature is for developers who
understand superset. If you are not sure, please do not fill in anything, it
will be ignored.'),
+ mapStateToProps: state => ({
+ language: state.controls && state.controls.markup_type ?
state.controls.markup_type.value : 'sql',
+ }),
+ default: '',
+ },
+
code: {
type: 'TextAreaControl',
label: t('Code'),
diff --git a/superset/assets/javascripts/explore/stores/visTypes.js
b/superset/assets/javascripts/explore/stores/visTypes.js
index 836da7b9cd..d2a2efe58b 100644
--- a/superset/assets/javascripts/explore/stores/visTypes.js
+++ b/superset/assets/javascripts/explore/stores/visTypes.js
@@ -1078,6 +1078,12 @@ export const visTypes = {
['metric'],
],
},
+ {
+ label: t('Code'),
+ controlSetRows: [
+ ['sql'],
+ ],
+ },
{
label: t('Chart Options'),
expanded: true,
diff --git a/superset/connectors/sqla/models.py
b/superset/connectors/sqla/models.py
index ef8c68ebb3..fa2e620b48 100644
--- a/superset/connectors/sqla/models.py
+++ b/superset/connectors/sqla/models.py
@@ -407,13 +407,28 @@ def get_template_processor(self, **kwargs):
def get_query_str(self, query_obj):
engine = self.database.get_sqla_engine()
- qry = self.get_sqla_query(**query_obj)
- sql = six.text_type(
- qry.compile(
- engine,
- compile_kwargs={'literal_binds': True},
- ),
- )
+ if 'form_data' in query_obj and \
+ 'sql' in query_obj['form_data'] and \
+ query_obj['form_data']['sql']:
+ dttm_to_str = [x for x in self.columns if
x.is_dttm][0].dttm_sql_literal
+ query_obj['from_dttm'] = dttm_to_str(query_obj['from_dttm'])
+ query_obj['to_dttm'] = dttm_to_str(query_obj['to_dttm'])
+ sql = query_obj['form_data']['sql']
+ for k in query_obj:
+ v = query_obj[k]
+ if isinstance(v, basestring):
+ sql = sql.replace(''.join(['{', k, '}']), v)
+ for v in query_obj['form_data']['filters']:
+ tmp_key, tmp_value = v['col'], v['val'][0]
+ sql = sql.replace(''.join(['{', tmp_key, '}']), tmp_value)
+ else:
+ qry = self.get_sqla_query(**query_obj)
+ sql = six.text_type(
+ qry.compile(
+ engine,
+ compile_kwargs={'literal_binds': True},
+ ),
+ )
logging.info(sql)
sql = sqlparse.format(sql, reindent=True)
if query_obj['is_prequery']:
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services