Skip to content
Open
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
91 changes: 91 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ sqlparser = "0.53"
tracing = "0.1"
tracing-subscriber = "0.3"

# Python bindings (optional)
pyo3 = { version = "0.22", features = ["extension-module"], optional = true }

[features]
default = []
python = ["pyo3"]

[lib]
name = "pgrsql"
path = "src/lib.rs"
crate-type = ["lib"]

[[bin]]
name = "pgrsql"
path = "src/main.rs"
Expand Down
30 changes: 30 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[build-system]
requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"

[project]
name = "pgrsql"
version = "0.1.3"
description = "High-performance SQL parsing, optimization, and analysis engine powered by Rust"
requires-python = ">=3.9"
license = { text = "MIT" }
keywords = ["sql", "postgresql", "parser", "optimizer", "database"]
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Rust",
"Topic :: Database",
"Topic :: Software Development :: Libraries",
]

[tool.maturin]
features = ["python"]
module-name = "pgrsql._pgrsql"
python-source = "python"
41 changes: 41 additions & 0 deletions python/pgrsql/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""pgrsql - High-performance SQL parsing, optimization, and analysis engine.

Built on a Rust core with PyO3 bindings for maximum performance.

Usage:
import pgrsql

# Parse SQL to normalized form
statements = pgrsql.parse_sql("SELECT * FROM users WHERE age > 18")

# Format and optimize SQL
formatted = pgrsql.format_sql("select id,name from users where age>18")

# Analyze query structure
analysis = pgrsql.analyze_query("SELECT * FROM a JOIN b ON a.id = b.id")
# {'has_select': True, 'has_joins': True, 'join_count': 1, ...}

# Parse EXPLAIN output
plan = pgrsql.parse_explain("Seq Scan on users (cost=0.00..35.50 rows=100 width=36)")

# Check if a query is an EXPLAIN query
pgrsql.is_explain_query("EXPLAIN SELECT 1") # True
"""

from pgrsql._pgrsql import (
__version__,
analyze_query,
format_sql,
is_explain_query,
parse_explain,
parse_sql,
)

__all__ = [
"__version__",
"parse_sql",
"format_sql",
"analyze_query",
"parse_explain",
"is_explain_query",
]
6 changes: 6 additions & 0 deletions src/db/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ pub struct ConnectionManager {
pub current_schema: String,
}

impl Default for ConnectionManager {
fn default() -> Self {
Self::new()
}
}

#[allow(dead_code)]
impl ConnectionManager {
pub fn new() -> Self {
Expand Down
1 change: 1 addition & 0 deletions src/editor/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl QueryHistory {
self.entries.get(idx)
}

#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Option<&HistoryEntry> {
if self.entries.is_empty() {
return None;
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub mod ast;
pub mod db;
pub mod editor;
pub mod explain;
pub mod export;
pub mod ui;

#[cfg(feature = "python")]
pub mod python;
13 changes: 3 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
pub mod ast;
mod db;
mod editor;
mod explain;
mod export;
mod ui;

use crate::db::ConnectionManager;
use crate::ui::App;
use anyhow::Result;
use clap::Parser;
use crossterm::{
Expand All @@ -16,6 +7,8 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use pgrsql::db::ConnectionManager;
use pgrsql::ui::App;
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;

Expand Down Expand Up @@ -108,7 +101,7 @@ async fn run_app(
app: &mut App,
) -> Result<()> {
loop {
terminal.draw(|f| ui::draw(f, app))?;
terminal.draw(|f| pgrsql::ui::draw(f, app))?;

if event::poll(std::time::Duration::from_millis(100))? {
if let Event::Key(key) = event::read()? {
Expand Down
Loading