<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://projets.admisource.gouv.fr/xemelios/technical/schemas/xsd2html.xsl"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="persistence-config">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="layer" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="layer">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="document" type="TDocument" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="name" type="xs:NCName" use="required"/>
      <xs:attribute name="persistence-model" type="xs:NCName" use="required" fixed="xemelios-sql"/>
    </xs:complexType>
  </xs:element>
  
    <xs:complexType name="TDocument">
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="table" type="TTable"/>
        <xs:element maxOccurs="unbounded" name="etat" type="TEtat"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:ID" use="required"/>
      <xs:attribute name="repository-import-xslt-file" type="xs:anyURI" use="required"/>
      <xs:attribute name="collectivite-code-colname" type="xs:string" use="optional" default="COLLECTIVITE"/>
      <xs:attribute name="collectivite-lib-colname" type="xs:string" use="optional" default="COLLECTIVITE_LIB"/>
      <xs:attribute name="budget-code-colname" type="xs:string" use="optional" default="BUDGET"/>
      <xs:attribute name="budget-lib-colname" type="xs:string" use="optional" default="BUDGET_LIB"/>
    </xs:complexType>

    <xs:complexType name="TEtat">
      <xs:sequence>
        <xs:element minOccurs="0" name="key1" type="TKey"/>
        <xs:element minOccurs="0" name="key2" type="TKey"/>
        <xs:element minOccurs="0" name="key3" type="TKey"/>
        <xs:element minOccurs="0" name="key4" type="TKey"/>
        <xs:element minOccurs="0" name="key5" type="TKey"/>
        <xs:element minOccurs="0" name="key6" type="TKey"/>
        <xs:element minOccurs="0" name="key7" type="TKey"/>
        <xs:element minOccurs="0" name="key8" type="TKey"/>
        <xs:element minOccurs="0" name="key9" type="TKey"/>
        <xs:element minOccurs="0" name="key10" type="TKey"/>
        <xs:element maxOccurs="unbounded" minOccurs="1" name="table" type="TTable"/>
        <xs:element maxOccurs="unbounded" minOccurs="0" name="criteria" type="TCriteria"/>
        <xs:element maxOccurs="unbounded" minOccurs="0" name="sql-index" type="TSqlIndex"/>
        <xs:element maxOccurs="unbounded" minOccurs="0" name="element" type="TElement"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:ID" use="required"/>
      <xs:attribute name="import-xslt-file" type="xs:anyURI" use="optional"/>
    </xs:complexType>

    <xs:complexType name="TCriteria">
      <xs:sequence>
        <xs:element minOccurs="0" ref="select-clause"/>
        <xs:element ref="where-clause"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:NCName" use="required"/>
      <xs:attribute name="sql-index-id" type="xs:IDREF" use="required"/>
      <xs:attribute name="type" type="xs:integer" use="optional"/>
    </xs:complexType>

  <xs:element name="select-clause" type="xs:string"/>
  <xs:element name="where-clause" type="xs:string"/>

    <xs:complexType name="TElement">
      <xs:sequence>
        <xs:element ref="sql-count"/>
        <xs:element ref="special-cond"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:NCName" use="required"/>
    </xs:complexType>

  <xs:element name="sql-count" type="xs:string"/>
  <xs:element name="special-cond" type="xs:string"/>

    <xs:complexType name="TKey">
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="path"/>
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="TSqlIndex">
      <xs:attribute name="id" type="xs:ID" use="required"/>
      <xs:attribute name="table" type="xs:IDREF" use="required"/>
      <xs:attribute name="column" type="xs:NCName" use="required"/>
      <xs:attribute name="path" use="required"/>
      <xs:attribute name="datatype" type="TIndexDatatype"/>
      <xs:attribute name="format" type="TFormat"/>
      <xs:attribute name="generate" type="xs:boolean" use="optional">
        <xs:annotation>
          <xs:documentation>Si vaut 'false', ce sql-index ne sera pas utilisé pour insérer dans la table. Dans ce cas, cela sert simplement à mapper un XPath sur une colonne</xs:documentation>
        </xs:annotation>
      </xs:attribute>
      <xs:attribute name="index-type" type="xs:integer" use="optional"/>
        <xs:attribute name="use-in-criteria" type="xs:boolean" use="optional" default="true">
            <xs:annotation>
                <xs:documentation>
                    Lorsque plusieurs index portent sur le même chemin, un seul peut être utilisé par le critère. Cet attribut doit être spécifié pour tous les index qui ne doivent pas être utilisés par le critère.
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="reset-on" type="TRefKey" use="optional">
            <xs:annotation>
                <xs:documentation>
                    Lorsqu'un index comporte une donnée d'un niveau supérieur à celui de la table, indique sur quelle clef il faut le remettre à vide
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
  
  <xs:simpleType name="TFormat">
    <xs:restriction base="xs:string">
      <!--xs:enumeration value="normalize-month"/>
      <xs:enumeration value="uppercase"/>
      <xs:enumeration value="lowercase"/>
      <xs:enumeration value="yyyy-MM-dd"/-->
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="TIndexDatatype">
    <xs:restriction base="xs:string">
      <xs:enumeration value="integer"/>
      <xs:enumeration value="string"/>
      <xs:enumeration value="decimal"/>
      <xs:enumeration value="date"/>
      <xs:enumeration value="boolean"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="TTable" mixed="true">
      <xs:sequence>
        <xs:element maxOccurs="unbounded" minOccurs="0" name="patch" type="TPatch"/>
      </xs:sequence>
      <xs:attribute name="base-name" type="xs:NCName" use="required"/>
      <xs:attribute name="based-on" type="xs:NCName"/>
      <xs:attribute name="id" type="xs:ID"/>
      <xs:attribute name="main" type="xs:boolean"/>
      <xs:attribute name="type" type="TTableType" use="required"/>
      <xs:attribute name="version" type="xs:integer" use="required"/>
      <xs:attribute name="ignoreConstraintError" type="xs:boolean" use="optional" default="false"/>
    </xs:complexType>

    <xs:complexType mixed="true" name="TPatch">
      <xs:sequence>
        <xs:element name="sql" type="TSql" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="from-version" type="xs:integer" use="required"/>
    </xs:complexType>
  
  <xs:complexType name="TSql" mixed="true">
    <xs:attribute name="failonerror" type="xs:boolean" use="optional" default="true"/>
  </xs:complexType>

  <xs:element name="path" type="xs:string"/>

  <xs:simpleType name="TTableType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="repository"/>
      <xs:enumeration value="special-key"/>
      <xs:enumeration value="list-bc"/>
      <xs:enumeration value="document"/>
      <xs:enumeration value="index"/>
    </xs:restriction>
  </xs:simpleType>

    <xs:simpleType name="TindexMain">
    <xs:restriction base="xs:string">
      <xs:enumeration value="no"/>
      <xs:enumeration value="yes"/>
    </xs:restriction>
    </xs:simpleType>
    
    <xs:simpleType name="TRefKey">
        <xs:restriction base="xs:string">
            <xs:enumeration value="key1"/>
            <xs:enumeration value="key2"/>
            <xs:enumeration value="key3"/>
            <xs:enumeration value="key4"/>
            <xs:enumeration value="key5"/>
            <xs:enumeration value="key6"/>
            <xs:enumeration value="key7"/>
            <xs:enumeration value="key8"/>
            <xs:enumeration value="key9"/>
            <xs:enumeration value="key10"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
