Skip to content

InitCommands

The InitCommands element defines commands that run just before the rest of the database profile is loaded. Use them to find out properties of the database and the session, such as the database version or the privileges of the current user. The results are stored in variables that you can use in conditions in the rest of the profile. For example, if the user has limited privileges, you can turn off some object types, views and actions.

The commands run in the order they are defined. Only the first row of each result set is used. Use an Output element to name the variable for a column.

The following example first gets the database version. Based on the version, a condition then selects one of two queries. The result of that query is stored in 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 name="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 name="METACAT" index="1"/>
   </Command>
</InitCommands>

With extends="true", the commands are added to the commands in the profile being extended.

The initialization commands run in two stages:

  1. All commands with method="runBeforeConditionsEval" run. Conditions are not evaluated yet.
  2. All other commands run. Conditions are evaluated first, so these commands can depend on the results of the first stage.

This example uses the METACAT variable in another command in the 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>

This example for Oracle gets the instance_type parameter from v$parameter and stores it 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 name="INSTANCE_TYPE" index="1"/>
   </Command>
</InitCommands>

The objects tree then shows the schemas only if INSTANCE_TYPE is RDBMS:

<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>

The last example uses the OnError and Message elements in InitCommands. Based on the major version of the database, it runs one of two queries. If the query fails with error 942 (table or view does not exist), the message is written to the DbVisualizer log. Messages for initialization commands are only logged, and the type attribute sets the log level: type="info" logs the message as information. Without type, or with type="warning", the message is logged as a warning.

<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>

For more information, see the Command element and conditions.