/*
 * Copyright (c) 2011-2014 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */

package io.vertx.ext.jdbc.impl.actions;

import io.vertx.core.Vertx;
import io.vertx.core.WorkerExecutor;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.sql.SQLRowStream;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
 */
public class StreamQuery extends AbstractJDBCAction<SQLRowStream> {

    private final String sql;
    private final JsonArray in;
    private final int timeout;

    public StreamQuery(Vertx vertx, JDBCStatementHelper helper, Connection connection, WorkerExecutor exec, int timeout, String sql, JsonArray in) {
        super(vertx, helper, connection, exec);
        this.sql = sql;
        this.in = in;
        this.timeout = timeout;
    }

    @Override
    protected SQLRowStream execute() throws SQLException {
        PreparedStatement st = null;

        try {
            st = conn.prepareStatement(sql);

            if (timeout >= 0) {
                st.setQueryTimeout(timeout);
            }

            helper.fillStatement(st, in);
            ResultSet rs = null;

            try {
                rs = st.executeQuery();
                return new JDBCSQLRowStream(exec, st, rs);
            } catch (SQLException e) {
                if (rs != null) {
                    rs.close();
                }
                throw e;
            }
        } catch (SQLException e) {
            if (st != null) {
                st.close();
            }
            throw e;
        }
    }

    @Override
    protected String name() {
        return "stream";
    }
}

@@ -1050,18 +1050,16 @@
 eam%3E %7B%0A%0A
-  
   privat
@@ -1078,18 +1078,16 @@
 ng sql;%0A
-  
   privat
@@ -1108,18 +1108,16 @@
 ray in;%0A
-  
   privat
@@ -1138,18 +1138,16 @@
 meout;%0A%0A
-  
   public
@@ -1281,28 +1281,24 @@
 Array in) %7B%0A
-    
     super(ve
@@ -1329,20 +1329,16 @@
  exec);%0A
-    
     this
@@ -1349,28 +1349,24 @@
  = sql;%0A    
-  
-  
 this.in = in
@@ -1367,20 +1367,16 @@
 n = in;%0A
-    
     this
@@ -1395,25 +1395,21 @@
 imeout;%0A
-  
   %7D%0A%0A
-  
   @Overr
@@ -1404,34 +1404,32 @@
  %7D%0A%0A  @Override%0A
-  
   protected SQLR
@@ -1469,20 +1469,16 @@
 ption %7B%0A
-    
     Prep
@@ -1507,32 +1507,22 @@
 l;%0A%0A    
-  
-  
 try %7B%0A
-      
       st
@@ -1555,30 +1555,24 @@
 ql);%0A%0A      
-    
-  
 if (timeout 
@@ -1575,32 +1575,24 @@
 out %3E= 0) %7B%0A
-        
         st.s
@@ -1626,23 +1626,11 @@
     
-  
-    %7D%0A%0A      
+%7D%0A%0A
     
@@ -1667,22 +1667,16 @@
 ;%0A      
-  
-    
 ResultSe
@@ -1689,22 +1689,16 @@
  null;%0A%0A
-      
       tr
@@ -1709,24 +1709,16 @@
         
-  
-      
 rs = st.
@@ -1741,24 +1741,16 @@
         
-     
-   
 return n
@@ -1784,22 +1784,16 @@
 t, rs);%0A
-      
       %7D 
@@ -1821,32 +1821,24 @@
 ) %7B%0A        
-   
-     
 if (rs != nu
@@ -1853,26 +1853,16 @@
         
-       
-   
 rs.close
@@ -1861,32 +1861,24 @@
 rs.close();%0A
-        
         %7D%0A  
@@ -1879,32 +1879,24 @@
   %7D%0A        
- 
-       
 throw e;%0A   
@@ -1894,36 +1894,26 @@
 ow e;%0A      
-     
- 
 %7D%0A
-    
     %7D catch 
@@ -1927,30 +1927,24 @@
 eption e) %7B%0A
-      
       if (st
@@ -1963,24 +1963,16 @@
         
- 
-       
 st.close
@@ -1981,30 +1981,18 @@
 ;%0A      
-     
- 
 %7D%0A
-      
       th
@@ -1998,28 +1998,24 @@
 hrow e;%0A
-    
     %7D%0A  
   %7D%0A%0A   
@@ -2006,23 +2006,19 @@
     %7D%0A  
- 
- 
 %7D%0A%0A
-  
   @Overr
@@ -2021,18 +2021,16 @@
 verride%0A
-  
   protec
@@ -2049,28 +2049,24 @@
 ame() %7B%0A    
- 
-   
 return %22stre
@@ -2070,16 +2070,14 @@
 tream%22;%0A
-  
   %7D%0A%7D%0A

apply correct code style