<?xml version="1.0"?>
<!-- Copyright (c) 2004, 2009, Oracle and/or its affiliates. 
All rights reserved. -->
<!--
NAME
    kusstorg.xsl
DESCRIPTION
    This is a utility stylesheet for generating SXML syntax for
    physical attributes:
     - segment_attributes_clause
     - physical_attributes_clause
     - storage_clause

MODIFIED        MM/DD/YY
    lbarton     11/04/09 - deferred segment creation for partitions
    abodge      09/22/09 - CONSOLIDATE STYLESHEETS: PHYSICAL_PROPERTIES hack
    lbarton     08/31/09 - bug 8820324: support all values in deferred_stg
    lbarton     08/17/09 - bug 8795792: no compress/logging in deferred stg for
                           indexes
    lbarton     07/10/09 - bug 8494344: archive compression
    rapayne     07/20/09 - bug 8692388: suppress STORAGE clause if there are no
                           relevant storage params.
    rapayne     05/22/09 - bug 8391296: Fix support for  CELL_FLASH_BUFFER and FLASH_CACHE
    rapayne     05/10/09 - bug 8355496: add framework for index deferred storage
                           support.
    adalee      04/16/09 - add FLASH_CACHE and CELL_FLASH_CACHE
    lbarton     02/11/08 - reference/system partitioning
    lbarton     03/19/08 - deferred segment creation
    slynn       10/12/06 - smartfile->securefileversion
    htseng      07/31/06 - add lobretention/maxsize support
    lbarton     11/09/05 - bug 4724986: fix handling of xmlns 
    sdavidso    11/02/05 - fix inconsistent stylesheet format 
    lbarton     09/12/05 - Move no blocksize error to StorageClause 
    lbarton     08/10/05 - lbarton_mddiff
    lbarton     11/04/04 - Initial version
 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://xmlns.oracle.com/ku">
 <!-- Top level imports -->
 <xsl:import href="kuscomm.xsl"/>
 <!-- Templates -->
 <xsl:template name="SegmentAttributes">
  <xsl:param name="ParentNode" select="''"/>
  <xsl:param name="Partitioned" select="''"/>
  <xsl:param name="ObjType">TABLE</xsl:param>
  <xsl:param name="PartitionType" select="''"/>
  <xsl:param name="DefaultBlocksize" select="''"/>
  <xsl:param name="TableNode" select="''"/>
  <xsl:param name="PartNode" select="''"/>
  <!-- *******************************************************************
Template: SegmentAttributes
Parameters:
  ParentNode - Parent node of TS_NAME, BLOCKSIZE, PCT_FREE, etc.
  Partitioned - 0 = non-partitioned, non-0 = partitioned
                Used to locate/interpret the storage and logging information.
                For non-partitioned objects storage information (INIEXTS, etc.)
                is stored under STORAGE (a peer of PCT_FREE, etc.);
                for partitioned objects the information (DEFINIEXTS, etc.)
                is at the same level as DEFPCTFREE, etc.
                The location and encoding of logging information is
                different for partitioned/non-partitioned objects.
                See "LoggingClause", below.
  ObjType - One of
              "TABLE" (default)
              "INDEX" (used for both indexes and IOTs)
              "ROLLBACK"
              "TABLESPACE"
              "PARTITION"
              "PIOT_PARTITION"
  PartitionType - RANGE, HASH, LIST, ''
  DefaultBlocksize - For StorageClause
  TableNode: TABLE_T
  PartNode: partition or subpartition node (PART_LIST_ITEM or SUBPARTS_ITEM)
******************************************************************** -->
  <xsl:if test="not ($SEGMENT_ATTRIBUTES=0) and 
                not ($CM_MODE=1 and $PHYSICAL_PROPERTIES=0)">
   <xsl:element name="SEGMENT_ATTRIBUTES">
    <xsl:if test="$PartitionType!='HASH'">
     <xsl:call-template name="PhysicalAttributes">
      <xsl:with-param name="ParentNode" select="$ParentNode"/>
      <xsl:with-param name="Partitioned" select="$Partitioned"/>
      <xsl:with-param name="ObjType" select="$ObjType"/>
      <xsl:with-param name="DefaultBlocksize" select="$DefaultBlocksize"/>
      <xsl:with-param name="TableNode" select="$TableNode"/>
      <xsl:with-param name="PartNode" select="$PartNode"/>
     </xsl:call-template>
    </xsl:if>
    <xsl:call-template name="Tablespace">
     <xsl:with-param name="ParentNode" select="$ParentNode"/>
    </xsl:call-template>
    <!--xsl:if test="$ObjType!='PARTITION' and $ObjType!='PIOT_PARTITION'"-->
    <xsl:if test="$PartitionType!='HASH'">
     <xsl:call-template name="LoggingClause">
      <xsl:with-param name="Partitioned" select="$Partitioned"/>
      <xsl:with-param name="FlagsNode" select="$ParentNode/FLAGS"/>
      <xsl:with-param name="DefloggingNode" select="$ParentNode/DEFLOGGING"/>
      <xsl:with-param name="PartitionType" select="$PartitionType"/>
     </xsl:call-template>
    </xsl:if>
   </xsl:element>
  </xsl:if>
 </xsl:template>
 <xsl:template name="PhysicalAttributes">
  <xsl:param name="ParentNode" select="''"/>
  <xsl:param name="Partitioned" select="''"/>
  <xsl:param name="ObjType">TABLE</xsl:param>
  <xsl:param name="DefaultBlocksize" select="''"/>
  <xsl:param name="TableNode" select="''"/>
  <xsl:param name="PartNode" select="''"/>
  <!-- *******************************************************************
