SQL Data Source PivotData Microservice Documentation


Cube schema is specified in the appsettings.json file.

Cube config for SQL database should have "SourceDb" entry:

{
  "Id": "orders",
  "Name": "Orders from MySQL sample DB",
  "SourceType": "SqlDb",
  "SourceDb": {
    "Connector": "mysql",
    "ConnectionString": "Server=db4free.net;Database=nreco_sampledb;Uid=nreco;Pwd=HRt5UbVD;",
    "SelectSql": "SELECT o.* FROM orders o LEFT JOIN customers c ON (c.customerNumber=o.customerNumber)"
  },
  "InferSchema": true,  /* dimensions and measures are determined automatically by SELECT query resultset */
  "Dimensions": [],
  "Measures": [],
  "Parameters": []
}

"SelectSql" determines tabular data that can be used in a pivot table report. If you don't need to perform any special calculations in SQL it is recommended to use SELECT * FROM ... or SELECT mainTablePrefix.* FROM ... instead of explicit list of columns for performance reasons. Also it is much more effective to resolve lookups after grouping stage; "Star-schema support" section below explains how to configure conditional post-group JOINs.

List of supported connectors:

Connector Description
mssql MS SQL Server, Azure SQL. ConnectionString sample:
Data Source=hostName;Database=db;User=user;Password=password;
Complete list of connection string options: System.Data.SqlClient documentation.
mysql MySql, MariaDB, MemSQL. ConnectionString sample:
Server=hostName;Database=db;Uid=user;Pwd=password;ConvertZeroDateTime=True;
Complete list of connection string options: MySqlConnector Driver documentation.
postgresql PostgreSql, Amazon Redshift. ConnectionString sample:
Host=hostName;Port=port;Database=db;User ID=user;Password=password;
For Redshift in some cases you'll need to add: Server Compatibility Mode=Redshift;
Complete list of connection string options: NpgSql .NET Driver documentation.
clickhouse Yandex ClickHouse. ConnectionString sample:
Compress=True;Compressor=lz4;SocketTimeout=15000;Host=hostName;Port=portNumber;Database=default;User=default;Password=
sqlite Local SQLite database file. ConnectionString sample:
Data Source=@CurrentDirectory/App_Data/northwind.db;
(you can use @CurrentDirectory token to specify working directory of the microservice.)
oracle Oracle DB. ConnectionString sample:
Data Source=TORCL;User Id=user;Password=password;
NOTE: currently this connector is supported only in .NET Framework 4.6.2 microservice build.
odbc Database that has ODBC Driver. ConnectionString sample:
Driver={any odbc driver's name};OdbcKey=someValue;
(Check concrete ODBC driver's documentation on available connection string options)
NOTE: this connector is not supported in .NET Core 1.0 microservice build.

NOTE: "InferSchema" is useful for one-time or for development purposes; for production use it is better to specify list of dimensions and measures explicitely to avoid some overhead and get full control over cube member options.

Dimensions

In case of SQL data source dimension name should refer to the column of the specified SQL SELECT:

  "Dimensions": [
     {
       "Name": "status",  /* or 'o.status' */
       "LabelText": "Status"
     }
  ]

It is possible to specify SQL expression to calculate dimension values on database level:

     {
       "Name": "orderDate_year",
       "LabelText": "Order Year",   
       "Params": [ "YEAR(orderDate)" ]
     }

Measures

Measure parameter (if used) should also refer to the column returned by "SelectSql" query:

  "Measures": [
     {
       "Type": "Count",  /* other types: "Sum", "Average", "Min", "Max" */
     },
     {
       "Type": "Sum",
       "Params": [ "orderNumber" ]  /* refers to column from SQL query */
     }  
  ]

Like dimensions each measure has unique name; if property "Name" is not specified measure name is generated automatically by its type and list of parameters. For the sample config from above suggested names will be "Count and "SumOforderNumber".

Parameters

In SQL data sources report parameters could be used in SELECT query as ADO.NET command parameters, for example:

  "Parameters": [
    {
      "Name": "country",
      "DataType": "string",
      "Multivalue": true
    }
  ]
    "SelectSql": "SELECT o.* FROM orders o LEFT JOIN customers c ON (c.customerNumber=o.customerNumber) WHERE 1=1 @country[ AND c.country IN ({0}) ]"

Token @country[ AND c.country IN ({0}) ] refers to the parameter with name "country". It works in the following way: if report parameter is empty, nothing is added to WHERE; otherwise "AND c.country IN ('Austria')" condition is added.

NOTE: Multivalue parameters should be used only with "IN" operator.

Star-schema support

If database has star schema with main facts table and dimension tables it is possible to resolve lookup values without JOINs in the main query ("SelectSql"). Instead of that JOINs could be applied after data grouping only if they are needed for the concrete pivot table report; this is possible with "JoinsAfterGroup" config entry.

Lets assume that 'facts' table has 'company_id' column that refers to 'companies' dimension table with 'id' and 'title' columns. The following config illustrates how to configure conditional JOIN to resolve 'Company Title' dimension:

  "SourceDb": {
    "Connector": "...",
    "ConnectionString": "...",
    "SelectSql": "SELECT * FROM facts",
    "JoinsAfterGroup": [
      {
        "JoinSql": "LEFT JOIN companies c ON (c.id=t.company_id)",  /* "SelectSql" has an alias 't' */
        "ApplyOnFields": [ "c.title" ]  /* join is applied only for specfied dimension names */
      }    
    ]
  },
  "Dimensions": [
    {
      "Name": "c.title",  /* refers to join specified in "JoinsAfterGroup" */
      "LabelText": "Company Title",
      "Params": ["company_id"]  /* FK column in facts table for group-by */
    }
  ],

You can use report parameter tokens (@ParamName) in "JoinSql" template in the same manner as in "SelectSql".

Notice for ClickHouse connector: CH has its own JOIN syntax + it doesn't support table aliases for column names; this means that joined table should have unique column names (it is possible to join sub-query to get unique columns).