Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed May 7, 2024
1 parent 85f8247 commit f201e60
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl EventDB {
let chunk_size = (i16::MAX / 5) as usize;
for chunk in values.chunks(chunk_size) {
// Build query VALUES statements
let values_strings = prepare_sql_params_list(5, chunk.len());
let values_strings = prepare_sql_params_list(&[None; 5], chunk.len());

let query = format!("INSERT INTO cardano_slot_index (slot_no, network, epoch_no, block_time, block_hash) VALUES {} ON CONFLICT DO NOTHING", values_strings.join(","));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl EventDB {
let chunk_size = (i16::MAX / 8) as usize;
for chunk in values.chunks(chunk_size) {
// Build query VALUES statements
let values_strings = prepare_sql_params_list(8, chunk.len());
let values_strings = prepare_sql_params_list(&[None; 8], chunk.len());

let query = format!(
r#"INSERT INTO cardano_voter_registration (tx_id, stake_credential, public_voting_key, payment_address, nonce, metadata_cip36, stats, valid) VALUES {}
Expand Down
9 changes: 6 additions & 3 deletions catalyst-gateway/bin/src/event_db/cardano/utxo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl EventDB {
// Postgres has a limit of i16::MAX parameters a query can have.
let chunk_size = (i16::MAX / 5) as usize;
for chunk in values.chunks(chunk_size) {
let values_strings = prepare_sql_params_list(5, chunk.len());
let values_strings = prepare_sql_params_list(&[None; 5], chunk.len());

let query = format!(
"INSERT INTO cardano_utxo (tx_id, index, asset, stake_credential, value) VALUES {} ON CONFLICT (index, tx_id) DO NOTHING",
Expand Down Expand Up @@ -177,7 +177,10 @@ impl EventDB {
// Postgres has a limit of i16::MAX parameters a query can have.
let chunk_size = (i16::MAX / 3) as usize;
for chunk in values.chunks(chunk_size) {
let values_strings = prepare_sql_params_list(3, chunk.len());
let values_strings = prepare_sql_params_list(
&[Some("bytea"), Some("bytea"), Some("integer")],
chunk.len(),
);

let query = format!(
"UPDATE cardano_utxo AS c SET spent_tx_id = v.tx_id FROM (VALUES {}) AS v(tx_id, output_hash, index) WHERE v.index = c.index AND v.output_hash = c.tx_id",
Expand Down Expand Up @@ -217,7 +220,7 @@ impl EventDB {
let chunk_size = (i16::MAX / 3) as usize;
for chunk in values.chunks(chunk_size) {
// Build query VALUES statements
let values_strings = prepare_sql_params_list(3, chunk.len());
let values_strings = prepare_sql_params_list(&[None; 3], chunk.len());

let query = format!(
"INSERT INTO cardano_txn_index (id, slot_no, network) VALUES {} ON CONFLICT (id) DO NOTHING",
Expand Down
15 changes: 12 additions & 3 deletions catalyst-gateway/bin/src/event_db/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
//! Utility functions for the event db operations

/// `ParamType` alias
pub(crate) type ParamType<'a> = Option<&'a str>;

/// Prepare SQL parameters list from the provided list size and number of parameters.
/// Output format: `[($1, $2, $3), ($4, $5, $6)]`
pub(crate) fn prepare_sql_params_list(params_num: usize, list_size: usize) -> Vec<String> {
pub(crate) fn prepare_sql_params_list(params: &[ParamType], list_size: usize) -> Vec<String> {
let params_num = params.len();
(0..list_size)
.map(|row| {
let placeholders: String = (0..params_num)
.map(|i| format!("${}", row * params_num + i + 1))
let placeholders: String = params
.iter()
.enumerate()
.map(|(i, param_type)| {
let param_type = param_type.map(|val| format!("::{val}")).unwrap_or_default();
format!("${}{param_type}", row * params_num + i + 1)
})
.collect::<Vec<_>>()
.join(",");
format!("({placeholders})")
Expand Down

0 comments on commit f201e60

Please sign in to comment.