Template: PhysicalAttributes
Parameters:
  ParentNode - Parent node of TS_NAME, BLOCKSIZE, PCT_FREE, etc.
  Partitioned - 0 = non-partitioned, non-0 = partitioned
  ObjType - One of
              "TABLE" (default)
              "INDEX" (used for both indexes and IOTs)
              "ROLLBACK"
              "TABLESPACE"
              "PARTITION"
              "PIOT_PARTITION"
  DefaultBlocksize - For StorageClause
  TableNode: TABLE_T
  PartNode: partition or subpartition node (PART_LIST_ITEM or SUBPARTS_ITEM)
******************************************************************** -->
  <xsl:if test="$ObjType='TABLE' or $ObjType='PARTITION'">
   <xsl:call-template name="DeferredSegmentCreationClause">
    <xsl:with-param name="TableNode" select="$TableNode"/>
    <xsl:with-param name="PartNode" select="$PartNode"/>
   </xsl:call-template>
  </xsl:if>
  <xsl:element name="PCTFREE">
   <xsl:value-of select="$ParentNode/PCT_FREE"/>
   <xsl:value-of select="$ParentNode/DEFPCTFREE"/>
  </xsl:element>
  <xsl:if test="($ObjType!='INDEX') and ($ObjType!='PIOT_PARTITION')">
   <xsl:element name="PCTUSED">
    <xsl:value-of select="$ParentNode/PCT_USED"/>
    <xsl:value-of select="$ParentNode/DEFPCTUSED"/>
   </xsl:element>
  </xsl:if>
  <xsl:element name="INITRANS">
   <xsl:value-of select="$ParentNode/INITRANS"/>
   <xsl:value-of select="$ParentNode/DEFINITRANS"/>
  </xsl:element>
  <!-- STORAGE -->
  <xsl:choose>
   <!-- non-partitioned -->
   <xsl:when test="$Partitioned=0">
    <xsl:choose>
     <!-- segment creation deferred for table and index -->
     <xsl:when test="(($ObjType='TABLE' or $ObjType='PARTITION') and
                      $ParentNode/DEFERRED_STG) or
                     ($ObjType = 'INDEX' and
                      string-length($ParentNode/BLOCKSIZE)!=0 and 
                      $ParentNode/DEFERRED_STG)">
      <xsl:call-template name="DoDeferredStg">
       <xsl:with-param name="DeferredStg" select="$ParentNode/DEFERRED_STG"/>
       <xsl:with-param name="BlockSize" select="$ParentNode/BLOCKSIZE | $ParentNode/DEFBLOCKSIZE"/>
      </xsl:call-template>
     </xsl:when>
     <xsl:when test="string-length($ParentNode/BLOCKSIZE)!=0">
      <xsl:call-template name="StorageClause">
       <xsl:with-param name="StorageNode" select="$ParentNode/STORAGE"/>
       <xsl:with-param name="BlkSize" select="$ParentNode/BLOCKSIZE"/>
      </xsl:call-template>
     </xsl:when>
     <xsl:when test="string-length($DefaultBlocksize)!=0">
      <xsl:call-template name="StorageClause">
       <xsl:with-param name="StorageNode" select="$ParentNode/STORAGE"/>
       <xsl:with-param name="BlkSize" select="$DefaultBlocksize"/>
      </xsl:call-template>
     </xsl:when>
    </xsl:choose>
   </xsl:when>
   <!-- partitioned -->
   <xsl:otherwise>
    <xsl:choose>
     <xsl:when test="string-length($ParentNode/BLOCKSIZE)!=0 or
                    string-length($ParentNode/DEFBLOCKSIZE)!=0">
      <xsl:call-template name="StorageClause">
       <xsl:with-param name="StorageNode" select="$ParentNode"/>
       <xsl:with-param name="BlkSize" select="$ParentNode/BLOCKSIZE | $ParentNode/DEFBLOCKSIZE"/>
      </xsl:call-template>
     </xsl:when>
     <xsl:when test="string-length($DefaultBlocksize)!=0">
      <xsl:call-template name="StorageClause">
       <xsl:with-param name="StorageNode" select="$ParentNode"/>
       <xsl:with-param name="BlkSize" select="$DefaultBlocksize"/>
      </xsl:call-template>
     </xsl:when>
    </xsl:choose>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 <xsl:template name="DeferredSegmentCreationClause">
  <xsl:param name="TableNode" select="''"/>
  <xsl:param name="PartNode" select="''"/>
  <!-- *******************************************************************
Template: DeferredSegmentCreationClause: Optionally emit the 
                deferred segment creation clause
Parameters:
  TableNode: TABLE_T
  PartNode: partition or subpartition node (PART_LIST_ITEM or SUBPARTS_ITEM)
