Returns the total number of rows present in the specified table.
(
client: &mut Client,
schema: &str,
table: &str,
)
| 873 | |
| 874 | /// Returns the total number of rows present in the specified table. |
| 875 | pub async fn snapshot_size( |
| 876 | client: &mut Client, |
| 877 | schema: &str, |
| 878 | table: &str, |
| 879 | ) -> Result<usize, SqlServerError> { |
| 880 | let query = format!( |
| 881 | "SELECT COUNT(*) FROM {schema_name}.{table_name};", |
| 882 | schema_name = quote_identifier(schema), |
| 883 | table_name = quote_identifier(table) |
| 884 | ); |
| 885 | let result = client.query(query, &[]).await?; |
| 886 | |
| 887 | match &result[..] { |
| 888 | [row] => match row.try_get::<i32, _>(0)? { |
| 889 | Some(count @ 0..) => Ok(usize::try_from(count).expect("known to fit")), |
| 890 | Some(negative) => Err(SqlServerError::InvalidData { |
| 891 | column_name: "count".to_string(), |
| 892 | error: format!("found negative count: {negative}"), |
| 893 | }), |
| 894 | None => Err(SqlServerError::InvalidData { |
| 895 | column_name: "count".to_string(), |
| 896 | error: "expected a value found NULL".to_string(), |
| 897 | }), |
| 898 | }, |
| 899 | other => Err(SqlServerError::InvariantViolated(format!( |
| 900 | "expected one row, got {other:?}" |
| 901 | ))), |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | /// Helper function to parse an expected result from a "system" query. |
| 906 | fn check_system_result<'a, T>( |