@Override
  public void exception(ExecuteContext ctx) {
    SQLDialect dialect = ctx.configuration().dialect();
    SQLExceptionTranslator translator =
        (dialect != null)
            ? new SQLErrorCodeSQLExceptionTranslator(dialect.name())
            : new SQLStateSQLExceptionTranslator();

    ctx.exception(translator.translate("jOOQ", ctx.sql(), ctx.sqlException()));
  }
Example #2
0
  private final int[] executePrepared() {
    ExecuteContext ctx = new DefaultExecuteContext(configuration, new Query[] {query});
    ExecuteListener listener = new ExecuteListeners(ctx);
    Connection connection = ctx.connection();

    // [#1371] fetch bind variables to restore them again, later
    DataType<?>[] paramTypes = dataTypes(query.getParams().values().toArray(new Field[0]));

    try {
      listener.renderStart(ctx);
      // [#1520] TODO: Should the number of bind values be checked, here?
      ctx.sql(create.render(query));
      listener.renderEnd(ctx);

      listener.prepareStart(ctx);
      ctx.statement(connection.prepareStatement(ctx.sql()));
      listener.prepareEnd(ctx);

      for (Object[] bindValues : allBindValues) {
        listener.bindStart(ctx);

        // [#1371] [#2139] Don't bind variables directly onto statement, bind them through the
        // collected params
        //                 list to preserve type information
        // [#3547]         The original query may have no Params specified - e.g. when it was
        // constructed with
        //                 plain SQL. In that case, infer the bind value type directly from the bind
        // value
        List<Field<?>> params =
            (paramTypes.length > 0) ? fields(bindValues, paramTypes) : fields(bindValues);

        visitAll(new DefaultBindContext(configuration, ctx.statement()), params);

        listener.bindEnd(ctx);
        ctx.statement().addBatch();
      }

      try {
        listener.executeStart(ctx);
        int[] result = ctx.statement().executeBatch();

        int[] batchRows = ctx.batchRows();
        for (int i = 0; i < batchRows.length && i < result.length; i++) batchRows[i] = result[i];

        listener.executeEnd(ctx);
        return result;
      } finally {
        consumeWarnings(ctx, listener);
      }
    } catch (RuntimeException e) {
      ctx.exception(e);
      listener.exception(ctx);
      throw ctx.exception();
    } catch (SQLException e) {
      ctx.sqlException(e);
      listener.exception(ctx);
      throw ctx.exception();
    } finally {
      Utils.safeClose(listener, ctx);
    }
  }
Example #3
0
 @Override
 public void exception(ExecuteContext ctx) {
     ctx.exception(new E("ERROR"));
 }
Example #4
0
  @Override
  public final int execute() {
    if (isExecutable()) {

      // Get the attached configuration of this query
      Configuration c = configuration();

      // [#1191] The following triggers a start event on all listeners.
      //         This may be used to provide jOOQ with a JDBC connection,
      //         in case this Query / Configuration was previously
      //         deserialised
      ExecuteContext ctx = new DefaultExecuteContext(c, this);
      ExecuteListener listener = new ExecuteListeners(ctx);

      int result = 0;
      try {
        if (ctx.connection() == null) {
          throw new DetachedException("Cannot execute query. No Connection configured");
        }

        // [#385] If a statement was previously kept open
        if (keepStatement() && statement != null) {
          ctx.sql(sql);
          ctx.statement(statement);
        }

        // [#385] First time statement preparing
        else {
          listener.renderStart(ctx);
          ctx.sql(getSQL0(ctx));
          listener.renderEnd(ctx);

          sql = ctx.sql();

          listener.prepareStart(ctx);
          prepare(ctx);
          listener.prepareEnd(ctx);

          statement = ctx.statement();
        }

        // [#1856] Set the query timeout onto the Statement
        if (timeout != 0) {
          ctx.statement().setQueryTimeout(timeout);
        }

        if (

        // [#1145] Bind variables only for true prepared statements
        // [#2414] Even if parameters are inlined here, child
        //         QueryParts may override this behaviour!
        executePreparedStatements(c.settings())
            &&

            // [#1520] Renderers may enforce static statements, too
            !Boolean.TRUE.equals(ctx.data(DATA_FORCE_STATIC_STATEMENT))) {

          listener.bindStart(ctx);
          using(c).bindContext(ctx.statement()).visit(this);
          listener.bindEnd(ctx);
        }

        result = execute(ctx, listener);
        return result;
      } catch (SQLException e) {
        ctx.sqlException(e);
        listener.exception(ctx);
        throw ctx.exception();
      } finally {

        // [#2385] Successful fetchLazy() needs to keep open resources
        if (!keepResultSet() || ctx.exception() != null) {
          Utils.safeClose(listener, ctx, keepStatement());
        }

        if (!keepStatement()) {
          statement = null;
          sql = null;
        }
      }
    } else {
      if (log.isDebugEnabled()) {
        log.debug("Query is not executable", this);
      }

      return 0;
    }
  }