******************************************************************** -->
   <!-- SEGMENT CREATION syntax is new in 11.2;
        not valid for IOTs
                      clustered tables
                      temporary tables
                      typed tables
                      AQ tables
                      external tables
                      tables owned by SYS, SYSTEM, PUBLIC, OUTLN, XDB
    -->
  <xsl:if test="string-length($TableNode)>0
                  and $VERSION>=1102000000
                  and (($TableNode/PROPERTY) mod 2)=0
                  and not ((($TableNode/PROPERTY) mod 262144)>=131072)
                  and $TableNode/SCHEMA_OBJ/OWNER_NAME!='SYS'
                  and $TableNode/SCHEMA_OBJ/OWNER_NAME!='SYSTEM'
                  and $TableNode/SCHEMA_OBJ/OWNER_NAME!='PUBLIC'
                  and $TableNode/SCHEMA_OBJ/OWNER_NAME!='OUTLN'
                  and $TableNode/SCHEMA_OBJ/OWNER_NAME!='XDB'">
   <xsl:choose>
    <xsl:when test="($TableNode/PROPERTY mod 64)&lt;32">
     <!-- non-partitioned table -->
      <xsl:if test="($TableNode/PROPERTY2 mod 8)>=4">
       <xsl:element name="SEGMENT_CREATION_DEFERRED"/>
      </xsl:if>
    </xsl:when>
    <xsl:when test="string-length($PartNode)=0">
     <!-- table-level setting of partitioned table -->
     <xsl:choose>
      <xsl:when test="($TableNode/PART_OBJ/PARTOBJ/FLAGS mod 4096)>=2048">
       <xsl:element name="SEGMENT_CREATION_DEFERRED"/>
      </xsl:when>
      <xsl:when test="($TableNode/PART_OBJ/PARTOBJ/FLAGS mod 8192)>=4096">
       <xsl:element name="SEGMENT_CREATION_IMMEDIATE"/>
      </xsl:when>
     </xsl:choose>
    </xsl:when>
    <xsl:when test="local-name($PartNode)='COMPART_LIST_ITEM'">
     <!-- composite partition setting -->
     <xsl:choose>
      <xsl:when test="(($PartNode/SPARE2) mod 512)>=256">
       <xsl:element name="SEGMENT_CREATION_DEFERRED"/>
      </xsl:when>
      <xsl:when test="(($PartNode/SPARE2) mod 1024)>=512">
       <xsl:element name="SEGMENT_CREATION_IMMEDIATE"/>
      </xsl:when>
     </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
     <!-- partition leaf setting -->
     <xsl:choose>
      <xsl:when test="(($PartNode/FLAGS) mod 131072)>=65536">
       <xsl:element name="SEGMENT_CREATION_DEFERRED"/>
      </xsl:when>
      <xsl:otherwise>
       <xsl:element name="SEGMENT_CREATION_IMMEDIATE"/>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:if>
 </xsl:template>
 <xsl:template name="DoDeferredStg">
  <xsl:param name="DeferredStg" select="''"/>
  <xsl:param name="Blocksize">8192</xsl:param>
  <!-- *******************************************************************
Template: DoDeferredStg - generate storage params when seg creation deferred
Current node:  INDEX_T/PARTITION_LIST/PARTITION_LIST_ITEM
            or TABLE_T
Parameters:
  DeferredStg - DEFERRED_STORAGE node
  Blocksize - 

need to build storage clause
  <STORAGE>
      <INITIAL>2097152</INITIAL>
      <NEXT>1048576</NEXT>
      <MINEXTENTS>1</MINEXTENTS>
      <MAXEXTENTS>2147483645</MAXEXTENTS>
      <PCTINCREASE>0</PCTINCREASE>
      <MAXSIZE>10485760</MAXSIZE>
      <FREELISTS>1</FREELISTS>
      <FREELIST_GROUPS>1</FREELIST_GROUPS>
      <BUFFER_POOL>DEFAULT</BUFFER_POOL>
  </STORAGE

******************************************************************** -->
  <xsl:variable name="hasINITIAL" select="$DeferredStg and $DeferredStg/INITIAL_STG"/>
  <xsl:variable name="hasNEXT" select="$DeferredStg and $DeferredStg/NEXT_STG"/>
  <xsl:variable name="hasMINEXT" select="$DeferredStg and $DeferredStg/MINEXT_STG"/>
  <xsl:variable name="hasMAXEXT" select="$DeferredStg and $DeferredStg/MAXEXT_STG"/>
  <xsl:variable name="hasPCTINC" select="$DeferredStg and $DeferredStg/PCTINC_STG"/>
  <xsl:variable name="hasMAXSIZE" select="$DeferredStg and $DeferredStg/MAXSIZ_STG"/>
  <xsl:variable name="hasFREELISTS" select="$DeferredStg and $DeferredStg/FRLINS_STG"/>
  <xsl:variable name="hasFREELIST_GROUPS" select="$DeferredStg and $DeferredStg/MAXINS_STG"/>
  <xsl:variable name="hasBUFFER_POOL" select="$DeferredStg and $DeferredStg/BFP_STG"/>
  <!-- Suppress STORAGE clause if there are no relevant deferred stg params -->
  <xsl:variable name="hasLine1" select="$hasINITIAL or $hasNEXT or $hasMINEXT or $hasMAXEXT"/>
  <xsl:variable name="hasLine2" select="$hasPCTINC or $hasMAXSIZE or $hasFREELISTS or $hasFREELIST_GROUPS or $hasBUFFER_POOL"/>
  <xsl:if test="$hasLine1 or $hasLine2">
   <xsl:element name="STORAGE">
    <xsl:call-template name="DoDeferredStgParam">
     <xsl:with-param name="ParamName">INITIAL</xsl:with-param>
     <xsl:with-param name="ParamVal" select="$DeferredStg/INITIAL_STG"/>
     <xsl:with-param name="testval" select="$hasINITIAL"/>
     <xsl:with-param name="mult" select="$Blocksize"/>
    </xsl:call-template>
    <xsl:call-template name="DoDeferredStgParam">
     <xsl:with-param name="ParamName">NEXT</xsl:with-param>
     <xsl:with-param name="ParamVal" select="$DeferredStg/NEXT_STG"/>
     <xsl:with-param name="testval" select="$hasNEXT"/>
     <xsl:with-param name="mult" select="$Blocksize"/>
    </xsl:call-template>
    <xsl:call-template name="DoDeferredStgParam">
     <xsl:with-param name="ParamName">MINEXTENTS</xsl:with-param>
     <xsl:with-param name="ParamVal" select="$DeferredStg/MINEXT_STG"/>
     <xsl:with-param name="testval" select="$hasMINEXT"/>
    </xsl:call-template>
    <xsl:call-template name="DoDeferredStgParam">
     <xsl:with-param name="ParamName">MAXEXTENTS</xsl:with-param>
     <xsl:with-param name="ParamVal" select="$DeferredStg/MAXEXT_STG"/>
     <xsl:with-param name="testval" select="$hasMAXEXT"/>
    </xsl:call-template>
    <xsl:call-template name="DoDeferredStgParam">
     <xsl:with-param name="ParamName">PCTINCREASE</xsl:with-param>
     <xsl:with-param name="ParamVal" select="$DeferredStg/PCTINC_STG"/>
     <xsl:with-param name="testval" select="$hasPCTINC"/>
    </xsl:call-template>
    <xsl:call-template name="DoDeferredStgParam">
     <xsl:with-param name="ParamName">MAXSIZE</xsl:with-param>
     <xsl:with-param name="ParamVal" select="$DeferredStg/MAXSIZ_STG"/>
     <xsl:with-param name="testval" select="$hasMAXSIZE"/>
     <xsl:with-param name="mult" select="$Blocksize"/>
    </xsl:call-template>
    <xsl:call-template name="DoDeferredStgParam">
     <xsl:with-param name="ParamName">FREELISTS</xsl:with-param>
     <xsl:with-param name="ParamVal" select="$DeferredStg/FRLINS_STG"/>
     <xsl:with-param name="testval" select="$hasFREELISTS"/>
    </xsl:call-template>
    <xsl:call-template name="DoDeferredStgParam">
     <xsl:with-param name="ParamName">FREELIST_GROUPS</xsl:with-param>
     <xsl:with-param name="ParamVal" select="$DeferredStg/MAXINS_STG"/>
     <xsl:with-param name="testval" select="$hasFREELIST_GROUPS"/>
    </xsl:call-template>
    <xsl:if test="$hasBUFFER_POOL">
     <xsl:apply-templates select="$DeferredStg/BFP_STG"/>
    </xsl:if>
   </xsl:element>
  </xsl:if>
 </xsl:template>
 <xsl:template name="DoIniNext">
  <xsl:param name="Size">0</xsl:param>
  <xsl:param name="BlkSize">0</xsl:param>
  <!-- *******************************************************************
