Skip to content

Commit

Permalink
Silently introduce set_psql_array to workaround issue about array +…
Browse files Browse the repository at this point in the history
… Postgres support

This is a temporary fix on the public interface though (so we will keep it around probably deprecated later).

This enables `substreams init` to handle array for Postgres case. A similar method is available for clickhouse but it seems `substreams-sink-sql` is unable to handle it properly, at least for `Int256/Uint256` types.
  • Loading branch information
maoueh committed Jan 30, 2024
1 parent 98f2b32 commit 818c26d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,50 @@ impl Row {
self.columns.insert(name.to_string(), value);
self
}

/// Set a field to an array of values compatible with PostgresSQL database,
/// this method is currently experimental and hidden as we plan to support
/// array natively in the model.
///
/// For now, this method should be used with great care as it ties the model
/// to the database implementation.
#[doc(hidden)]
pub fn set_psql_array<T: ToDatabaseValue>(&mut self, name: &str, value: Vec<T>) -> &mut Row {
if self.operation == Operation::Delete {
panic!("cannot set fields on a delete operation")
}

let values = value
.into_iter()
.map(|x| x.to_value())
.collect::<Vec<_>>()
.join(",");

self.columns.insert(name.to_string(), format!("'{{{}}}'", values));
self
}

/// Set a field to an array of values compatible with Clickhouse database,
/// this method is currently experimental and hidden as we plan to support
/// array natively in the model.
///
/// For now, this method should be used with great care as it ties the model
/// to the database implementation.
#[doc(hidden)]
pub fn set_clickhouse_array<T: ToDatabaseValue>(&mut self, name: &str, value: Vec<T>) -> &mut Row {
if self.operation == Operation::Delete {
panic!("cannot set fields on a delete operation")
}

let values = value
.into_iter()
.map(|x| x.to_value())
.collect::<Vec<_>>()
.join(",");

self.columns.insert(name.to_string(), format!("[{}]", values));
self
}
}

macro_rules! impl_to_database_value_proxy_to_ref {
Expand Down

0 comments on commit 818c26d

Please sign in to comment.