/*
 * 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.Context;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.sql.UpdateResult;

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

import static io.vertx.ext.jdbc.impl.actions.JDBCStatementHelper.*;

/**
 * @author <a href="mailto:nscavell@redhat.com">Nick Scavelli</a>
 */
public class JDBCUpdate extends AbstractJDBCAction<UpdateResult> {

  private final String sql;
  private final JsonArray in;

  public JDBCUpdate(Vertx vertx, Connection connection, Context context, String sql, JsonArray in) {
    super(vertx, connection, context);
    this.sql = sql;
    this.in = in;
  }

  @Override
  protected UpdateResult execute(Connection conn) throws SQLException {
    try (PreparedStatement statement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
      fillStatement(statement, in);

      int updated = statement.executeUpdate();
      JsonArray keys = new JsonArray();

      // Create JsonArray of keys
      try (ResultSet rs = statement.getGeneratedKeys()) {
        if (rs != null) {
          while (rs.next()) {
            Object key = rs.getObject(1);
            if (key != null) {
              keys.add(convertSqlValue(key));
            }
          }
        }
      }

      return new UpdateResult(updated, keys);
    }
  }

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

@@ -929,16 +929,48 @@
 atement;
+%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -1174,16 +1174,133 @@
 ult%3E %7B%0A%0A
+  private static final Pattern regex = Pattern.compile(%22.*insert.*%22, Pattern.CASE_INSENSITIVE + Pattern.MULTILINE);%0A%0A
   privat
@@ -1617,16 +1617,76 @@
 ption %7B%0A
+    final boolean returKeys = regex.matcher(sql).matches();%0A
     try 
@@ -1742,16 +1742,28 @@
 ent(sql,
+ returKeys ?
  Stateme
@@ -1786,16 +1786,46 @@
 TED_KEYS
+ : Statement.NO_GENERATED_KEYS
 )) %7B%0A   
@@ -1980,16 +1980,41 @@
 of keys%0A
+      if (returKeys) %7B%0A  
       tr
@@ -2067,24 +2067,26 @@
 ) %7B%0A        
+  
 if (rs != nu
@@ -2083,32 +2083,34 @@
  (rs != null) %7B%0A
+  
           while 
@@ -2135,16 +2135,18 @@
         
+  
 Object k
@@ -2167,16 +2167,18 @@
 ect(1);%0A
+  
         
@@ -2214,16 +2214,18 @@
         
+  
 keys.add
@@ -2252,32 +2252,34 @@
 ));%0A            
+ 
+ 
 %7D%0A          %7D%0A  
@@ -2266,33 +2266,126 @@
     %7D%0A          
-%7D
+  %7D%0A          %7D%0A        %7D catch (SQLException e) %7B%0A          // do not crash if no permissions
 %0A        %7D%0A     

fix #46 try to identify if it is an insert statement or a pure update, if that even generates a false positive do not crash on sql exceptions while retrieving the generated keys