Template: DoIniNext emit the numeric value for INITIAL, NEXT, etc.
Parameters:
 Size: size value in blocks
 BlkSize: value of BLOCKSIZE
******************************************************************** -->
  <xsl:value-of select="$Size*$BlkSize"/>
 </xsl:template>
 <xsl:template name="DoDeferredStgParam">
  <xsl:param name="ParamName" select="''"/>
  <xsl:param name="ParamVal" select="''"/>
  <xsl:param name="testval" select="''"/>
  <xsl:param name="mult">1</xsl:param>
  <!-- *******************************************************************
Template: DoDeferredStgParam - Utility template to emit a single
  STORAGE param
Parameters:
  ParamName - name, e.g., INITIAL
  ParamVal - value if any
  testval - true if ParamVal is valid
  mult - factor to multiply ParamVal (e.g., INITIAL is stored as
         a number of blocks, so mult would be the blocksize)
******************************************************************** -->
  <xsl:if test="$testval">
   <xsl:choose>
    <xsl:when test="$ParamName='MAXSIZE'">
     <xsl:call-template name="DoMAXSIZE">
      <xsl:with-param name="MaxSize" select="$ParamVal"/>
      <xsl:with-param name="BlkSize" select="$mult"/>
     </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
     <xsl:element name="{$ParamName}">
      <xsl:call-template name="DoIniNext">
       <xsl:with-param name="Size" select="$ParamVal"/>
       <xsl:with-param name="BlkSize" select="$mult"/>
      </xsl:call-template>
     </xsl:element>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:if>
 </xsl:template>
 <xsl:template name="StorageClause">
  <xsl:param name="StorageNode" select="''"/>
  <xsl:param name="ObjType">TABLE</xsl:param>
  <xsl:param name="BlkSize">0</xsl:param>
  <!-- *******************************************************************
Template: StorageClause
Parameters:
  StorageNode - STORAGE node
  ObjType - One of
              "TABLE" (default)
              "INDEX" (used for both indexes and IOTs)
              "ROLLBACK"
              "TABLESPACE"
              "PARTITION"
              "PIOT_PARTITION"
  BlkSize - tablespace blocksize
******************************************************************** -->
  <xsl:if test="not ($STORAGE=0)">
   <xsl:element name="STORAGE">
    <xsl:if test="$StorageNode/DEFINIEXTS or $StorageNode/INIEXTS
             or $StorageNode/DFLINIT">
     <xsl:if test="$BlkSize=0">
      <xsl:text>
       **** kusstorg.xsl/StorageClause ERROR: No BLOCKSIZE VALUE ***
</xsl:text>
     </xsl:if>
     <xsl:element name="INITIAL">
      <xsl:choose>
       <xsl:when test="$StorageNode/INIEXTS">
        <xsl:value-of select="($StorageNode/INIEXTS)*$BlkSize"/>
       </xsl:when>
       <xsl:when test="$StorageNode/DEFINIEXTS">
        <xsl:value-of select="($StorageNode/DEFINIEXTS)*$BlkSize"/>
       </xsl:when>
       <xsl:when test="$StorageNode/DFLINIT">
        <xsl:value-of select="($StorageNode/DFLINIT)*$BlkSize"/>
       </xsl:when>
      </xsl:choose>
     </xsl:element>
    </xsl:if>
    <xsl:if test="$StorageNode/DEFEXTSIZE or $StorageNode/EXTSIZE
             or $StorageNode/DFLINCR">
     <xsl:if test="$BlkSize=0">
      <xsl:text>
       **** kusstorg.xsl/StorageClause ERROR: No BLOCKSIZE VALUE ***
