pentaho cde include/exclude where 子句在 sql 查询中

pentaho cde include/exclude where clause in sql query

我正在使用 Pentaho CDE 构建一个仪表板,其中 table 将显示来自如下查询的数据:

select * from types where id = ${id}

现在我从 url 参数中获取 id

id = Dashboards.getQueryParameter('id');

如果我不在 url 中提供 id,table 不会显示任何内容,因为 id='' 不匹配任何内容。但我想做的是,如果我不提供 id,它将排除 where 子句并显示查询结果,如

select * from types

如何在 pentaho CDE 中实现这个?

如何创建一个自定义 javascript 参数(例如,'where_clause'),有点像这样:

id = Dashboards.getQueryParameter('id');
if (id != null)
    where_clause = 'id = ' + id;
else
    where_clause = '1=1';
 return where_clause;

然后构建您的查询:

select * from types where ${where_clause}

您可以使用两个数据源并在 table 的 Pre Execution 阶段选择一个。仪表板组件(table、图表)的 数据源 由 属性 dataAccessId.

表示

您可以在组件内使用 JavaScript 代码设置 dataAccessId,如下所示:this.chartDefinition.dataAccessId = {datasource name}

1) 数据源 sql_with_id:

select * from types where id = ${id}

2) 数据源 = sql_no_id:

select * from types

Table的Pre Execution代码:

function f() {
   var id = Dashboards.getQueryParameter('id');
   if (id && id !== "") {
      this.chartDefinition.dataAccessId = "sql_with_id";
   } else {
      this.chartDefinition.dataAccessId = "sql_no_id";
   }
}