Skip to content

Commit

Permalink
Add type mapping for 'pg_sys::TimestampTz' to 'timestamp with time zone'
Browse files Browse the repository at this point in the history
This allows the `pg_sys::TimestampTz' type to be used in function
signatures, providing zero-cost access to the underlying i64 timestamp
of a 'timestamp with time zone' field.
  • Loading branch information
JamesGuthrie committed Jan 25, 2022
1 parent de8ce98 commit b4d0276
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pgx-examples/builtin_types/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.

[target.x86_64-unknown-linux-gnu]
linker = ".cargo/pgx-linker-script.sh"

[target.aarch64-unknown-linux-gnu]
linker = ".cargo/pgx-linker-script.sh"

[target.x86_64-unknown-linux-musl]
linker = ".cargo/pgx-linker-script.sh"

[target.aarch64-unknown-linux-musl]
linker = ".cargo/pgx-linker-script.sh"

[target.x86_64-apple-darwin]
linker = ".cargo/pgx-linker-script.sh"

[target.aarch64-apple-darwin]
linker = ".cargo/pgx-linker-script.sh"

[target.x86_64-unknown-freebsd]
linker = ".cargo/pgx-linker-script.sh"
19 changes: 19 additions & 0 deletions pgx-examples/builtin_types/.cargo/pgx-linker-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /usr/bin/env bash
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.

if [[ $CARGO_BIN_NAME == "sql-generator" ]]; then
UNAME=$(uname)
if [[ $UNAME == "Darwin" ]]; then
TEMP=$(mktemp pgx-XXX)
echo "*_pgx_internals_*" > ${TEMP}
gcc -exported_symbols_list ${TEMP} $@
rm -rf ${TEMP}
else
TEMP=$(mktemp pgx-XXX)
echo "{ __pgx_internals_*; };" > ${TEMP}
gcc -Wl,-dynamic-list=${TEMP} $@
rm -rf ${TEMP}
fi
else
gcc -Wl,-undefined,dynamic_lookup $@
fi
6 changes: 6 additions & 0 deletions pgx-examples/builtin_types/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.idea/
/target
*.iml
**/*.rs.bk
Cargo.lock
38 changes: 38 additions & 0 deletions pgx-examples/builtin_types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "builtin_types"
version = "0.1.0"
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

# remove this empty 'workspace' declaration if compiling outside of 'pgx'
[workspace]

[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" ]
pg_test = []

[dependencies]
pgx = { path = "../../../pgx/pgx", default-features = false }
pgx-macros = { path = "../../../pgx/pgx-macros", default-features = false }


[dev-dependencies]
pgx-tests = { path = "../../../pgx/pgx-tests" }

# uncomment these if compiling outside of 'pgx'
# [profile.dev]
# panic = "unwind"
# lto = "thin"

# [profile.release]
# panic = "unwind"
# opt-level = 3
# lto = "fat"
# codegen-units = 1
5 changes: 5 additions & 0 deletions pgx-examples/builtin_types/builtin_types.control
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
comment = 'builtin_types: Created by pgx'
default_version = '@CARGO_VERSION@'
module_pathname = '$libdir/builtin_types'
relocatable = false
superuser = false
2 changes: 2 additions & 0 deletions pgx-examples/builtin_types/src/bin/sql-generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Auto-generated by pgx. You may edit this, or delete it to have a new one created. */
pgx::pg_binary_magic!(builtin_types);
39 changes: 39 additions & 0 deletions pgx-examples/builtin_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use pgx::*;

pg_module_magic!();

#[pg_extern]
fn timestamptz(tstz: pg_sys::TimestampTz) -> i64 {
tstz
}

#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgx::*;

use std::time::Duration;

#[pg_test]
fn test_timestamptz() {
let result = Spi::get_one::<i64>(r#"
SELECT timestamptz('2000-01-01 00:01:00.0000000+00'::timestamptz)
"#).expect("SQL query failed");

assert_eq!(result, Duration::from_secs(60).as_micros() as i64);
}

}

#[cfg(test)]
pub mod pg_test {

pub fn setup(_options: Vec<&str>) {
// perform one-off initialization when the pg_test framework starts
}

pub fn postgresql_conf_options() -> Vec<&'static str> {
// return any postgresql.conf settings that are required for your tests
vec![]
}
}
1 change: 1 addition & 0 deletions pgx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub static DEFAULT_SOURCE_ONLY_SQL_MAPPING: Lazy<HashSet<RustSourceOnlySqlMappin
let mut m = HashSet::new();

map_source_only!(m, pg_sys::Oid, "Oid");
map_source_only!(m, pg_sys::TimestampTz, "timestamp with time zone");

m
});
Expand Down

0 comments on commit b4d0276

Please sign in to comment.