Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ repository = "https://github.com/eirtools/sqlgrep"


[dependencies]
sqlx = { version = "0.7.4", features = ["runtime-tokio-native-tls", "sqlite", "chrono"]}
tokio = { version = "1.20.0", features = ["macros", "rt-multi-thread"]}
sqlparser = "0.45.0"
chrono = {version = "0.4.38", features = ["alloc"]}
log = "0.4.21"
futures = {version = "0.3.30", features = ["async-await"]}
clap = { version = "4.5.0", features = ["derive", "wrap_help"] }
indoc = "2.0.5"
sqlx = { version = "0.8.4", features = ["runtime-tokio-native-tls", "sqlite", "chrono"]}
tokio = { version = "1.44.2", features = ["macros", "rt-multi-thread"]}
sqlparser = "0.55.0"
chrono = {version = "0.4.40", features = ["alloc"]}
log = "0.4.27"
futures = {version = "0.3.31", features = ["async-await"]}
clap = { version = "4.5.36", features = ["derive", "wrap_help"] }
indoc = "2.0.6"
stderrlog ="0.6.0"
regex = "1.10.4"
regex = "1.11.1"
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,12 @@ where
}

async fn sqlite_select_tables(db: &Pool<Sqlite>) -> Result<impl Iterator<Item = String>, Error> {
let result = db
.fetch_all(
"SELECT name
let select_query = "SELECT name
FROM sqlite_schema
WHERE type ='table';",
)
.await?;
WHERE type ='table'";

log::debug!("Execute query: {select_query}");
let result = db.fetch_all(select_query).await?;

Ok(result
.into_iter()
Expand All @@ -130,10 +129,10 @@ async fn sqlite_check_rows(
use std::sync::atomic::AtomicI64;
use std::sync::atomic::Ordering;

log::debug!("Execute query: {select_query}");
let mut rows = db.fetch(select_query);

log::debug!("==> {table_name}");
// REVIEW: investigate if there's a better way to enumerate async stream.
let row_idx: AtomicI64 = AtomicI64::new(-1);
loop {
row_idx.fetch_add(1, Ordering::SeqCst);
Expand Down
40 changes: 28 additions & 12 deletions src/select.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use sqlparser::ast::helpers::attached_token::AttachedToken;
use sqlparser::ast::*;
use sqlparser::dialect::Dialect;
use sqlparser::tokenizer::Span;

/// Generates wildcard select for given dialect
///
/// Generates wildcard select for given dialect:
///
/// ```rust
/// // connect to SQLite
Expand All @@ -10,11 +13,15 @@ use sqlparser::dialect::Dialect;
/// let query = generate_select("table", driver);
/// assert_eq!("SELECT * FROM `table`", query.as_str());
/// ```
///
pub(crate) fn generate_select(table_name: &str, dialect: &impl Dialect) -> String {
let ast = SetExpr::Select(Box::new(Select {
flavor: SelectFlavor::Standard,
distinct: None,
top: None,
projection: [SelectItem::Wildcard(WildcardAdditionalOptions {
wildcard_token: AttachedToken::empty(),
opt_ilike: None,
opt_exclude: None,
opt_except: None,
opt_rename: None,
Expand All @@ -24,32 +31,41 @@ pub(crate) fn generate_select(table_name: &str, dialect: &impl Dialect) -> Strin
into: None,
from: [TableWithJoins {
relation: TableFactor::Table {
name: ObjectName(
[Ident {
value: table_name.to_owned(),
quote_style: dialect.identifier_quote_style(table_name),
}]
.to_vec(),
),
name: [Ident {
value: table_name.to_owned(),
quote_style: dialect.identifier_quote_style(table_name),
span: Span::empty(),
}]
.to_vec()
.into(),
alias: None,
args: None,
with_hints: [].to_vec(),
with_hints: vec![],
version: None,
partitions: [].to_vec(),
partitions: vec![],
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
},
joins: [].to_vec(),
joins: vec![],
}]
.to_vec(),
lateral_views: [].to_vec(),
selection: None,
group_by: GroupByExpr::Expressions([].to_vec()),
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: [].to_vec(),
distribute_by: [].to_vec(),
sort_by: [].to_vec(),
having: None,
named_window: [].to_vec(),
qualify: None,
value_table_mode: None,
select_token: AttachedToken::empty(),
top_before_distinct: false,
prewhere: None,
window_before_qualify: false,
connect_by: None,
}));

ast.to_string()
Expand Down