Skip to content

fix: Make all the examples work with latest pgx #2

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ crate-type = ["cdylib"]

[features]
default = ["pg13"]
pg10 = [ "pgx/pg10", "pgx-tests/pg10" ]
pg11 = [ "pgx/pg11", "pgx-tests/pg11" ]
pg12 = [ "pgx/pg12", "pgx-tests/pg12" ]
pg13 = [ "pgx/pg13", "pgx-tests/pg13" ]
pg14 = [ "pgx/pg14", "pgx-tests/pg14" ]
pg15 = ["pgx/pg15", "pgx-tests/pg15" ]
pg_test = []

[dependencies]
pgx = "0.4.0"
pgx-macros = "0.4.0"
pgx = "0.7.3"
pgx-macros = "0.7.3"
rand = "0.7.3"
serde = "1.0.117"

[dev-dependencies]
pgx-tests = "0.4.0"
pgx-tests = "0.7.3"

[profile.dev]
panic = "unwind"
Expand Down
40 changes: 21 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use pgx::*;
use pgx::prelude::*;

mod phone_number;

// Required by all `pgx` extensions. Indicates to Postgres, when it loads the shared library
// that the library is really a Postgres extension
pg_module_magic!();
pgx::pg_module_magic!();

#[pg_extern]
fn hello_postgresconf() -> &'static str {
Expand All @@ -17,12 +17,8 @@ fn sum_array(input: Vec<i64>) -> i64 {
}

#[pg_extern]
fn my_generate_series(
start: i64,
end: i64,
step: default!(i64, 1),
) -> impl std::iter::Iterator<Item = i64> {
(start..=end).into_iter().step_by(step as usize)
fn my_generate_series(start: i64, end: i64, step: default!(i64, 1)) -> SetOfIterator<'static, i64> {
SetOfIterator::new((start..=end).into_iter().step_by(step as usize))
}

#[derive(PostgresEnum)]
Expand All @@ -33,8 +29,9 @@ pub enum Species {
}

#[pg_extern]
fn set_of_animals() -> impl std::iter::Iterator<
Item = (
fn set_of_animals() -> TableIterator<
'static,
(
name!(name, &'static str),
name!(species, Species),
name!(age, f32),
Expand All @@ -44,17 +41,22 @@ fn set_of_animals() -> impl std::iter::Iterator<
let species = vec![Species::Dog, Species::Cat, Species::Fish];
let ages = vec![4.5, 4.0, 3.25];

names
.into_iter()
.zip(species.into_iter())
.zip(ages.into_iter())
// need to map the values to convert into a single tuple of three elements
.map(|((name, species), age)| (name, species, age))
TableIterator::new(
names
.into_iter()
.zip(species.into_iter())
.zip(ages.into_iter())
// need to map the values to convert into a single tuple of three elements
.map(|((name, species), age)| (name, species, age)),
)
}

#[pg_extern]
fn rust_tuple(name: &str, age: i32) -> (name!(name, &str), name!(age, i32)) {
(name, age)
fn rust_tuple(
name: &'static str,
age: i32,
) -> TableIterator<'static, (name!(name, &'static str), name!(age, i32))> {
TableIterator::once((name, age))
}

#[pg_extern]
Expand Down Expand Up @@ -82,7 +84,7 @@ mod tests {
let result =
Spi::get_one::<Vec<i64>>("SELECT array_agg(g) FROM my_generate_series(1, 10) g;")
.expect("SPI result was NULL");
assert_eq!(result, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
assert_eq!(result, Some(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
}

#[pg_test]
Expand Down
5 changes: 3 additions & 2 deletions src/phone_number/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use pgx::*;
use std::ffi::CStr;

use pgx::{prelude::*, StringInfo};
use rand::Rng;
use serde::{Deserialize, Serialize};
use pgx::cstr_core::CStr;

mod implementation;

Expand Down