Skip to content

Commit

Permalink
Merge pull request #153 from supabase/chore/helloworld-fdw-error
Browse files Browse the repository at this point in the history
chore(helloworld_fdw): refactor error reporting
  • Loading branch information
burmecia authored Sep 20, 2023
2 parents 91c9d7f + 1099f5d commit b5c615c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
14 changes: 8 additions & 6 deletions supabase-wrappers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
//! use pgrx::PgSqlErrorCode;
//! use supabase_wrappers::prelude::*;
//! #[wrappers_fdw(
//! version = "0.1.0",
//! version = "0.1.1",
//! author = "Supabase",
//! website = "https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/helloworld_fdw",
//! error_type = "HelloWorldFdwError"
Expand All @@ -75,7 +75,7 @@
//! //row counter
//! row_cnt: i64,
//!
//! // target column name list
//! // target column list
//! tgt_cols: Vec<Column>,
//! }
//!
Expand All @@ -87,8 +87,10 @@
//! }
//! }
//!
//! type HelloWorldFdwResult<T> = Result<T, HelloWorldFdwError>;
//!
//! impl ForeignDataWrapper<HelloWorldFdwError> for HelloWorldFdw {
//! fn new(options: &HashMap<String, String>) -> Result<Self, HelloWorldFdwError> {
//! fn new(options: &HashMap<String, String>) -> HelloWorldFdwResult<Self> {
//! // 'options' is the key-value pairs defined in `CREATE SERVER` SQL, for example,
//! //
//! // create server my_helloworld_server
Expand All @@ -108,17 +110,17 @@
//! })
//! }
//!
//! fn begin_scan(&mut self, quals: &[Qual], columns: &[Column], sorts: &[Sort], limit: &Option<Limit>, options: &HashMap<String, String>) -> Result<(), HelloWorldFdwError> {
//! fn begin_scan(&mut self, quals: &[Qual], columns: &[Column], sorts: &[Sort], limit: &Option<Limit>, options: &HashMap<String, String>) -> HelloWorldFdwResult<()> {
//! // Do any initilization
//! Ok(())
//! }
//!
//! fn iter_scan(&mut self, row: &mut Row) -> Result<Option<()>, HelloWorldFdwError> {
//! fn iter_scan(&mut self, row: &mut Row) -> HelloWorldFdwResult<Option<()>> {
//! // Return None when done
//! Ok(None)
//! }
//!
//! fn end_scan(&mut self) -> Result<(), HelloWorldFdwError> {
//! fn end_scan(&mut self) -> HelloWorldFdwResult<()> {
//! // Cleanup any resources
//! Ok(())
//! }
Expand Down
1 change: 1 addition & 0 deletions wrappers/src/fdw/helloworld_fdw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ wrappers=# select * from hello;

| Version | Date | Notes |
| ------- | ---------- | ---------------------------------------------------- |
| 0.1.1 | 2023-09-20 | Error reporting refactoring |
| 0.1.0 | 2022-11-30 | Initial version |
14 changes: 8 additions & 6 deletions wrappers/src/fdw/helloworld_fdw/helloworld_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use supabase_wrappers::prelude::*;

// A simple demo FDW
#[wrappers_fdw(
version = "0.1.0",
version = "0.1.1",
author = "Supabase",
website = "https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/helloworld_fdw",
error_type = "HelloWorldFdwError"
Expand All @@ -14,7 +14,7 @@ pub(crate) struct HelloWorldFdw {
// row counter
row_cnt: i64,

// target column name list
// target column list
tgt_cols: Vec<Column>,
}

Expand All @@ -26,6 +26,8 @@ impl From<HelloWorldFdwError> for ErrorReport {
}
}

type HelloWorldFdwResult<T> = Result<T, HelloWorldFdwError>;

impl ForeignDataWrapper<HelloWorldFdwError> for HelloWorldFdw {
// 'options' is the key-value pairs defined in `CREATE SERVER` SQL, for example,
//
Expand All @@ -40,7 +42,7 @@ impl ForeignDataWrapper<HelloWorldFdwError> for HelloWorldFdw {
// You can do any initalization in this new() function, like saving connection
// info or API url in an variable, but don't do any heavy works like making a
// database connection or API call.
fn new(_options: &HashMap<String, String>) -> Result<Self, HelloWorldFdwError> {
fn new(_options: &HashMap<String, String>) -> HelloWorldFdwResult<Self> {
Ok(Self {
row_cnt: 0,
tgt_cols: Vec::new(),
Expand All @@ -54,7 +56,7 @@ impl ForeignDataWrapper<HelloWorldFdwError> for HelloWorldFdw {
_sorts: &[Sort],
_limit: &Option<Limit>,
_options: &HashMap<String, String>,
) -> Result<(), HelloWorldFdwError> {
) -> HelloWorldFdwResult<()> {
// reset row counter
self.row_cnt = 0;

Expand All @@ -64,7 +66,7 @@ impl ForeignDataWrapper<HelloWorldFdwError> for HelloWorldFdw {
Ok(())
}

fn iter_scan(&mut self, row: &mut Row) -> Result<Option<()>, HelloWorldFdwError> {
fn iter_scan(&mut self, row: &mut Row) -> HelloWorldFdwResult<Option<()>> {
// this is called on each row and we only return one row here
if self.row_cnt < 1 {
// add values to row if they are in target column list
Expand All @@ -86,7 +88,7 @@ impl ForeignDataWrapper<HelloWorldFdwError> for HelloWorldFdw {
Ok(None)
}

fn end_scan(&mut self) -> Result<(), HelloWorldFdwError> {
fn end_scan(&mut self) -> HelloWorldFdwResult<()> {
// we do nothing here, but you can do things like resource cleanup and etc.
Ok(())
}
Expand Down

0 comments on commit b5c615c

Please sign in to comment.