Skip to content

Commit

Permalink
use pgx::prelude::*; (pgcentralfoundation#699)
Browse files Browse the repository at this point in the history
* Add beginning of pgx::prelude

* Make `default!` work with `use pgx::prelude::*`

Previously, PGX has relied on `use pgx::*;` in order to make the framework happen.
However, this severely pollutes the namespaces of our users. We're hogging idents.
Worse, a user collision with our identifiers may result in displacing our logic,
resulting in undesirable and unexpected side effects.

PGX depends on the macros being at least mostly correct in order to be sound,
so this is not at all acceptable.

* Make `name!` work with `use pgx::prelude::*;`

This macro is also fairly critical to PGX's functioning,
being integral to any and all "table"-like returns.

* Stop exporting `pgx::iter::*`

`use pgx::prelude::*;` is already sufficient for some test files now.

* Make `#[derive(PostgresType)]` work with the prelude
* Simplify imports for HeapTuples to `use pgx::*;` less
* Expose more of pgx-macros in prelude
* Introduce temporal types in prelude
* Export PostgresGucEnum from pgx::guc
* Use explicit imports to finish preludization
* Sweep most examples for preludization
* Stop glob-exporting `pgx::heap_tuple::*`
* Also export `spi::Spi` from the prelude
* Remove last `use pgx::*;` from pgx-tests
* use prelude in cargo pgx templates
* Fully qualify `pgx::pg_module_magic!` in templates
  • Loading branch information
workingjubilee committed Sep 22, 2022
1 parent 5fe0b33 commit b48c8db
Show file tree
Hide file tree
Showing 75 changed files with 233 additions and 192 deletions.
2 changes: 1 addition & 1 deletion cargo-pgx/src/templates/bgworker_lib_rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use pgx::bgworkers::*;
use pgx::*;
use pgx::prelude::*;
use std::time::Duration;

pg_module_magic!();
Expand Down
6 changes: 3 additions & 3 deletions cargo-pgx/src/templates/lib_rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pgx::*;
use pgx::prelude::*;

pg_module_magic!();
pgx::pg_module_magic!();

#[pg_extern]
fn hello_{name}() -> &'static str {{
Expand All @@ -10,7 +10,7 @@ fn hello_{name}() -> &'static str {{
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {{
use pgx::*;
use pgx::prelude::*;

#[pg_test]
fn test_hello_{name}() {{
Expand Down
6 changes: 3 additions & 3 deletions nix/templates/default/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use pgx::*;
use pgx::prelude::*;

pg_module_magic!();
pgx::pg_module_magic!();

#[pg_extern]
fn hello() -> &'static str {
Expand All @@ -18,7 +18,7 @@ fn hello() -> &'static str {
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgx::*;
use pgx::prelude::*;

#[pg_test]
fn test_hello() {
Expand Down
4 changes: 2 additions & 2 deletions pgx-examples/aggregate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use pgx::cstr_core::CStr;
use pgx::*;
use pgx::{aggregate::*, pgx, prelude::*, PgVarlena, PgVarlenaInOutFuncs, StringInfo};
use serde::{Deserialize, Serialize};
use std::str::FromStr;

pg_module_magic!();
pgx::pg_module_magic!();

#[derive(Copy, Clone, PostgresType, Serialize, Deserialize)]
#[pgvarlena_inoutfuncs]
Expand Down
7 changes: 4 additions & 3 deletions pgx-examples/arrays/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

use pgx::*;
use pgx::{prelude::*, Array};
use serde::*;

pg_module_magic!();
pgx::pg_module_magic!();

#[pg_extern]
fn sq_euclid_pgx(a: Array<f32>, b: Array<f32>) -> f32 {
Expand Down Expand Up @@ -123,7 +123,8 @@ pub mod pg_test {
#[cfg(any(test, feature = "pg_test"))]
pub mod tests {
use crate::SomeStruct;
use pgx::*;
use pgx::prelude::*;

#[pg_test]
#[search_path(@extschema@)]
fn test_vec_of_customtype() {
Expand Down
6 changes: 4 additions & 2 deletions pgx-examples/bad_ideas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use pgx::*;
use pgx::{
check_for_interrupts, info, prelude::*, register_xact_callback, PgRelation, PgXactCallbackEvent,
};
use std::fs::File;
use std::io::Write;
use std::process::Command;

pg_module_magic!();
pgx::pg_module_magic!();

#[pg_extern]
fn exec<'a>(
Expand Down
10 changes: 7 additions & 3 deletions pgx-examples/bgworker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use pgx::bgworkers::*;
use pgx::*;
use pgx::{
bgworkers::*,
datum::{FromDatum, IntoDatum},
log,
prelude::*,
};
use std::time::Duration;

/*
Expand All @@ -25,7 +29,7 @@ use std::time::Duration;
this background worker
*/

pg_module_magic!();
pgx::pg_module_magic!();

#[pg_guard]
pub extern "C" fn _PG_init() {
Expand Down
6 changes: 3 additions & 3 deletions pgx-examples/bytea/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Use of this source code is governed by the MIT license that can be found in the
*/

use libflate::gzip::{Decoder, Encoder};
use pgx::*;
use pgx::prelude::*;
use std::io::{Read, Write};

pg_module_magic!();
pgx::pg_module_magic!();

/// gzip bytes. Postgres will automatically convert `text`/`varchar` data into `bytea`
#[pg_extern]
Expand Down Expand Up @@ -43,7 +43,7 @@ fn gunzip_as_text(bytes: &[u8]) -> String {
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgx::*;
use pgx::prelude::*;

#[pg_test]
fn test_gzip_text() {
Expand Down
6 changes: 3 additions & 3 deletions pgx-examples/composite_type/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ A number of considerations are detailed in the [`pgx::composite_type`][pgx::comp
[`PgHeapTuple`][pgx::PgHeapTuple] documentation which may assist in productionalizing extensions
using composite types.
*/
use pgx::*;
use pgx::{opname, pg_operator, prelude::*, Aggregate};

// All `pgx` extensions will do this:
pg_module_magic!();
pgx::pg_module_magic!();

/* Composite types must be defined either before they are used.
Expand Down Expand Up @@ -259,7 +259,7 @@ fn add_scritches_to_dog(
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgx::*;
use pgx::{prelude::*, AllocatedByRust};

#[pg_test]
fn test_create_dog() {
Expand Down
6 changes: 3 additions & 3 deletions pgx-examples/custom_sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

use pgx::*;
use pgx::prelude::*;
use serde::{Deserialize, Serialize};

pg_module_magic!();
pgx::pg_module_magic!();

#[pg_schema]
mod home {
Expand Down Expand Up @@ -82,7 +82,7 @@ extension_sql_file!("../sql/finalizer.sql", finalize);
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgx::*;
use pgx::prelude::*;

#[pg_test]
fn test_ordering() {
Expand Down
4 changes: 2 additions & 2 deletions pgx-examples/custom_types/src/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Use of this source code is governed by the MIT license that can be found in the
*/

use maplit::*;
use pgx::*;
use pgx::{prelude::*, Array};
use serde::*;
use std::collections::HashMap;

Expand Down Expand Up @@ -56,7 +56,7 @@ fn add_animal(mut animals: Animals, name: String, age: i32) -> Animals {
mod tests {
use crate::complex::{known_animals, Animals};
use maplit::*;
use pgx::*;
use pgx::prelude::*;

#[pg_test]
fn test_known_animals_via_spi() {
Expand Down
2 changes: 1 addition & 1 deletion pgx-examples/custom_types/src/fixed_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use pgx::cstr_core::CStr;
use pgx::*;
use pgx::{opname, pg_operator, prelude::*, PgVarlena, PgVarlenaInOutFuncs, StringInfo};
use std::str::FromStr;

#[derive(Copy, Clone, PostgresType)]
Expand Down
2 changes: 1 addition & 1 deletion pgx-examples/custom_types/src/generic_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

use pgx::*;
use pgx::prelude::*;
use serde::*;
use std::fmt::{Display, Formatter};

Expand Down
2 changes: 1 addition & 1 deletion pgx-examples/custom_types/src/hstore_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Use of this source code is governed by the MIT license that can be found in the
*/

use maplit::*;
use pgx::*;
use pgx::prelude::*;
use serde::*;
use std::collections::HashMap;

Expand Down
4 changes: 2 additions & 2 deletions pgx-examples/custom_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

use pgx::*;
use pgx::prelude::*;

mod complex;
mod fixed_size;
mod generic_enum;
mod hstore_clone;

pg_module_magic!();
pgx::pg_module_magic!();

#[cfg(test)]
#[pg_schema]
Expand Down
6 changes: 3 additions & 3 deletions pgx-examples/errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

use pgx::*;
use pgx::{error, info, prelude::*, warning, PgRelation, FATAL, PANIC};

pg_module_magic!();
pgx::pg_module_magic!();

#[pg_extern]
fn array_with_null_and_panic(input: Vec<Option<i32>>) -> i64 {
Expand Down Expand Up @@ -68,7 +68,7 @@ fn throw_pg_fatal(message: &str) {
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgx::*;
use pgx::prelude::*;

#[pg_test]
fn test_it() {
Expand Down
2 changes: 1 addition & 1 deletion pgx-examples/nostd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serde::{Deserialize, Serialize};

use alloc::string::String;

pg_module_magic!();
pgx::pg_module_magic!();

/// standard Rust equality/comparison derives
#[derive(Eq, PartialEq, Ord, Hash, PartialOrd)]
Expand Down
4 changes: 2 additions & 2 deletions pgx-examples/operators/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

use pgx::*;
use pgx::{opname, pg_operator, prelude::*};
use serde::{Deserialize, Serialize};
mod derived;

pg_module_magic!();
pgx::pg_module_magic!();

#[derive(PostgresType, Serialize, Deserialize, Eq, PartialEq)]
pub struct MyType {
Expand Down
8 changes: 4 additions & 4 deletions pgx-examples/schemas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Use of this source code is governed by the MIT license that can be found in the
/// All top-level pgx objects, **regardless** of the ".rs" file they're defined in, are created
/// in the schema determined by `CREATE EXTENSION`. It could be `public` (the default), or a
/// user-specified schema. We have no idea what that is.
use pgx::*;
use pgx::prelude::*;
use serde::{Deserialize, Serialize};

pg_module_magic!();
pgx::pg_module_magic!();

#[derive(PostgresType, Serialize, Deserialize)]
pub struct MyType(pub(crate) String);
Expand Down Expand Up @@ -54,7 +54,7 @@ mod pg_catalog {
/// proper permissions by the user calling `CREATE EXTENSION`
#[pg_schema]
mod public {
use pgx::*;
use pgx::prelude::*;

#[pg_extern]
pub fn hello_public() -> &'static str {
Expand All @@ -68,7 +68,7 @@ mod tests {
use crate::pg_catalog::MyPgCatalogType;
use crate::some_schema::MySomeSchemaType;
use crate::MyType;
use pgx::*;
use pgx::prelude::*;

#[pg_test]
fn test_hello_default_schema() {
Expand Down
4 changes: 2 additions & 2 deletions pgx-examples/shmem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use pgx::*;
use pgx::{atomics::*, lwlock::PgLwLock, pg_shmem_init, prelude::*, shmem::*, warning};
use serde::*;
use std::iter::Iterator;
use std::sync::atomic::Ordering;

pg_module_magic!();
pgx::pg_module_magic!();

// types behind a `LwLock` must derive/implement `Copy` and `Clone`
#[derive(Copy, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions pgx-examples/spi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use pgx::*;
use pgx::{info, prelude::*, IntoDatum};

pg_module_magic!();
pgx::pg_module_magic!();

extension_sql!(
r#"
Expand Down
6 changes: 3 additions & 3 deletions pgx-examples/srf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

use pgx::*;
use pgx::prelude::*;

pg_module_magic!();
pgx::pg_module_magic!();

#[pg_extern]
fn generate_series(start: i64, finish: i64, step: default!(i64, 1)) -> SetOfIterator<'static, i64> {
Expand All @@ -36,7 +36,7 @@ fn return_tuple(
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgx::*;
use pgx::prelude::*;

#[pg_test]
fn test_it() {
Expand Down
6 changes: 3 additions & 3 deletions pgx-examples/strings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/

use pgx::*;
use pgx::prelude::*;

pg_module_magic!();
pgx::pg_module_magic!();

#[pg_extern]
fn return_static() -> &'static str {
Expand Down Expand Up @@ -44,7 +44,7 @@ fn split_set<'a>(input: &'a str, pattern: &'a str) -> SetOfIterator<'a, &'a str>
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use pgx::*;
use pgx::prelude::*;

#[pg_test]
fn test_it() {
Expand Down
Loading

0 comments on commit b48c8db

Please sign in to comment.