</xsl:text>
     </xsl:if>
     <xsl:element name="NEXT">
      <xsl:choose>
       <xsl:when test="$StorageNode/EXTSIZE">
        <xsl:value-of select="($StorageNode/EXTSIZE)*$BlkSize"/>
       </xsl:when>
       <xsl:when test="$StorageNode/DEFEXTSIZE">
        <xsl:value-of select="($StorageNode/DEFEXTSIZE)*$BlkSize"/>
       </xsl:when>
       <xsl:when test="$StorageNode/DFLINCR">
        <xsl:value-of select="($StorageNode/DFLINCR)*$BlkSize"/>
       </xsl:when>
      </xsl:choose>
     </xsl:element>
    </xsl:if>
    <xsl:if test="$StorageNode/DEFMINEXTS or $StorageNode/MINEXTS
             or $StorageNode/DFLMINEXT">
     <xsl:element name="MINEXTENTS">
      <xsl:choose>
       <xsl:when test="$StorageNode/MINEXTS">
        <xsl:value-of select="($StorageNode/MINEXTS)"/>
       </xsl:when>
       <xsl:when test="$StorageNode/DEFMINEXTS">
        <xsl:value-of select="($StorageNode/DEFMINEXTS)"/>
       </xsl:when>
       <xsl:when test="$StorageNode/DFLMINEXT">
        <xsl:value-of select="($StorageNode/DFLMINEXT)"/>
       </xsl:when>
      </xsl:choose>
     </xsl:element>
    </xsl:if>
    <xsl:if test="$StorageNode/DEFMAXEXTS or $StorageNode/MAXEXTS
             or $StorageNode/DFLMAXEXT">
     <xsl:element name="MAXEXTENTS">
      <xsl:choose>
       <xsl:when test="$StorageNode/MAXEXTS">
        <xsl:value-of select="($StorageNode/MAXEXTS)"/>
       </xsl:when>
       <xsl:when test="$StorageNode/DEFMAXEXTS">
        <xsl:value-of select="($StorageNode/DEFMAXEXTS)"/>
       </xsl:when>
       <xsl:when test="$StorageNode/DFLMAXEXT">
        <xsl:value-of select="($StorageNode/DFLMAXEXT)"/>
       </xsl:when>
      </xsl:choose>
     </xsl:element>
    </xsl:if>
    <xsl:if test="not($ObjType='ROLLBACK')">
     <xsl:if test="$StorageNode/DEFEXTPCT or $StorageNode/EXTPCT
              or $StorageNode/DFLEXTPCT">
      <xsl:element name="PCTINCREASE">
       <xsl:choose>
        <xsl:when test="$StorageNode/EXTPCT">
         <xsl:value-of select="($StorageNode/EXTPCT)"/>
        </xsl:when>
        <xsl:when test="$StorageNode/DEFEXTPCT">
         <xsl:value-of select="($StorageNode/DEFEXTPCT)"/>
        </xsl:when>
        <xsl:when test="$StorageNode/DFLEXTPCT">
         <xsl:value-of select="($StorageNode/DFLEXTPCT)"/>
        </xsl:when>
       </xsl:choose>
      </xsl:element>
     </xsl:if>
    </xsl:if>
    <xsl:if test="($ObjType != 'ROLLBACK') or
                ($ObjType != 'TABLESPACE')">
     <!-- MAXSIZE clause -->
     <!-- 11g: BITMAPRANGES => MAXSIZE -->
     <xsl:if test="$StorageNode/BITMAPRANGES and 
        ($StorageNode/FLAGS mod 8388608)>=4194304 or $StorageNode/DEFMAXSIZE">
      <xsl:call-template name="DoMAXSIZE">
       <xsl:with-param name="MaxSize" select="$StorageNode/BITMAPRANGES | $StorageNode/DEFMAXSIZE"/>
       <xsl:with-param name="BlkSize" select="$BlkSize"/>
      </xsl:call-template>
     </xsl:if>
     <xsl:if test="2097152 > ($StorageNode/FLAGS mod 4194304)">
      <xsl:if test="$StorageNode/DEFLISTS > 0 or $StorageNode/LISTS">
       <xsl:element name="FREELISTS">
        <xsl:value-of select="$StorageNode/LISTS"/>
        <xsl:value-of select="$StorageNode/DEFLISTS"/>
       </xsl:element>
      </xsl:if>
      <xsl:if test="$StorageNode/DEFGROUPS > 0 or $StorageNode/GROUPS">
       <xsl:element name="FREELIST_GROUPS">
        <xsl:value-of select="$StorageNode/GROUPS"/>
        <xsl:value-of select="$StorageNode/DEFGROUPS"/>
       </xsl:element>
      </xsl:if>
     </xsl:if>
     <xsl:if test="$StorageNode/CACHEHINT or $StorageNode/DEFBUFPOOL">
      <xsl:apply-templates select="$StorageNode/CACHEHINT | $StorageNode/DEFBUFPOOL"/>
     </xsl:if>
    </xsl:if>
   </xsl:element>
  </xsl:if>
 </xsl:template>
 <xsl:template match="CACHEHINT | DEFBUFPOOL | BFP_STG">
  <!-- *******************************************************************
