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

Add support for PostgreSQL HSTORE data type #3343

Merged
merged 5 commits into from
Jul 19, 2024

Conversation

KobusEllis
Copy link
Contributor

@KobusEllis KobusEllis commented Jul 12, 2024

Adds support for postgres hstore data type by mapping it to a BTreeMap<String, Option>. Using a BTreeMap rather than a HashMap ensures that the keys remain sorted. This is helpful for auto generated history data. Previously discussed as #151.

Copy link
Collaborator

@abonander abonander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd at least like to start with something that gives us more room to evolve the API later (this also makes it clear that the expected SQL type is hstore):

pub struct PgHStore(pub BTreeMap<String, Option<String>>);

impl Deref for PgHStore {
    type Target = BTreeMap<String, Option<String>>;

    ...
}

impl DerefMut for PgHStore {
    ...
}

impl FromIterator<(String, String)> for PgHStore { ... }

impl FromIterator<(String, Option<String>)> for PgHStore { ... }

impl IntoIterator for PgHStore {
    type Iterator = btree_map::IntoIter<(String, Option<String>)>;

    ...
}

With this, we could add a defaulted type parameter, e.g. T = BTreeMap<String, Option<String>> to allow the use of other types without breaking existing usage (for the most part, anyway). I was thinking if we used serde::Deserialize and serde::Serialize, the user could use any map type they wanted, or even a struct.

sqlx-postgres/src/types/mod.rs Outdated Show resolved Hide resolved
@abonander abonander merged commit 4683cc3 into launchbadge:main Jul 19, 2024
65 checks passed
@KobusEllis KobusEllis deleted the postgres-hstore-feature-pr branch July 23, 2024 13:34
jrasanen pushed a commit to jrasanen/sqlx that referenced this pull request Oct 14, 2024
* Add support for PostgreSQL HSTORE data type

* Changes to make the future evolution of the API easier

* Fix clippy lints

* Add basic documentation
jrasanen pushed a commit to jrasanen/sqlx that referenced this pull request Oct 14, 2024
* Add support for PostgreSQL HSTORE data type

* Changes to make the future evolution of the API easier

* Fix clippy lints

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

Successfully merging this pull request may close these issues.

2 participants