Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgmq-core - make the enqueue method return a parametrized query #186

Merged
merged 6 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pgmq-core"
version = "0.8.4"
version = "0.8.5"
edition = "2021"
authors = ["Tembo.io"]
description = "Core functionality shared between the PGMQ Rust SDK and Postgres Extension"
Expand Down
22 changes: 7 additions & 15 deletions core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,14 @@ pub fn purge_queue(name: &str) -> Result<String, PgmqError> {
Ok(format!("DELETE FROM {PGMQ_SCHEMA}.{QUEUE_PREFIX}_{name};"))
}

pub fn enqueue(
name: &str,
messages: &[serde_json::Value],
delay: &u64,
) -> Result<String, PgmqError> {
pub fn enqueue(name: &str, messages_num: usize, delay: &u64) -> Result<String, PgmqError> {
// construct string of comma separated messages
check_input(name)?;
let mut values = "".to_owned();
for message in messages.iter() {
let full_msg = format!("((now() + interval '{delay} seconds'), '{message}'::json),");
values.push_str(&full_msg)

for i in 0..messages_num {
let full_msg = format!("((now() + interval '{delay} seconds'), ${}::json),", i + 1);
values.push_str(&full_msg);
}
// drop trailing comma from constructed string
values.pop();
Expand Down Expand Up @@ -408,14 +405,9 @@ $$ LANGUAGE plpgsql;

#[test]
fn test_enqueue() {
let mut msgs: Vec<serde_json::Value> = Vec::new();
let msg = serde_json::json!({
"foo": "bar"
});
msgs.push(msg);
let query = enqueue("yolo", &msgs, &0).unwrap();
let query = enqueue("yolo", 1, &0).unwrap();
assert!(query.contains("q_yolo"));
assert!(query.contains("{\"foo\":\"bar\"}"));
assert!(query.contains("(now() + interval '0 seconds'), $1::json)"));
}

#[test]
Expand Down
Loading