Template: CACHEHINT | DEFBUFPOOL - output BUFFER_POOL value
******************************************************************** -->
  <!-- BUFFER_POOL is the least significant 2 bits (bit [0-1]) -->
  <xsl:element name="BUFFER_POOL">
   <xsl:choose>
    <xsl:when test="(. mod 4)=1">KEEP</xsl:when>
    <xsl:when test="(. mod 4)=2">RECYCLE</xsl:when>
    <xsl:otherwise>DEFAULT</xsl:otherwise>
   </xsl:choose>
  </xsl:element>
  <!-- new cachehint FLASH_CACHE/ CELL_FLASH_CACHE requires 11.2 comp -->
  <xsl:if test="$VERSION>=1102000000">
   <!-- FLASH_CACHE is the next 2 bits (bit [2-3]) -->
   <xsl:element name="FLASH_CACHE">
    <xsl:choose>
     <xsl:when test="floor((. mod 16) div 4)=1">KEEP</xsl:when>
     <xsl:when test="floor((. mod 16) div 4)=2">NONE</xsl:when>
     <xsl:otherwise>DEFAULT</xsl:otherwise>
    </xsl:choose>
   </xsl:element>
   <!-- CELL_FLASH_CACHE is the next 2 bits (bit [4-5]) -->
   <xsl:element name="CELL_FLASH_CACHE">
    <xsl:choose>
     <xsl:when test="floor((. mod 64) div 16)=1">KEEP</xsl:when>
     <xsl:when test="floor((. mod 64) div 16)=2">NONE</xsl:when>
     <xsl:otherwise>DEFAULT</xsl:otherwise>
    </xsl:choose>
   </xsl:element>
  </xsl:if>
 </xsl:template>
 <xsl:template name="Tablespace">
  <xsl:param name="ParentNode" select="''"/>
  <!-- *******************************************************************
Template: Tablespace
Parameters:
  ParentNode - Parent node of TS_NAME or DEFTS_NAME
******************************************************************** -->
  <xsl:if test="not ($TABLESPACE=0)">
   <xsl:if test="$ParentNode/TS_NAME or $ParentNode/DEFTS_NAME">
    <xsl:element name="TABLESPACE">
     <xsl:value-of select="$ParentNode/TS_NAME"/>
     <xsl:value-of select="$ParentNode/DEFTS_NAME"/>
    </xsl:element>
   </xsl:if>
  </xsl:if>
 </xsl:template>
 <xsl:template name="LoggingClause">
  <xsl:param name="Partitioned" select="''"/>
  <xsl:param name="FlagsNode" select="''"/>
  <xsl:param name="DefloggingNode" select="''"/>
  <xsl:param name="PartitionType" select="''"/>
  <!-- *******************************************************************
Template: LoggingClause
Parameters:
  Partitioned - 0 = non-partitioned, non-0 = partitioned
  FlagsNode - FLAGS node with logging/nologging bit (tab$.flags)
  DefloggingNode - DEFLOGGING node (partobj$.deflogging)
  PartitionType - RANGE, HASH, LIST, ''
******************************************************************** -->
  <xsl:choose>
   <!-- non-partitioned, either a non-partitioned table or
        a leaf partition -->
   <xsl:when test="$Partitioned=0">
    <xsl:choose>
     <xsl:when test="string-length($PartitionType)=0">
      <xsl:element name="LOGGING">
       <xsl:choose>
        <xsl:when test="($FlagsNode mod 64)>=32">N</xsl:when>
        <xsl:otherwise>Y</xsl:otherwise>
       </xsl:choose>
      </xsl:element>
     </xsl:when>
     <!-- a leaf partition: NoLogging is bit 0x04 in FLAGS=1 (in tabpart$) -->
     <xsl:when test="($FlagsNode mod 8)>=4">
      <xsl:element name="LOGGING">N</xsl:element>
     </xsl:when>
    </xsl:choose>
   </xsl:when>
   <!-- partitioned -->
   <xsl:otherwise>
    <xsl:choose>
     <xsl:when test="$DefloggingNode=1">
      <xsl:element name="LOGGING">Y</xsl:element>
     </xsl:when>
     <xsl:when test="$DefloggingNode=2">
      <xsl:element name="LOGGING">N</xsl:element>
     </xsl:when>
    </xsl:choose>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 <xsl:template name="ParallelClause">
  <xsl:param name="ParentNode" select="''"/>
  <!-- *******************************************************************
Template: ParallelClause
Parameters:
  ParentNode - Parent node of DEGREE
******************************************************************** -->
  <!-- For compatibility support both old and new syntax -->
  <xsl:if test="$ParentNode/DEGREE">
   <xsl:choose>
    <!-- old syntax: PARALLEL (DEGREE n [INSTANCES m])
          For this we use a PARALLEL_DEGREE element -->
    <xsl:when test="32767>($ParentNode/INSTANCES)">
     <xsl:element name="PARALLEL_DEGREE">
      <xsl:choose>
       <xsl:when test="$ParentNode/DEGREE=32767">
        <xsl:element name="DEGREE">DEFAULT</xsl:element>
       </xsl:when>
       <xsl:otherwise>
        <xsl:element name="DEGREE">
         <xsl:value-of select="$ParentNode/DEGREE"/>
        </xsl:element>
       </xsl:otherwise>
      </xsl:choose>
      <xsl:choose>
       <xsl:when test="$ParentNode/INSTANCES=32767">
        <xsl:element name="INSTANCES">DEFAULT</xsl:element>
       </xsl:when>
       <xsl:when test="32767>$ParentNode/INSTANCES">
        <xsl:element name="INSTANCES">
         <xsl:value-of select="$ParentNode/INSTANCES"/>
        </xsl:element>
       </xsl:when>
      </xsl:choose>
     </xsl:element>
    </xsl:when>
    <!-- new syntax PARALLEL [n] -->
    <xsl:otherwise>
     <xsl:element name="PARALLEL">
      <xsl:choose>
       <xsl:when test="32767>($ParentNode/DEGREE)">
        <xsl:value-of select="$ParentNode/DEGREE"/>
       </xsl:when>
       <xsl:otherwise>AUTOMATIC</xsl:otherwise>
      </xsl:choose>
     </xsl:element>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:if>
 </xsl:template>
 <xsl:template name="DoMAXSIZE">
  <xsl:param name="MaxSize">0</xsl:param>
  <xsl:param name="BlkSize">0</xsl:param>
  <!--  *******************************************************************
