Skip to main content

pgorm_check/
lib.rs

1//! pgorm-check
2//!
3//! Runtime helpers for checking whether SQL references match a live database schema.
4//!
5//! This crate can cache schema metadata into a local directory (default: `./.pgorm/`)
6//! so subsequent runs can skip a full refresh when nothing has changed.
7//!
8//! # Features
9//!
10//! - **Schema validation**: Check if SQL queries reference valid tables and columns
11//! - **SQL linting**: Detect common issues like DELETE without WHERE, SELECT without LIMIT
12//! - **Syntax validation**: Verify SQL syntax is correct
13//! - **Statement analysis**: One-pass parse into `SqlAnalysis` (tables/aliases/columns/CTEs/etc)
14//! - **Parse cache**: Optional LRU `SqlParseCache` to reuse analysis across calls
15//!
16//! # Example
17//!
18//! ```ignore
19//! use pgorm_check::{is_valid_sql, lint_sql, lint_select_many, delete_has_where};
20//!
21//! // Check SQL syntax
22//! assert!(is_valid_sql("SELECT * FROM users").valid);
23//!
24//! // Lint for dangerous operations
25//! let result = lint_sql("DELETE FROM users");
26//! assert!(result.has_errors()); // Missing WHERE clause
27//!
28//! // Check select_many patterns
29//! let result = lint_select_many("SELECT * FROM users");
30//! assert!(result.has_warnings()); // Missing LIMIT
31//!
32//! // Individual checks
33//! assert_eq!(delete_has_where("DELETE FROM users WHERE id = 1"), Some(true));
34//! ```
35
36pub mod client;
37pub mod error;
38pub mod schema_cache;
39pub mod schema_introspect;
40
41#[cfg(feature = "sql")]
42pub mod sql_analysis;
43
44#[cfg(feature = "sql")]
45pub mod sql_check;
46
47#[cfg(feature = "sql")]
48pub mod sql_lint;
49
50#[cfg(feature = "sql")]
51pub mod sql_rewrite;
52
53pub use client::{CheckClient, RowExt};
54pub use error::{CheckError, CheckResult};
55pub use schema_cache::{SchemaCache, SchemaCacheConfig, SchemaCacheLoad};
56pub use schema_introspect::{ColumnInfo, DbSchema, RelationKind, TableInfo};
57
58#[cfg(feature = "sql")]
59pub use sql_analysis::{
60    ColumnRefFull, InsertAnalysis, OnConflictAnalysis, RangeVarRef, SqlAnalysis, SqlParseCache,
61    TargetColumn, UpdateAnalysis, analyze_sql,
62};
63
64#[cfg(feature = "sql")]
65pub use sql_rewrite::ensure_select_limit;
66
67#[cfg(feature = "sql")]
68pub use sql_check::{
69    SqlCheckIssue, SqlCheckIssueKind, SqlCheckLevel, check_sql, check_sql_analysis,
70    check_sql_cached,
71};
72
73#[cfg(feature = "sql")]
74pub use sql_lint::{
75    ColumnRef, LintIssue, LintLevel, LintResult, ParseResult, StatementKind, delete_has_where,
76    detect_statement_kind, get_column_refs, get_table_names, is_valid_sql, lint_select_many,
77    lint_sql, select_has_limit, select_has_star, update_has_where,
78};