Exemple #1
0
  private PreparedStatement buildPreparedStatement(boolean allowArrayParameters) {
    // array parameter handling
    parsedQuery =
        ArrayParameters.updateQueryAndParametersIndexes(
            parsedQuery, paramNameToIdxMap, parameters, allowArrayParameters);

    // prepare statement creation
    if (preparedStatement == null) {
      try {
        if (columnNames != null && columnNames.length > 0) {
          preparedStatement =
              connection.getJdbcConnection().prepareStatement(parsedQuery, columnNames);
        } else if (returnGeneratedKeys) {
          preparedStatement =
              connection
                  .getJdbcConnection()
                  .prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS);
        } else {
          preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery);
        }
      } catch (SQLException ex) {
        throw new Sql2oException(
            String.format("Error preparing statement - %s", ex.getMessage()), ex);
      }
      connection.registerStatement(preparedStatement);
    }

    // parameters assignation to query
    for (Map.Entry<String, ParameterSetter> parameter : parameters.entrySet()) {
      for (int paramIdx : paramNameToIdxMap.get(parameter.getKey())) {
        try {
          parameter.getValue().setParameter(paramIdx, preparedStatement);
        } catch (SQLException e) {
          throw new RuntimeException(
              String.format("Error adding parameter '%s' - %s", parameter.getKey(), e.getMessage()),
              e);
        }
      }
    }
    // the parameters need to be cleared, so in case of batch, only new parameters will be added
    parameters.clear();

    return preparedStatement;
  }
Exemple #2
0
  @Test
  public void testUpdateNoTransaction() throws SQLException {
    String ddlQuery = "create table testUpdateNoTransaction(id int primary key, value varchar(50))";
    Connection connection = sql2o.createQuery(ddlQuery).executeUpdate();

    assertTrue(connection.getJdbcConnection().isClosed());

    String insQuery = "insert into testUpdateNoTransaction(id, value) values (:id, :value)";
    sql2o
        .createQuery(insQuery)
        .addParameter("id", 1)
        .addParameter("value", "test1")
        .executeUpdate()
        .createQuery(insQuery)
        .addParameter("id", 2)
        .addParameter("value", "val2")
        .executeUpdate();

    assertTrue(connection.getJdbcConnection().isClosed());
  }
Exemple #3
0
  public Query(Connection connection, String queryText, String name) {
    this.connection = connection;
    this.name = name;

    try {
      statement = new NamedParameterStatement(connection.getJdbcConnection(), queryText);
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }

    this.setColumnMappings(connection.getSql2o().getDefaultColumnMappings());
    this.caseSensitive = connection.getSql2o().isDefaultCaseSensitive();
    this.methodsMap = new HashMap<String, Method>();
  }