Skip to content

Commit

Permalink
Introduce #[pg_extern(create_or_replace)] (#683)
Browse files Browse the repository at this point in the history
* Parameterize fn_sql with `CREATE {or_replace}`
* Add `{ExternArgs,Attribute}::CreateOrReplace` to SQL gen
* CREATE OR REPLACE when `create_or_replace` is used
  • Loading branch information
workingjubilee authored Sep 14, 2022
1 parent bb393a1 commit eb33247
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pgx-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod __reexports {

#[derive(Debug, Hash, Eq, PartialEq, Clone, PartialOrd, Ord)]
pub enum ExternArgs {
CreateOrReplace,
Immutable,
Strict,
Stable,
Expand All @@ -48,6 +49,7 @@ pub enum ExternArgs {
impl core::fmt::Display for ExternArgs {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ExternArgs::CreateOrReplace => write!(f, "CREATE OR REPLACE"),
ExternArgs::Immutable => write!(f, "IMMUTABLE"),
ExternArgs::Strict => write!(f, "STRICT"),
ExternArgs::Stable => write!(f, "STABLE"),
Expand All @@ -69,6 +71,7 @@ impl core::fmt::Display for ExternArgs {
impl ToTokens for ExternArgs {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
ExternArgs::CreateOrReplace => tokens.append(format_ident!("CreateOrReplace")),
ExternArgs::Immutable => tokens.append(format_ident!("Immutable")),
ExternArgs::Strict => tokens.append(format_ident!("Strict")),
ExternArgs::Stable => tokens.append(format_ident!("Stable")),
Expand Down Expand Up @@ -148,6 +151,7 @@ pub fn parse_extern_attributes(attr: TokenStream) -> HashSet<ExternArgs> {
TokenTree::Ident(i) => {
let name = i.to_string();
match name.as_str() {
"create_or_replace" => args.insert(ExternArgs::CreateOrReplace),
"immutable" => args.insert(ExternArgs::Immutable),
"strict" => args.insert(ExternArgs::Strict),
"stable" => args.insert(ExternArgs::Stable),
Expand Down
4 changes: 4 additions & 0 deletions pgx-utils/src/sql_entity_graph/pg_extern/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum Attribute {
Volatile,
Raw,
NoGuard,
CreateOrReplace,
ParallelSafe,
ParallelUnsafe,
ParallelRestricted,
Expand All @@ -43,6 +44,7 @@ impl Attribute {
Attribute::Volatile => quote! { ::pgx::utils::ExternArgs::Volatile },
Attribute::Raw => quote! { ::pgx::utils::ExternArgs::Raw },
Attribute::NoGuard => quote! { ::pgx::utils::ExternArgs::NoGuard },
Attribute::CreateOrReplace => quote! { ::pgx::utils::ExternArgs::CreateOrReplace },
Attribute::ParallelSafe => {
quote! { ::pgx::utils::ExternArgs::ParallelSafe }
}
Expand Down Expand Up @@ -88,6 +90,7 @@ impl ToTokens for Attribute {
Attribute::Volatile => quote! { volatile },
Attribute::Raw => quote! { raw },
Attribute::NoGuard => quote! { no_guard },
Attribute::CreateOrReplace => quote! { create_or_replace },
Attribute::ParallelSafe => {
quote! { parallel_safe }
}
Expand Down Expand Up @@ -135,6 +138,7 @@ impl Parse for Attribute {
"volatile" => Self::Volatile,
"raw" => Self::Raw,
"no_guard" => Self::NoGuard,
"create_or_replace" => Self::CreateOrReplace,
"parallel_safe" => Self::ParallelSafe,
"parallel_unsafe" => Self::ParallelUnsafe,
"parallel_restricted" => Self::ParallelRestricted,
Expand Down
3 changes: 2 additions & 1 deletion pgx-utils/src/sql_entity_graph/pg_extern/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,13 @@ impl ToSql for PgExternEntity {

let fn_sql = format!(
"\
CREATE FUNCTION {schema}\"{name}\"({arguments}) {returns}\n\
CREATE {or_replace} FUNCTION {schema}\"{name}\"({arguments}) {returns}\n\
{extern_attrs}\
{search_path}\
LANGUAGE c /* Rust */\n\
AS '{module_pathname}', '{unaliased_name}_wrapper';\
",
or_replace = if extern_attrs.contains(&ExternArgs::CreateOrReplace) { "OR REPLACE" } else { "" },
schema = self
.schema
.map(|schema| format!("{}.", schema))
Expand Down

0 comments on commit eb33247

Please sign in to comment.