Template: DoMAXSIZE
Parameters:
 MaxSize: Maxsimum Size
 BlkSize: Block Size
******************************************************************** -->
  <xsl:choose>
   <xsl:when test="$MaxSize=0">
   </xsl:when>
   <xsl:when test="$MaxSize=4294967295 or $MaxSize=2147483645">
   </xsl:when>
   <xsl:otherwise>
    <xsl:element name="MAXSIZE">
     <xsl:choose>
      <xsl:when test="($MaxSize * $BlkSize) >= (2147483646 * 512)">
       <xsl:text>UNLIMITED</xsl:text>
      </xsl:when>
      <xsl:when test="($MaxSize * $BlkSize) > 2147483647">
       <xsl:value-of select="floor(($MaxSize * $BlkSize) div 1048576)"/>
       <xsl:text>M</xsl:text>
      </xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="$MaxSize * $BlkSize"/>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:element>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
 <xsl:template name="DoSegCompress">
  <xsl:param name="Compress">0</xsl:param>
  <!-- *******************************************************************
Template: DoSegCompress - emit [NO]COMPRESS for leaf partition
                          or non-partitioned table
             seg$.spare1:
                   /* #define KTSSEGM_FLAG_COMPRESSED 0x0800    (2048) */
                   /* #define KTSSEGM_FLAG_OLTP  0x1000000  (16777216) */
                   /* #define KTSSEGM_FLAG_ARCH1 0x2000000  (33554432) */
                   /* #define KTSSEGM_FLAG_ARCH2 0x4000000  (67108864) */
                   /* #define KTSSEGM_FLAG_ARCH3 0x8000000 (134217728) */
          (names defined in ktscts.h)
  OLTP compression only valid in 11.1 and later
  archive compression only valid in 11.2 and later
Parameters:
  Compress: Node with the compression bit
******************************************************************** -->
  <xsl:variable name="flagCompressed">2048</xsl:variable>
  <xsl:variable name="isCompressed" select="($Compress mod ($flagCompressed * 2))>=$flagCompressed"/>
  <xsl:variable name="flagOLTPCompressed">16777216</xsl:variable>
  <xsl:variable name="isOLTPCompressed" select="($Compress mod ($flagOLTPCompressed * 2))>=$flagOLTPCompressed"/>
  <xsl:variable name="flagArch1">33554432</xsl:variable>
  <xsl:variable name="compressLevel" select="floor(($Compress mod ($flagArch1 * 8)) div $flagArch1)"/>
  <xsl:variable name="isArchCompressed" select="$compressLevel!=0"/>
  <xsl:call-template name="DoCompressSyntax">
   <xsl:with-param name="isCompressed" select="$isCompressed"/>
   <xsl:with-param name="isOLTPCompressed" select="$isOLTPCompressed"/>
   <xsl:with-param name="isArchCompressed" select="$isArchCompressed"/>
   <xsl:with-param name="compressLevel" select="$compressLevel"/>
  </xsl:call-template>
 </xsl:template>
 <xsl:template name="DoPartOBJCompress">
  <xsl:param name="Compress">0</xsl:param>
  <!-- *******************************************************************
Template: DoPartOBJCompress - emit [NO]COMPRESS default value
          for subpartitions of a composite partitioned table
  [from dpart.bsq]
  /*  compression attribute of the partition */
  /*            following bit patterns are possible: */
  /*            00000000 : Compression not specified */
  /*            00000001 : Compression enabled for direct load operations */
  /*            00000010 : Compression disabled      */
  /*            00000101 : Compression enabled for all operations */
  /*            00001001 : Archive Compression: level 1 */
  /*            00010001 : Archive Compression: level 2 */
  /*            00011001 : Archive Compression: level 3 */
  /*            00100001 : Archive Compression: level 4 */
  /*            00101001 : Archive Compression: level 5 */
  /*            00110001 : Archive Compression: level 6 */
  /*            00111001 : Archive Compression: level 7 */
  /*            All other bit patterns are incorrect. */
  These are stored in byte 4 of partobj$.spare2 or byte 0 of tabcompart$.spare2
Parameters:
  Compress: byte containing the partition compression attribute
******************************************************************** -->
  <xsl:variable name="locCompress" select="floor($Compress)"/>
  <xsl:variable name="flagCompressed">1</xsl:variable>
  <xsl:variable name="isCompressed" select="($locCompress mod ($flagCompressed * 2))>=$flagCompressed"/>
  <xsl:variable name="flagOLTPCompressed">4</xsl:variable>
  <xsl:variable name="isOLTPCompressed" select="($Compress mod ($flagOLTPCompressed * 2))>=$flagOLTPCompressed"/>
  <xsl:variable name="flagArch1">8</xsl:variable>
  <xsl:variable name="compressLevel" select="floor(($locCompress mod ($flagArch1 * 8)) div $flagArch1)"/>
  <xsl:variable name="isArchCompressed" select="$compressLevel!=0"/>
  <!-- emit nothing if compression not specified -->
  <xsl:if test="$locCompress!=0">
   <xsl:call-template name="DoCompressSyntax">
    <xsl:with-param name="isCompressed" select="$isCompressed"/>
    <xsl:with-param name="isOLTPCompressed" select="$isOLTPCompressed"/>
    <xsl:with-param name="isArchCompressed" select="$isArchCompressed"/>
    <xsl:with-param name="compressLevel" select="$compressLevel"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>
 <xsl:template name="DoTSCompress">
  <xsl:param name="Compress">0</xsl:param>
  <!-- *******************************************************************
