<?xml version="1.0" encoding="UTF-8" ?>
<!--
  ~ Copyright (c) DbVis Software AB. All Rights Reserved.
  -->

<!DOCTYPE DatabaseProfile SYSTEM "dbvis-defs.dtd">

<DatabaseProfile
        desc="Profile for Amazon Redshift"
        minver="9.5"
        extends="generic">

   <!-- =================================================================== -->
   <!-- Definition of the commands -->
   <!-- =================================================================== -->

   <Commands extends="true">

      <Command id="redshift.getCatalogs">
         <SQL>
            <![CDATA[
select * from pg_catalog.pg_database order by 1
            ]]>
         </SQL>
         <ProcessDataSet action="movecolumn" index="datname" value="first"/>
      </Command>

      <Command id="redshift.getConnectableDatabases">
         <SQL>
            <![CDATA[
SELECT
    *
FROM
    pg_catalog.pg_database
WHERE
    datallowconn IS true
AND datistemplate IS false
ORDER BY 1
             ]]>            
         </SQL>
         <ProcessDataSet action="movecolumn" index="datname" value="first"/>
      </Command>
      
      <Command id="redshift.getSchemas">
         <SQL>
            <![CDATA[
SELECT n.nspname                                         AS "Name",
       r.usename                                         AS "Owner"
FROM pg_catalog.pg_namespace n
   LEFT JOIN pg_catalog.pg_user r ON n.nspowner= r.usesysid
WHERE n.nspname NOT IN (SELECT schemaname FROM pg_catalog.svv_external_schemas)
ORDER BY 1
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getSchemaNames">
         <SQL>
            <![CDATA[
SELECT n.nspname                                         AS "Name",
       r.usename                                         AS "Owner"
FROM pg_catalog.pg_namespace n
   LEFT JOIN pg_catalog.pg_user r ON n.nspowner= r.usesysid
WHERE n.nspname NOT IN (SELECT schemaname FROM pg_catalog.svv_external_schemas)
ORDER BY 1
            ]]>
         </SQL>
         <!--Must have Filter element here since this command is fetched from code-->
         <Filter index="Name" label="Name"/>
      </Command>

      <Command id="redshift.getTables">
         <SQL>
            <![CDATA[
SELECT c.relname                                     AS "Name",
       n.nspname                                     AS "Schema",
       pg_get_userbyid(c.relowner)                   AS "Owner",
       t.spcname                                     AS "Tablespace",
       c.relhasindex                                 AS "Has Indexes",
       c.relhasrules                                 AS "Has Rules",
       (c.reltriggers > 0)                            AS "Has Triggers",
       c.relisshared                                 AS "Is Shared",
       c.relnatts                                    AS "N Cols",
       CASE c.reldiststyle
         WHEN 0 THEN 'EVEN'
         WHEN 1 THEN 'KEY'
         WHEN 8 THEN 'ALL'
       END                                           AS "Dist Style",
       pg_catalog.obj_description(c.oid, 'pg_class') AS "Comment"
FROM   pg_class c
   LEFT JOIN pg_namespace n   ON n.oid = c.relnamespace
   LEFT JOIN pg_tablespace t  ON t.oid = c.reltablespace
WHERE c.relkind = 'r'::"char"
AND   n.nspname LIKE '${schema}'
AND   c.relname LIKE '${table}'
ORDER BY n.nspname, c.relname
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getViews">
         <SQL>
            <![CDATA[
SELECT c.relname                                      AS "Name",
       n.nspname                                      AS "Schema",
       pg_get_userbyid(c.relowner)                    AS "Owner",
       t.spcname                                      AS "Tablespace",
       c.relhasindex                                  AS "Has Indexes",
       c.relhasrules                                  AS "Has Rules",
       (c.reltriggers > 0)                            AS "Has Triggers",
       c.relisshared                                  AS "Is Shared",
       c.relnatts                                     AS "N Cols",
       pg_catalog.obj_description(c.oid, 'pg_class')  AS "Comment"
FROM   pg_class c
   LEFT JOIN pg_namespace n   ON n.oid = c.relnamespace
   LEFT JOIN pg_tablespace t  ON t.oid = c.reltablespace
WHERE c.relkind = 'v'::"char"
AND   n.nspname LIKE '${schema}'
AND   c.relname LIKE '${table}'
ORDER BY n.nspname, c.relname
            ]]>
         </SQL>
      </Command>

<!-- 
Get the functions, in version 7287 and later we are using the view pg_proc_info instead of the table pg_proc
The pg_proc_info adds the following attributes (included in later version of Postgresql in the pg_proc table )
 prooid   oid             
 prokind  "char"         
 proargmodes "char"[ ]   
 proallargtypes oid[ ]
 -->
      <Command id="redshift.getFunctions">
         <SQL>
            <![CDATA[
SELECT p.proname AS "Function Name",
       n.nspname AS "Schema Name",
      CASE WHEN p.proretset THEN 'setof ' ELSE '' END ||
      pg_catalog.format_type(p.prorettype, NULL) AS "Result data type",
      pg_catalog.oidvectortypes(p.proargtypes) AS "Argument data types",
      r.usename AS "Owner",
      l.lanname AS "Language",
       ]]>
            <If test="#util.isDatabaseVersionGTE(10000,7287)">
              <![CDATA[
               pg_catalog.obj_description(p.prooid, 'pg_proc') AS "Comment"
              ]]>
            </If>
            <Else>
            <![CDATA[
      pg_catalog.obj_description(p.oid, 'pg_proc') AS "Comment"
              ]]>
            </Else>
      <![CDATA[
      
      FROM 
           ]]>
            <If test="#util.isDatabaseVersionGTE(10000,7287)">
              <![CDATA[
                pg_catalog.pg_proc_info p
              ]]>
            </If>
            <Else>
            <![CDATA[
      pg_catalog.pg_proc p
              ]]>
            </Else>
      <![CDATA[
   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
   LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang
   LEFT JOIN pg_catalog.pg_user r ON r.usesysid = p.proowner
WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype
   AND (p.proargtypes[0] IS NULL
      OR   p.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype)
   AND NOT p.proisagg
   AND n.nspname LIKE '${schema}'
     ]]>
       <If test="#util.isDatabaseVersionGTE(10000,7287)">
              <![CDATA[
               AND p.prokind = '${kind}'
              ]]>
            </If>
      <![CDATA[
ORDER BY 2, 1, 3, 4
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getFunctionSource">
         <SQL>
            <![CDATA[
SELECT p.prosrc as "Source code"
FROM pg_catalog.pg_proc p
   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE n.nspname = '${schema}'
AND p.proname = '${funcname}';
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getDomains">
         <SQL>
            <![CDATA[
SELECT t.typname                                          AS "Domain Name",
       n.nspname                                          AS "Schema Name",
       pg_catalog.format_type(t.typbasetype, t.typtypmod) AS "Type",
       CASE WHEN  t.typdefault IS NOT NULL
          THEN ''||t.typdefault
          ELSE ''
       END                                                AS "Default",
       CASE WHEN t.typnotnull
          THEN 'Yes'
          ELSE 'No'
       END                                                AS "Not null",
       pg_catalog.pg_get_constraintdef(r.oid, true)       AS "Check",
       pg_catalog.obj_description(t.oid, 'pg_type')       AS "Comment"
FROM pg_catalog.pg_type t
   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
   LEFT JOIN pg_catalog.pg_constraint r ON t.oid = r.contypid
WHERE t.typtype = 'd' 
AND n.nspname LIKE '${schema}'
ORDER BY 1, 2
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getAggregates">
         <SQL>
            <![CDATA[
SELECT p.proname AS "Aggregate Name", 
   n.nspname as "Schema Name",
   CASE p.proargtypes[0] 
      WHEN 'pg_catalog."any"'::pg_catalog.regtype
      THEN CAST('(all types)' AS pg_catalog.text)
      ELSE pg_catalog.format_type(p.proargtypes[0], NULL)
   END AS "Data Type",
   pg_catalog.obj_description(p.oid, 'pg_proc') AS "Comment"
FROM pg_catalog.pg_proc p
   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE p.proisagg 
   AND n.nspname LIKE '${schema}'
ORDER BY 1, 2, 3
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getOperators">
         <SQL>
            <![CDATA[
SELECT o.oprname AS "Operator Name",
   n.nspname as "Schema Name",
   CASE WHEN o.oprkind='l' THEN NULL ELSE pg_catalog.format_type(o.oprleft, NULL) END AS "Left arg type",
   CASE WHEN o.oprkind='r' THEN NULL ELSE pg_catalog.format_type(o.oprright, NULL) END AS "Right arg type",
   pg_catalog.format_type(o.oprresult, NULL) AS "Result type",
   coalesce(pg_catalog.obj_description(o.oid, 'pg_operator'),
            pg_catalog.obj_description(o.oprcode, 'pg_proc')) AS "Comment"
FROM pg_catalog.pg_operator o
   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace
WHERE n.nspname LIKE '${schema}'
ORDER BY n.nspname, o.oprname, o.oprleft, o.oprright
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getTypes">
         <SQL>
            <![CDATA[
SELECT  pg_catalog.format_type(t.oid, NULL) AS "Type Name",
   n.nspname as "Schema Name",
   pg_catalog.obj_description(t.oid, 'pg_type') as "Comment"
FROM pg_catalog.pg_type t
   LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) AND t.typname !~ '^_'
   AND t.typtype != 'd'
   AND n.nspname LIKE '${schema}'
ORDER BY 2, 1
            ]]>
         </SQL>
      </Command>


      <!-- ================================================================= -->
      <!-- DBA related commands requiring certain privileges in the database -->
      <!-- ================================================================= -->

      <Command id="redshift.getUsers">
         <SQL>
            <![CDATA[
select * from pg_user
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getGroups">
         <SQL>
            <![CDATA[
select * from pg_group
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getSettings">
         <SQL>
            <![CDATA[
select * from pg_settings
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getSessions">
         <SQL>
            <![CDATA[
select * from pg_stat_activity
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getLocks">
         <SQL>
            <![CDATA[
select * from pg_locks
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getPrivileges">
         <SQL>
            <![CDATA[
SELECT
    table_catalog as "TABLE_CAT",
    table_schema AS "TABLE_SCHEM",
    table_name AS "TABLE_NAME",
    grantor AS "GRANTOR",
    grantee AS "GRANTEE",
    privilege_type AS "PRIVILEGE",
    with_hierarchy AS "IS_GRANTABLE"
FROM
    information_schema.table_privileges
WHERE
    table_catalog = '${catalog}'
AND table_schema = '${schema}'
AND table_name = '${objectname}'
ORDER BY privilege_type
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getExternalSchemas">
         <SQL>
            <![CDATA[
SELECT *
FROM pg_catalog.svv_external_schemas
ORDER BY 3
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getExternalTables">
         <SQL>
            <![CDATA[
SELECT *
FROM pg_catalog.svv_external_tables
WHERE schemaname = '${schema}'
ORDER BY 2
            ]]>
         </SQL>
      </Command>

      <Command id="redshift.getExternalTableColumns">
         <SQL>
            <![CDATA[
SELECT *
FROM pg_catalog.svv_external_columns
WHERE schemaname = '${schema}'
AND tablename = '${table}'
ORDER BY 5
            ]]>
         </SQL>
      </Command>

   </Commands>

   <!-- =================================================================== -->
   <!-- Definition of the object actions that are used by the tree and views -->
   <!-- =================================================================== -->

   <ObjectsActionDef extends="false">

      <ActionGroup type="Databases">
         <Action id="redshift-database-create" label="Create Database" reload="true"
                 resetcatalogs="true" icon="add">
            <Input label="New Database Name" name="newDatabase" style="text"/>
            <Input label="Owner" name="owner" style="list">
               <Values>
                  <Command>
                     <SQL>
            <![CDATA[select '' union select usename from pg_catalog.pg_user]]>
                     </SQL>
                  </Command>
               </Values>
            </Input>
            <SetVar name="_owner" value='#owner.equals("") ? "" : "owner = ${QB}" + #owner + "${QE}"'/>
            <Command>
               <SQL><![CDATA[create database ${QB}${newDatabase}${QE} ${_owner}]]></SQL>
            </Command>
            <Confirm>
               Confirm create of database ${newDatabase}?
            </Confirm>
            <Result>
               Database ${newDatabase} created!
            </Result>
         </Action>
      </ActionGroup>

      <ActionGroup type="Catalog">
         <Action id="redshift-database-drop" label="Drop Database" reload="true"
                 resetcatalogs="true" icon="remove">
            <Input label="Database" style="text" editable="false">
               <Default>${catalog}</Default>
            </Input>
            <Command>
               <SQL>
                  <![CDATA[                  
drop database "${catalog}";
                  ]]>
               </SQL></Command>
            <Confirm>
               Really drop database ${catalog}?
            </Confirm>
            <Result>
               database ${catalog} has been dropped!
            </Result>
         </Action>
      </ActionGroup>
      
      <ActionGroup type="Schemas">
         <Action id="redshift-schemas-create-schema" label="Create Schema" reload="true" resetschemas="true" icon="add">
            <Input label="Schema" name="newschema" style="text" editable="true"/>
            <Input label="Authorization" name="authorization" style="list">
               <Values>
                  <Command>
                     <SQL><![CDATA[select '' union select usename from pg_user]]></SQL>
                  </Command>
               </Values>
            </Input>

            <SetVar name="_authorization" value='#authorization.equals("") ? "" : " authorization ${QB}" + #authorization + "${QE}"'/>

            <Command>
               <SQL><![CDATA[create schema ${QB}${newschema}${QE}${_authorization}]]></SQL>
            </Command>
            <Confirm>
               Create new schema ${newSchema}?
            </Confirm>
         </Action>
      </ActionGroup>

      <ActionGroup type="Schema">
         <Action id="redshift-schema-drop" label="Drop Schema" reload="true" resetschemas="true" icon="remove">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Cascade" name="cascade" style="check">
               <Values>cascade</Values>
            </Input>
            <Command>
               <SQL><![CDATA[drop schema "${schema}" ${cascade}]]></SQL>
            </Command>
            <Confirm>
               Really drop schema ${schema}?
            </Confirm>
         </Action>
         <Action id="redshift-schema-comment" label="Comment Schema" reload="true">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Comment" name="comment" style="wrapped-text-editor">
               <Default>
                  <Command>
                     <SQL><![CDATA[
SELECT pg_catalog.obj_description(n.oid, 'pg_namespace')
FROM pg_catalog.pg_namespace n
WHERE n.nspname = '${schema}'
                     ]]></SQL>
                  </Command>
               </Default>
            </Input>
            <Command>
               <SQL><![CDATA[comment on schema "${schema}" is '${comment}']]></SQL>
            </Command>
            <Confirm>
               Apply new comment on ${schema}?
            </Confirm>
         </Action>
         <Action id="redshift-schema-export" label="Export Schema"
                 icon="export" reload="false"
                 class="schemaExport"
                 doclink="http://www.dbvis.com/doclink/export-schema"/>
      </ActionGroup>

      <ActionGroup type="Tables">
         <Action id="redshift-tables-create-table" label="Create Table"
                 icon="add" reload="true"
                 class="tableCreateOrAlter"
                 classargs="create"
                 doclink="http://www.dbvis.com/doclink/create-table"/>
         <Action id="redshift-tables-import-table-data"
                 label="Import Table Data"
                 icon="import"
                 reload="true"
                 class="tableDataImport"
                 doclink="http://www.dbvis.com/doclink/import-table"/>
      </ActionGroup>

      <ActionGroup type="Table">
         <Action id="redshift-tables-alter-table" label="Alter Table"
                 icon="edit" reload="true"
                 class="tableCreateOrAlter"
                 classargs="alter"
                 doclink="http://www.dbvis.com/doclink/alter-table"/>

         <Action id="redshift-table-rename" label="Rename Table" reload="true" icon="rename">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Table" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="New Table Name" name="newTable" style="text"/>
            <Command>
               <SQL><![CDATA[alter table "${schema}"."${objectname}" rename to ${QB}${newTable}${QE}]]></SQL>
            </Command>
            <Confirm>
               Confirm rename of ${schema}.${objectname} to ${newTable}?
            </Confirm>
            <Result>
               Table ${schema}.${objectname} renamed to ${newTable}!
            </Result>
         </Action>

         <Action id="redshift-table-clear" label="Empty Table" reload="true">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Table" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="Use Command" name="useCommand" style="radio">
               <Values>TRUNCATE|DELETE</Values>
               <Default>TRUNCATE</Default>
            </Input>

            <SetVar name="_command" value="#useCommand.equals('TRUNCATE') ? 'truncate table' : 'delete from'"/>

            <Command>
               <SQL><![CDATA[${_command} "${schema}"."${objectname}"]]></SQL>
            </Command>
            <Confirm>
               Really remove all rows in table ${schema}.${objectname}?
            </Confirm>
            <Result>
               All rows in table ${schema}.${objectname} has been removed!
            </Result>
         </Action>

         <Action id="redshift-table-drop" label="Drop Table" reload="true" icon="remove">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Table" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>            
            <Input label="Drop Dependent Object(s)" name="cascade" style="check"
                   tip="Automatically drop objects that depend on the table (such as views)">
               <Values>cascade</Values>
            </Input>
            <Command><SQL><![CDATA[drop table "${schema}"."${objectname}" ${cascade}]]></SQL></Command>
            <Confirm>
               Really drop table ${schema}.${objectname}?
            </Confirm>
            <Result>
               Table ${schema}.${objectname} has been dropped!
            </Result>
         </Action>

         <Action id="redshift-table-comment" label="Comment Table" reload="true">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Table" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="Comment" name="comment" style="wrapped-text-editor">
               <Default>${redshift.getTables.Comment}</Default>
            </Input>
            <Command>
               <SQL><![CDATA[comment on table "${schema}"."${objectname}" is '${comment}']]></SQL>
            </Command>
            <Confirm>
               Apply new comment on ${schema}.${objectname}?
            </Confirm>
         </Action>

         <Action id="redshift-table-import-table-data" label="Import Table Data" icon="import"
                 class="tableDataImport"
                 doclink="http://www.dbvis.com/doclink/import-table"/>

         <Action id="redshift-table-export" label="Export Table" icon="export"
                 class="tableExport"
                 doclink="http://www.dbvis.com/doclink/export-table"/>

         <Action id="redshift-table-analyze" label="Analyze Table">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Table" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>            
            <Command><SQL><![CDATA[analyze "${schema}"."${objectname}"]]></SQL></Command>
            <Confirm>
               Really analyze table ${schema}.${objectname}?
            </Confirm>
            <Result>
               Table ${schema}.${objectname} has been analyzed!
            </Result>
         </Action>

         <Action id="redshift-table-vacuum" label="Vacuum Table">
            <If test="#util.singlePhysicalConnection">
               <Message>
<![CDATA[<html>
The VACUUM command cannot run within a transaction block, so the current transaction must be commited
before running the command. Since this connection uses a <b>Single Shared Physical Connection</b>,
this means that pending transactions in all DbVisualizer tabs will then be commited, if Auto Commit is disabled.
</html>]]>
               </Message>
            </If>
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Table" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>            
            <SetVar name="commit" value='#util.singlePhysicalConnection ? "end transaction;" : ""'/>
            <Command><SQL><![CDATA[
${commit}
vacuum "${schema}"."${objectname}"
]]></SQL>
            </Command>
            <Confirm>
               Really vacuum table ${schema}.${objectname}?
            </Confirm>
            <Result>
               Vacuum has been executed for ${schema}.${objectname}!
            </Result>
         </Action>

         <Separator/>

         <Action id="redshift-table-script" label="Script Table" icon="scriptObject"
                 class="sqlBuilder"
                 classargs="script-dialog,SELECT,SELECT *,INSERT,UPDATE,DELETE,CREATE,DROP"/>
      </ActionGroup>

      <ActionGroup type="Column">
         <Action id="redshift-column-comment" label="Comment Column" reload="true">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Table" style="text" editable="false">
               <Default>${theTableName}</Default>
            </Input>
            <Input label="Column" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="Comment" name="comment" style="wrapped-text-editor">
               <Default>${getColumns.REMARKS}</Default>
            </Input>
            <Command>
               <SQL><![CDATA[comment on column "${schema}"."${theTableName}"."${objectname}" is '${comment}']]></SQL>
            </Command>
            <Confirm>
               Apply new comment on ${schema}.${theTableName}.${objectname}?
            </Confirm>
         </Action>
      </ActionGroup>

      <ActionGroup type="View">
         <Action id="redshift-view-drop" label="Drop View" reload="true" icon="remove">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="View" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>            
            <Input label="Cascade" name="cascade" style="check">
               <Values>cascade</Values>
            </Input>
            <Command><SQL><![CDATA[drop view "${schema}"."${objectname}" ${cascade}]]></SQL></Command>
            <Confirm>
               Really drop view ${schema}.${objectname}?
            </Confirm>
            <Result>
               View ${schema}.${objectname} has been dropped!
            </Result>
         </Action>

         <Action id="redshift-view-rename" label="Rename View" reload="true" icon="rename">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="View" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="New View Name" name="newTable" style="text"/>
            <Command>
               <SQL><![CDATA[alter table "${schema}"."${objectname}" rename to ${QB}${newTable}${QE}]]></SQL>
            </Command>
            <Confirm>
               Confirm rename of ${schema}.${objectname} to ${newTable}?
            </Confirm>
            <Result>
               View ${schema}.${objectname} renamed to ${newTable}!
            </Result>
         </Action>

         <Action id="redshift-view-comment" label="Comment View" reload="true">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="View" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="Comment" name="comment" style="wrapped-text-editor">
               <Default>${redshift.getViews.Comment}</Default>
            </Input>
            <Command>
               <SQL><![CDATA[comment on view "${schema}"."${objectname}" is '${comment}']]></SQL>
            </Command>
            <Confirm>
               Apply new comment on ${schema}.${objectname}?
            </Confirm>
         </Action>

         <Action id="redshift-view-export" label="Export View" icon="export"
                 class="viewExport"
                 doclink="http://www.dbvis.com/doclink/export-view"/>

         <Separator/>

         <Action id="redshift-view-script" label="Script View" icon="scriptObject"
                 class="sqlBuilder"
                 classargs="script-dialog,SELECT,SELECT *,INSERT,UPDATE,DELETE,CREATE,DROP"/>
      </ActionGroup>

    <ActionGroup type="Procedures">
         <Action id="redshift-procedures-create" label="Create Procedure" reload="true" icon="add"
                 doclink="http://www.dbvis.com/doclink/create-procedure">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Procedure" name="procedureName" style="text"/>
            <Input label="Options" style="separator"/>
            <Input label="Parameters" style="separator"/>

            <Input name="parameters" style="grid">
               <Arg name="output" value="${direction} ${QB}${name}${QE} ${type}"/>
               <Arg name="newline" value=", "/>

               <Input name="direction" label="Direction" style="list">
                  <Values>IN|INOUT|OUT</Values>
                  <Default>IN</Default>
               </Input>
               <Input name="name" label="Name" style="text">
                  <Default>parm</Default>
               </Input>
               <Input name="type" label="Type" style="text">
                  <Default>INTEGER</Default>
               </Input>
            </Input>

            <SetVar name="_parameters" value='#parameters.trim().equals("") ? "()" : "(" + #parameters + ")"'/>
            <Input label="Source" style="separator"/>
            <Input name="body" style="text-editor">
               <Default>RAISE INFO 'My procedure executed';</Default>
            </Input>
            <Command exectype="asis">
               <SQL>
                  <![CDATA[
CREATE PROCEDURE "${schema}".${QB}${procedureName}${QE}
${_parameters}  
AS $$
BEGIN
   ${body}
 END;
$$ LANGUAGE plpgsql
]]>
               </SQL>
            </Command>
            <Result>
               <![CDATA[Procedure "${schema}"."${procedureName}" created!]]>
            </Result>
         </Action>
      </ActionGroup>

      <ActionGroup type="Procedure">
         <Action id="redshift-procedure-drop" label="Drop Procedure" reload="true" icon="remove">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Procedure" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
          
            <Command>
               <SQL>
                  <![CDATA[drop Procedure "${schema}"."${objectname}" (${redshift.getFunctions.Argument data types})]]></SQL>
            </Command>
            <Confirm>
               Really drop Procedure ${schema}.${objectname}?
            </Confirm>
            <Result>
               Procedure ${schema}.${objectname} dropped!
            </Result>
         </Action>

         <Action id="redshift-procedure-rename" label="Rename Procedure" reload="true" icon="rename">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Procedure" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="New Procedure Name" name="newProcedure" style="text"/>
            <Command>
               <SQL>
                  <![CDATA[alter procedure "${schema}"."${objectname}" (${redshift.getFunctions.Argument data types}) rename to ${QB}${newProcedure}${QE}]]></SQL>
            </Command>
            <Confirm>
               Confirm rename of procedure ${objectname} to ${newProcedure}?
            </Confirm>
            <Result>
               Procedure ${objectname} renamed to ${newProcedure}!
            </Result>
         </Action>

         <Action id="redshift-procedure-export" label="Export Procedure" icon="export"
                 class="procedureExport"
                 doclink="http://www.dbvis.com/doclink/export-code-object"/>

         <Separator/>

         <Action id="redshift-procedure-script" label="Script Procedure" icon="scriptObject"
                 class="sqlBuilder"
                 classargs="script-dialog,CALL,CREATE,DROP"/>
      </ActionGroup>
      
      <ActionGroup type="Functions">
         <Action id="redshift-functions-create" label="Create Function" reload="true" icon="add"
                 doclink="http://www.dbvis.com/doclink/create-function">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Function" name="functionName" style="text"/>

            <Input label="Options" style="separator"/>
            <Input label="Return Data Type" name="returns" style="text">
               <Default>INTEGER</Default>
            </Input>
            <Input label="Parameters" style="separator"/>

            <Input name="parameters" style="grid">
               <Arg name="output" value="${name} ${type}"/>
               <Arg name="newline" value=", "/>

               <Input name="name" label="Name" style="text">
                  <Default>parm</Default>
               </Input>
               <Input name="type" label="Type" style="text">
                  <Default>INTEGER</Default>
               </Input>
            </Input>
            <Input label="Volatility" name="volatility" style="radio">
               <Values>VOLATILE|STABLE|IMMUTABLE</Values>
               <Default>VOLATILE</Default>
            </Input>

            <SetVar name="_parameters" value='#parameters.trim().equals("") ? "()" : "(" + #parameters + ")"'/>
            <Input label="Source" style="separator"/>
            <Input name="body" style="text-editor">
               <Default>
  if a > b:
    return a
  return b
               </Default>
            </Input>
            <Command exectype="asis">
               <SQL>
                  <![CDATA[
CREATE FUNCTION "${schema}".${QB}${functionName}${QE}
${_parameters}
RETURNS ${returns} 
${volatility}
AS $dbvis$
  ${body}
$dbvis$
LANGUAGE plpythonu]]>
               </SQL>
            </Command>
            <Result>
               <![CDATA[Function "${schema}"."${functionName}" created!]]>
            </Result>
         </Action>
      </ActionGroup>

      <ActionGroup type="Function">
         <Action id="redshift-function-drop" label="Drop Function" reload="true" icon="remove">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Function" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="Cascade" name="cascade" style="check">
               <Values>cascade</Values>
            </Input>
            <Command><SQL><![CDATA[drop function "${schema}"."${objectname}" (${redshift.getFunctions.Argument data types}) ${cascade}]]></SQL></Command>
            <Confirm>
               Really drop function ${schema}.${objectname}?
            </Confirm>
            <Result>
               Function ${schema}.${objectname} dropped!
            </Result>
         </Action>

         <Action id="redshift-function-rename" label="Rename Function" reload="true" icon="rename">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Function" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="New Function Name" name="newFunction" style="text"/>
            <Command>
               <SQL><![CDATA[alter function "${schema}"."${objectname}" (${redshift.getFunctions.Argument data types}) rename to ${QB}${newFunction}${QE}]]></SQL>
            </Command>
            <Confirm>
               Confirm rename of function ${objectname} to ${newFunction}?
            </Confirm>
            <Result>
               Function ${objectname} renamed to ${newFunction}!
            </Result>
         </Action>

         <Action id="redshift-function-comment" label="Comment Function" reload="true">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Function" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="Comment" name="comment" style="wrapped-text-editor">
               <Default>${redshift.getFunctions.Comment}</Default>
            </Input>
            <Command>
               <SQL><![CDATA[comment on function "${schema}"."${objectname}" (${redshift.getFunctions.Argument data types}) is '${comment}']]></SQL>
            </Command>
            <Confirm>
               Apply new comment on ${schema}.${getDDLName}?
            </Confirm>
         </Action>

         <Action id="redshift-function-export" label="Export Function" icon="export"
                 class="functionExport"
                 doclink="http://www.dbvis.com/doclink/export-code-object"/>

         <Separator/>

         <Action id="redshift-function-script" label="Script Function" icon="scriptObject"
                 class="sqlBuilder"
                 classargs="script-dialog,CALL,CREATE,DROP"/>
      </ActionGroup>

      <ActionGroup type="Domain">
         <Action id="redshift-domain-drop" label="Drop Domain" reload="true" icon="remove">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Domain" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="Cascade" name="cascade" style="check">
               <Values>cascade</Values>
            </Input>
            <Command><SQL><![CDATA[drop domain "${schema}"."${objectname}" ${cascade}]]></SQL></Command>
            <Confirm>
               Really drop domain ${schema}.${objectname}?
            </Confirm>
            <Result>
               Domain ${schema}.${objectname} dropped!
            </Result>
         </Action>
      </ActionGroup>

      <ActionGroup type="Aggregate">
         <Action id="redshift-aggregate-drop" label="Drop Aggregate" reload="true" icon="remove">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Aggregate" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>            
            <Input label="Cascade" name="cascade" style="check">
               <Values>cascade</Values>
            </Input>
             <Command><SQL><![CDATA[drop aggregate "${schema}"."${objectname}" (${redshift.getAggregates.Data Type}) ${cascade}]]></SQL></Command>
            <Confirm>
               Really drop aggregate ${schema}.${objectname}?
            </Confirm>
            <Result>
               Aggregate ${schema}.${objectname} has been dropped!
            </Result>
         </Action>

         <Action id="redshift-aggregate-rename" label="Rename Aggregate" reload="true" icon="rename">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Aggregate" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="New Aggregate Name" name="newAggregate" style="text"/>
            <Command>
               <SQL><![CDATA[alter aggregate "${schema}"."${objectname}" (${redshift.getAggregates.Data Type}) rename to ${QB}${newAggregate}${QE}]]></SQL>
            </Command>
            <Confirm>
               Confirm rename of ${schema}.${objectname} to ${newAggregate}?
            </Confirm>
            <Result>
               Aggregate ${schema}.${objectname} renamed to ${newAggregate}!
            </Result>
         </Action>
      </ActionGroup>

      <ActionGroup type="Type">
         <Action id="redshift-type-drop" label="Drop Type" reload="true" icon="remove">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Type" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="Cascade" name="cascade" style="check">
               <Values>cascade</Values>
            </Input>
            <!-- Do not quote type name as it is already quoted -->
            <Command><SQL><![CDATA[drop type "${schema}".${objectname} ${cascade}]]></SQL></Command>
            <Confirm>
               Really drop type ${schema}.${objectname}?
            </Confirm>
            <Result>
               Type ${schema}.${objectname} dropped!
            </Result>
         </Action>

         <Action id="redshift-type-comment" label="Comment Type" reload="true">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="Type" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>
            <Input label="Comment" name="comment" style="wrapped-text-editor">
               <Default>${redshift.getTypes.Comment}</Default>
            </Input>
            <Command>
               <!-- Do not quote type name as it is already quoted -->
               <SQL><![CDATA[comment on type "${schema}".${objectname} is '${comment}']]></SQL>
            </Command>
            <Confirm>
               Apply new comment on ${schema}.${objectname}?
            </Confirm>
         </Action>
      </ActionGroup>

      <ActionGroup type="Users">
         <Action id="redshift-users-create-user" label="Create User" reload="true" icon="add">
            <Input label="User" name="newuser" style="text" editable="true"/>
            <Input label="Password" name="password" style="text" editable="true"/>
            <Input label="In Group" name="group" style="list">
               <Values>
                  <Command>
                     <SQL><![CDATA[select '' union select groname from pg_group]]></SQL>
                  </Command>
               </Values>
            </Input>
            <SetVar name="_group" value='#group.equals("") ? "" : "in group ${QB}" + #group + "${QE}"'/>
            <Input label="Create DB" name="createdb" style="check">
               <Values>CREATEDB</Values>
            </Input>
            <Input label="Create User" name="createuser" style="check">
               <Values>CREATEUSER</Values>
            </Input>

            <Command>
               <SQL><![CDATA[create user ${QB}${newuser}${QE} with password '${password}' ${_group} ${createdb} ${createuser}]]></SQL>
            </Command>
            <Confirm>
               Create new user ${newuser}?
            </Confirm>
         </Action>
      </ActionGroup>

      <ActionGroup type="User">
         <Action id="redshift-user-drop-user" label="Drop User" reload="true" icon="remove">
            <Input label="User" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>

            <Command>
               <SQL><![CDATA[drop user "${objectname}"]]></SQL>
            </Command>
            <Confirm>
               Really drop user ${objectname}?
            </Confirm>
         </Action>
      </ActionGroup>

      <ActionGroup type="Groups">
         <Action id="redshift-groups-create-group" label="Create Group" reload="true" icon="add">
            <Input label="Group" name="newgroup" style="text" editable="true"/>

            <Command>
               <SQL><![CDATA[create group ${QE}${newgroup}${QE}]]></SQL>
            </Command>
            <Confirm>
               Create new group ${newgroup}?
            </Confirm>
         </Action>
      </ActionGroup>

      <ActionGroup type="Group">
         <Action id="redshift-group-drop-group" label="Drop Group" reload="true" icon="remove">
            <Input label="Group" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>

            <Command>
               <SQL><![CDATA[drop group "${objectname}"]]></SQL>
            </Command>
            <Confirm>
               Really drop group ${objectname}?
            </Confirm>
         </Action>
      </ActionGroup>

      <ActionGroup type="ExternalTable">
         <Action id="redshift-external-table-drop" label="Drop External Table" reload="true" icon="remove">
            <Input label="Schema" style="text" editable="false">
               <Default>${schema}</Default>
            </Input>
            <Input label="External Table" style="text" editable="false">
               <Default>${objectname}</Default>
            </Input>            
            <Input label="Drop Dependent Object(s)" name="cascade" style="check"
                   tip="Automatically drop objects that depend on the table (such as views)">
               <Values>cascade</Values>
            </Input>
            <Command><SQL><![CDATA[drop table "${schema}"."${objectname}" ${cascade}]]></SQL></Command>
            <Confirm>
               Really drop table ${schema}.${objectname}?
            </Confirm>
            <Result>
               Table ${schema}.${objectname} has been dropped!
            </Result>
         </Action>
      </ActionGroup>
   </ObjectsActionDef>

   <!-- =================================================================== -->
   <!-- Definition of the database objects tree structure -->
   <!-- =================================================================== -->

   <ObjectsTreeDef extends="false">

      <GroupNode type="Databases" label="Databases">
         <DataNode type="Catalog" label="${redshift.getConnectableDatabases.datname}" is-catalog-database="true">
            <SetVar name="catalog" value="${redshift.getConnectableDatabases.datname}"/>
            <Command idref="redshift.getConnectableDatabases">
               <Filter index="datname" label="Name"/>
            </Command>

            <GroupNode type="Schemas" label="Schemas">
               <DataNode type="Schema" label="${redshift.getSchemas.Name}">
                  <SetVar name="schema" value="${redshift.getSchemas.Name}"/>
                  <Command idref="redshift.getSchemas">
                     <Filter index="Name" label="Name"/>
                  </Command>

                  <GroupNode type="Tables" label="Tables">
                     <DataNode type="Table" label="${redshift.getTables.Name}">
                        <SetVar name="objectname" value="${redshift.getTables.Name}"/>
                        <SetVar name="rowcount" value="true"/>
                        <SetVar name="acceptInQB" value="true"/>
                        <Command idref="redshift.getTables">
                           <Input name="schema" value="${schema}"/>
                           <Input name="table" value="%"/>
                        </Command>

                        <!-- Need to keep the objectname for the current table in
                       a new variable so that the viewers will be able to pick up
                       the correct input to display -->
                        <SetVar name="theTableName" value="${objectname}"/>

                        <GroupNode type="Columns" label="Columns">
                           <DataNode type="Column" label="${getColumnDefinitions.COLUMN_NAME}"
                                     label1="${getColumnDefinitions.TYPE_NAME}"
                                     isLeaf="true">
                              <SetVar name="objectname" value="${getColumnDefinitions.COLUMN_NAME}"/>
                              <Command idref="getColumnDefinitions">
                                 <Input name="schema" value="${schema}"/>
                                 <Input name="objectname" value="${theTableName}"/>
                                 <Input name="tableType" value="Table"/>
                                 <Filter index="TYPE_NAME" label="Type"/>
                              </Command>
                           </DataNode>
                        </GroupNode>

                        <GroupNode type="DistKeyColumns" label="DistKey">
                           <DataNode type="DistKeyColumn" label="${getDistKeyColumnDefinitions.COLUMN_NAME}"
                                     label1="${getDistKeyColumnDefinitions.TYPE_NAME}"
                                     isLeaf="true"
                                     icon="Column">
                              <SetVar name="objectname" value="${getDistKeyColumnDefinitions.COLUMN_NAME}"/>
                              <Command idref="getDistKeyColumnDefinitions">
                                 <Input name="schema" value="${schema}"/>
                                 <Input name="catalog" value="${catalog}"/>
                                 <Input name="objectname" value="${theTableName}"/>
                                 <Filter index="TYPE_NAME" label="Type"/>
                              </Command>
                           </DataNode>
                        </GroupNode>

                        <GroupNode type="SortKeyColumns" label="SortKey">
                           <DataNode type="SortKeyColumn" label="${getSortKeyColumnDefinitions.COLUMN_NAME}"
                                     label1="${getSortKeyColumnDefinitions.TYPE_NAME}"
                                     isLeaf="true"
                                     icon="Column">
                              <SetVar name="objectname" value="${getSortKeyColumnDefinitions.COLUMN_NAME}"/>
                              <Command idref="getSortKeyColumnDefinitions">
                                 <Input name="schema" value="${schema}"/>
                                 <Input name="catalog" value="${catalog}"/>
                                 <Input name="objectname" value="${theTableName}"/>
                                 <Filter index="TYPE_NAME" label="Type"/>
                              </Command>
                           </DataNode>
                        </GroupNode>
                     </DataNode>
                  </GroupNode>
                  
                  <GroupNode type="Views" label="Views">
                     <DataNode type="View" label="${redshift.getViews.Name}">
                        <SetVar name="objectname" value="${redshift.getViews.Name}"/>
                        <SetVar name="rowcount" value="true"/>
                        <SetVar name="acceptInQB" value="true"/>
                        <Command idref="redshift.getViews">
                           <Input name="schema" value="${schema}"/>
                           <Input name="table" value="%"/>
                        </Command>
                        
                        <!-- Need to keep the objectname for the current table in
                       a new variable so that the viewers will be able to pick up
                       the correct input to display -->
                        <SetVar name="theTableName" value="${objectname}"/>

                        <GroupNode type="Columns" label="Columns">
                           <DataNode type="Column" label="${getColumnDefinitions.COLUMN_NAME}"
                                     label1="${getColumnDefinitions.TYPE_NAME}"
                                     isLeaf="true">
                              <SetVar name="objectname" value="${getColumnDefinitions.COLUMN_NAME}"/>
                              <Command idref="getColumnDefinitions">
                                 <Input name="schema" value="${schema}"/>
                                 <Input name="objectname" value="${theTableName}"/>
                                 <Input name="tableType" value="View"/>
                                 <Filter index="TYPE_NAME" label="Type"/>
                              </Command>
                           </DataNode>
                        </GroupNode>
                     </DataNode>
                  </GroupNode>

                  <SetVar name="theTableName" value="%"/>

                  <GroupNode type="Functions" label="Functions">
                     <DataNode type="Function" label="${redshift.getFunctions.Function Name} (${redshift.getFunctions.Argument data types})"
                               isLeaf="true">
                        <SetVar name="objectname" value="${redshift.getFunctions.Function Name}"/>
                        <SetVar name="getDDLName" value="${redshift.getFunctions.Function Name} (${redshift.getFunctions.Argument data types})"/>
                        <Command idref="redshift.getFunctions">
                           <Input name="schema" value="${schema}"/>
                           <Input name="kind" value="f"/>
                        </Command>
                     </DataNode>
                  </GroupNode>

                  <If test="#util.isDatabaseVersionGTE(10000,7287)">
                     <GroupNode type="Procedures" label="Procedures">
                        <DataNode type="Procedure" label="${redshift.getFunctions.Procedure Name} (${redshift.getFunctions.Argument data types})"
                                  isLeaf="true">
                           <SetVar name="objectname" value="${redshift.getFunctions.Procedure Name}"/>
                           <SetVar name="getDDLName" value="${redshift.getFunctions.Procedure Name} (${redshift.getFunctions.Argument data types})"/>
                           <Command idref="redshift.getFunctions">
                              <Input name="schema" value="${schema}"/>
                              <Input name="kind" value="p"/>
                              <ProcessDataSet action="renamecolumn" index="Function Name" value="Procedure Name"/>
                           </Command>
                        </DataNode>
                     </GroupNode>
                  </If>
                  
                  <GroupNode type="Domains" label="Domains">
                     <DataNode type="Domain" label="${redshift.getDomains.Domain Name}" isLeaf="true">
                        <SetVar name="objectname" value="${redshift.getDomains.Domain Name}"/>
                        <Command idref="redshift.getDomains">
                           <Input name="schema" value="${schema}"/>
                        </Command>
                     </DataNode>
                  </GroupNode>

                  <GroupNode type="Aggregates" label="Aggregates">
                     <DataNode type="Aggregate" label="${redshift.getAggregates.Aggregate Name}"
                               label1="${redshift.getAggregates.Data Type}"
                               isLeaf="true">
                        <SetVar name="objectname" value="${redshift.getAggregates.Aggregate Name}"/>
                        <Command idref="redshift.getAggregates">
                           <Input name="schema" value="${schema}"/>
                        </Command>
                     </DataNode>
                  </GroupNode>

                  <GroupNode type="Operators" label="Operators">
                     <DataNode type="Operator" label="${redshift.getOperators.Operator Name}"
                               label1="${redshift.getOperators.Left arg type},${redshift.getOperators.Right arg type}"
                               isLeaf="true">
                        <SetVar name="objectname" value="${redshift.getOperators.Operator Name}"/>
                        <Command idref="redshift.getOperators">
                           <Input name="schema" value="${schema}"/>
                        </Command>
                     </DataNode>
                  </GroupNode>

                  <GroupNode type="Types" label="Types">
                     <DataNode type="Type" label="${redshift.getTypes.Type Name}" isLeaf="true">
                        <SetVar name="objectname" value="${redshift.getTypes.Type Name}"/>
                        <Command idref="redshift.getTypes">
                           <Input name="schema" value="${schema}"/>
                        </Command>
                     </DataNode>
                  </GroupNode>

                  <!-- todo? <GroupNode type="Rules" label="Rules"/> -->

               </DataNode>
            </GroupNode>

            <GroupNode type="ExternalSchemas" label="External Schemas">
               <DataNode type="ExternalSchema" label="${redshift.getExternalSchemas.schemaname}">
                  <SetVar name="schema" value="${redshift.getExternalSchemas.schemaname}"/>
                  <Command idref="redshift.getExternalSchemas">
                  </Command>
                  
                  <GroupNode type="ExternalTables" label="External Tables">
                     <DataNode type="ExternalTable" label="${redshift.getExternalTables.tablename}">
                        <SetVar name="objectname" value="${redshift.getExternalTables.tablename}"/>
                        <SetVar name="rowcount" value="true"/>
                        <SetVar name="acceptInQB" value="true"/>
                        <Command idref="redshift.getExternalTables">
                           <Input name="schema" value="${schema}"/>
                           <Input name="table" value="%"/>
                        </Command>

                        <!-- Need to keep the objectname for the current table in
                       a new variable so that the viewers will be able to pick up
                       the correct input to display -->
                        <SetVar name="theExternalTableName" value="${objectname}"/>

                        <GroupNode type="ExternalColumns" label="Columns">
                           <DataNode type="ExternalColumn" label="${redshift.getExternalTableColumns.columnname}"
                                     label1="${redshift.getExternalTableColumns.external_type}"
                                     isLeaf="true">
                              <SetVar name="objectname" value="${redshift.getExternalTableColumns.columnname}"/>
                              <Command idref="redshift.getExternalTableColumns">
                                 <Input name="schema" value="${schema}"/>
                                 <Input name="table" value="${theExternalTableName}"/>
                              </Command>
                           </DataNode>
                        </GroupNode>

                     </DataNode>
                  </GroupNode>
               </DataNode>
            </GroupNode>
         </DataNode>
      </GroupNode>

      <!-- ================================================================= -->
      <!-- DBA Nodes -->
      <!-- ================================================================= -->

      <GroupNode type="DBA" label="DBA Views">
         <GroupNode type="Users" label="Users">
            <DataNode type="User" label="${redshift.getUsers.usename}" isLeaf="true">
               <SetVar name="objectname" value="${redshift.getUsers.usename}"/>
               <Command idref="redshift.getUsers"/>
            </DataNode>
         </GroupNode>
         <GroupNode type="Groups" label="Groups">
            <DataNode type="Group" label="${redshift.getGroups.groname}" isLeaf="true">
               <SetVar name="objectname" value="${redshift.getGroups.groname}"/>
               <Command idref="redshift.getGroups"/>
            </DataNode>
         </GroupNode>
         <GroupNode type="Settings" label="Settings" isLeaf="true"/>
         <GroupNode type="Locks" label="Locks" isLeaf="true"/>
         <GroupNode type="Sessions" label="Sessions" isLeaf="true"/>
      </GroupNode>

   </ObjectsTreeDef>

   <!-- =================================================================== -->
   <!-- Definition of the database objects views -->
   <!-- =================================================================== -->

   <ObjectsViewDef extends="true">

      <ObjectView type="Databases">
         <DataView id="redshift-databases-databases" icon="Catalog" label="Databases" viewer="grid">
            <Command idref="redshift.getCatalogs"/>
            <Message>
               <![CDATA[
<html>
List of all available databases in the database server. 
</html>
               ]]>
            </Message>
         </DataView>
      </ObjectView>

      <ObjectView type="Schemas">
         <DataView id="generic-schemas-schemas" action="drop"/>

         <DataView id="redshift-schemas-schemas" icon="Schemas" label="Schemas" viewer="grid">
            <Command idref="redshift.getSchemas"/>
         </DataView>
      </ObjectView>

      <ObjectView type="Catalog" action="drop"/>

      <ObjectView type="Schema">
         <DataView id="generic-schema-tables" action="drop"/>
         <DataView id="generic-schema-references" action="drop"/>

         <DataView id="redshift-schema-info" icon="Info" label="Info" viewer="node-form"/>
      </ObjectView>

      <ObjectView type="Tables">
         <DataView id="generic-tables-tables" action="drop"/>
         <DataView id="generic-tables-references" action="drop"/>

         <DataView id="redshift-tables-tables" icon="Tables" label="Tables" viewer="grid">
            <Input name="menuItem" value="Open in New Tab...">
               <Input name="action" value="open-object-in-new-tab-command ${schema||Schema}${object||Name}"/>
            </Input>
            <Input name="menuItem" value="Open in Floating Tab...">
               <Input name="action" value="open-object-in-floating-tab-command ${schema||Schema}${object||Name}"/>
            </Input>
            <Input name="menuItem" value="Script: SELECT ALL">
               <Input name="command" value="select * from ${schema||Schema}${object||Name}"/>
            </Input>
            <Input name="menuItem" value="Script: DROP TABLE">
               <Input name="command" value="drop table ${schema||Schema}${object||Name}"/>
            </Input>

            <Command idref="redshift.getTables">
               <Input name="schema" value="${schema}"/>
               <Input name="table" value="%"/>
            </Command>
         </DataView>
         <DataView id="redshift-tables-references" icon="References" label="References" viewer="tables-refs"
                   doclink="http://www.dbvis.com/doclink/show-references">
            <Command idref="getTables">
               <Input name="catalog" value="${catalog}"/>
               <Input name="schema" value="${schema}"/>
               <Input name="objectname" value="%"/>
               <Input name="tableType" value="SYSTEM TABLE,TABLE"/>
            </Command>
         </DataView>
      </ObjectView>

      <ObjectView type="Table">
         <DataView id="generic-table-info" action="drop"/>
         <DataView id="generic-table-indexes" action="drop"/>
         <DataView id="generic-table-navigator" action="drop"/>

         <DataView id="redshift-table-info" icon="Info" label="Info" viewer="form" order-before="0">
            <Command idref="redshift.getTables">
               <Input name="schema" value="${schema}"/>
               <Input name="table" value="${objectname}"/>
            </Command>
         </DataView>
         <DataView id="generic-table-ddl">
            <Input name="formatSQL" value="true"/>
            <Message/>
         </DataView>
      </ObjectView>

      <ObjectView type="Views">
         <DataView id="redshift-views-views" icon="Views" label="Views" viewer="grid">
            <Input name="menuItem" value="Open in New Tab...">
               <Input name="action" value="open-object-in-new-tab-command ${schema||Schema}${object||Name}"/>
            </Input>
            <Input name="menuItem" value="Open in Floating Tab...">
               <Input name="action" value="open-object-in-floating-tab-command ${schema||Schema}${object||Name}"/>
            </Input>
            <Input name="menuItem" value="Script: SELECT ALL">
               <Input name="command" value="select * from ${schema||Schema}${object||Name}"/>
            </Input>
            <Input name="menuItem" value="Script: DROP VIEW">
               <Input name="command" value="drop view ${schema||Schema}${object||Name}"/>
            </Input>

            <Command idref="redshift.getViews">
               <Input name="schema" value="${schema}"/>
               <Input name="table" value="%"/>
            </Command>
         </DataView>
      </ObjectView>

      <ObjectView type="View">
         <DataView id="redshift-view-info" icon="Info" label="Info" viewer="form">
            <Command idref="redshift.getViews">
               <Input name="schema" value="${schema}"/>
               <Input name="table" value="${objectname}"/>
            </Command>
         </DataView>
         <DataView id="redshift-view-columns" icon="Columns" label="Columns" viewer="grid">
            <Command idref="getColumns">
               <Input name="catalog" value="${catalog}"/>
               <Input name="schema" value="${schema}"/>
               <Input name="objectname" value="${objectname}"/>
            </Command>
         </DataView>
         <DataView id="redshift-view-data" icon="Data" label="Data" viewer="table-data"/>
         <DataView id="redshift-view-rowcount" icon="RowCount" label="Row Count" viewer="table-rowcount"/>
         <DataView id="redshift-view-grants" icon="TablePrivileges" label="Grants" viewer="grid">
            <Command idref="redshift.getPrivileges">
               <Input name="catalog" value="${catalog}"/>
               <Input name="schema" value="${schema}"/>
               <Input name="objectname" value="${objectname}"/>
            </Command>
         </DataView>
         <DataView id="redshift-view-ddl" icon="Source" label="DDL" viewer="ddl">
            <Input name="objectType" value="View"/>
            <Input name="formatSQL" value="true"/>
         </DataView>
      </ObjectView>

      <ObjectView type="Functions">
         <DataView id="redshift-functions-functions" icon="Functions" label="Functions" viewer="grid">
            <Command idref="redshift.getFunctions">
               <Input name="schema" value="${schema}"/>
               <Input name="kind" value="f"/>
            </Command>
         </DataView>
      </ObjectView>

      <ObjectView type="Function">
         <DataView classargs="FUNCTION" id="redshift-function-functioneditor" icon="SourceEditor" label="Function Editor" viewer="ProcedureViewer"
                   doclink="http://www.dbvis.com/doclink/edit-code-object">
         </DataView>
         <DataView id="redshift-function-info" icon="Info" label="Info" viewer="node-form"/>
      </ObjectView>

     <ObjectView type="Procedures" action="drop"/>
      
      <ObjectView type="Procedures">
         <DataView id="redshift-procedures-Procedures" icon="Procedures" label="Procedures" viewer="grid">
            <Command idref="redshift.getFunctions">
               <Input name="schema" value="${schema}"/>
               <Input name="kind" value="p"/>
            </Command>
         </DataView>
      </ObjectView>
      
      <ObjectView type="Procedure" action="drop"/>
      <ObjectView type="Procedure">
         <DataView classargs="PROCEDURE" id="redshift-procedure-procedureeditor" icon="SourceEditor" label="Procedure Editor" viewer="ProcedureViewer"
                   doclink="http://www.dbvis.com/doclink/edit-code-object">
         </DataView>
         <DataView id="redshift-procedure-info" icon="Info" label="Info" viewer="node-form"/>
       
      </ObjectView>

      <ObjectView type="Domains">
         <DataView id="redshift-domains-domains" icon="Domains" label="Domains" viewer="grid">
            <Command idref="redshift.getDomains">
               <Input name="schema" value="${schema}"/>
            </Command>
         </DataView>
      </ObjectView>

      <ObjectView type="Domain">
         <DataView id="redshift-domain-info" icon="Info" label="Info" viewer="node-form"/>
      </ObjectView>

      <ObjectView type="Aggregates">
         <DataView id="redshift-aggregates-aggregates" icon="Aggregates" label="Aggregates" viewer="grid">
            <Command idref="redshift.getAggregates">
               <Input name="schema" value="${schema}"/>
            </Command>
         </DataView>
      </ObjectView>

      <ObjectView type="Aggregate">
         <DataView id="redshift-aggregate-info" icon="Info" label="Info" viewer="node-form"/>
      </ObjectView>

      <ObjectView type="Operators">
         <DataView id="redshift-operators-operators" icon="Operators" label="Operators" viewer="grid">
            <Command idref="redshift.getOperators">
               <Input name="schema" value="${schema}"/>
            </Command>
         </DataView>
      </ObjectView>

      <ObjectView type="Operator">
         <DataView id="redshift-operator-info" icon="Info" label="Info" viewer="node-form"/>
      </ObjectView>

      <ObjectView type="Types">
         <DataView id="redshift-types-types" icon="Types" label="Types" viewer="grid">
            <Command idref="redshift.getTypes">
               <Input name="schema" value="${schema}"/>
            </Command>
         </DataView>
      </ObjectView>

      <ObjectView type="Type">
         <DataView id="redshift-type-info" icon="Info" label="Info" viewer="node-form"/>
      </ObjectView>

      <!-- DBA views -->

      <ObjectView type="Users">
         <DataView id="redshift-users-users" icon="Users" label="Users" viewer="grid">
            <Command idref="redshift.getUsers"/>
         </DataView>
      </ObjectView>

      <ObjectView type="User">
         <DataView id="redshift-user-info" icon="Info" label="Info" viewer="node-form"/>
      </ObjectView>

      <ObjectView type="Groups">
         <DataView id="redshift-groups-groups" icon="Groups" label="Groups" viewer="grid">
            <Command idref="redshift.getGroups"/>
         </DataView>
      </ObjectView>

      <ObjectView type="Group">
         <DataView id="redshift-group-info" icon="Info" label="Info" viewer="node-form"/>
      </ObjectView>

      <ObjectView type="Settings">
         <DataView id="redshift-settings-settings" icon="Settings" label="Settings" viewer="grid">
            <Command idref="redshift.getSettings"/>
         </DataView>
      </ObjectView>

      <ObjectView type="Sessions">
         <DataView id="redshift-sessions-sessions" icon="Sessions" label="Sessions" viewer="grid">
            <Input name="menuItem" value="Script: CANCEL PROCESS">
               <Input name="command" value="cancel ${procpid}"/>
            </Input>
            <Command idref="redshift.getSessions"/>
         </DataView>
      </ObjectView>

      <ObjectView type="Locks">
         <DataView id="redshift-locks-locks" icon="Locks" label="Locks" viewer="grid">
            <Command idref="redshift.getLocks"/>
         </DataView>
      </ObjectView>

      <ObjectView type="ExternalSchemas">
         <DataView id="redshift-external-schemas-schemas" icon="ExternalSchemas" label="External Schemas" viewer="grid">
            <Command idref="redshift.getExternalSchemas"/>
         </DataView>
      </ObjectView>

      <ObjectView type="ExternalTables">
         <DataView id="redshift-external-tables-tables" icon="ExternalTables" label="External Tables" viewer="grid">
            <Command idref="redshift.getExternalTables"/>
         </DataView>
      </ObjectView>
      
      <ObjectView type="ExternalTable">
         <DataView id="redshift-external-table-info" icon="Info" label="Info" viewer="form">
            <Command idref="redshift.getExternalTables">
               <Input name="schema" value="${schema}"/>
               <Input name="table" value="${objectname}"/>
            </Command>
         </DataView>
         <DataView id="redshift-external-table-columns" icon="Columns" label="Columns" viewer="grid">
            <Command idref="redshift.getExternalTableColumns">
               <Input name="schema" value="${schema}"/>
               <Input name="table" value="${objectname}"/>
            </Command>
         </DataView>
         <DataView id="redshift-external-table-data" icon="Data" label="Data" viewer="table-data">
            <Input name="editDisabled" value="true"/>
         </DataView>
         <DataView id="redshift-external-table-rowcount" icon="RowCount" label="Row Count" viewer="table-rowcount"/>
      </ObjectView>
   </ObjectsViewDef>

</DatabaseProfile>
