<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="alfresco.avm">
    
    <!--                -->
    <!-- Type Defs      -->
    <!--                -->
    
    <typeAlias alias="AVMStore" type="org.alfresco.repo.domain.avm.AVMStoreEntity"/>
    <typeAlias alias="AVMNode" type="org.alfresco.repo.domain.avm.AVMNodeEntity"/>
    <typeAlias alias="AVMNodeAspect" type="org.alfresco.repo.domain.avm.AVMAspectEntity"/>
    <typeAlias alias="AVMChildEntry" type="org.alfresco.repo.domain.avm.AVMChildEntryEntity"/>
    <typeAlias alias="AVMVersionRoot" type="org.alfresco.repo.domain.avm.AVMVersionRootEntity"/>
    <typeAlias alias="AVMHistoryLink" type="org.alfresco.repo.domain.avm.AVMHistoryLinkEntity"/>
    <typeAlias alias="AVMMergeLink" type="org.alfresco.repo.domain.avm.AVMMergeLinkEntity"/>
    <typeAlias alias="AVMVersionLayeredNodeEntry" type="org.alfresco.repo.domain.avm.AVMVersionLayeredNodeEntryEntity"/>
    <typeAlias alias="AVMStoreProperty" type="org.alfresco.repo.domain.avm.AVMStorePropertyEntity"/>
    <typeAlias alias="AVMNodeProperty" type="org.alfresco.repo.domain.avm.AVMNodePropertyEntity"/>
    
    
    <!--                -->
    <!-- Result Maps    -->
    <!--                -->
    
    <resultMap id="result_AVMStore" class="AVMStore">
        <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="name" column="name" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="nextVersion" column="next_version_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="rootNodeId" column="current_root_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="aclId" column="acl_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="vers" column="vers" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </resultMap>
    
    <resultMap id="result_AVMStoreProperty" class="AVMStoreProperty">
        <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="avmStoreId" column="avm_store_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="qnameId" column="qname_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="actualType" column="actual_type_n" jdbcType="INT" javaType="java.lang.Integer"/>
        <result property="persistedType" column="persisted_type_n" jdbcType="INT" javaType="java.lang.Integer"/>
        <result property="isMultiValued" column="multi_valued" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <result property="booleanValue" column="boolean_value" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <result property="longValue" column="long_value" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="floatValue" column="float_value" jdbcType="FLOAT" javaType="java.lang.Float"/>
        <result property="doubleValue" column="double_value" jdbcType="DOUBLE" javaType="java.lang.Double"/>
        <result property="stringValue" column="string_value" jdbcType="TEXT" javaType="java.lang.String"/>
        <result property="serializableValue" column="serializable_value" jdbcType="BLOB" javaType="java.io.Serializable"/>
    </resultMap>
    
    <resultMap id="result_AVMNode" class="AVMNode">
        <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="storeNewId" column="store_new_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="aclId" column="acl_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="layerId" column="layer_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="version" column="version_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="classType" column="class_type" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="guid" column="guid" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="owner" column="owner" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="creator" column="creator" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="createdDate" column="createDate" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="modifier" column="lastModifier" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="modifiedDate" column="modDate" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="accessDate" column="accessDate" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="isRoot" column="is_root" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <result property="deletedType" column="deletedType" jdbcType="INT" javaType="java.lang.Integer"/>
        <result property="indirection" column="indirection" jdbcType="TEXT" javaType="java.lang.String"/>
        <result property="indirectionVersion" column="indirection_version" jdbcType="INT" javaType="java.lang.Integer"/>
        <result property="primaryIndirection" column="primary_indirection" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <result property="opacity" column="opacity" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <result property="contentUrl" column="content_url" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="mimetype" column="mime_type" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="encoding" column="encoding" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="length" column="length" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="vers" column="vers" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </resultMap>
    
    <resultMap id="result_AVMNodeProperty" class="AVMNodeProperty">
        <result property="nodeId" column="node_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="qnameId" column="qname_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="actualType" column="actual_type_n" jdbcType="INT" javaType="java.lang.Integer"/>
        <result property="persistedType" column="persisted_type_n" jdbcType="INT" javaType="java.lang.Integer"/>
        <result property="isMultiValued" column="multi_valued" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <result property="booleanValue" column="boolean_value" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <result property="longValue" column="long_value" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="floatValue" column="float_value" jdbcType="FLOAT" javaType="java.lang.Float"/>
        <result property="doubleValue" column="double_value" jdbcType="DOUBLE" javaType="java.lang.Double"/>
        <result property="stringValue" column="string_value" jdbcType="TEXT" javaType="java.lang.String"/>
        <result property="serializableValue" column="serializable_value" jdbcType="BLOB" javaType="java.io.Serializable"/>
    </resultMap>
    
    <resultMap id="result_AVMNodeAspect" class="AVMNodeAspect">
        <result property="nodeId" column="node_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="qnameId" column="qname_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </resultMap>
    
    <resultMap id="result_AVMChildEntry" class="AVMChildEntry">
        <result property="parentNodeId" column="parent_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="name" column="name" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="childNodeId" column="child_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </resultMap>
    
    <resultMap id="result_AVMVersionRoot" class="AVMVersionRoot">
        <result property="id" column="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="storeId" column="avm_store_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="rootNodeId" column="root_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="version" column="version_id" jdbcType="INT" javaType="java.lang.Integer"/>
        <result property="creator" column="creator" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="createdDate" column="create_date" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="tag" column="tag" jdbcType="TEXT" javaType="java.lang.String"/>
        <result property="description" column="description" jdbcType="TEXT" javaType="java.lang.String"/>
    </resultMap>
    
    <resultMap id="result_AVMVersionLayeredNodeEntry" class="AVMVersionLayeredNodeEntry">
        <result property="versionRootId" column="version_root_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="md5sum" column="md5sum" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result property="path" column="path" jdbcType="TEXT" javaType="java.lang.String"/>
    </resultMap>
    
    <resultMap id="result_AVMHistoryLink" class="AVMHistoryLink">
        <result property="ancestorNodeId" column="ancestor" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="descendentNodeId" column="descendent" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </resultMap>
    
    <resultMap id="result_AVMMergeLink" class="AVMMergeLink">
        <result property="mergeFromNodeId" column="mfrom" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <result property="mergeToNodeId" column="mto" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </resultMap>
    
    <!--                -->
    <!-- Parameter Maps -->
    <!--                -->
    
    <parameterMap id="parameter_IdMap" class="map">
        <parameter property="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
    
    <parameterMap id="parameter_BoolMap" class="map">
        <parameter property="bool" jdbcType="BIT" javaType="java.lang.Boolean"/>
    </parameterMap>
    
    <parameterMap id="parameter_IdFromMap" class="map">
        <parameter property="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="from" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
    
    <parameterMap id="parameter_IdToMap" class="map">
        <parameter property="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="to" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
    
    <parameterMap id="parameter_IdFromToMap" class="map">
        <parameter property="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="from" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="to" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
    
    <parameterMap id="parameter_KeyPattern" class="map">
        <parameter property="uri" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <parameter property="localname" jdbcType="VARCHAR" javaType="java.lang.String"/>
    </parameterMap>
    
    <parameterMap id="parameter_IdAndKeyPattern" class="map">
        <parameter property="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="uri" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <parameter property="localname" jdbcType="VARCHAR" javaType="java.lang.String"/>
    </parameterMap>
    
    <parameterMap id="parameter_IdPatternMap" class="map">
        <parameter property="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="pattern" jdbcType="VARCHAR" javaType="java.lang.String"/>
    </parameterMap>
    
    <parameterMap id="parameter_NodePropertyPatternMap" class="AVMNodeProperty">
        <parameter property="actualType" jdbcType="INT" javaType="java.lang.Integer"/>
        <parameter property="persistedType" jdbcType="INT" javaType="java.lang.Integer"/>
        <parameter property="isMultiValued" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <parameter property="booleanValue" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <parameter property="longValue" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="floatValue" jdbcType="FLOAT" javaType="java.lang.Float"/>
        <parameter property="doubleValue" jdbcType="DOUBLE" javaType="java.lang.Double"/>
        <parameter property="stringValue" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <parameter property="serializableValue" jdbcType="BLOB" javaType="java.io.Serializable"/>
        <parameter property="nodeId" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="qnameId" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
    
    <parameterMap id="parameter_StorePropertyPatternMap" class="AVMStoreProperty">
        <parameter property="actualType" jdbcType="INT" javaType="java.lang.Integer"/>
        <parameter property="persistedType" jdbcType="INT" javaType="java.lang.Integer"/>
        <parameter property="isMultiValued" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <parameter property="booleanValue" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <parameter property="longValue" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="floatValue" jdbcType="FLOAT" javaType="java.lang.Float"/>
        <parameter property="doubleValue" jdbcType="DOUBLE" javaType="java.lang.Double"/>
        <parameter property="stringValue" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <parameter property="serializableValue" jdbcType="BLOB" javaType="java.io.Serializable"/>
        <parameter property="avmStoreId" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="qnameId" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
    
    <parameterMap id="parameter_IdStorePropertyPatternMap" class="AVMStoreProperty">
        <parameter property="id" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="actualType" jdbcType="INT" javaType="java.lang.Integer"/>
        <parameter property="persistedType" jdbcType="INT" javaType="java.lang.Integer"/>
        <parameter property="isMultiValued" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <parameter property="booleanValue" jdbcType="BIT" javaType="java.lang.Boolean"/>
        <parameter property="longValue" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="floatValue" jdbcType="FLOAT" javaType="java.lang.Float"/>
        <parameter property="doubleValue" jdbcType="DOUBLE" javaType="java.lang.Double"/>
        <parameter property="stringValue" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <parameter property="serializableValue" jdbcType="BLOB" javaType="java.io.Serializable"/>
        <parameter property="avmStoreId" jdbcType="BIGINT" javaType="java.lang.Long"/>
        <parameter property="qnameId" jdbcType="BIGINT" javaType="java.lang.Long"/>
    </parameterMap>
    
    
    <!--                -->
    <!-- SQL Snippets   -->
    <!--                -->
    
    <sql id="insert_AVMStore_AutoIncrement">
        insert into avm_stores (next_version_id, name, vers, current_root_id, acl_id)
        values (#version#, #name#, 0, null, null)
    </sql>
    
    <sql id="insert_AVMNode_AutoIncrement">
        insert into avm_nodes (class_type, deletedType, version_id, vers, store_new_id, acl_id, guid, owner, creator, createDate, lastModifier, modDate, accessDate, is_root, layer_id, indirection, indirection_version, primary_indirection, opacity, content_url, mime_type, encoding, length)
        values (#classType#, #deletedType#, #version#, 0, #storeNewId#, #aclId#, #guid#, #owner#, #creator#, #createdDate#, #modifier#, #modifiedDate#, #accessDate#, #isRoot#, #layerId#, #indirection#, #indirectionVersion#, #primaryIndirection#, #opacity#, #contentUrl#, #mimetype#, #encoding#, #length#)
    </sql>
    
    <sql id="insert_AVMVersionRoot_AutoIncrement">
        insert into avm_version_roots (avm_store_id, root_id, version_id, creator, create_date, tag, description)
        values (#storeId#, #rootNodeId#, #version#, #creator#, #createdDate#, #tag#, #description#)
    </sql>
    
    <sql id="insert_AVMStoreProperty_AutoIncrement">
        insert into avm_store_properties (actual_type_n, persisted_type_n, multi_valued, boolean_value, long_value, float_value, double_value, string_value, serializable_value, avm_store_id, qname_id)
        values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    </sql>
    
    
    <!--                   -->
    <!-- Insert Statements -->
    <!--                    -->
    
    <insert id="insert_AVMNodeAspect">
        insert into avm_aspects (node_id, qname_id)
        values (#nodeId#, #qnameId#)
    </insert>
    
    <insert id="insert_AVMChildEntry">
        insert into avm_child_entries (parent_id, name, child_id)
        values (#parentNodeId#, #name#, #childNodeId#)
    </insert>
    
    <insert id="insert_AVMHistoryLink">
        insert into avm_history_links (ancestor, descendent)
        values (#ancestorNodeId#, #descendentNodeId#)
    </insert>
    
    <insert id="insert_AVMMergeLink">
        insert into avm_merge_links (mfrom, mto)
        values (#mergeFromNodeId#, #mergeToNodeId#)
    </insert>
    
    <insert id="insert_AVMVersionLayeredNodeEntry">
        insert into avm_version_layered_node_entry (version_root_id, md5sum, path)
        values (#versionRootId#, #md5sum#, #path#)
    </insert>

    <insert id="insert_AVMNodeProperty" parameterMap="parameter_NodePropertyPatternMap">
        insert into avm_node_properties (actual_type_n, persisted_type_n, multi_valued, boolean_value, long_value, float_value, double_value, string_value, serializable_value, node_id, qname_id)
        values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    </insert>

    <!--                      -->
    <!-- AVM Store Statements -->
    <!--                      -->
    
    <!-- Get AVMStore by name -->
    <select id="select_AVMStoreByKey" parameterClass="AVMStore" resultMap="result_AVMStore">
        select
            *
        from
            avm_stores
        where
            name = #name#
    </select>
    
    <select id="select_AVMStoreByKeyL" parameterClass="AVMStore" resultMap="result_AVMStore">
        select
            *
        from
            avm_stores
        where
            lower(name) = lower(#name#)
    </select>
    
    <!-- Get AVMStore by ID -->
    <select id="select_AVMStoreById" parameterMap="parameter_IdMap" resultMap="result_AVMStore">
        select
            *
        from
            avm_stores
        where
            id = ?
    </select>
    
    <!-- Get AVMStore by root Node ID -->
    <select id="select_AVMStoreByRootNodeId" parameterMap="parameter_IdMap" resultMap="result_AVMStore">
        select
            *
        from
            avm_stores
        where
            current_root_id = ?
    </select>
    
    <!-- Get all AVMStores -->
    <select id="select_AVMStoreAll" resultMap="result_AVMStore">
        select
            *
        from
            avm_stores
    </select>
    
    <!-- Optimistic update of the store -->
    <update id="update_AVMStore" parameterClass="AVMStore">
        update
            avm_stores
        set
            next_version_id = #nextVersion#,
            name = #name#,
            current_root_id = #rootNodeId#,
            acl_id = #aclId#,
            vers = #vers#
        where
            id = #id#
        <isGreaterThan property="vers" compareValue="1">
            and vers = (#vers#-1)
        </isGreaterThan>
    </update>
    
    <!-- Delete AVMStore -->
    <delete id="delete_AVMStore" parameterMap="parameter_IdMap">
        delete
        from
            avm_stores
        where
            id = ?
    </delete>
    
    <!--                               -->
    <!-- AVM Store Property Statements -->
    <!--                               -->
    
    <select id="select_AVMStoreProperty" parameterClass="AVMStoreProperty" resultMap="result_AVMStoreProperty">
        select
            *
        from
            avm_store_properties
        where
            avm_store_id = #avmStoreId# and
            qname_id = #qnameId#
    </select>
    
    <select id="select_AVMStoreProperties" parameterMap="parameter_IdMap" resultMap="result_AVMStoreProperty">
        select
            *
        from
            avm_store_properties
        where
            avm_store_id = ?
    </select>
    
    <select id="select_AVMStorePropertiesByKeyPattern" parameterMap="parameter_KeyPattern" resultMap="result_AVMStoreProperty">
        select
            asp.*
        from
            avm_store_properties asp
            join alf_qname qn on (qn.id = asp.qname_id)
            join alf_namespace ns on (ns.id = qn.ns_id)
        where
            ns.uri like ? and
            qn.local_name like ?
    </select>
    
    <select id="select_AVMStorePropertiesByKeyPatternL" parameterMap="parameter_KeyPattern" resultMap="result_AVMStoreProperty">
        select
            asp.*
        from
            avm_store_properties asp
            join alf_qname qn on (qn.id = asp.qname_id)
            join alf_namespace ns on (ns.id = qn.ns_id)
        where
            ns.uri like ? and
            lower(qn.local_name) like lower(?)
    </select>
    
    <select id="select_AVMStorePropertiesByStoreAndKeyPattern" parameterMap="parameter_IdAndKeyPattern" resultMap="result_AVMStoreProperty">
        select
            asp.*
        from
            avm_store_properties asp
            join alf_qname qn on (qn.id = asp.qname_id)
            join alf_namespace ns on (ns.id = qn.ns_id)
        where
            asp.avm_store_id = ? and
            ns.uri like ? and
            qn.local_name like ?
    </select>
    
    <select id="select_AVMStorePropertiesByStoreAndKeyPatternL" parameterMap="parameter_IdAndKeyPattern" resultMap="result_AVMStoreProperty">
        select
            asp.*
        from
            avm_store_properties asp
            join alf_qname qn on (qn.id = asp.qname_id)
            join alf_namespace ns on (ns.id = qn.ns_id)
        where
            asp.avm_store_id = ? and
            ns.uri like ? and
            lower(qn.local_name) like lower(?)
    </select>
    
    <!-- Optimistic update of the store property -->
    <update id="update_AVMStoreProperty" parameterMap="parameter_StorePropertyPatternMap">
        update
            avm_store_properties
        set
            actual_type_n = ?,
            persisted_type_n = ?,
            multi_valued = ?,
            boolean_value = ?,
            long_value = ?,
            float_value = ?,
            double_value = ?,
            string_value = ?,
            serializable_value = ?
        where
            avm_store_id = ? and
            qname_id = ?
    </update>
    
    <delete id="delete_AVMStoreProperty" parameterClass="AVMStoreProperty">
        delete
        from
            avm_store_properties
        where
            avm_store_id = #avmStoreId# and
            qname_id = #qnameId#
    </delete>
    
    <delete id="delete_AVMStoreProperties" parameterMap="parameter_IdMap">
        delete
        from
            avm_store_properties
        where
            avm_store_id = ?
    </delete>
    
    <!--                     -->
    <!-- AVM Node Statements -->
    <!--                     -->
    
    <select id="select_AVMNodeById" parameterMap="parameter_IdMap" resultMap="result_AVMNode">
        select
            *
        from
            avm_nodes
        where
            id = ?
    </select>
    
    <select id="select_AVMNodes_newInStore" parameterMap="parameter_IdMap" resultMap="result_AVMNode">
        select
            *
        from
            avm_nodes
        where
            store_new_id = ?
    </select>
    
    <select id="select_AVMNodes_IDs_newLayeredInStore" parameterMap="parameter_IdMap" resultClass="long">
        select
            id
        from
            avm_nodes
        where
            store_new_id = ? and
            (class_type = 'layereddirectory' or class_type = 'layeredfile')
    </select>
    
    <select id="select_AVMNodes_newLayeredInStore" parameterMap="parameter_IdMap" resultMap="result_AVMNode">
        select
            *
        from
            avm_nodes
        where
            (class_type = 'layereddirectory' or class_type = 'layeredfile') and
            store_new_id = ?
    </select>
    
    <select id="select_AVMNodes_layeredDirectoriesAndPrimaryTF" parameterMap="parameter_BoolMap" resultMap="result_AVMNode">
        select
            *
        from 
            avm_nodes
        where
            class_type = 'layereddirectory' and
            primary_indirection = ?
    </select>
    
    <select id="select_AVMNodes_layeredFiles" resultMap="result_AVMNode">
        select
            *
        from
            avm_nodes
        where
            class_type = 'layeredfile'
    </select>
    
    <select id="select_AVMNodes_IDs_byAcl" parameterMap="parameter_IdMap" resultClass="long">
        select
            id
        from
            avm_nodes
        where
            acl_id = ?
    </select>
    
        <select id="select_ContentUrlsForPlainFiles" resultClass="String">
        select
           content_url
        from
            avm_nodes
        where
            class_type = 'plainfile' and
            content_url is not null
    </select>
    
    <update id="update_AVMNodes_clearNewInStore" parameterMap="parameter_IdMap">
        update
            avm_nodes
        set
            store_new_id = null
        where
            store_new_id = ?
    </update>
    
    <!-- Optimistic update of the node -->
    <update id="update_AVMNode" parameterClass="AVMNode">
        update
            avm_nodes
        set
            store_new_id = #storeNewId#,
            acl_id = #aclId#,
            layer_id = #layerId#,
            version_id = #version#,
            class_type = #classType#,
            deletedType = #deletedType#,
            owner = #owner#,
            modDate = #modifiedDate#,
            accessDate = #accessDate#,
            is_root = #isRoot#,
            lastModifier = #modifier#,
            indirection = #indirection#,
            indirection_version = #indirectionVersion#,
            primary_indirection = #primaryIndirection#,
            opacity = #opacity#,
            content_url = #contentUrl#,
            mime_type = #mimetype#,
            encoding = #encoding#,
            length = #length#,
            guid = #guid#,
            vers = #vers#
        where
            id = #id#
        <isGreaterThan property="vers" compareValue="1">
            and vers = (#vers#-1)
        </isGreaterThan>
    </update>
    
    <update id="update_AVMNode_modTimeAndGuid" parameterClass="AVMNode">
        update
            avm_nodes
        set
            modDate = #modifiedDate#,
            guid = #guid#,
            vers = #vers#
        where
            id = #id#
        <isGreaterThan property="vers" compareValue="1">
            and vers = (#vers#-1)
        </isGreaterThan>
    </update>
    
    <update id="update_AVMNode_modTimeAndContentData" parameterClass="AVMNode">
        update
            avm_nodes
        set
            modDate = #modifiedDate#,
            content_url = #contentUrl#,
            mime_type = #mimetype#,
            encoding = #encoding#,
            length = #length#,
            vers = #vers#
        where
            id = #id#
        <isGreaterThan property="vers" compareValue="1">
            and vers = (#vers#-1)
        </isGreaterThan>
    </update>
    
    <delete id="delete_AVMNode" parameterMap="parameter_IdMap">
        delete
        from
            avm_nodes
        where
            id = ?
    </delete>
    
    <!--                              -->
    <!-- AVM Node Property Statements -->
    <!--                              -->
    
    <select id="select_AVMNodeProperty" parameterClass="AVMNodeProperty" resultMap="result_AVMNodeProperty">
        select
            *
        from
            avm_node_properties
        where
            node_id = #nodeId# and
            qname_id = #qnameId#
    </select>
    
    <select id="select_AVMNodeProperties" parameterMap="parameter_IdMap" resultMap="result_AVMNodeProperty">
        select
            *
        from
            avm_node_properties
        where
            node_id = ?
    </select>
    
    <!-- Optimistic update of the node property -->
    <update id="update_AVMNodeProperty" parameterMap="parameter_NodePropertyPatternMap">
        update
            avm_node_properties
        set
            actual_type_n = ?,
            persisted_type_n = ?,
            multi_valued = ?,
            boolean_value = ?,
            long_value = ?,
            float_value = ?,
            double_value = ?,
            string_value = ?,
            serializable_value = ?
        where
            node_id = ? and
            qname_id = ?
    </update>
    
    <delete id="delete_AVMNodeProperty" parameterClass="AVMNodeProperty">
        delete
        from
            avm_node_properties
        where
            node_id = #nodeId# and
            qname_id = #qnameId#
    </delete>
    
    <delete id="delete_AVMNodeProperties" parameterMap="parameter_IdMap">
        delete
        from
            avm_node_properties
        where
            node_id = ?
    </delete>
    
    <!--                            -->
    <!-- AVM VersionRoot Statements -->
    <!--                            -->
    
    <select id="select_AVMVersionRootMaxVersionID" parameterMap="parameter_IdMap" resultClass="int">
        select
             max(version_id)
        from
            avm_version_roots
        where
            avm_store_id = ?
    </select>
    
    <select id="select_AVMVersionRootMaxVersion" parameterClass="AVMStore" resultMap="result_AVMVersionRoot">
        select
            *
        from
            avm_version_roots
        where
            avm_store_id = #id#
            and version_id = (select 
                                  max(vr.version_id)
                              from 
                                  avm_version_roots vr
                              where 
                                  vr.avm_store_id = #id#
                             )
    </select>
    
    <select id="select_AVMVersionRootById" parameterMap="parameter_IdMap" resultMap="result_AVMVersionRoot">
        select
            *
        from
            avm_version_roots
        where
            id = ?
    </select>
    
    <select id="select_AVMVersionRootByRootNodeId" parameterMap="parameter_IdMap" resultMap="result_AVMVersionRoot">
        select
            *
        from
            avm_version_roots
        where
            root_id = ?
    </select>
    
    <select id="select_AVMVersionRootByStoreVersion" parameterClass="AVMVersionRoot" resultMap="result_AVMVersionRoot">
        select
            *
        from
            avm_version_roots
        where
            avm_store_id = #storeId# and
            version_id = #version#
    </select>
    
    <select id="select_AVMVersionRootsByStoreId" parameterMap="parameter_IdMap" resultMap="result_AVMVersionRoot">
        select
            *
        from
            avm_version_roots
        where
            avm_store_id = ?
        order by version_id
    </select>
    
    <select id="select_AVMVersionRootsByTo" parameterMap="parameter_IdToMap" resultMap="result_AVMVersionRoot">
        <![CDATA[
        select
            *
        from
            avm_version_roots
        where
            avm_store_id = ? and
            create_date <= ?
        order by version_id
        ]]>
    </select>
    
    <select id="select_AVMVersionRootsByFrom" parameterMap="parameter_IdFromMap" resultMap="result_AVMVersionRoot">
        select
            *
        from
            avm_version_roots
        where
            avm_store_id = ? and
            create_date >= ?
        order by version_id
    </select>
    
    <select id="select_AVMVersionRootsBetween" parameterMap="parameter_IdFromToMap" resultMap="result_AVMVersionRoot">
        select
            *
        from
            avm_version_roots
        where
            avm_store_id = ? and
            create_date between ? and ?
        order by version_id
    </select>
    
    <!-- by version ids -->
    
    <select id="select_AVMVersionRootsByVersionTo" parameterMap="parameter_IdToMap" resultMap="result_AVMVersionRoot">
        <![CDATA[
        select
            *
        from
            avm_version_roots
        where
            avm_store_id = ? and
            version_id <= ?
        order by version_id
        ]]>
    </select>
    
    <select id="select_AVMVersionRootsByVersionFrom" parameterMap="parameter_IdFromMap" resultMap="result_AVMVersionRoot">
        select
            *
        from
            avm_version_roots
        where
            avm_store_id = ? and
            version_id >= ?
        order by version_id
    </select>
    
    <select id="select_AVMVersionRootsByVersionsBetween" parameterMap="parameter_IdFromToMap" resultMap="result_AVMVersionRoot">
        select
            *
        from
            avm_version_roots
        where
            avm_store_id = ? and
            version_id between ? and ?
        order by version_id
    </select>
    
    <!-- Optimistic update of the version root -->
    <update id="update_AVMVersionRoot" parameterClass="AVMVersionRoot">
        update
            avm_version_roots
        set
            tag = #tag#,
            description = #description#
        where
            id = #id#
    </update>
    
    <delete id="delete_AVMVersionRoot" parameterMap="parameter_IdMap">
        delete
        from
            avm_version_roots
        where
            id = ?
    </delete>
    
    <!--                                        -->
    <!-- AVM VersionLayeredNodeEntry Statements -->
    <!--                                        -->
    
    <select id="select_AVMVersionLayeredNodeEntries" parameterMap="parameter_IdMap" resultMap="result_AVMVersionLayeredNodeEntry">
        select
            *
        from
            avm_version_layered_node_entry
        where
            version_root_id = ?
    </select>
    
    <delete id="delete_AVMVersionLayeredNodeEntries" parameterMap="parameter_IdMap">
        delete
        from
            avm_version_layered_node_entry
        where
            version_root_id = ?
    </delete>
    
    <!--                            -->
    <!-- AVM Node Aspect Statements -->
    <!--                            -->
    
    <!-- Get AVMNodeAspects for given AVMNode ID -->
    <select id="select_AVMNodeAspects" parameterMap="parameter_IdMap" resultClass="long">
        select
            qname_id
        from
            avm_aspects
        where
            node_id = ?
    </select>
    
    <!-- Delete AVMNodeAspect -->
    <delete id="delete_AVMNodeAspect" parameterClass="AVMNodeAspect">
        delete
        from
            avm_aspects
        where
            node_id = #nodeId# and
            qname_id = #qnameId#
    </delete>
    
    <delete id="delete_AVMNodeAspects" parameterMap="parameter_IdMap">
        delete
        from
            avm_aspects
        where
            node_id = ?
    </delete>
    
    <!--                           -->
    <!-- AVM ChildEntry Statements -->
    <!--                           -->
    
    <!-- Get AVMChildEntry -->
    <select id="select_AVMChildEntry" parameterClass="AVMChildEntry" resultMap="result_AVMChildEntry">
        select
            *
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            name = #name# and
            child_id = #childNodeId#
    </select>
    
    <select id="select_AVMChildEntryL" parameterClass="AVMChildEntry" resultMap="result_AVMChildEntry">
        select
            *
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            lower(name) = lower(#name#) and
            child_id = #childNodeId#
    </select>
    
    <!-- Get AVMNodeChildEntries by parent node ID -->
    <select id="select_AVMNodeChildEntriesByParent" parameterMap="parameter_IdMap" resultMap="result_AVMChildEntry">
        select
            *
        from
            avm_child_entries
        where
            parent_id = ?
    </select>
    
    <!-- Get AVMNodeChildEntries by parent node ID and name pattern -->
    <select id="select_AVMNodeChildEntriesByParentAndNamePattern" parameterMap="parameter_IdPatternMap" resultMap="result_AVMChildEntry">
        select
            *
        from
            avm_child_entries
        where
            parent_id = ? and
            name like ?
    </select>
    
    <select id="select_AVMNodeChildEntriesByParentAndNamePatternL" parameterMap="parameter_IdPatternMap" resultMap="result_AVMChildEntry">
        select
            *
        from
            avm_child_entries
        where
            parent_id = ? and
            lower(name) like lower(?)
    </select>
    
    <!-- Get AVMNodeChildEntries by child node ID -->
    <select id="select_AVMNodeChildEntriesByChild" parameterMap="parameter_IdMap" resultMap="result_AVMChildEntry">
        select
            *
        from
            avm_child_entries
        where
            child_id = ?
    </select>
    
    <!-- Get AVMChildEntry by parent node ID and name -->
    <select id="select_AVMChildEntryByParentAndName" parameterClass="AVMChildEntry" resultMap="result_AVMChildEntry">
        select
            *
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            name = #name#
    </select>
    
    <select id="select_AVMChildEntryByParentAndNameL" parameterClass="AVMChildEntry" resultMap="result_AVMChildEntry">
        select
            *
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            lower(name) = lower(#name#)
    </select>
    
    <!-- Get AVMChildEntry by parent node ID and child node ID -->
    <select id="select_AVMChildEntryByParentAndChild" parameterClass="AVMChildEntry" resultMap="result_AVMChildEntry">
        select
            *
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            child_id = #childNodeId#
    </select>
    
    <!-- Remove AVMChildEntry -->
    <!-- unused
    <delete id="delete_AVMChildEntry" parameterClass="AVMChildEntry">
        delete
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            name = #name# and
            child_id = #childNodeId#
    </delete>
    
    <delete id="delete_AVMChildEntryL" parameterClass="AVMChildEntry">
        delete
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            lower(name) = lower(#name#) and
            child_id = #childNodeId#
    </delete>
    -->

    <!-- Update child entry name (currently used for specific rename 'case' only) -->
    <update id="update_AVMChildEntry" parameterClass="AVMChildEntry">
        update
            avm_child_entries
        set
            name = #name#
        where
            parent_id = #parentNodeId# and
            child_id = #childNodeId#
    </update>
    
    <!-- Remove AVMChildEntry by parent node ID and name -->
    <delete id="delete_AVMChildEntryByParentAndName" parameterClass="AVMChildEntry">
        delete
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            name = #name#
    </delete>
    
    <delete id="delete_AVMChildEntryByParentAndNameL" parameterClass="AVMChildEntry">
        delete
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            lower(name) = lower(#name#)
    </delete>
    
    <!-- Remove AVMChildEntry by parent node ID and child node ID -->
    <delete id="delete_AVMChildEntryByParentAndChild" parameterClass="AVMChildEntry">
        delete
        from
            avm_child_entries
        where
            parent_id = #parentNodeId# and
            child_id = #childNodeId#
    </delete>
    
    <!-- Remove AVMNodeChildEntries -->
    <delete id="delete_AVMNodeChildEntriesByParent" parameterMap="parameter_IdMap">
        delete
        from
            avm_child_entries
        where
            parent_id = ?
    </delete>
    
    <select id="select_AVMNodes_nullParentAndIsRootTF" parameterMap="parameter_BoolMap" resultMap="result_AVMNode">
        select
            an.*
        from
            avm_child_entries ce
            right outer join avm_nodes an on (an.id = ce.child_id)
        where 
            ce.parent_id is null and 
            an.is_root = ?
    </select>
    
    <!--                          -->
    <!-- AVM MergeLink Statements -->
    <!--                          -->
    
    <select id="select_AVMMergeLinksByFrom" parameterMap="parameter_IdMap" resultMap="result_AVMMergeLink">
        select
            *
        from
            avm_merge_links
        where
            mfrom = ?
    </select>
    
    <select id="select_AVMMergeLinkByTo" parameterMap="parameter_IdMap" resultMap="result_AVMMergeLink">
        select
            *
        from
            avm_merge_links
        where
            mto = ?
    </select>
    
    <delete id="delete_AVMMergeLink" parameterClass="AVMMergeLink">
        delete
        from
            avm_merge_links
        where
            mfrom = #mergeFromNodeId# and
            mto = #mergeToNodeId#
    </delete>
    
    <!--                            -->
    <!-- AVM HistoryLink Statements -->
    <!--                            -->
    
    <select id="select_AVMHistoryLinksByAncestor" parameterMap="parameter_IdMap" resultMap="result_AVMHistoryLink">
        select
            *
        from
            avm_history_links
        where
            ancestor = ?
    </select>
    
    <select id="select_AVMHistoryLinkByDescendent" parameterMap="parameter_IdMap" resultMap="result_AVMHistoryLink">
        select
            *
        from
            avm_history_links
        where
            descendent = ?
    </select>
    
    <select id="select_AVMHistoryLink" parameterClass="AVMHistoryLink" resultMap="result_AVMHistoryLink">
        select
            *
        from
            avm_history_links
        where
            ancestor = #ancestorNodeId# and
            descendent = #descendentNodeId#
    </select>
    
    <delete id="delete_AVMHistoryLink" parameterClass="AVMHistoryLink">
        delete
        from
            avm_history_links
        where
            ancestor = #ancestorNodeId# and
            descendent = #descendentNodeId#
    </delete>
    
    
    <!-- For patches -->
    
    <select id="select_AVMNodeEntitiesCountWhereNewInStore">
        select 
            count(*)
        from 
            avm_nodes node
        where 
            node.store_new_id is not null
    </select>
    
    <select id="select_AVMNodesWithEmptyGUID" resultMap="result_AVMNode">
        select
            id
        from
            avm_nodes
        where
            guid is null
    </select>
    
    <select id="select_AVMNodes_nullVersionLayeredDirectories" resultMap="result_AVMNode">
        select
            *
        from 
            avm_nodes
        where 
            indirection_version is null and
            class_type = 'layereddirectory'
    </select>
    
    <!-- for patch -->
    <select id="select_AVMNodes_nullVersionLayeredFiles" resultMap="result_AVMNode">
        select
            *
        from 
            avm_nodes
        where 
            indirection_version is null and
            class_type = 'layeredfile'
    </select>
    
</sqlMap>