Skip to content

Commit 964dc51

Browse files
committed
fix: Make all the examples work with latest pgx
Fixes: #1 Signed-off-by: Kaviraj <[email protected]>
1 parent f72a1f0 commit 964dc51

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ crate-type = ["cdylib"]
88

99
[features]
1010
default = ["pg13"]
11-
pg10 = [ "pgx/pg10", "pgx-tests/pg10" ]
1211
pg11 = [ "pgx/pg11", "pgx-tests/pg11" ]
1312
pg12 = [ "pgx/pg12", "pgx-tests/pg12" ]
1413
pg13 = [ "pgx/pg13", "pgx-tests/pg13" ]
1514
pg14 = [ "pgx/pg14", "pgx-tests/pg14" ]
15+
pg15 = ["pgx/pg15", "pgx-tests/pg15" ]
1616
pg_test = []
1717

1818
[dependencies]
19-
pgx = "0.4.0"
20-
pgx-macros = "0.4.0"
19+
pgx = "0.7.3"
20+
pgx-macros = "0.7.3"
2121
rand = "0.7.3"
2222
serde = "1.0.117"
2323

2424
[dev-dependencies]
25-
pgx-tests = "0.4.0"
25+
pgx-tests = "0.7.3"
2626

2727
[profile.dev]
2828
panic = "unwind"

src/lib.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use pgx::*;
1+
use pgx::prelude::*;
22

33
mod phone_number;
44

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

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

1919
#[pg_extern]
20-
fn my_generate_series(
21-
start: i64,
22-
end: i64,
23-
step: default!(i64, 1),
24-
) -> impl std::iter::Iterator<Item = i64> {
25-
(start..=end).into_iter().step_by(step as usize)
20+
fn my_generate_series(start: i64, end: i64, step: default!(i64, 1)) -> SetOfIterator<'static, i64> {
21+
SetOfIterator::new((start..=end).into_iter().step_by(step as usize))
2622
}
2723

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

3531
#[pg_extern]
36-
fn set_of_animals() -> impl std::iter::Iterator<
37-
Item = (
32+
fn set_of_animals() -> TableIterator<
33+
'static,
34+
(
3835
name!(name, &'static str),
3936
name!(species, Species),
4037
name!(age, f32),
@@ -44,17 +41,22 @@ fn set_of_animals() -> impl std::iter::Iterator<
4441
let species = vec![Species::Dog, Species::Cat, Species::Fish];
4542
let ages = vec![4.5, 4.0, 3.25];
4643

47-
names
48-
.into_iter()
49-
.zip(species.into_iter())
50-
.zip(ages.into_iter())
51-
// need to map the values to convert into a single tuple of three elements
52-
.map(|((name, species), age)| (name, species, age))
44+
TableIterator::new(
45+
names
46+
.into_iter()
47+
.zip(species.into_iter())
48+
.zip(ages.into_iter())
49+
// need to map the values to convert into a single tuple of three elements
50+
.map(|((name, species), age)| (name, species, age)),
51+
)
5352
}
5453

5554
#[pg_extern]
56-
fn rust_tuple(name: &str, age: i32) -> (name!(name, &str), name!(age, i32)) {
57-
(name, age)
55+
fn rust_tuple(
56+
name: &'static str,
57+
age: i32,
58+
) -> TableIterator<'static, (name!(name, &'static str), name!(age, i32))> {
59+
TableIterator::once((name, age))
5860
}
5961

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

8890
#[pg_test]

src/phone_number/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use pgx::*;
1+
use std::ffi::CStr;
2+
3+
use pgx::{prelude::*, StringInfo};
24
use rand::Rng;
35
use serde::{Deserialize, Serialize};
4-
use pgx::cstr_core::CStr;
56

67
mod implementation;
78

0 commit comments

Comments
 (0)