Skip to content
The latest version of DbVisualizer was released 2024-04-15DOWNLOAD HERE ->

XML element - InitCommands

The InitCommands element define initialization commands that are executed just before the rest of the database profile is loaded. These commands are typically used to determine characteristics of the target database and database session. The result of these commands are stored in variables that can be used in conditions that are evaluated when the rest of the profile is loaded. A common use case is to find out the authorization level of the current user as defined in the database. If the user have limited privileges then some object types, views and actions should be disabled.

Multiple commands may be defined in the InitCommands element and these are executed in order.

The main purpose with the following sample and the commands is to first determine the database version by querying a system table. Based on the database version a condition controls which of two queries will be executed to find out another property from the database. The result of the executed query is stored in a the METACAT variable.

<InitCommands extends="true">
   <Command id="neoview.getDbVersion" method="runBeforeConditionsEval">
      <SQL>
         <![CDATA[
SELECT SUBSTRING(SYSTEM_VERSION FROM 10)
FROM (GET VERSION OF SYSTEM) V(SYSTEM_VERSION)
         ]]>
      </SQL>
      <Output id="DBVERSION" index="1"/>
   </Command>

   <Command id="neoview.getMaster">
      <If test="#DBVERSION gte 2400">
         <SQL>
            <![CDATA[
SELECT MIN(SYSTEM_CATALOGS) AS MASTER_CAT
FROM (GET SYSTEM CATALOGS) V(SYSTEM_CATALOGS)
WHERE SYSTEM_CATALOGS LIKE _ISO88591'NONSTOP_SQLMX_%'
           ]]>
         </SQL>
      </If>
      <Else>
         <SQL>
            <![CDATA[
SELECT 'NONSTOP_SQLMX_${#dp.METACAT}'
FROM (VALUES(1)) AS T1
           ]]>
         </SQL>
      </Else>
      <Output id="METACAT" index="1"/>
   </Command>
</InitCommands>

The extends="true" attribute specifies that the list of commands will extend the list of commands defined in the profile being extended.

Initialization commands are processed in two stages:

  1. First stage is to execute all commands having the attribute method="runBeforeConditionsEval" set. As the attribute reveal, these commands are execute before any conditions are evaluated,
  2. The second and last stage will execute all commands with no method="runBeforeConditionsEval" set. This time any conditions are evaluated.

The reason for these stages is that the processing of initialization commands may also rely on conditions.

Here is an example how the new METACAT variable is used in the rest of the database profile:

<Command id="neoview.getCatalogs">
   <SQL>
      <![CDATA[
SELECT
    TRIM(CAT_NAME) AS CATALOG_NAME
FROM
${METACAT}.SYSTEM_SCHEMA.CATSYS C
WHERE
    CAT_NAME     NOT LIKE _ISO88591'NONSTOP_SQLMX_%'
    AND CAT_NAME NOT IN (_ISO88591'NSMWEB', _ISO88591'NVSCRIPT', _ISO88591'METRIC', 
                         _ISO88591'MATRIX', _ISO88591'GENUSCAT', _ISO88591'MANAGEABILITY')
ORDER BY
    CATALOG_NAME
FOR READ UNCOMMITTED ACCESS
      ]]>
   </SQL>
</Command>

Here is another example for Oracle getting the instance_type property from the v$parameter table and put the value in the INSTANCE_TYPE variable.

<InitCommands extends="true">
   <Command id="oracle.initGetInstanceType" method="runBeforeConditionsEval">
      <SQL>
         <![CDATA[
select value from v$parameter where name = 'instance_type';
         ]]>
      </SQL>
      <Output id="INSTANCE_TYPE" index="1"/>
   </Command>
</InitCommands>

Below show that only if INSTANCE_TYPE have the value RDBMS schema objects should be displayed in the database objects tree:

<ObjectsTreeDef extends="false">
   <If test="#INSTANCE_TYPE eq 'RDBMS'">
      <GroupNode type="Schemas" label="Schemas">
         ...
      </GroupNode>
   </If>

   <GroupNode type="Properties" label="Session Properties" isLeaf="true"/>
   <GroupNode type="DBA" label="DBA Views">
      ...
   </GroupNode>
</ObjectsTreeDef>

One more example showing the use of <OnError> and <Message> element <InitCommands>. Based on the database major version it runs one of two SQLs. If any 924 error (table not found) is raised the message will be logged in the DbVisualizer log. Thetype="info" defines that the log entry should be logged without further notification. Leaving the type out or setting it to type="warning" will also raise the error balloon in the DbVisualizer tool. Use this only when really necessary.

<InitCommands extends="true">
   <Command id="oracle.initInstanceNumber">
      <If test="#util.isDatabaseVersionLT(9)">
         <SQL><![CDATA[select -1 from dual;]]></SQL>
         <Output name="OWN_INSTANCE_NUMBER" index="1"/>
      </If>
      <Else>
         <SQL><![CDATA[select instance_number from v$instance;]]></SQL>
         <Output name="OWN_INSTANCE_NUMBER" index="1"/>
      </Else>
      <OnError>
         <If test="#result.getErrorCode() eq 942" context="runtime">
            <Message type="info">
               <![CDATA[
Could not to retrieve own INST_ID (probably due to missing privileges) which is needed to browse DBA->Sessions, etc. DbVisualizer will work fine but related features will be disabled
               ]]>
            </Message>
         </If>
      </OnError>
   </Command>
</InitCommands>

Click these links for more information about the command element and conditions.