Skip to content

Commit

Permalink
add commented_query commented_query_with_transaction for mysql
Browse files Browse the repository at this point in the history
Summary:
In source control we would like to add comments to our sql queries so we can see them in mysql_client_logs and server-side (xdb is the server in this scenario) to be able to trace sql usage e2e.

This for now adds the functionality of adding comments to just mysql queries and just read queries

Reviewed By: liubov-dmitrieva

Differential Revision: D52954034

fbshipit-source-id: 48988164832d76b9138e381399deaece68127368
  • Loading branch information
mzr authored and facebook-github-bot committed Jan 23, 2024
1 parent fe31e0a commit 985bf05
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 17 deletions.
120 changes: 108 additions & 12 deletions shed/sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,19 @@ macro_rules! queries {
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<Vec<($( $rtype, )*)>, Error> {
query_internal(connection $( , $pname )* $( , $lname )*)
query_internal(connection, None $( , $pname )* $( , $lname )*)
.await
.context(stringify!(While executing $name query))
}

#[allow(dead_code)]
pub async fn commented_query(
connection: & Connection,
comment: &str,
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<Vec<($( $rtype, )*)>, Error> {
query_internal(connection, Some(comment) $( , $pname )* $( , $lname )*)
.await
.context(stringify!(While executing $name query))
}
Expand All @@ -174,7 +186,19 @@ macro_rules! queries {
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<(Transaction, Vec<($( $rtype, )*)>), Error> {
query_internal_with_transaction(transaction $( , $pname )* $( , $lname )*)
query_internal_with_transaction(transaction, None $( , $pname )* $( , $lname )*)
.await
.context(stringify!(While executing $name query in transaction))
}

#[allow(dead_code)]
pub async fn commented_query_with_transaction(
transaction: Transaction,
comment: &str,
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<(Transaction, Vec<($( $rtype, )*)>), Error> {
query_internal_with_transaction(transaction, Some(comment) $( , $pname )* $( , $lname )*)
.await
.context(stringify!(While executing $name query in transaction))
}
Expand Down Expand Up @@ -219,7 +243,19 @@ macro_rules! queries {
values: &[($( & $vtype, )*)],
$( $pname: & $ptype ),*
) -> Result<WriteResult, Error> {
query_internal(connection, values $( , $pname )*)
query_internal(connection, None, values $( , $pname )*)
.await
.context(stringify!(While executing $name query))
}

#[allow(dead_code)]
pub async fn commented_query(
connection: &Connection,
comment: &str,
values: &[($( & $vtype, )*)],
$( $pname: & $ptype ),*
) -> Result<WriteResult, Error> {
query_internal(connection, Some(comment), values $( , $pname )*)
.await
.context(stringify!(While executing $name query))
}
Expand All @@ -230,7 +266,19 @@ macro_rules! queries {
values: &[($( & $vtype, )*)],
$( $pname: & $ptype ),*
) -> Result<(Transaction, WriteResult), Error> {
query_internal_with_transaction(transaction, values $( , $pname )*)
query_internal_with_transaction(transaction, None, values $( , $pname )*)
.await
.context(stringify!(While executing $name query))
}

#[allow(dead_code)]
pub async fn commented_query_with_transaction(
transaction: Transaction,
comment: &str,
values: &[($( & $vtype, )*)],
$( $pname: & $ptype ),*
) -> Result<(Transaction, WriteResult), Error> {
query_internal_with_transaction(transaction, Some(comment), values $( , $pname )*)
.await
.context(stringify!(While executing $name query))
}
Expand Down Expand Up @@ -278,7 +326,19 @@ macro_rules! queries {
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<WriteResult, Error> {
query_internal(connection $( , $pname )* $( , $lname )*)
query_internal(connection, None $( , $pname )* $( , $lname )*)
.await
.context(stringify!(While executing $name query))
}

#[allow(dead_code)]
pub async fn commented_query(
connection: &Connection,
comment: &str,
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<WriteResult, Error> {
query_internal(connection, Some(comment) $( , $pname )* $( , $lname )*)
.await
.context(stringify!(While executing $name query))
}
Expand All @@ -289,7 +349,19 @@ macro_rules! queries {
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<(Transaction, WriteResult), Error> {
query_internal_with_transaction(transaction $( , $pname )* $( , $lname )*)
query_internal_with_transaction(transaction, None $( , $pname )* $( , $lname )*)
.await
.context(stringify!(While executing $name query))
}

#[allow(dead_code)]
pub async fn commented_query_with_transaction(
transaction: Transaction,
comment: &str,
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<(Transaction, WriteResult), Error> {
query_internal_with_transaction(transaction, Some(comment) $( , $pname )* $( , $lname )*)
.await
.context(stringify!(While executing $name query))
}
Expand Down Expand Up @@ -345,6 +417,7 @@ macro_rules! _read_query_impl {

async fn query_internal(
connection: &Connection,
comment: Option<&str>,
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<Vec<($( $rtype, )*)>, Error> {
Expand All @@ -353,7 +426,10 @@ macro_rules! _read_query_impl {
sqlite_query(multithread_con $( , $pname )* $( , $lname )*).await
}
Connection::Mysql(conn) => {
let query = mysql_query($( $pname, )* $( $lname, )*);
let mut query = mysql_query($( $pname, )* $( $lname, )*);
if let Some(comment) = comment {
query.push_str(&format!(" # {}", comment));
}
conn.read_query(query).map_err(Error::from).await
}
Connection::OssMysql(conn) => {
Expand Down Expand Up @@ -394,6 +470,7 @@ macro_rules! _read_query_impl {

async fn query_internal_with_transaction(
mut transaction: Transaction,
comment: Option<&str>,
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<(Transaction, Vec<($( $rtype, )*)>), Error>{
Expand All @@ -410,7 +487,10 @@ macro_rules! _read_query_impl {
})
}
Transaction::Mysql(ref mut transaction) => {
let query = mysql_query($( $pname, )* $( $lname, )*);
let mut query = mysql_query($( $pname, )* $( $lname, )*);
if let Some(comment) = comment {
query.push_str(&format!(" # {}", comment));
}
let mut tr = transaction.take()
.expect("should be Some before transaction ended");
let result = tr.read_query(query).map_err(Error::from).await?;
Expand Down Expand Up @@ -549,6 +629,7 @@ macro_rules! _write_query_impl {

async fn query_internal(
connection: &Connection,
comment: Option<&str>,
values: &[($( & $vtype, )*)],
$( $pname: & $ptype ),*
) -> Result<WriteResult, Error> {
Expand All @@ -561,7 +642,10 @@ macro_rules! _write_query_impl {
sqlite_exec_query(multithread_con, values, $( $pname ),*).await
}
Connection::Mysql(conn) => {
let query = mysql_query(values, $( $pname ),*);
let mut query = mysql_query(values, $( $pname ),*);
if let Some(comment) = comment {
query.push_str(&format!(" # {}", comment));
}
let res = conn.write_query(query).map_err(Error::from).await?;
Ok(res.into())
}
Expand All @@ -575,6 +659,7 @@ macro_rules! _write_query_impl {

async fn query_internal_with_transaction(
mut transaction: Transaction,
comment: Option<&str>,
values: &[($( & $vtype, )*)],
$( $pname: & $ptype ),*
) -> Result<(Transaction, WriteResult), Error> {
Expand All @@ -595,7 +680,10 @@ macro_rules! _write_query_impl {
})
}
Transaction::Mysql(ref mut transaction) => {
let query = mysql_query(values, $( $pname ),*);
let mut query = mysql_query(values, $( $pname ),*);
if let Some(comment) = comment {
query.push_str(&format!(" # {}", comment));
}
let mut tr = transaction.take()
.expect("should be Some before transaction ended");

Expand Down Expand Up @@ -748,6 +836,7 @@ macro_rules! _write_query_impl {

async fn query_internal(
connection: &Connection,
comment: Option<&str>,
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<WriteResult, Error> {
Expand All @@ -756,7 +845,10 @@ macro_rules! _write_query_impl {
sqlite_exec_query(multithread_con $( , $pname )* $( , $lname )*).await
}
Connection::Mysql(conn) => {
let query = mysql_query($( $pname, )* $( $lname, )*);
let mut query = mysql_query($( $pname, )* $( $lname, )*);
if let Some(comment) = comment {
query.push_str(&format!(" # {}", comment));
}
let res = conn.write_query(query).map_err(Error::from).await?;
Ok(res.into())
}
Expand All @@ -770,6 +862,7 @@ macro_rules! _write_query_impl {

async fn query_internal_with_transaction(
mut transaction: Transaction,
comment: Option<&str>,
$( $pname: & $ptype, )*
$( $lname: & [ $ltype ], )*
) -> Result<(Transaction, WriteResult), Error> {
Expand All @@ -786,7 +879,10 @@ macro_rules! _write_query_impl {
})
}
Transaction::Mysql(ref mut transaction) => {
let query = mysql_query($( $pname, )* $( $lname, )*);
let mut query = mysql_query($( $pname, )* $( $lname, )*);
if let Some(comment) = comment {
query.push_str(&format!(" # {}", comment));
}
let mut tr = transaction.take()
.expect("should be Some before transaction ended");
let result = tr.write_query(query).map_err(Error::from).await?;
Expand Down
16 changes: 11 additions & 5 deletions shed/sql/tests_lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ pub async fn test_read_query(conn: Connection, semantics: TestSemantics) {
TestQuery::query(&conn, &A, &72).await.unwrap(),
vec![(44, B, B, 72)]
);
assert_eq!(TestQuery2::query(&conn).await.unwrap(), vec![(44, B)]);
assert_eq!(
TestQuery2::commented_query(&conn, "comment").await.unwrap(),
vec![(44, B)]
);
assert_eq!(
TestQuery6::query(&conn).await.unwrap(),
vec![(match semantics {
Expand All @@ -174,7 +177,9 @@ pub async fn test_datetime_query(conn: Connection) {
}

pub async fn test_write_query(conn: Connection) {
let res = TestQuery3::query(&conn, &[(&44,)]).await.unwrap();
let res = TestQuery3::commented_query(&conn, "comment", &[(&44,)])
.await
.unwrap();
assert_eq!(res.affected_rows(), 1);
assert_eq!(res.last_insert_id(), Some(1));

Expand Down Expand Up @@ -243,9 +248,10 @@ pub async fn in_transaction(transaction: Transaction, semantics: TestSemantics)
TestSemantics::Sqlite => assert_eq!(res.last_insert_id(), Some(3)),
}

let (transaction, res) = TestQuery4::query_with_transaction(transaction, &1, &3)
.await
.unwrap();
let (transaction, res) =
TestQuery4::commented_query_with_transaction(transaction, "comment", &1, &3)
.await
.unwrap();
assert_eq!(res, vec![(44,), (72,), (53,)]);

let (transaction, res) = TestQuery7::query_with_transaction(transaction, &123)
Expand Down

0 comments on commit 985bf05

Please sign in to comment.