From fe7f7d4a34b37770a4626f297ca0168f93845c50 Mon Sep 17 00:00:00 2001 From: pawurb Date: Wed, 24 Jan 2024 09:34:09 +0100 Subject: [PATCH] Use sqlx --- Cargo.toml | 6 +- README.md | 18 +- src/lib.rs | 245 ++++++++++++++-------------- src/structs/all_locks.rs | 37 ++--- src/structs/bloat.rs | 20 +-- src/structs/blocking.rs | 31 ++-- src/structs/buffercache_stats.rs | 19 +-- src/structs/buffercache_usage.rs | 9 +- src/structs/cache_hit.rs | 14 +- src/structs/calls.rs | 27 ++- src/structs/connections.rs | 11 +- src/structs/db_settings.rs | 13 +- src/structs/duplicate_indexes.rs | 15 +- src/structs/extensions.rs | 13 +- src/structs/index_cache_hit.rs | 15 +- src/structs/index_scans.rs | 31 ++-- src/structs/index_size.rs | 11 +- src/structs/index_usage.rs | 13 +- src/structs/indexes.rs | 13 +- src/structs/locks.rs | 37 ++--- src/structs/long_running_queries.rs | 22 ++- src/structs/mandelbrot.rs | 7 +- src/structs/null_indexes.rs | 33 ++-- src/structs/outliers.rs | 25 +-- src/structs/records_rank.rs | 9 +- src/structs/seq_scans.rs | 9 +- src/structs/shared.rs | 14 +- src/structs/ssl_used.rs | 7 +- src/structs/table_cache_hit.rs | 15 +- src/structs/table_index_scans.rs | 9 +- src/structs/table_indexes_size.rs | 9 +- src/structs/table_size.rs | 11 +- src/structs/tables.rs | 9 +- src/structs/total_index_size.rs | 7 +- src/structs/total_table_size.rs | 9 +- src/structs/unused_indexes.rs | 13 +- src/structs/vacuum_stats.rs | 21 +-- 37 files changed, 421 insertions(+), 406 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c2bb1b9..1a30728 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,7 @@ exclude = [ ] [dependencies] -pg_interval = "0.4.2" -postgres = "0.19.7" prettytable-rs = "^0.10" -rust_decimal = {version = "1.32", features = ["db-postgres"]} -rust_decimal_macros = "1.33" +sqlx = {version = "0.6", features = ["runtime-tokio-rustls", "postgres", "macros", "bigdecimal"]} thiserror = "1.0" +tokio = {version = "1", features = ["full"]} diff --git a/README.md b/README.md index 29dd2bb..7b484b4 100644 --- a/README.md +++ b/README.md @@ -240,10 +240,10 @@ This command provides information on the efficiency of indexes, represented as w ```rust struct Locks { - pid: String, + pid: i32, relname: String, transactionid: String, - granted: String, + granted: bool, mode: String, query_snippet: String, age: String, @@ -291,10 +291,10 @@ This command displays all the current locks, regardless of their type. ```rust struct Outliers { - total_exec_time: Interval, + total_exec_time: PgInterval, prop_exec_time: String, ncalls: String, - sync_io_time: Interval, + sync_io_time: PgInterval, query: String, } @@ -323,10 +323,10 @@ Typically, an efficient query will have an appropriate ratio of calls to total e ```rust struct Calls { qry: String, - exec_time: Interval, + exec_time: PgInterval, prop_exec_time: String, ncalls: String, - sync_io_time: Interval, + sync_io_time: PgInterval, } calls(limit: Option) -> Result, PgExtrasError> @@ -353,10 +353,10 @@ This command is much like `pg:outliers`, but ordered by the number of times a st struct Blocking { blocked_pid: i32, blocking_statement: String, - blocking_duration: Interval, + blocking_duration: PgInterval, blocking_pid: i32, blocked_statement: String, - blocked_duration: Interval, + blocked_duration: PgInterval, blocked_sql_app: String, blocking_sql_app: String, } @@ -536,7 +536,7 @@ struct NullIndexes { oid: String, index: String, index_size: String, - unique: String, + unique: bool, indexed_column: String, table: String, null_frac: String, diff --git a/src/lib.rs b/src/lib.rs index 413bf32..8a914ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -use postgres::{Client, NoTls}; use std::env; pub mod structs; pub use structs::all_locks::AllLocks; @@ -51,186 +50,190 @@ pub fn render_table(items: Vec) { table.printstd(); } -pub fn bloat() -> Result, PgExtrasError> { +pub async fn bloat() -> Result, PgExtrasError> { let query = Query::read_file(Query::Bloat); - get_rows::(query) + get_rows::(query).await } -pub fn blocking(limit: Option) -> Result, PgExtrasError> { +pub async fn blocking(limit: Option) -> Result, PgExtrasError> { let limit = limit.unwrap_or("10".to_string()); let query = Query::read_file(Query::Blocking).replace("%{limit}", limit.as_str()); - get_rows::(&query) + get_rows::(&query).await } -pub fn calls(limit: Option) -> Result, PgExtrasError> { +pub async fn calls(limit: Option) -> Result, PgExtrasError> { let limit = limit.unwrap_or("10".to_string()); let query = Query::read_file(Query::Calls).replace("%{limit}", limit.as_str()); - get_rows::(&query) + get_rows::(&query).await } -pub fn extensions() -> Result, PgExtrasError> { +pub async fn extensions() -> Result, PgExtrasError> { let query = Query::read_file(Query::Extensions); - get_rows::(query) + get_rows::(query).await } -pub fn table_cache_hit() -> Result, PgExtrasError> { +pub async fn table_cache_hit() -> Result, PgExtrasError> { let query = Query::read_file(Query::TableCacheHit); - get_rows::(query) + get_rows::(query).await } -pub fn tables(schema: Option) -> Result, PgExtrasError> { +pub async fn tables(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::Tables).replace("%{schema}", &schema_name); - get_rows::(&query) + get_rows::(&query).await } -pub fn index_cache_hit(schema: Option) -> Result, PgExtrasError> { +pub async fn index_cache_hit(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::IndexCacheHit).replace("%{schema}", &schema_name); - get_rows::(&query) + get_rows::(&query).await } -pub fn indexes() -> Result, PgExtrasError> { +pub async fn indexes() -> Result, PgExtrasError> { let query = Query::read_file(Query::Indexes); - get_rows::(query) + get_rows::(query).await } -pub fn index_size() -> Result, PgExtrasError> { +pub async fn index_size() -> Result, PgExtrasError> { let query = Query::read_file(Query::IndexSize); - get_rows::(query) + get_rows::(query).await } -pub fn index_usage(schema: Option) -> Result, PgExtrasError> { +pub async fn index_usage(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::IndexUsage).replace("%{schema}", &schema_name); - get_rows::(&query) + get_rows::(&query).await } -pub fn index_scans(schema: Option) -> Result, PgExtrasError> { +pub async fn index_scans(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::IndexScans).replace("%{schema}", &schema_name); - get_rows::(&query) + get_rows::(&query).await } -pub fn null_indexes( +pub async fn null_indexes( min_relation_size_mb: Option, ) -> Result, PgExtrasError> { let min_relation_size_mb = min_relation_size_mb.unwrap_or("0".to_string()); let query = Query::read_file(Query::NullIndexes) .replace("%{min_relation_size_mb}", &min_relation_size_mb); - get_rows::(&query) + get_rows::(&query).await } -pub fn locks() -> Result, PgExtrasError> { +pub async fn locks() -> Result, PgExtrasError> { let query = Query::read_file(Query::Locks); - get_rows::(query) + get_rows::(query).await } -pub fn all_locks() -> Result, PgExtrasError> { +pub async fn all_locks() -> Result, PgExtrasError> { let query = Query::read_file(Query::AllLocks); - get_rows::(query) + get_rows::(query).await } -pub fn long_running_queries() -> Result, PgExtrasError> { +pub async fn long_running_queries() -> Result, PgExtrasError> { let query = Query::read_file(Query::LongRunningQueries); - get_rows::(query) + get_rows::(query).await } -pub fn mandelbrot() -> Result, PgExtrasError> { +pub async fn mandelbrot() -> Result, PgExtrasError> { let query = Query::read_file(Query::Mandelbrot); - get_rows::(query) + get_rows::(query).await } -pub fn outliers() -> Result, PgExtrasError> { +pub async fn outliers() -> Result, PgExtrasError> { let query = Query::read_file(Query::Outliers); - get_rows::(query) + get_rows::(query).await } -pub fn records_rank(schema: Option) -> Result, PgExtrasError> { +pub async fn records_rank(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::RecordsRank).replace("%{schema}", schema_name.as_str()); - get_rows::(&query) + get_rows::(&query).await } -pub fn seq_scans(schema: Option) -> Result, PgExtrasError> { +pub async fn seq_scans(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::SeqScans).replace("%{schema}", schema_name.as_str()); - get_rows::(&query) + get_rows::(&query).await } -pub fn table_index_scans(schema: Option) -> Result, PgExtrasError> { +pub async fn table_index_scans( + schema: Option, +) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::TableIndexScans).replace("%{schema}", schema_name.as_str()); - get_rows::(&query) + get_rows::(&query).await } -pub fn table_indexes_size(schema: Option) -> Result, PgExtrasError> { +pub async fn table_indexes_size( + schema: Option, +) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::TableIndexesSize).replace("%{schema}", schema_name.as_str()); - get_rows::(&query) + get_rows::(&query).await } -pub fn table_size() -> Result, PgExtrasError> { +pub async fn table_size() -> Result, PgExtrasError> { let query = Query::read_file(Query::TableSize); - get_rows::(query) + get_rows::(query).await } -pub fn total_index_size() -> Result, PgExtrasError> { +pub async fn total_index_size() -> Result, PgExtrasError> { let query = Query::read_file(Query::TotalIndexSize); - get_rows::(query) + get_rows::(query).await } -pub fn total_table_size() -> Result, PgExtrasError> { +pub async fn total_table_size() -> Result, PgExtrasError> { let query = Query::read_file(Query::TotalTableSize); - get_rows::(query) + get_rows::(query).await } -pub fn unused_indexes(schema: Option) -> Result, PgExtrasError> { +pub async fn unused_indexes(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::UnusedIndexes).replace("%{schema}", schema_name.as_str()); - get_rows::(&query) + get_rows::(&query).await } -pub fn duplicate_indexes() -> Result, PgExtrasError> { +pub async fn duplicate_indexes() -> Result, PgExtrasError> { let query = Query::read_file(Query::DuplicateIndexes); - get_rows::(query) + get_rows::(query).await } -pub fn vacuum_stats() -> Result, PgExtrasError> { +pub async fn vacuum_stats() -> Result, PgExtrasError> { let query = Query::read_file(Query::VacuumStats); - get_rows::(query) + get_rows::(query).await } -pub fn buffercache_stats() -> Result, PgExtrasError> { +pub async fn buffercache_stats() -> Result, PgExtrasError> { let query = Query::read_file(Query::BuffercacheStats); - get_rows::(query) + get_rows::(query).await } -pub fn buffercache_usage() -> Result, PgExtrasError> { +pub async fn buffercache_usage() -> Result, PgExtrasError> { let query = Query::read_file(Query::BuffercacheUsage); - get_rows::(query) + get_rows::(query).await } -pub fn ssl_used() -> Result, PgExtrasError> { +pub async fn ssl_used() -> Result, PgExtrasError> { let query = Query::read_file(Query::SslUsed); - get_rows::(query) + get_rows::(query).await } -pub fn connections() -> Result, PgExtrasError> { +pub async fn connections() -> Result, PgExtrasError> { let query = Query::read_file(Query::Connections); - get_rows::(query) + get_rows::(query).await } -pub fn cache_hit(schema: Option) -> Result, PgExtrasError> { +pub async fn cache_hit(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = Query::read_file(Query::CacheHit).replace("%{schema}", schema_name.as_str()); - get_rows::(&query) + get_rows::(&query).await } -pub fn db_settings() -> Result, PgExtrasError> { +pub async fn db_settings() -> Result, PgExtrasError> { let query = Query::read_file(Query::DbSettings); - get_rows::(query) + get_rows::(query).await } enum Query { @@ -319,66 +322,70 @@ pub enum PgExtrasError { Unknown, } -fn get_rows(query: &str) -> Result, PgExtrasError> { - Ok(connection()? - .query(query, &[]) - .unwrap_or_else(|_| Vec::new()) - .iter() - .map(T::new) - .collect()) -} +async fn get_rows(query: &str) -> Result, PgExtrasError> { + use sqlx::postgres::PgPoolOptions; + let pool = match PgPoolOptions::new() + .max_connections(5) + .connect(db_url()?.as_str()) + .await + { + Ok(pool) => pool, + Err(_) => return Err(PgExtrasError::ConnectionError()), + }; -fn connection() -> Result { - let database_url = - match env::var("PG_EXTRAS_DATABASE_URL").or_else(|_| env::var("DATABASE_URL")) { - Ok(url) => url, - Err(_) => return Err(PgExtrasError::MissingConfigVars()), - }; + Ok(match sqlx::query(query).fetch_all(&pool).await { + Ok(rows) => rows.iter().map(T::new).collect(), + Err(_) => return Err(PgExtrasError::Unknown), + }) +} - match Client::connect(&database_url, NoTls) { - Ok(client) => Ok(client), - Err(_) => Err(PgExtrasError::ConnectionError()), - } +fn db_url() -> Result { + env::var("PG_EXTRAS_DATABASE_URL") + .or_else(|_| env::var("DATABASE_URL")) + .map_err(|_| PgExtrasError::MissingConfigVars()) } #[cfg(test)] mod tests { + use crate::structs::db_settings; + use super::*; - #[test] - fn it_works() -> Result<(), PgExtrasError> { - render_table(cache_hit(None)?); - render_table(bloat()?); - render_table(blocking(None)?); - render_table(calls(None)?); - render_table(extensions()?); - render_table(table_cache_hit()?); - render_table(tables(None)?); - render_table(index_cache_hit(None)?); - render_table(indexes()?); - render_table(index_size()?); - render_table(index_usage(None)?); - render_table(index_scans(None)?); - render_table(null_indexes(None)?); - render_table(locks()?); - render_table(all_locks()?); - render_table(long_running_queries()?); - render_table(mandelbrot()?); - render_table(outliers()?); - render_table(records_rank(None)?); - render_table(seq_scans(None)?); - render_table(table_index_scans(None)?); - render_table(table_indexes_size(None)?); - render_table(table_size()?); - render_table(total_index_size()?); - render_table(total_table_size()?); - render_table(unused_indexes(None)?); - render_table(duplicate_indexes()?); - render_table(vacuum_stats()?); - render_table(buffercache_stats()?); - render_table(buffercache_usage()?); - render_table(ssl_used()?); - render_table(connections()?); + #[tokio::test] + async fn it_works() -> Result<(), PgExtrasError> { + render_table(cache_hit(None).await?); + render_table(bloat().await?); + render_table(blocking(None).await?); + render_table(calls(None).await?); + render_table(extensions().await?); + render_table(table_cache_hit().await?); + render_table(seq_scans(None).await?); + render_table(table_index_scans(None).await?); + render_table(table_indexes_size(None).await?); + render_table(tables(None).await?); + render_table(index_cache_hit(None).await?); + render_table(indexes().await?); + render_table(index_size().await?); + render_table(index_usage(None).await?); + render_table(index_scans(None).await?); + render_table(null_indexes(None).await?); + render_table(locks().await?); + render_table(all_locks().await?); + render_table(long_running_queries().await?); + render_table(mandelbrot().await?); + render_table(outliers().await?); + render_table(records_rank(None).await?); + render_table(table_size().await?); + render_table(total_index_size().await?); + render_table(total_table_size().await?); + render_table(unused_indexes(None).await?); + render_table(duplicate_indexes().await?); + render_table(vacuum_stats().await?); + render_table(buffercache_stats().await?); + render_table(buffercache_usage().await?); + render_table(ssl_used().await?); + render_table(connections().await?); + // render_table(db_settings().await?); Ok(()) } } diff --git a/src/structs/all_locks.rs b/src/structs/all_locks.rs index 4c582bb..b401fb7 100644 --- a/src/structs/all_locks.rs +++ b/src/structs/all_locks.rs @@ -1,36 +1,31 @@ use crate::structs::shared::{get_default_interval, Tabular}; -use pg_interval::Interval; -use postgres::Row; +use sqlx::postgres::types::PgInterval; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct AllLocks { - pid: String, + pid: i32, relname: String, transactionid: String, - granted: String, + granted: bool, mode: String, query_snippet: String, - age: String, + age: PgInterval, application: String, } impl Tabular for AllLocks { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - pid: row.get::<_, Option>(0).unwrap_or_default().to_string(), - relname: row.get::<_, Option>(1).unwrap_or_default(), - transactionid: row.get::<_, Option>(2).unwrap_or_default(), - granted: row - .get::<_, Option>(3) - .unwrap_or_default() - .to_string(), - mode: row.get::<_, Option>(4).unwrap_or_default(), - query_snippet: row.get::<_, Option>(5).unwrap_or_default(), - age: row - .get::<_, Option>(6) - .unwrap_or(get_default_interval()) - .to_iso_8601(), - application: row.get::<_, Option>(7).unwrap_or_default(), + pid: row.try_get("pid").unwrap_or_default(), + relname: row.try_get("relname").unwrap_or_default(), + transactionid: row.try_get("transactionid").unwrap_or_default(), + granted: row.try_get("granted").unwrap_or_default(), + mode: row.try_get("mode").unwrap_or_default(), + query_snippet: row.try_get("query_snippet").unwrap_or_default(), + age: row.try_get("age").unwrap_or(get_default_interval()), + application: row.try_get("application").unwrap_or_default(), } } @@ -42,7 +37,7 @@ impl Tabular for AllLocks { self.granted, self.mode, self.query_snippet, - self.age, + format!("{:?}", self.age), self.application ] } diff --git a/src/structs/bloat.rs b/src/structs/bloat.rs index 96bd2ed..3ad0441 100644 --- a/src/structs/bloat.rs +++ b/src/structs/bloat.rs @@ -1,25 +1,25 @@ use crate::structs::shared::Tabular; -use postgres::Row; -use rust_decimal::prelude::Decimal; -use rust_decimal_macros::dec; +use sqlx::postgres::PgRow; +use sqlx::types::BigDecimal; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Bloat { typefield: String, schemaname: String, object_name: String, - bloat: Decimal, + bloat: BigDecimal, waste: String, } impl Tabular for Bloat { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - typefield: row.get::<_, Option>(0).unwrap_or_default(), - schemaname: row.get::<_, Option>(1).unwrap_or_default(), - object_name: row.get::<_, Option>(2).unwrap_or_default(), - bloat: row.get::<_, Option>(3).unwrap_or(dec!(0)), - waste: row.get::<_, Option>(4).unwrap_or_default(), + typefield: row.try_get("type").unwrap_or_default(), + schemaname: row.try_get("schemaname").unwrap_or_default(), + object_name: row.try_get("object_name").unwrap_or_default(), + bloat: row.try_get("bloat").unwrap_or_default(), + waste: row.try_get("waste").unwrap_or_default(), } } diff --git a/src/structs/blocking.rs b/src/structs/blocking.rs index f53127c..ec2148b 100644 --- a/src/structs/blocking.rs +++ b/src/structs/blocking.rs @@ -1,34 +1,35 @@ use crate::structs::shared::{get_default_interval, Tabular}; -use pg_interval::Interval; -use postgres::Row; +use sqlx::postgres::types::PgInterval; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Blocking { blocked_pid: i32, blocking_statement: String, - blocking_duration: Interval, + blocking_duration: PgInterval, blocking_pid: i32, blocked_statement: String, - blocked_duration: Interval, + blocked_duration: PgInterval, blocked_sql_app: String, blocking_sql_app: String, } impl Tabular for Blocking { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - blocked_pid: row.get::<_, Option>(0).unwrap_or_default(), - blocking_statement: row.get::<_, Option>(1).unwrap_or_default(), + blocked_pid: row.try_get("blocked_pid").unwrap_or_default(), + blocking_statement: row.try_get("blocking_statement").unwrap_or_default(), blocking_duration: row - .get::<_, Option>(2) + .try_get("blocking_duration") .unwrap_or(get_default_interval()), - blocking_pid: row.get::<_, Option>(3).unwrap_or_default(), - blocked_statement: row.get::<_, Option>(4).unwrap_or_default(), + blocking_pid: row.try_get("blocking_pid").unwrap_or_default(), + blocked_statement: row.try_get("blocked_statement").unwrap_or_default(), blocked_duration: row - .get::<_, Option>(5) + .try_get("blocked_duration") .unwrap_or(get_default_interval()), - blocked_sql_app: row.get::<_, Option>(6).unwrap_or_default(), - blocking_sql_app: row.get::<_, Option>(7).unwrap_or_default(), + blocked_sql_app: row.try_get("blocked_sql_app").unwrap_or_default(), + blocking_sql_app: row.try_get("blocking_sql_app").unwrap_or_default(), } } @@ -36,10 +37,10 @@ impl Tabular for Blocking { row![ self.blocked_pid, self.blocking_statement, - self.blocking_duration.to_iso_8601(), + format!("{:?}", self.blocking_duration), self.blocking_pid, self.blocked_statement, - self.blocked_duration.to_iso_8601(), + format!("{:?}", self.blocked_duration), self.blocked_sql_app, self.blocking_sql_app ] diff --git a/src/structs/buffercache_stats.rs b/src/structs/buffercache_stats.rs index 7d4e53e..af7e9f8 100644 --- a/src/structs/buffercache_stats.rs +++ b/src/structs/buffercache_stats.rs @@ -1,23 +1,22 @@ use crate::structs::shared::Tabular; -use postgres::Row; -use rust_decimal::prelude::Decimal; -use rust_decimal_macros::dec; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct BuffercacheStats { relname: String, buffered: String, - buffer_percent: Decimal, - percent_of_relation: Decimal, + buffer_percent: f64, + percent_of_relation: f64, } impl Tabular for BuffercacheStats { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - relname: row.get::<_, Option>(0).unwrap_or_default(), - buffered: row.get::<_, Option>(1).unwrap_or_default(), - buffer_percent: row.get::<_, Option>(2).unwrap_or(dec!(0)), - percent_of_relation: row.get::<_, Option>(2).unwrap_or(dec!(0)), + relname: row.try_get("relname").unwrap_or_default(), + buffered: row.try_get("buffered").unwrap_or_default(), + buffer_percent: row.try_get("buffer_percent").unwrap_or_default(), + percent_of_relation: row.try_get("percent_of_relation").unwrap_or_default(), } } diff --git a/src/structs/buffercache_usage.rs b/src/structs/buffercache_usage.rs index 27c246f..26102b0 100644 --- a/src/structs/buffercache_usage.rs +++ b/src/structs/buffercache_usage.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct BuffercacheUsage { @@ -8,10 +9,10 @@ pub struct BuffercacheUsage { } impl Tabular for BuffercacheUsage { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - relname: row.get::<_, Option>(0).unwrap_or_default(), - buffers: row.get::<_, Option>(1).unwrap_or_default(), + relname: row.try_get("relname").unwrap_or_default(), + buffers: row.try_get("buffers").unwrap_or_default(), } } diff --git a/src/structs/cache_hit.rs b/src/structs/cache_hit.rs index 579a707..891fff2 100644 --- a/src/structs/cache_hit.rs +++ b/src/structs/cache_hit.rs @@ -1,19 +1,19 @@ use crate::structs::shared::Tabular; -use postgres::Row; -use rust_decimal::prelude::Decimal; -use rust_decimal_macros::dec; +use sqlx::postgres::PgRow; +use sqlx::types::BigDecimal; +use sqlx::Row; #[derive(Debug, Clone)] pub struct CacheHit { name: String, - ratio: Decimal, + ratio: BigDecimal, } impl Tabular for CacheHit { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - ratio: row.get::<_, Option>(1).unwrap_or(dec!(0)), + name: row.try_get("name").unwrap_or_default(), + ratio: row.try_get("ratio").unwrap_or_default(), } } diff --git a/src/structs/calls.rs b/src/structs/calls.rs index 58befa5..4d8b364 100644 --- a/src/structs/calls.rs +++ b/src/structs/calls.rs @@ -1,27 +1,26 @@ use crate::structs::shared::{get_default_interval, Tabular}; -use pg_interval::Interval; -use postgres::Row; +use sqlx::postgres::types::PgInterval; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Calls { qry: String, - exec_time: Interval, + exec_time: PgInterval, prop_exec_time: String, ncalls: String, - sync_io_time: Interval, + sync_io_time: PgInterval, } impl Tabular for Calls { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - qry: row.get::<_, Option>(0).unwrap_or_default(), - exec_time: row - .get::<_, Option>(1) - .unwrap_or(get_default_interval()), - prop_exec_time: row.get::<_, Option>(2).unwrap_or_default(), - ncalls: row.get::<_, Option>(3).unwrap_or_default(), + qry: row.try_get("qry").unwrap_or_default(), + exec_time: row.try_get("exec_time").unwrap_or(get_default_interval()), + prop_exec_time: row.try_get("prop_exec_time").unwrap_or_default(), + ncalls: row.try_get("ncalls").unwrap_or_default(), sync_io_time: row - .get::<_, Option>(4) + .try_get("sync_io_time") .unwrap_or(get_default_interval()), } } @@ -29,10 +28,10 @@ impl Tabular for Calls { fn to_row(&self) -> prettytable::Row { row![ self.qry, - self.exec_time.to_iso_8601(), + format!("{:?}", self.exec_time), self.prop_exec_time, self.ncalls, - self.sync_io_time.to_iso_8601() + format!("{:?}", self.sync_io_time), ] } diff --git a/src/structs/connections.rs b/src/structs/connections.rs index bdc9aee..84521d3 100644 --- a/src/structs/connections.rs +++ b/src/structs/connections.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Connections { @@ -9,11 +10,11 @@ pub struct Connections { } impl Tabular for Connections { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - username: row.get::<_, Option>(0).unwrap_or_default(), - pid: row.get::<_, Option>(1).unwrap_or_default(), - client_addr: row.get::<_, Option>(2).unwrap_or_default(), + username: row.try_get("username").unwrap_or_default(), + pid: row.try_get("pid").unwrap_or_default(), + client_addr: row.try_get("client_addr").unwrap_or_default(), } } diff --git a/src/structs/db_settings.rs b/src/structs/db_settings.rs index 4d86266..43c52f2 100644 --- a/src/structs/db_settings.rs +++ b/src/structs/db_settings.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct DbSetting { @@ -10,12 +11,12 @@ pub struct DbSetting { } impl Tabular for DbSetting { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - setting: row.get::<_, Option>(1).unwrap_or_default(), - unit: row.get::<_, Option>(2).unwrap_or_default(), - short_desc: row.get::<_, Option>(3).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + setting: row.try_get("setting").unwrap_or_default(), + unit: row.try_get("unit").unwrap_or_default(), + short_desc: row.try_get("short_desc").unwrap_or_default(), } } diff --git a/src/structs/duplicate_indexes.rs b/src/structs/duplicate_indexes.rs index 8775b86..d57bb85 100644 --- a/src/structs/duplicate_indexes.rs +++ b/src/structs/duplicate_indexes.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct DuplicateIndexes { @@ -11,13 +12,13 @@ pub struct DuplicateIndexes { } impl Tabular for DuplicateIndexes { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - size: row.get::<_, Option>(0).unwrap_or_default(), - idx1: row.get::<_, Option>(1).unwrap_or_default(), - idx2: row.get::<_, Option>(2).unwrap_or_default(), - idx3: row.get::<_, Option>(3).unwrap_or_default(), - idx4: row.get::<_, Option>(4).unwrap_or_default(), + size: row.try_get("size").unwrap_or_default(), + idx1: row.try_get("idx1").unwrap_or_default(), + idx2: row.try_get("idx2").unwrap_or_default(), + idx3: row.try_get("idx3").unwrap_or_default(), + idx4: row.try_get("idx4").unwrap_or_default(), } } diff --git a/src/structs/extensions.rs b/src/structs/extensions.rs index 55d4fa6..3bd1eec 100644 --- a/src/structs/extensions.rs +++ b/src/structs/extensions.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Extensions { @@ -10,12 +11,12 @@ pub struct Extensions { } impl Tabular for Extensions { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - default_version: row.get::<_, Option>(1).unwrap_or_default(), - installed_version: row.get::<_, Option>(2).unwrap_or_default(), - comment: row.get::<_, Option>(3).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + default_version: row.try_get("default_version").unwrap_or_default(), + installed_version: row.try_get("installed_version").unwrap_or_default(), + comment: row.try_get("comment").unwrap_or_default(), } } diff --git a/src/structs/index_cache_hit.rs b/src/structs/index_cache_hit.rs index e110635..be8d59e 100644 --- a/src/structs/index_cache_hit.rs +++ b/src/structs/index_cache_hit.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct IndexCacheHit { @@ -11,13 +12,13 @@ pub struct IndexCacheHit { } impl Tabular for IndexCacheHit { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - buffer_hits: row.get::<_, Option>(1).unwrap_or_default(), - block_reads: row.get::<_, Option>(2).unwrap_or_default(), - total_read: row.get::<_, Option>(3).unwrap_or_default(), - ratio: row.get::<_, Option>(4).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + buffer_hits: row.try_get("buffer_hits").unwrap_or_default(), + block_reads: row.try_get("block_reads").unwrap_or_default(), + total_read: row.try_get("total_read").unwrap_or_default(), + ratio: row.try_get("ratio").unwrap_or_default(), } } diff --git a/src/structs/index_scans.rs b/src/structs/index_scans.rs index 521a24a..f8bbce1 100644 --- a/src/structs/index_scans.rs +++ b/src/structs/index_scans.rs @@ -1,43 +1,38 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct IndexScans { schemaname: String, - tablename: String, - indexname: String, + table: String, + index: String, index_size: String, index_scans: i64, } impl Tabular for IndexScans { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - schemaname: row.get::<_, Option>(0).unwrap_or_default(), - tablename: row.get::<_, Option>(1).unwrap_or_default(), - indexname: row.get::<_, Option>(2).unwrap_or_default(), - index_size: row.get::<_, Option>(3).unwrap_or_default(), - index_scans: row.get::<_, Option>(4).unwrap_or_default(), + schemaname: row.try_get("schemaname").unwrap_or_default(), + table: row.try_get("table").unwrap_or_default(), + index: row.try_get("index").unwrap_or_default(), + index_size: row.try_get("index_size").unwrap_or_default(), + index_scans: row.try_get("index_scans").unwrap_or_default(), } } fn to_row(&self) -> prettytable::Row { row![ self.schemaname, - self.tablename, - self.indexname, + self.table, + self.index, self.index_size, self.index_scans.to_string() ] } fn headers() -> prettytable::Row { - row![ - "schemaname", - "tablename", - "indexname", - "index_size", - "index_scans" - ] + row!["schemaname", "table", "index", "index_size", "index_scans"] } } diff --git a/src/structs/index_size.rs b/src/structs/index_size.rs index 8ec3fd0..0cfd1b1 100644 --- a/src/structs/index_size.rs +++ b/src/structs/index_size.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct IndexSize { @@ -9,11 +10,11 @@ pub struct IndexSize { } impl Tabular for IndexSize { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - size: row.get::<_, Option>(1).unwrap_or_default(), - schema: row.get::<_, Option>(2).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + size: row.try_get("size").unwrap_or_default(), + schema: row.try_get("schema").unwrap_or_default(), } } diff --git a/src/structs/index_usage.rs b/src/structs/index_usage.rs index d43b68e..5ec3769 100644 --- a/src/structs/index_usage.rs +++ b/src/structs/index_usage.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct IndexUsage { @@ -9,11 +10,13 @@ pub struct IndexUsage { } impl Tabular for IndexUsage { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - relname: row.get::<_, Option>(0).unwrap_or_default(), - percent_of_times_index_used: row.get::<_, Option>(1).unwrap_or_default(), - rows_in_table: row.get::<_, Option>(2).unwrap_or_default(), + relname: row.try_get("relname").unwrap_or_default(), + percent_of_times_index_used: row + .try_get("percent_of_times_index_used") + .unwrap_or_default(), + rows_in_table: row.try_get("rows_in_table").unwrap_or_default(), } } diff --git a/src/structs/indexes.rs b/src/structs/indexes.rs index 1f5c7a2..85df282 100644 --- a/src/structs/indexes.rs +++ b/src/structs/indexes.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Indexes { @@ -10,12 +11,12 @@ pub struct Indexes { } impl Tabular for Indexes { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - schemaname: row.get::<_, Option>(0).unwrap_or_default(), - indexname: row.get::<_, Option>(1).unwrap_or_default(), - tablename: row.get::<_, Option>(2).unwrap_or_default(), - columns: row.get::<_, Option>(3).unwrap_or_default(), + schemaname: row.try_get("schemaname").unwrap_or_default(), + indexname: row.try_get("indexname").unwrap_or_default(), + tablename: row.try_get("tablename").unwrap_or_default(), + columns: row.try_get("columns").unwrap_or_default(), } } diff --git a/src/structs/locks.rs b/src/structs/locks.rs index b5e9995..6e28b1c 100644 --- a/src/structs/locks.rs +++ b/src/structs/locks.rs @@ -1,36 +1,31 @@ use crate::structs::shared::{get_default_interval, Tabular}; -use pg_interval::Interval; -use postgres::Row; +use sqlx::postgres::types::PgInterval; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Locks { - pid: String, + pid: i32, relname: String, transactionid: String, - granted: String, + granted: bool, mode: String, query_snippet: String, - age: String, + age: PgInterval, application: String, } impl Tabular for Locks { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - pid: row.get::<_, Option>(0).unwrap_or_default().to_string(), - relname: row.get::<_, Option>(1).unwrap_or_default(), - transactionid: row.get::<_, Option>(2).unwrap_or_default(), - granted: row - .get::<_, Option>(3) - .unwrap_or_default() - .to_string(), - mode: row.get::<_, Option>(4).unwrap_or_default(), - query_snippet: row.get::<_, Option>(5).unwrap_or_default(), - age: row - .get::<_, Option>(6) - .unwrap_or(get_default_interval()) - .to_iso_8601(), - application: row.get::<_, Option>(7).unwrap_or_default(), + pid: row.try_get("pid").unwrap_or_default(), + relname: row.try_get("relname").unwrap_or_default(), + transactionid: row.try_get("transactionid").unwrap_or_default(), + granted: row.try_get("granted").unwrap_or_default(), + mode: row.try_get("mode").unwrap_or_default(), + query_snippet: row.try_get("query_snippet").unwrap_or_default(), + age: row.try_get("age").unwrap_or(get_default_interval()), + application: row.try_get("application").unwrap_or_default(), } } @@ -42,7 +37,7 @@ impl Tabular for Locks { self.granted, self.mode, self.query_snippet, - self.age, + format!("{:?}", self.age), self.application ] } diff --git a/src/structs/long_running_queries.rs b/src/structs/long_running_queries.rs index 2270c16..b6aa69b 100644 --- a/src/structs/long_running_queries.rs +++ b/src/structs/long_running_queries.rs @@ -1,28 +1,26 @@ use crate::structs::shared::{get_default_interval, Tabular}; -use pg_interval::Interval; -use postgres::Row; +use sqlx::postgres::types::PgInterval; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct LongRunningQueries { - pid: String, - duration: String, + pid: i32, + duration: PgInterval, query: String, } impl Tabular for LongRunningQueries { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - pid: row.get::<_, Option>(0).unwrap_or_default().to_string(), - duration: row - .get::<_, Option>(1) - .unwrap_or(get_default_interval()) - .to_iso_8601(), - query: row.get::<_, Option>(2).unwrap_or_default(), + pid: row.try_get("pid").unwrap_or_default(), + duration: row.try_get("duration").unwrap_or(get_default_interval()), + query: row.try_get("query").unwrap_or_default(), } } fn to_row(&self) -> prettytable::Row { - row![self.pid, self.duration, self.query] + row![self.pid, format!("{:?}", self.duration), self.query] } fn headers() -> prettytable::Row { diff --git a/src/structs/mandelbrot.rs b/src/structs/mandelbrot.rs index 61f7259..4991a3f 100644 --- a/src/structs/mandelbrot.rs +++ b/src/structs/mandelbrot.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Mandelbrot { @@ -7,9 +8,9 @@ pub struct Mandelbrot { } impl Tabular for Mandelbrot { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - array_to_string: row.get::<_, Option>(0).unwrap_or_default(), + array_to_string: row.try_get("array_to_string").unwrap_or_default(), } } diff --git a/src/structs/null_indexes.rs b/src/structs/null_indexes.rs index dde2d92..d0ae628 100644 --- a/src/structs/null_indexes.rs +++ b/src/structs/null_indexes.rs @@ -1,12 +1,14 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::types::Oid; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct NullIndexes { - oid: String, + oid: Oid, index: String, index_size: String, - unique: String, + unique: bool, indexed_column: String, table: String, null_frac: String, @@ -15,26 +17,23 @@ pub struct NullIndexes { } impl Tabular for NullIndexes { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - oid: row.get::<_, Option>(0).unwrap_or_default().to_string(), - index: row.get::<_, Option>(1).unwrap_or_default(), - index_size: row.get::<_, Option>(2).unwrap_or_default(), - unique: row - .get::<_, Option>(3) - .unwrap_or_default() - .to_string(), - indexed_column: row.get::<_, Option>(4).unwrap_or_default(), - table: row.get::<_, Option>(5).unwrap_or_default(), - null_frac: row.get::<_, Option>(6).unwrap_or_default(), - expected_saving: row.get::<_, Option>(7).unwrap_or_default(), - schema: row.get::<_, Option>(8).unwrap_or_default(), + oid: row.try_get("oid").unwrap_or_default(), + index: row.try_get("index").unwrap_or_default(), + index_size: row.try_get("index_size").unwrap_or_default(), + unique: row.try_get("unique").unwrap_or_default(), + indexed_column: row.try_get("indexed_column").unwrap_or_default(), + table: row.try_get("table").unwrap_or_default(), + null_frac: row.try_get("null_frac").unwrap_or_default(), + expected_saving: row.try_get("expected_saving").unwrap_or_default(), + schema: row.try_get("schema").unwrap_or_default(), } } fn to_row(&self) -> prettytable::Row { row![ - self.oid, + format!("{:?}", self.oid), self.index, self.index_size, self.unique, diff --git a/src/structs/outliers.rs b/src/structs/outliers.rs index ae539f8..65edb6c 100644 --- a/src/structs/outliers.rs +++ b/src/structs/outliers.rs @@ -1,37 +1,38 @@ use crate::structs::shared::{get_default_interval, Tabular}; -use pg_interval::Interval; -use postgres::Row; +use sqlx::postgres::types::PgInterval; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Outliers { - total_exec_time: Interval, + total_exec_time: PgInterval, prop_exec_time: String, ncalls: String, - sync_io_time: Interval, + sync_io_time: PgInterval, query: String, } impl Tabular for Outliers { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { total_exec_time: row - .get::<_, Option>(0) + .try_get("total_exec_time") .unwrap_or(get_default_interval()), - prop_exec_time: row.get::<_, Option>(1).unwrap_or_default(), - ncalls: row.get::<_, Option>(2).unwrap_or_default(), + prop_exec_time: row.try_get("prop_exec_time").unwrap_or_default(), + ncalls: row.try_get("ncalls").unwrap_or_default(), sync_io_time: row - .get::<_, Option>(3) + .try_get("sync_io_time") .unwrap_or(get_default_interval()), - query: row.get::<_, Option>(4).unwrap_or_default(), + query: row.try_get("query").unwrap_or_default(), } } fn to_row(&self) -> prettytable::Row { row![ - self.total_exec_time.to_iso_8601(), + format!("{:?}", self.total_exec_time), self.prop_exec_time, self.ncalls, - self.sync_io_time.to_iso_8601(), + format!("{:?}", self.sync_io_time), self.query ] } diff --git a/src/structs/records_rank.rs b/src/structs/records_rank.rs index e75876a..4551790 100644 --- a/src/structs/records_rank.rs +++ b/src/structs/records_rank.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct RecordsRank { @@ -8,10 +9,10 @@ pub struct RecordsRank { } impl Tabular for RecordsRank { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - esiimated_count: row.get::<_, Option>(1).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + esiimated_count: row.try_get("estimated_count").unwrap_or_default(), } } diff --git a/src/structs/seq_scans.rs b/src/structs/seq_scans.rs index 67aa3dc..f51aa29 100644 --- a/src/structs/seq_scans.rs +++ b/src/structs/seq_scans.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct SeqScans { @@ -8,10 +9,10 @@ pub struct SeqScans { } impl Tabular for SeqScans { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - count: row.get::<_, Option>(1).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + count: row.try_get("count").unwrap_or_default(), } } diff --git a/src/structs/shared.rs b/src/structs/shared.rs index 4d4bffd..d22a673 100644 --- a/src/structs/shared.rs +++ b/src/structs/shared.rs @@ -1,15 +1,19 @@ -use pg_interval::Interval; -use postgres::Row; +use sqlx::postgres::types::PgInterval; +use sqlx::postgres::PgRow; use std::env; pub trait Tabular { - fn new(row: &Row) -> Self; + fn new(row: &PgRow) -> Self; fn to_row(&self) -> prettytable::Row; fn headers() -> prettytable::Row; } -pub fn get_default_interval() -> Interval { - Interval::from_postgres("0 seconds").unwrap() +pub fn get_default_interval() -> PgInterval { + PgInterval { + microseconds: 0, + days: 0, + months: 0, + } } pub fn get_default_schema() -> String { diff --git a/src/structs/ssl_used.rs b/src/structs/ssl_used.rs index f962634..e803ef9 100644 --- a/src/structs/ssl_used.rs +++ b/src/structs/ssl_used.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct SslUsed { @@ -7,9 +8,9 @@ pub struct SslUsed { } impl Tabular for SslUsed { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - ssl_used: row.get::<_, Option>(0).unwrap_or_default(), + ssl_used: row.try_get("ssl_used").unwrap_or(false), } } diff --git a/src/structs/table_cache_hit.rs b/src/structs/table_cache_hit.rs index 9ad3e19..7ad09bb 100644 --- a/src/structs/table_cache_hit.rs +++ b/src/structs/table_cache_hit.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct TableCacheHit { @@ -11,13 +12,13 @@ pub struct TableCacheHit { } impl Tabular for TableCacheHit { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - buffer_hits: row.get::<_, Option>(1).unwrap_or_default(), - block_reads: row.get::<_, Option>(2).unwrap_or_default(), - total_read: row.get::<_, Option>(3).unwrap_or_default(), - ratio: row.get::<_, Option>(4).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + buffer_hits: row.try_get("buffer_hits").unwrap_or_default(), + block_reads: row.try_get("block_reads").unwrap_or_default(), + total_read: row.try_get("total_read").unwrap_or_default(), + ratio: row.try_get("ratio").unwrap_or_default(), } } diff --git a/src/structs/table_index_scans.rs b/src/structs/table_index_scans.rs index 6fe7c8b..980bbd1 100644 --- a/src/structs/table_index_scans.rs +++ b/src/structs/table_index_scans.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct TableIndexScans { @@ -8,10 +9,10 @@ pub struct TableIndexScans { } impl Tabular for TableIndexScans { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - count: row.get::<_, Option>(1).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + count: row.try_get("count").unwrap_or_default(), } } diff --git a/src/structs/table_indexes_size.rs b/src/structs/table_indexes_size.rs index 372faa2..34c8d3a 100644 --- a/src/structs/table_indexes_size.rs +++ b/src/structs/table_indexes_size.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct TableIndexesSize { @@ -8,10 +9,10 @@ pub struct TableIndexesSize { } impl Tabular for TableIndexesSize { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - table: row.get::<_, Option>(0).unwrap_or_default(), - index_size: row.get::<_, Option>(1).unwrap_or_default(), + table: row.try_get("table").unwrap_or_default(), + index_size: row.try_get("index_size").unwrap_or_default(), } } diff --git a/src/structs/table_size.rs b/src/structs/table_size.rs index bd4a13c..5f21f0f 100644 --- a/src/structs/table_size.rs +++ b/src/structs/table_size.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct TableSize { @@ -9,11 +10,11 @@ pub struct TableSize { } impl Tabular for TableSize { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - size: row.get::<_, Option>(1).unwrap_or_default(), - schema: row.get::<_, Option>(2).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + size: row.try_get("size").unwrap_or_default(), + schema: row.try_get("schema").unwrap_or_default(), } } diff --git a/src/structs/tables.rs b/src/structs/tables.rs index 3e8d5b0..dd30bd9 100644 --- a/src/structs/tables.rs +++ b/src/structs/tables.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct Tables { @@ -8,10 +9,10 @@ pub struct Tables { } impl Tabular for Tables { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - tablename: row.get::<_, Option>(0).unwrap_or_default(), - schemaname: row.get::<_, Option>(1).unwrap_or_default(), + tablename: row.try_get("tablename").unwrap_or_default(), + schemaname: row.try_get("schemaname").unwrap_or_default(), } } diff --git a/src/structs/total_index_size.rs b/src/structs/total_index_size.rs index 3528097..aea58d8 100644 --- a/src/structs/total_index_size.rs +++ b/src/structs/total_index_size.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct TotalIndexSize { @@ -7,9 +8,9 @@ pub struct TotalIndexSize { } impl Tabular for TotalIndexSize { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - size: row.get::<_, Option>(0).unwrap_or_default(), + size: row.try_get("size").unwrap_or_default(), } } diff --git a/src/structs/total_table_size.rs b/src/structs/total_table_size.rs index 6bf4fcc..25b4979 100644 --- a/src/structs/total_table_size.rs +++ b/src/structs/total_table_size.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct TotalTableSize { @@ -8,10 +9,10 @@ pub struct TotalTableSize { } impl Tabular for TotalTableSize { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - name: row.get::<_, Option>(0).unwrap_or_default(), - size: row.get::<_, Option>(1).unwrap_or_default(), + name: row.try_get("name").unwrap_or_default(), + size: row.try_get("size").unwrap_or_default(), } } diff --git a/src/structs/unused_indexes.rs b/src/structs/unused_indexes.rs index 0757af6..9591b96 100644 --- a/src/structs/unused_indexes.rs +++ b/src/structs/unused_indexes.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct UnusedIndexes { @@ -10,12 +11,12 @@ pub struct UnusedIndexes { } impl Tabular for UnusedIndexes { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - table: row.get::<_, Option>(0).unwrap_or_default(), - index: row.get::<_, Option>(1).unwrap_or_default(), - index_size: row.get::<_, Option>(2).unwrap_or_default(), - index_scans: row.get::<_, Option>(3).unwrap_or_default(), + table: row.try_get("table").unwrap_or_default(), + index: row.try_get("index").unwrap_or_default(), + index_size: row.try_get("index_size").unwrap_or_default(), + index_scans: row.try_get("index_scans").unwrap_or_default(), } } diff --git a/src/structs/vacuum_stats.rs b/src/structs/vacuum_stats.rs index 76e3891..caf437c 100644 --- a/src/structs/vacuum_stats.rs +++ b/src/structs/vacuum_stats.rs @@ -1,5 +1,6 @@ use crate::structs::shared::Tabular; -use postgres::Row; +use sqlx::postgres::PgRow; +use sqlx::Row; #[derive(Debug, Clone)] pub struct VacuumStats { @@ -14,16 +15,16 @@ pub struct VacuumStats { } impl Tabular for VacuumStats { - fn new(row: &Row) -> Self { + fn new(row: &PgRow) -> Self { Self { - schema: row.get::<_, Option>(0).unwrap_or_default(), - table: row.get::<_, Option>(1).unwrap_or_default(), - last_vacuum: row.get::<_, Option>(2).unwrap_or_default(), - last_autovacuum: row.get::<_, Option>(3).unwrap_or_default(), - rowcount: row.get::<_, Option>(4).unwrap_or_default(), - dead_rowcount: row.get::<_, Option>(4).unwrap_or_default(), - autovacuum_threshold: row.get::<_, Option>(4).unwrap_or_default(), - expect_autovacuum: row.get::<_, Option>(7).unwrap_or_default(), + schema: row.try_get("schema").unwrap_or_default(), + table: row.try_get("table").unwrap_or_default(), + last_vacuum: row.try_get("last_vacuum").unwrap_or_default(), + last_autovacuum: row.try_get("last_autovacuum").unwrap_or_default(), + rowcount: row.try_get("rowcount").unwrap_or_default(), + dead_rowcount: row.try_get("dead_rowcount").unwrap_or_default(), + autovacuum_threshold: row.try_get("autovacuum_threshold").unwrap_or_default(), + expect_autovacuum: row.try_get("expect_autovacuum").unwrap_or_default(), } }