<?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="upload">

    <typeAlias alias="Upload" type="data.domains.Upload"   />

   <select id="getNotUploaded" parameterClass="String" resultClass="long">
       SELECT  id
        FROM upload
        where serial_number=#serial#  AND (status=1 OR status=0)
   </select>

    <select id="getWaitingUpload" parameterClass="String" resultClass="Upload">
        SELECT *
            FROM upload
               WHERE  serial_number=#serial#  AND status=0
    </select>

   <insert id="addUpload"  parameterClass="Map">
          INSERT
              INTO upload(id,serial_number,upload_date,status,file_id,url)
                  VALUES(#uploadId#,#serial#,#uploadDate#,0,#fileId#,#url#)

          <selectKey resultClass="long" keyProperty="uploadId">   <!--type="post" kam "pre"-->
                SELECT LAST_INSERT_ID() AS ID
          </selectKey>
    </insert>

   <update id="setUploadCompleted" parameterClass="String">
          Update upload
            Set status=2
            Where serial_number=#serial# and status=1
   </update>

</sqlMap>