diff --git a/src/lib.rs b/src/lib.rs index 4292455..21577c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ pub use structs::null_indexes::NullIndexes; pub use structs::outliers::Outliers; pub use structs::records_rank::RecordsRank; pub use structs::seq_scans::SeqScans; -pub use structs::shared::{get_default_schema, Query, Tabular}; +pub use structs::shared::{get_default_schema, Tabular}; pub use structs::ssl_used::SslUsed; pub use structs::table_cache_hit::TableCacheHit; pub use structs::table_index_scans::TableIndexScans; @@ -52,63 +52,63 @@ pub fn render_table(items: Vec) { } pub async fn bloat() -> Result, PgExtrasError> { - let query = Query::read_file(Bloat::FILE_NAME); + let query = Bloat::read_file(); get_rows(query).await } pub async fn blocking(limit: Option) -> Result, PgExtrasError> { let limit = limit.unwrap_or("10".to_string()); - let query = Query::read_file(Blocking::FILE_NAME).replace("%{limit}", limit.as_str()); + let query = Blocking::read_file().replace("%{limit}", limit.as_str()); get_rows(&query).await } pub async fn calls(limit: Option) -> Result, PgExtrasError> { let limit = limit.unwrap_or("10".to_string()); - let query = Query::read_file(Calls::FILE_NAME).replace("%{limit}", limit.as_str()); + let query = Calls::read_file().replace("%{limit}", limit.as_str()); get_rows(&query).await } pub async fn extensions() -> Result, PgExtrasError> { - let query = Query::read_file(Extensions::FILE_NAME); + let query = Extensions::read_file(); get_rows(query).await } pub async fn table_cache_hit() -> Result, PgExtrasError> { - let query = Query::read_file(TableCacheHit::FILE_NAME); + let query = TableCacheHit::read_file(); get_rows(query).await } pub async fn tables(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Tables::FILE_NAME).replace("%{schema}", &schema_name); + let query = Tables::read_file().replace("%{schema}", &schema_name); get_rows(&query).await } pub async fn index_cache_hit(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(IndexCacheHit::FILE_NAME).replace("%{schema}", &schema_name); + let query = IndexCacheHit::read_file().replace("%{schema}", &schema_name); get_rows(&query).await } pub async fn indexes() -> Result, PgExtrasError> { - let query = Query::read_file(Indexes::FILE_NAME); + let query = Indexes::read_file(); get_rows(query).await } pub async fn index_size() -> Result, PgExtrasError> { - let query = Query::read_file(IndexSize::FILE_NAME); + let query = IndexSize::read_file(); get_rows(query).await } pub async fn index_usage(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(IndexUsage::FILE_NAME).replace("%{schema}", &schema_name); + let query = IndexUsage::read_file().replace("%{schema}", &schema_name); get_rows(&query).await } pub async fn index_scans(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(IndexScans::FILE_NAME).replace("%{schema}", &schema_name); + let query = IndexScans::read_file().replace("%{schema}", &schema_name); get_rows(&query).await } @@ -116,45 +116,44 @@ 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(NullIndexes::FILE_NAME) - .replace("%{min_relation_size_mb}", &min_relation_size_mb); + let query = NullIndexes::read_file().replace("%{min_relation_size_mb}", &min_relation_size_mb); get_rows(&query).await } pub async fn locks() -> Result, PgExtrasError> { - let query = Query::read_file(Locks::FILE_NAME); + let query = Locks::read_file(); get_rows(query).await } pub async fn all_locks() -> Result, PgExtrasError> { - let query = Query::read_file(AllLocks::FILE_NAME); + let query = AllLocks::read_file(); get_rows(query).await } pub async fn long_running_queries() -> Result, PgExtrasError> { - let query = Query::read_file(LongRunningQueries::FILE_NAME); + let query = LongRunningQueries::read_file(); get_rows(query).await } pub async fn mandelbrot() -> Result, PgExtrasError> { - let query = Query::read_file(Mandelbrot::FILE_NAME); + let query = Mandelbrot::read_file(); get_rows(query).await } pub async fn outliers() -> Result, PgExtrasError> { - let query = Query::read_file(Outliers::FILE_NAME); + let query = Outliers::read_file(); get_rows(query).await } pub async fn records_rank(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(RecordsRank::FILE_NAME).replace("%{schema}", schema_name.as_str()); + let query = RecordsRank::read_file().replace("%{schema}", schema_name.as_str()); get_rows(&query).await } pub async fn seq_scans(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(SeqScans::FILE_NAME).replace("%{schema}", schema_name.as_str()); + let query = SeqScans::read_file().replace("%{schema}", schema_name.as_str()); get_rows(&query).await } @@ -162,8 +161,7 @@ pub async fn table_index_scans( schema: Option, ) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = - Query::read_file(TableIndexScans::FILE_NAME).replace("%{schema}", schema_name.as_str()); + let query = TableIndexScans::read_file().replace("%{schema}", schema_name.as_str()); get_rows(&query).await } @@ -171,71 +169,69 @@ pub async fn table_indexes_size( schema: Option, ) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = - Query::read_file(TableIndexesSize::FILE_NAME).replace("%{schema}", schema_name.as_str()); + let query = TableIndexesSize::read_file().replace("%{schema}", schema_name.as_str()); get_rows(&query).await } pub async fn table_size() -> Result, PgExtrasError> { - let query = Query::read_file(TableSize::FILE_NAME); + let query = TableSize::read_file(); get_rows(query).await } pub async fn total_index_size() -> Result, PgExtrasError> { - let query = Query::read_file(TotalIndexSize::FILE_NAME); + let query = TotalIndexSize::read_file(); get_rows(query).await } pub async fn total_table_size() -> Result, PgExtrasError> { - let query = Query::read_file(TotalTableSize::FILE_NAME); + let query = TotalTableSize::read_file(); get_rows(query).await } pub async fn unused_indexes(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = - Query::read_file(UnusedIndexes::FILE_NAME).replace("%{schema}", schema_name.as_str()); + let query = UnusedIndexes::read_file().replace("%{schema}", schema_name.as_str()); get_rows(&query).await } pub async fn duplicate_indexes() -> Result, PgExtrasError> { - let query = Query::read_file(DuplicateIndexes::FILE_NAME); + let query = DuplicateIndexes::read_file(); get_rows(query).await } pub async fn vacuum_stats() -> Result, PgExtrasError> { - let query = Query::read_file(VacuumStats::FILE_NAME); + let query = VacuumStats::read_file(); get_rows(query).await } pub async fn buffercache_stats() -> Result, PgExtrasError> { - let query = Query::read_file(BuffercacheStats::FILE_NAME); + let query = BuffercacheStats::read_file(); get_rows(query).await } pub async fn buffercache_usage() -> Result, PgExtrasError> { - let query = Query::read_file(BuffercacheUsage::FILE_NAME); + let query = BuffercacheUsage::read_file(); get_rows(query).await } pub async fn ssl_used() -> Result, PgExtrasError> { - let query = Query::read_file(SslUsed::FILE_NAME); + let query = SslUsed::read_file(); get_rows(query).await } pub async fn connections() -> Result, PgExtrasError> { - let query = Query::read_file(Connections::FILE_NAME); + let query = Connections::read_file(); get_rows(query).await } pub async fn cache_hit(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(CacheHit::FILE_NAME).replace("%{schema}", schema_name.as_str()); + let query = CacheHit::read_file().replace("%{schema}", schema_name.as_str()); get_rows(&query).await } pub async fn db_settings() -> Result, PgExtrasError> { - let query = Query::read_file(DbSettings::FILE_NAME); + let query = DbSettings::read_file(); get_rows(query).await } diff --git a/src/structs/all_locks.rs b/src/structs/all_locks.rs index ee50f9b..b3a95fc 100644 --- a/src/structs/all_locks.rs +++ b/src/structs/all_locks.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Query, Tabular}; +use crate::structs::shared::{get_default_interval, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -15,8 +15,6 @@ pub struct AllLocks { } impl Tabular for AllLocks { - const FILE_NAME: Query = Query::AllLocks; - fn new(row: &PgRow) -> Self { Self { pid: row.try_get("pid").unwrap_or_default(), @@ -55,4 +53,8 @@ impl Tabular for AllLocks { "application" ] } + + fn read_file() -> &'static str { + include_str!("../queries/all_locks.sql") + } } diff --git a/src/structs/bloat.rs b/src/structs/bloat.rs index 9e55e73..f0b314e 100644 --- a/src/structs/bloat.rs +++ b/src/structs/bloat.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::types::BigDecimal; use sqlx::Row; @@ -13,8 +13,6 @@ pub struct Bloat { } impl Tabular for Bloat { - const FILE_NAME: Query = Query::Bloat; - fn new(row: &PgRow) -> Self { Self { typefield: row.try_get("type").unwrap_or_default(), @@ -38,4 +36,8 @@ impl Tabular for Bloat { fn headers() -> prettytable::Row { row!["type", "schemaname", "object_name", "bloat", "waste"] } + + fn read_file() -> &'static str { + include_str!("../queries/bloat.sql") + } } diff --git a/src/structs/blocking.rs b/src/structs/blocking.rs index a4de073..c17de11 100644 --- a/src/structs/blocking.rs +++ b/src/structs/blocking.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Query, Tabular}; +use crate::structs::shared::{get_default_interval, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -15,8 +15,6 @@ pub struct Blocking { } impl Tabular for Blocking { - const FILE_NAME: Query = Query::Blocking; - fn new(row: &PgRow) -> Self { Self { blocked_pid: row.try_get("blocked_pid").unwrap_or_default(), @@ -59,4 +57,8 @@ impl Tabular for Blocking { "blocking_sql_app" ] } + + fn read_file() -> &'static str { + include_str!("../queries/blocking.sql") + } } diff --git a/src/structs/buffercache_stats.rs b/src/structs/buffercache_stats.rs index a0d44fd..7d488f9 100644 --- a/src/structs/buffercache_stats.rs +++ b/src/structs/buffercache_stats.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -11,8 +11,6 @@ pub struct BuffercacheStats { } impl Tabular for BuffercacheStats { - const FILE_NAME: Query = Query::BuffercacheStats; - fn new(row: &PgRow) -> Self { Self { relname: row.try_get("relname").unwrap_or_default(), @@ -39,4 +37,8 @@ impl Tabular for BuffercacheStats { "percent_of_relation" ] } + + fn read_file() -> &'static str { + include_str!("../queries/buffercache_stats.sql") + } } diff --git a/src/structs/buffercache_usage.rs b/src/structs/buffercache_usage.rs index 7da6310..0e1442c 100644 --- a/src/structs/buffercache_usage.rs +++ b/src/structs/buffercache_usage.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,8 +9,6 @@ pub struct BuffercacheUsage { } impl Tabular for BuffercacheUsage { - const FILE_NAME: Query = Query::BuffercacheUsage; - fn new(row: &PgRow) -> Self { Self { relname: row.try_get("relname").unwrap_or_default(), @@ -25,4 +23,8 @@ impl Tabular for BuffercacheUsage { fn headers() -> prettytable::Row { row!["relname", "buffers"] } + + fn read_file() -> &'static str { + include_str!("../queries/buffercache_usage.sql") + } } diff --git a/src/structs/cache_hit.rs b/src/structs/cache_hit.rs index ad7d9f3..e72cf93 100644 --- a/src/structs/cache_hit.rs +++ b/src/structs/cache_hit.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::types::BigDecimal; use sqlx::Row; @@ -10,8 +10,6 @@ pub struct CacheHit { } impl Tabular for CacheHit { - const FILE_NAME: Query = Query::CacheHit; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -26,4 +24,8 @@ impl Tabular for CacheHit { fn headers() -> prettytable::Row { row!["name", "ratio"] } + + fn read_file() -> &'static str { + include_str!("../queries/cache_hit.sql") + } } diff --git a/src/structs/calls.rs b/src/structs/calls.rs index c5a033e..6aa8206 100644 --- a/src/structs/calls.rs +++ b/src/structs/calls.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Query, Tabular}; +use crate::structs::shared::{get_default_interval, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -12,8 +12,6 @@ pub struct Calls { } impl Tabular for Calls { - const FILE_NAME: Query = Query::Calls; - fn new(row: &PgRow) -> Self { Self { qry: row.try_get("qry").unwrap_or_default(), @@ -45,4 +43,8 @@ impl Tabular for Calls { "sync_io_time" ] } + + fn read_file() -> &'static str { + include_str!("../queries/calls.sql") + } } diff --git a/src/structs/connections.rs b/src/structs/connections.rs index 01280af..d06ab17 100644 --- a/src/structs/connections.rs +++ b/src/structs/connections.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -10,8 +10,6 @@ pub struct Connections { } impl Tabular for Connections { - const FILE_NAME: Query = Query::Connections; - fn new(row: &PgRow) -> Self { Self { username: row.try_get("username").unwrap_or_default(), @@ -27,4 +25,8 @@ impl Tabular for Connections { fn headers() -> prettytable::Row { row!["username", "pid", "client_addr"] } + + fn read_file() -> &'static str { + include_str!("../queries/connections.sql") + } } diff --git a/src/structs/db_settings.rs b/src/structs/db_settings.rs index 5127987..576aed1 100644 --- a/src/structs/db_settings.rs +++ b/src/structs/db_settings.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -11,8 +11,6 @@ pub struct DbSettings { } impl Tabular for DbSettings { - const FILE_NAME: Query = Query::DbSettings; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -29,4 +27,8 @@ impl Tabular for DbSettings { fn headers() -> prettytable::Row { row!["name", "setting", "unit", "short_desc"] } + + fn read_file() -> &'static str { + include_str!("../queries/db_settings.sql") + } } diff --git a/src/structs/duplicate_indexes.rs b/src/structs/duplicate_indexes.rs index a9401ad..b06e144 100644 --- a/src/structs/duplicate_indexes.rs +++ b/src/structs/duplicate_indexes.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -12,8 +12,6 @@ pub struct DuplicateIndexes { } impl Tabular for DuplicateIndexes { - const FILE_NAME: Query = Query::DuplicateIndexes; - fn new(row: &PgRow) -> Self { Self { size: row.try_get("size").unwrap_or_default(), @@ -31,4 +29,8 @@ impl Tabular for DuplicateIndexes { fn headers() -> prettytable::Row { row!["size", "idx1", "idx2", "idx3", "idx4"] } + + fn read_file() -> &'static str { + include_str!("../queries/duplicate_indexes.sql") + } } diff --git a/src/structs/extensions.rs b/src/structs/extensions.rs index 26525b3..d202777 100644 --- a/src/structs/extensions.rs +++ b/src/structs/extensions.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -11,8 +11,6 @@ pub struct Extensions { } impl Tabular for Extensions { - const FILE_NAME: Query = Query::Extensions; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -34,4 +32,8 @@ impl Tabular for Extensions { fn headers() -> prettytable::Row { row!["name", "default_version", "installed_version", "comment"] } + + fn read_file() -> &'static str { + include_str!("../queries/extensions.sql") + } } diff --git a/src/structs/index_cache_hit.rs b/src/structs/index_cache_hit.rs index b7c3a39..5069589 100644 --- a/src/structs/index_cache_hit.rs +++ b/src/structs/index_cache_hit.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -12,8 +12,6 @@ pub struct IndexCacheHit { } impl Tabular for IndexCacheHit { - const FILE_NAME: Query = Query::IndexCacheHit; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -37,4 +35,8 @@ impl Tabular for IndexCacheHit { fn headers() -> prettytable::Row { row!["name", "buffer_hits", "block_reads", "total_read", "ratio"] } + + fn read_file() -> &'static str { + include_str!("../queries/index_cache_hit.sql") + } } diff --git a/src/structs/index_scans.rs b/src/structs/index_scans.rs index 3fc5950..c90c0f3 100644 --- a/src/structs/index_scans.rs +++ b/src/structs/index_scans.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -12,8 +12,6 @@ pub struct IndexScans { } impl Tabular for IndexScans { - const FILE_NAME: Query = Query::IndexScans; - fn new(row: &PgRow) -> Self { Self { schemaname: row.try_get("schemaname").unwrap_or_default(), @@ -37,4 +35,8 @@ impl Tabular for IndexScans { fn headers() -> prettytable::Row { row!["schemaname", "table", "index", "index_size", "index_scans"] } + + fn read_file() -> &'static str { + include_str!("../queries/index_scans.sql") + } } diff --git a/src/structs/index_size.rs b/src/structs/index_size.rs index 4ffa32b..dd6bd70 100644 --- a/src/structs/index_size.rs +++ b/src/structs/index_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -10,8 +10,6 @@ pub struct IndexSize { } impl Tabular for IndexSize { - const FILE_NAME: Query = Query::IndexSize; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -27,4 +25,8 @@ impl Tabular for IndexSize { fn headers() -> prettytable::Row { row!["name", "size", "schema"] } + + fn read_file() -> &'static str { + include_str!("../queries/index_size.sql") + } } diff --git a/src/structs/index_usage.rs b/src/structs/index_usage.rs index c4fc0df..f13fe39 100644 --- a/src/structs/index_usage.rs +++ b/src/structs/index_usage.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -10,8 +10,6 @@ pub struct IndexUsage { } impl Tabular for IndexUsage { - const FILE_NAME: Query = Query::IndexUsage; - fn new(row: &PgRow) -> Self { Self { relname: row.try_get("relname").unwrap_or_default(), @@ -33,4 +31,8 @@ impl Tabular for IndexUsage { fn headers() -> prettytable::Row { row!["relname", "percent_of_times_index_used", "rows_in_table"] } + + fn read_file() -> &'static str { + include_str!("../queries/index_usage.sql") + } } diff --git a/src/structs/indexes.rs b/src/structs/indexes.rs index 93c6bd5..f003257 100644 --- a/src/structs/indexes.rs +++ b/src/structs/indexes.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -11,8 +11,6 @@ pub struct Indexes { } impl Tabular for Indexes { - const FILE_NAME: Query = Query::Indexes; - fn new(row: &PgRow) -> Self { Self { schemaname: row.try_get("schemaname").unwrap_or_default(), @@ -34,4 +32,8 @@ impl Tabular for Indexes { fn headers() -> prettytable::Row { row!["schemaname", "indexname", "tablename", "columns"] } + + fn read_file() -> &'static str { + include_str!("../queries/indexes.sql") + } } diff --git a/src/structs/locks.rs b/src/structs/locks.rs index 1143e08..b29269f 100644 --- a/src/structs/locks.rs +++ b/src/structs/locks.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Query, Tabular}; +use crate::structs::shared::{get_default_interval, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -15,8 +15,6 @@ pub struct Locks { } impl Tabular for Locks { - const FILE_NAME: Query = Query::Locks; - fn new(row: &PgRow) -> Self { Self { pid: row.try_get("pid").unwrap_or_default(), @@ -55,4 +53,8 @@ impl Tabular for Locks { "application" ] } + + fn read_file() -> &'static str { + include_str!("../queries/locks.sql") + } } diff --git a/src/structs/long_running_queries.rs b/src/structs/long_running_queries.rs index 9bec66a..bb320b1 100644 --- a/src/structs/long_running_queries.rs +++ b/src/structs/long_running_queries.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Query, Tabular}; +use crate::structs::shared::{get_default_interval, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -10,8 +10,6 @@ pub struct LongRunningQueries { } impl Tabular for LongRunningQueries { - const FILE_NAME: Query = Query::LongRunningQueries; - fn new(row: &PgRow) -> Self { Self { pid: row.try_get("pid").unwrap_or_default(), @@ -27,4 +25,8 @@ impl Tabular for LongRunningQueries { fn headers() -> prettytable::Row { row!["pid", "duration", "query"] } + + fn read_file() -> &'static str { + include_str!("../queries/long_running_queries.sql") + } } diff --git a/src/structs/mandelbrot.rs b/src/structs/mandelbrot.rs index 49d8582..db8aff7 100644 --- a/src/structs/mandelbrot.rs +++ b/src/structs/mandelbrot.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -8,8 +8,6 @@ pub struct Mandelbrot { } impl Tabular for Mandelbrot { - const FILE_NAME: Query = Query::Mandelbrot; - fn new(row: &PgRow) -> Self { Self { array_to_string: row.try_get("array_to_string").unwrap_or_default(), @@ -23,4 +21,8 @@ impl Tabular for Mandelbrot { fn headers() -> prettytable::Row { row!["array_to_string"] } + + fn read_file() -> &'static str { + include_str!("../queries/mandelbrot.sql") + } } diff --git a/src/structs/null_indexes.rs b/src/structs/null_indexes.rs index 1961370..a53a8ca 100644 --- a/src/structs/null_indexes.rs +++ b/src/structs/null_indexes.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::types::Oid; use sqlx::postgres::PgRow; use sqlx::Row; @@ -17,8 +17,6 @@ pub struct NullIndexes { } impl Tabular for NullIndexes { - const FILE_NAME: Query = Query::NullIndexes; - fn new(row: &PgRow) -> Self { Self { oid: row.try_get("oid").unwrap_or_default(), @@ -60,4 +58,8 @@ impl Tabular for NullIndexes { "schema" ] } + + fn read_file() -> &'static str { + include_str!("../queries/null_indexes.sql") + } } diff --git a/src/structs/outliers.rs b/src/structs/outliers.rs index caf7b39..5e48575 100644 --- a/src/structs/outliers.rs +++ b/src/structs/outliers.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Query, Tabular}; +use crate::structs::shared::{get_default_interval, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -12,8 +12,6 @@ pub struct Outliers { } impl Tabular for Outliers { - const FILE_NAME: Query = Query::Outliers; - fn new(row: &PgRow) -> Self { Self { total_exec_time: row @@ -47,4 +45,8 @@ impl Tabular for Outliers { "sync_io_time" ] } + + fn read_file() -> &'static str { + include_str!("../queries/outliers.sql") + } } diff --git a/src/structs/records_rank.rs b/src/structs/records_rank.rs index b7b1525..77f2000 100644 --- a/src/structs/records_rank.rs +++ b/src/structs/records_rank.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,8 +9,6 @@ pub struct RecordsRank { } impl Tabular for RecordsRank { - const FILE_NAME: Query = Query::RecordsRank; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -25,4 +23,8 @@ impl Tabular for RecordsRank { fn headers() -> prettytable::Row { row!["name", "estimated_count"] } + + fn read_file() -> &'static str { + include_str!("../queries/records_rank.sql") + } } diff --git a/src/structs/seq_scans.rs b/src/structs/seq_scans.rs index feabace..3fd1185 100644 --- a/src/structs/seq_scans.rs +++ b/src/structs/seq_scans.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,8 +9,6 @@ pub struct SeqScans { } impl Tabular for SeqScans { - const FILE_NAME: Query = Query::SeqScans; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -25,4 +23,8 @@ impl Tabular for SeqScans { fn headers() -> prettytable::Row { row!["name", "count"] } + + fn read_file() -> &'static str { + include_str!("../queries/seq_scans.sql") + } } diff --git a/src/structs/shared.rs b/src/structs/shared.rs index 6f210e8..5c714a1 100644 --- a/src/structs/shared.rs +++ b/src/structs/shared.rs @@ -2,86 +2,10 @@ use sqlx::postgres::{types::PgInterval, PgRow}; use std::env; pub trait Tabular { - const FILE_NAME: Query; fn new(row: &PgRow) -> Self; fn to_row(&self) -> prettytable::Row; fn headers() -> prettytable::Row; -} - -pub enum Query { - CacheHit, - Bloat, - Blocking, - Calls, - Extensions, - TableCacheHit, - Tables, - IndexCacheHit, - DbSettings, - Indexes, - IndexSize, - IndexUsage, - IndexScans, - NullIndexes, - Locks, - AllLocks, - LongRunningQueries, - Mandelbrot, - Outliers, - RecordsRank, - SeqScans, - TableIndexScans, - TableIndexesSize, - TableSize, - TotalIndexSize, - TotalTableSize, - UnusedIndexes, - DuplicateIndexes, - VacuumStats, - BuffercacheStats, - BuffercacheUsage, - SslUsed, - Connections, -} - -impl Query { - pub fn read_file(query: Query) -> &'static str { - match query { - Self::CacheHit => include_str!("../queries/cache_hit.sql"), - Self::DbSettings => include_str!("../queries/db_settings.sql"), - Self::BuffercacheStats => include_str!("../queries/buffercache_stats.sql"), - Self::BuffercacheUsage => include_str!("../queries/buffercache_usage.sql"), - Self::SslUsed => include_str!("../queries/ssl_used.sql"), - Self::Connections => include_str!("../queries/connections.sql"), - Self::Bloat => include_str!("../queries/bloat.sql"), - Self::Blocking => include_str!("../queries/blocking.sql"), - Self::Calls => include_str!("../queries/calls.sql"), - Self::Extensions => include_str!("../queries/extensions.sql"), - Self::TableCacheHit => include_str!("../queries/table_cache_hit.sql"), - Self::Tables => include_str!("../queries/tables.sql"), - Self::IndexCacheHit => include_str!("../queries/index_cache_hit.sql"), - Self::Indexes => include_str!("../queries/indexes.sql"), - Self::IndexSize => include_str!("../queries/index_size.sql"), - Self::IndexUsage => include_str!("../queries/index_usage.sql"), - Self::IndexScans => include_str!("../queries/index_scans.sql"), - Self::NullIndexes => include_str!("../queries/null_indexes.sql"), - Self::Locks => include_str!("../queries/locks.sql"), - Self::AllLocks => include_str!("../queries/all_locks.sql"), - Self::LongRunningQueries => include_str!("../queries/long_running_queries.sql"), - Self::Mandelbrot => include_str!("../queries/mandelbrot.sql"), - Self::Outliers => include_str!("../queries/outliers.sql"), - Self::RecordsRank => include_str!("../queries/records_rank.sql"), - Self::SeqScans => include_str!("../queries/seq_scans.sql"), - Self::TableIndexScans => include_str!("../queries/table_index_scans.sql"), - Self::TableIndexesSize => include_str!("../queries/table_indexes_size.sql"), - Self::TableSize => include_str!("../queries/table_size.sql"), - Self::TotalIndexSize => include_str!("../queries/total_index_size.sql"), - Self::TotalTableSize => include_str!("../queries/total_table_size.sql"), - Self::UnusedIndexes => include_str!("../queries/unused_indexes.sql"), - Self::DuplicateIndexes => include_str!("../queries/duplicate_indexes.sql"), - Self::VacuumStats => include_str!("../queries/vacuum_stats.sql"), - } - } + fn read_file() -> &'static str; } pub fn get_default_interval() -> PgInterval { diff --git a/src/structs/ssl_used.rs b/src/structs/ssl_used.rs index 15edb3a..18bc67e 100644 --- a/src/structs/ssl_used.rs +++ b/src/structs/ssl_used.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -8,8 +8,6 @@ pub struct SslUsed { } impl Tabular for SslUsed { - const FILE_NAME: Query = Query::SslUsed; - fn new(row: &PgRow) -> Self { Self { ssl_used: row.try_get("ssl_used").unwrap_or(false), @@ -23,4 +21,8 @@ impl Tabular for SslUsed { fn headers() -> prettytable::Row { row!["ssl_used"] } + + fn read_file() -> &'static str { + include_str!("../queries/ssl_used.sql") + } } diff --git a/src/structs/table_cache_hit.rs b/src/structs/table_cache_hit.rs index b6451d1..877be77 100644 --- a/src/structs/table_cache_hit.rs +++ b/src/structs/table_cache_hit.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -12,8 +12,6 @@ pub struct TableCacheHit { } impl Tabular for TableCacheHit { - const FILE_NAME: Query = Query::TableCacheHit; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -37,4 +35,8 @@ impl Tabular for TableCacheHit { fn headers() -> prettytable::Row { row!["name", "buffer_hits", "block_reads", "total_read", "ratio"] } + + fn read_file() -> &'static str { + include_str!("../queries/table_cache_hit.sql") + } } diff --git a/src/structs/table_index_scans.rs b/src/structs/table_index_scans.rs index 5ad5753..369be1a 100644 --- a/src/structs/table_index_scans.rs +++ b/src/structs/table_index_scans.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,8 +9,6 @@ pub struct TableIndexScans { } impl Tabular for TableIndexScans { - const FILE_NAME: Query = Query::TableIndexScans; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -25,4 +23,8 @@ impl Tabular for TableIndexScans { fn headers() -> prettytable::Row { row!["name", "count"] } + + fn read_file() -> &'static str { + include_str!("../queries/table_index_scans.sql") + } } diff --git a/src/structs/table_indexes_size.rs b/src/structs/table_indexes_size.rs index 90cae28..50e328a 100644 --- a/src/structs/table_indexes_size.rs +++ b/src/structs/table_indexes_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,8 +9,6 @@ pub struct TableIndexesSize { } impl Tabular for TableIndexesSize { - const FILE_NAME: Query = Query::TableIndexesSize; - fn new(row: &PgRow) -> Self { Self { table: row.try_get("table").unwrap_or_default(), @@ -25,4 +23,8 @@ impl Tabular for TableIndexesSize { fn headers() -> prettytable::Row { row!["table", "index_size"] } + + fn read_file() -> &'static str { + include_str!("../queries/table_indexes_size.sql") + } } diff --git a/src/structs/table_size.rs b/src/structs/table_size.rs index d3c29a7..4dc5d61 100644 --- a/src/structs/table_size.rs +++ b/src/structs/table_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -10,8 +10,6 @@ pub struct TableSize { } impl Tabular for TableSize { - const FILE_NAME: Query = Query::TableSize; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -27,4 +25,8 @@ impl Tabular for TableSize { fn headers() -> prettytable::Row { row!["name", "size", "schema"] } + + fn read_file() -> &'static str { + include_str!("../queries/table_size.sql") + } } diff --git a/src/structs/tables.rs b/src/structs/tables.rs index 6c6c8e4..5aa54f6 100644 --- a/src/structs/tables.rs +++ b/src/structs/tables.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,8 +9,6 @@ pub struct Tables { } impl Tabular for Tables { - const FILE_NAME: Query = Query::Tables; - fn new(row: &PgRow) -> Self { Self { tablename: row.try_get("tablename").unwrap_or_default(), @@ -25,4 +23,8 @@ impl Tabular for Tables { fn headers() -> prettytable::Row { row!["tablename", "schemaname"] } + + fn read_file() -> &'static str { + include_str!("../queries/tables.sql") + } } diff --git a/src/structs/total_index_size.rs b/src/structs/total_index_size.rs index 3ef2684..3ae0166 100644 --- a/src/structs/total_index_size.rs +++ b/src/structs/total_index_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -8,8 +8,6 @@ pub struct TotalIndexSize { } impl Tabular for TotalIndexSize { - const FILE_NAME: Query = Query::TotalIndexSize; - fn new(row: &PgRow) -> Self { Self { size: row.try_get("size").unwrap_or_default(), @@ -23,4 +21,8 @@ impl Tabular for TotalIndexSize { fn headers() -> prettytable::Row { row!["size"] } + + fn read_file() -> &'static str { + include_str!("../queries/total_index_size.sql") + } } diff --git a/src/structs/total_table_size.rs b/src/structs/total_table_size.rs index 29aa593..a42e32a 100644 --- a/src/structs/total_table_size.rs +++ b/src/structs/total_table_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,8 +9,6 @@ pub struct TotalTableSize { } impl Tabular for TotalTableSize { - const FILE_NAME: Query = Query::TotalTableSize; - fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), @@ -25,4 +23,8 @@ impl Tabular for TotalTableSize { fn headers() -> prettytable::Row { row!["name", "size"] } + + fn read_file() -> &'static str { + include_str!("../queries/total_table_size.sql") + } } diff --git a/src/structs/unused_indexes.rs b/src/structs/unused_indexes.rs index 7507020..86d2246 100644 --- a/src/structs/unused_indexes.rs +++ b/src/structs/unused_indexes.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -11,8 +11,6 @@ pub struct UnusedIndexes { } impl Tabular for UnusedIndexes { - const FILE_NAME: Query = Query::UnusedIndexes; - fn new(row: &PgRow) -> Self { Self { table: row.try_get("table").unwrap_or_default(), @@ -29,4 +27,8 @@ impl Tabular for UnusedIndexes { fn headers() -> prettytable::Row { row!["table", "index", "index_size", "index_scans"] } + + fn read_file() -> &'static str { + include_str!("../queries/unused_indexes.sql") + } } diff --git a/src/structs/vacuum_stats.rs b/src/structs/vacuum_stats.rs index 8a521b7..4625996 100644 --- a/src/structs/vacuum_stats.rs +++ b/src/structs/vacuum_stats.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{Query, Tabular}; +use crate::structs::shared::Tabular; use sqlx::postgres::PgRow; use sqlx::Row; @@ -15,8 +15,6 @@ pub struct VacuumStats { } impl Tabular for VacuumStats { - const FILE_NAME: Query = Query::VacuumStats; - fn new(row: &PgRow) -> Self { Self { schema: row.try_get("schema").unwrap_or_default(), @@ -55,4 +53,8 @@ impl Tabular for VacuumStats { "expect_autovacuum" ] } + + fn read_file() -> &'static str { + include_str!("../queries/vacuum_stats.sql") + } }