Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return inserted row as a result of Insert action #36

Open
shulkaolka opened this issue Sep 25, 2019 · 1 comment
Open

Return inserted row as a result of Insert action #36

shulkaolka opened this issue Sep 25, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@shulkaolka
Copy link
Contributor

  1. Define Primary Key from dbMetadata

  2. Define is Primary Key generated

  3. After executing Insert query:
    3.1. If the table contains PrimaryKey:

    • If Primary Key is generated - get Primary Key Value from the statement
    • If Primary Key isn't generated - get Primary Key Value from body

    execute query Select By Primary Key and return selected row
    3.2. If the table doesn't contain PrimaryKey - return input body

@shulkaolka
Copy link
Contributor Author

As example:

  public JsonObject executeInsertGeneral(Connection connection, String tableName, JsonObject body,
      String primaryKeyName)
      throws SQLException {
    validateQuery();
    JsonObject keysValues = getKeyValue(body);
    String keys = keysValues.getString("keys");
    String values = keysValues.getString("values");
    String sql = "INSERT INTO " + tableName +
        " (" + keys + ")" +
        " VALUES (" + values + ")";
    try (PreparedStatement stmt = connection
        .prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
      int i = 1;
      for (String key : body.keySet()) {
        Utils.setStatementParam(stmt, i, key, body);
        i++;
      }
      int affectedRows = stmt.executeUpdate();
      if (affectedRows != 1) {
        throw new SQLException("Inserting failed, no rows affected.");
      }
      if (primaryKeyName != null) {
        JsonObjectBuilder primaryKeyBody = Json.createObjectBuilder();
        int generatedKeysCount = 0;
        try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
          while (generatedKeys.next()) {
            primaryKeyBody.add(primaryKeyName, generatedKeys.getLong(1));
            generatedKeysCount++;
            if (generatedKeysCount > 1) {
              throw new SQLException("More than one generated key!");
            }
          }
        }
        if (generatedKeysCount == 0) {
          return getRowByPrimaryKey(connection, primaryKeyName, body);
        }
        return getRowByPrimaryKey(connection, primaryKeyName, primaryKeyBody.build());
      } else {
        return body;
      }
    }
  }

@stas-fomenko stas-fomenko added the enhancement New feature or request label Sep 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants