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

DbClient - can we return generated id(s)? #2279

Open
tomas-langer opened this issue Aug 20, 2020 · 6 comments · May be fixed by #9081
Open

DbClient - can we return generated id(s)? #2279

tomas-langer opened this issue Aug 20, 2020 · 6 comments · May be fixed by #9081
Assignees
Labels
DB client Helidon DB Client enhancement New feature or request P3

Comments

@tomas-langer
Copy link
Member

Environment Details

  • Helidon Version: 2.0
  • Helidon SE

Problem Description

When using autogenerated ids (or sequences), we need to return the id used from the insert statement. This is currently not possible with db client.
In JDBC, this is possible using
Statement.RETURN_GENERATED_KEYS
and
Statement::getGeneratedKeys

see https://stackoverflow.com/questions/9353167/auto-increment-id-in-h2-database

@Tomas-Kraus
Copy link
Member

Tomas-Kraus commented Aug 25, 2020

Good question. We have no statement that keeps execution info in it. Everything is in returned Future. In case of DML it's just number of modified records. Maybe we can extend this future value to contain a bit more.
But it will be significant API change.
Both Mongo and JDBC support this so implementing such a feature should not be a problem.

@m0mus m0mus added the P3 label Aug 27, 2020
@hantsy
Copy link

hantsy commented Dec 14, 2020

Currently, I have to handle it myself.

public Single<UUID> save(Post post) {
	return this.dbClient
			.execute(exec -> exec
					.query("INSERT INTO posts(title, content) VALUES (?, ?) RETURNING id", post.getTitle(), post.getContent())

			)
			.first()
			.map(data -> data.column("id").as(UUID.class));
}

@tomas-langer tomas-langer self-assigned this Jan 25, 2021
@tomas-langer
Copy link
Member Author

Investigating - for now I am trying to add a new method to the API that would return the generated ids, so I do not break backward compatibility.

@spericas
Copy link
Member

This seems like a very useful addition to the API

@Tomas-Kraus
Copy link
Member

I have some time to have a look at this now. But I'll do it as part of 4.x where DB Client is synchronnous.

@Tomas-Kraus
Copy link
Member

@tomas-langer We have to extend DbStatementDml inerface methods. Currently we have single prototype there:

long execute();

Which returns value of statement.executeUpdate() which is usually the number of modified records.
We also need to retrieve content of ResultSet returned by getGeneratedKeys(). JDBC ResultSet is mapped to List of DB client DbRow so I have 2 options:
1st:

List<DbRow> insert();

This will return DbRows containing generated keys.

2nd:

public record DMLResult (int result, List<DbRow> keys) {
}

DMLResult insert();

This will return both statement.executeUpdate() value and generated keys, but looks quite obscure to me.

Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Aug 1, 2024
@Tomas-Kraus Tomas-Kraus linked a pull request Aug 1, 2024 that will close this issue
Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Aug 1, 2024
Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Aug 2, 2024
Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Aug 2, 2024
Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Aug 2, 2024
Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Aug 2, 2024
Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Aug 6, 2024
Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Aug 7, 2024
Tomas-Kraus added a commit to Tomas-Kraus/helidon that referenced this issue Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DB client Helidon DB Client enhancement New feature or request P3
Projects
Status: Normal priority
Development

Successfully merging a pull request may close this issue.

5 participants