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

Support non-integer primary keys for JDBC inegrations #351

Closed
vladimir-bukhtoyarov opened this issue Feb 7, 2023 · 1 comment
Closed

Comments

@vladimir-bukhtoyarov
Copy link
Collaborator

vladimir-bukhtoyarov commented Feb 7, 2023

Currently 64-bit long is only one possible datatype for primary key. But sometimes user can prefer to use non-integer primary keys like strings or UUIDs, so it would be nice to provide ability for user to specify way to read/write id from/to prepared statements:

Proposed inteface for id-mapping abstraction:

public interface KeyMapper<T> {

    T get(ResultSet rs, int i) throws SQLException;

    void set(PreparedStatement statement, T value, int i) throws SQLException;

    KeyMapper<Long> LONG = new KeyMapper<Long>() {
        @Override
        public Long get(ResultSet rs, int i) throws SQLException {
            return rs.getLong(i);
        }

        @Override
        public void set(PreparedStatement statement, Long value, int i) throws SQLException {
            statement.setLong(i, value);
        }
    };

    KeyMapper<Integer> INT = new KeyMapper<Integer>() {
        @Override
        public Integer get(ResultSet rs, int i) throws SQLException {
            return rs.getInt(i);
        }

        @Override
        public void set(PreparedStatement statement, Integer value, int i) throws SQLException {
            statement.setInt(i, value);
        }
    };

    KeyMapper<String> STRING = new KeyMapper<String>() {
        @Override
        public String get(ResultSet rs, int i) throws SQLException {
            return rs.getString(i);
        }

        @Override
        public void set(PreparedStatement statement, String value, int i) throws SQLException {
            statement.setString(i, value);
        }
    };

}

Inspired by this discussion https://stackoverflow.com/questions/74700506/how-to-use-bucket4j-with-postgresql/

@vladimir-bukhtoyarov
Copy link
Collaborator Author

Released with version 8.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant