pg_pool_safe_query/
lib.rs1#[derive(Debug)]
2pub struct TransactionPoolCompatibility {
3 is_compatible: bool,
4 reason: String,
5 statement_types: Vec<String>
6}
7
8impl TransactionPoolCompatibility {
9 pub fn new(is_compatible: bool, reason: String, statement_types: Vec<String>) -> TransactionPoolCompatibility {
10 TransactionPoolCompatibility {
11 is_compatible,
12 reason,
13 statement_types
14 }
15 }
16
17 pub fn parse(subject: String) -> TransactionPoolCompatibility {
19 let result = pg_query::parse(&subject).unwrap();
20 for (node, depth, parent) in result.protobuf.nodes() {
21 match node {
23 pg_query::NodeRef::VariableSetStmt(stmt) => println!("got em! {:?} {:?}", stmt, stmt.is_local),
24 _ => println!("nope"),
25 }
26 }
27
28 TransactionPoolCompatibility {
29 is_compatible: true,
30 reason: "it's compatible".to_string(),
31 statement_types: vec!["SELECT".to_string(), "INSERT".to_string()]
32 }
33 }
34}
35
36pub fn is_pgbouncer_compatible(subject: String) -> String {
37 let result = pg_query::parse(&subject.to_string());
38 assert!(result.is_ok());
39 let result = result.unwrap();
40 println!("{:?}", result.statement_types());
41 result.statement_types()[0].to_string()
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn it_is_pgbouncer_compatible() {
50 let result = TransactionPoolCompatibility::parse(
60 "SELECT * FROM contacts; SET statement_timeout TO '1s';".to_string()
61 );
62 println!("{:?}", result);
63
64 assert_eq!(is_pgbouncer_compatible("SET LOCAL lock_timeout TO '10s';".to_string()), "VariableSetStmt");
88 assert_eq!(is_pgbouncer_compatible("LISTEN channel_name;".to_string()), "ListenStmt");
89 assert_eq!(is_pgbouncer_compatible("UNLISTEN channel_name;".to_string()), "UnlistenStmt");
90 assert_eq!(is_pgbouncer_compatible("BEGIN;".to_string()), "TransactionStmt");
91 assert_eq!(is_pgbouncer_compatible("COMMIT;".to_string()), "TransactionStmt");
92 assert_eq!(is_pgbouncer_compatible("ROLLBACK;".to_string()), "TransactionStmt");
93 assert_eq!(is_pgbouncer_compatible("PREPARE prepared_statement AS SELECT * FROM my_table;".to_string()), "PrepareStmt");
94 assert_eq!(is_pgbouncer_compatible("EXECUTE prepared_statement;".to_string()), "ExecuteStmt");
95 assert_eq!(is_pgbouncer_compatible("DEALLOCATE prepared_statement;".to_string()), "DeallocateStmt");
96 }
97}