Template: DoTSCompress - emit [NO]COMPRESS value for tablespaces
             ts$.flags:
              /* #define KTT_COMPRESSED               0x40 (64)              */
              /* #define KTT_OLTP_COMPRESSION   0x10000(65536)   */
#define KTT_ARCH1_COMPRESSION  ((ub4)0x20000)    /* Archive compressed table */
#define KTT_ARCH2_COMPRESSION  ((ub4)0x40000)    /* Archive compressed table */
#define KTT_ARCH3_COMPRESSION  ((ub4)0x80000)    /* Archive compressed table */
Parameters:
  Compress: Node with the compression bits
******************************************************************** -->
  <xsl:variable name="locCompress" select="floor($Compress)"/>
  <xsl:variable name="flagCompressed">64</xsl:variable>
  <xsl:variable name="isCompressed" select="($locCompress mod ($flagCompressed * 2))>=$flagCompressed"/>
  <xsl:variable name="flagOLTPCompressed">65536</xsl:variable>
  <xsl:variable name="isOLTPCompressed" select="($Compress mod ($flagOLTPCompressed * 2))>=$flagOLTPCompressed"/>
  <xsl:variable name="flagArch1">131072</xsl:variable>
  <xsl:variable name="compressLevel" select="floor(($locCompress mod ($flagArch1 * 8)) div $flagArch1)"/>
  <xsl:variable name="isArchCompressed" select="$compressLevel!=0"/>
  <xsl:call-template name="DoCompressSyntax">
   <xsl:with-param name="isCompressed" select="$isCompressed"/>
   <xsl:with-param name="isOLTPCompressed" select="$isOLTPCompressed"/>
   <xsl:with-param name="isArchCompressed" select="$isArchCompressed"/>
   <xsl:with-param name="compressLevel" select="$compressLevel"/>
  </xsl:call-template>
 </xsl:template>
 <xsl:template name="DoCompressSyntax">
  <xsl:param name="isCompressed" select="false()"/>
  <xsl:param name="isOLTPCompressed" select="false()"/>
  <xsl:param name="isArchCompressed" select="false()"/>
  <xsl:param name="compressLevel">0</xsl:param>
  <!-- *******************************************************************
Template: DoCompressSyntax - common template to emit [NO]COMPRESS 
Parameters:
  isCompressed
  isOLTPCompressed
  isArchCompressed - archive compressed
  compressLevel - archive compression level
Implicit input:
  VERSION
Notes (copied from kustorag.xsl):
  Archive level compression is new in 11.2 and has a unique block format
  which is not supported in earlier versions.  Consequently, if VERSION
  is earlier than 11.2, we must specify NOCOMPRESS.
  On the other hand, the block formats for OLTP compression (new in 11.1)
  and DIRECT_LOAD are the same, so if VERSION < 11.1 we can specify COMPRESS.
  (Update: in 11.2 COMPRESS FOR ALL OPERATIONS is renamed COMPRESS FOR OLTP;
   COMPRESS FOR DIRECT_LOAD_OPERATIONS is renamed COMPRESS FOR BASIC.)
******************************************************************** -->
<!-- When segment_attributes are disabled then we should not emit the COMPRESS clause -->
<xsl:if test="$SEGMENT_ATTRIBUTES=1">
  <xsl:choose>
   <xsl:when test=" $isCompressed and $isArchCompressed">
    <xsl:choose>
     <xsl:when test="$VERSION>=1102000000">
      <xsl:choose>
       <xsl:when test="$compressLevel=1 or $compressLevel=2">
        <xsl:element name="COMPRESS">QUERY</xsl:element>
       </xsl:when>
       <xsl:when test="$compressLevel=3 or $compressLevel=4">
        <xsl:element name="COMPRESS">ARCHIVE</xsl:element>
       </xsl:when>
       <xsl:otherwise>
        <xsl:element name="COMPRESS">
         <xsl:text>**INVALID COMPRESSION VALUE (</xsl:text>
         <xsl:value-of select="$compressLevel"/>
         <xsl:text>)** </xsl:text>
        </xsl:element>
       </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="$compressLevel=1 or $compressLevel=3">
       <xsl:element name="COMPRESS_LEVEL">LOW</xsl:element>
      </xsl:if>
     </xsl:when>
     <xsl:otherwise>
      <xsl:element name="COMPRESS">N</xsl:element>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:when>
   <xsl:when test="$isCompressed and $isOLTPCompressed">
    <xsl:choose>
     <xsl:when test="$VERSION>=1102000000">
      <xsl:element name="COMPRESS">OLTP</xsl:element>
     </xsl:when>
     <xsl:when test="$VERSION>=1100000000">
      <xsl:element name="COMPRESS">ALL</xsl:element>
     </xsl:when>
     <xsl:otherwise>
      <xsl:element name="COMPRESS">DIRECT_LOAD</xsl:element>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:when>
   <xsl:when test="$isCompressed">
    <xsl:choose>
     <xsl:when test="$VERSION>=1102000000">
      <xsl:element name="COMPRESS">BASIC</xsl:element>
     </xsl:when>
     <xsl:otherwise>
      <xsl:element name="COMPRESS">DIRECT_LOAD</xsl:element>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:when>
   <xsl:otherwise>
    <xsl:element name="COMPRESS">N</xsl:element>
   </xsl:otherwise>
  </xsl:choose>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>
