Dashboards & Visualizations

Tokens in dbconnect 2 sql query??

mrgibbon
Contributor

Hi all, I'm wondering if it possible to tokenise parameters within an sql query when using dbconnect 2.

eg - "SELECT * FROM "Permanent"."db"."Status" WHERE LoadDate=$datetoken$"
OR
"SELECT * FROM "Permanent"."db"."Status" WHERE $datetoken$"

Thanks in advance.

0 Karma
1 Solution

dart
Splunk Employee
Splunk Employee

You can use this with dbxquery, and you probably want to use the $token_name|u$ form to urlescape your query:

<form>
  <label>Query_Builder</label>
  <fieldset submitButton="true">
    <input type="dropdown" token="connection">
      <label>Connection</label>
      <search>
        <query>| rest /servicesNS/-/-/db_connect/connections | table title</query>
      </search>
      <fieldForLabel>title</fieldForLabel>
      <fieldForValue>title</fieldForValue>
    </input>
    <input type="radio" token="options">
    <label> Options </label>
    <choice value="shortnames=true">Short field names </choice>
        <choice value="shortnames=false"> Long field names </choice>

    </input>
    <input type="text" token="db_query">
      <label>DB Query</label>
    </input>
    <input type="text" token="splunk_query">
      <label>Splunk PostProcess Query</label>
      <default></default>
    </input>
  </fieldset>
  <row>
    <panel>
      <event>
        <title>| dbxquery connection=$connection$ query=$db_query|u$ $options$| $splunk_query$ </title>
        <search>
          <query>| dbxquery connection=$connection$ query=$db_query|u$ $options$ | $splunk_query$</query>
          <earliest>0</earliest>
        </search>
        <option name="count">10</option>
        <option name="list.drilldown">full</option>
        <option name="list.wrap">1</option>
        <option name="maxLines">5</option>
        <option name="raw.drilldown">full</option>
        <option name="rowNumbers">0</option>
        <option name="table.drilldown">all</option>
        <option name="table.wrap">1</option>
        <option name="type">list</option>
        <fields>[]</fields>
      </event>
    </panel>
  </row>
</form>

View solution in original post

0 Karma

mrgibbon
Contributor

Fixed, thanks @Dart

0 Karma

mrgibbon
Contributor

Thanks @dart
This is the code I'm working with as part of a wider project. This is the simplified version, part of a drilldown:
LoadDate_tok is one of the tokens passed through from the previous dashboard.
I haven't been able to get the urlescape to work.

<search>
    <query>
     | stats count 
     | eval LoadDate = strftime($LoadDate_tok$, "%Y-%m-%d")         
     | eval query="SELECT * FROM database.table WHERE LoadDate=$LoadDate$"
  | eval query=replace(query, " ", "%20")
  | eval query=replace(query, "=", "%3D")
    </query>
        <preview>
          <condition match="'job.resultCount' > 0 ">
            <set token="DBTable">$result.DBTable$</set>
            <set token="LoadDate">$result.LoadDate$</set>
            <set token="query">$result.query$</set>
          </condition>
        </preview>
  </search>
   <row>
    <panel>
      <table>
          <search>
          <query> | dbxquery connection=test query=$query|u$</query>
          </search>
      </table>
    </panel>
  </row>
0 Karma

dart
Splunk Employee
Splunk Employee

You can use this with dbxquery, and you probably want to use the $token_name|u$ form to urlescape your query:

<form>
  <label>Query_Builder</label>
  <fieldset submitButton="true">
    <input type="dropdown" token="connection">
      <label>Connection</label>
      <search>
        <query>| rest /servicesNS/-/-/db_connect/connections | table title</query>
      </search>
      <fieldForLabel>title</fieldForLabel>
      <fieldForValue>title</fieldForValue>
    </input>
    <input type="radio" token="options">
    <label> Options </label>
    <choice value="shortnames=true">Short field names </choice>
        <choice value="shortnames=false"> Long field names </choice>

    </input>
    <input type="text" token="db_query">
      <label>DB Query</label>
    </input>
    <input type="text" token="splunk_query">
      <label>Splunk PostProcess Query</label>
      <default></default>
    </input>
  </fieldset>
  <row>
    <panel>
      <event>
        <title>| dbxquery connection=$connection$ query=$db_query|u$ $options$| $splunk_query$ </title>
        <search>
          <query>| dbxquery connection=$connection$ query=$db_query|u$ $options$ | $splunk_query$</query>
          <earliest>0</earliest>
        </search>
        <option name="count">10</option>
        <option name="list.drilldown">full</option>
        <option name="list.wrap">1</option>
        <option name="maxLines">5</option>
        <option name="raw.drilldown">full</option>
        <option name="rowNumbers">0</option>
        <option name="table.drilldown">all</option>
        <option name="table.wrap">1</option>
        <option name="type">list</option>
        <fields>[]</fields>
      </event>
    </panel>
  </row>
</form>
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...

Skip the Awkward Silence: Have a .conf-ersation at .conf26

Picture this. You arrive at .conf26 already having your socializing and networking plans mapped out. No ...

Rethinking Zero Trust: From Product Purchases to Logical Control Evidence

Note: This post outlines a proposed architecture and serves as an interest check. If we secure commitments ...