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

Composite type support. #311

Merged
merged 3 commits into from
Jun 1, 2023
Merged

Composite type support. #311

merged 3 commits into from
Jun 1, 2023

Conversation

eeeebbbbrrrr
Copy link
Contributor

@eeeebbbbrrrr eeeebbbbrrrr commented May 24, 2023

This will replace PR #57 when complete.

Here's an example:

drop type person cascade;
drop table people cascade;
create type person as (name text, age float8);
create function make_person(name text, age float8) returns person strict parallel safe language plrust as $$
    let mut p = PgHeapTuple::new_composite_type("person")?;
    p.set_by_name("name", name)?;
    p.set_by_name("age", age)?;
    Ok(Some(p))
$$;

create function get_person_name(p person) returns text strict parallel safe language plrust as $$
   Ok(p.get_by_name("name")?)
$$;
create function get_person_age(p person) returns float8 strict parallel safe language plrust as $$
   Ok(p.get_by_name("age")?)
$$;
create function get_person_property(p person, prop text) returns text strict parallel safe language plrust as $$
   match prop.to_lowercase().as_str() {
    "age" => {
        let age:Option<f64> = p.get_by_name("age")?;
        Ok(age.map(|v| v.to_string()))
    },
    "name" => {
        Ok(p.get_by_name("name")?)
    },
    _ => panic!("unknown property: `{prop}`")
   }
$$;

create table people (
    id serial8 not null primary key,
    p person
);

insert into people (p) values (make_person('Eric', 46.24));
insert into people (p) values (make_person('Joe', 99.09));
insert into people (p) values (make_person('Dr. Beverly Crusher of the Starship Enterprise', 32.0));

select *, get_person_name(p), get_person_age(p) from people order by get_person_age(p) desc;

create operator -> (function = get_person_name, rightarg = person);
create operator -# (function = get_person_age, rightarg = person);

select ->p as name, -#p as age from people;

create operator ->> (function = get_person_property, leftarg = person, rightarg = text);
select p->>'name' as name, (p->>'age')::float8 as age from people;

@eeeebbbbrrrr eeeebbbbrrrr changed the title working on composite type support. this will replace PR #57 when complete Composite type support. May 31, 2023
Copy link
Contributor

@BradyBonnette BradyBonnette left a comment

Choose a reason for hiding this comment

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

All of this makes sense! 👍

Co-authored-by: Brady Bonnette <BradyBonnette@users.noreply.github.com>
@eeeebbbbrrrr eeeebbbbrrrr merged commit c8a19ef into develop Jun 1, 2023
@eeeebbbbrrrr eeeebbbbrrrr mentioned this pull request Jun 14, 2023
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