Metadata-Version: 2.4
Name: sqlspec
Version: 0.36.3
Summary: SQL Experiments in Python
Project-URL: Discord, https://discord.gg/litestar
Project-URL: Issue, https://github.com/litestar-org/sqlspec/issues/
Project-URL: Source, https://github.com/litestar-org/sqlspec
Author-email: Cody Fincher <cody@litestar.dev>
Maintainer-email: Litestar Developers <hello@litestar.dev>
License-Expression: MIT
License-File: LICENSE
Requires-Python: <4.0,>=3.10
Requires-Dist: mypy-extensions
Requires-Dist: rich-click>=1.9.0
Requires-Dist: sqlglot>=19.9.0
Requires-Dist: tomli>=2.0.0; python_version < '3.11'
Requires-Dist: typing-extensions
Provides-Extra: adbc
Requires-Dist: adbc-driver-manager; extra == 'adbc'
Requires-Dist: pyarrow; extra == 'adbc'
Provides-Extra: adk
Requires-Dist: google-adk; extra == 'adk'
Provides-Extra: aioodbc
Requires-Dist: aioodbc; extra == 'aioodbc'
Provides-Extra: aiosqlite
Requires-Dist: aiosqlite; extra == 'aiosqlite'
Provides-Extra: alloydb
Requires-Dist: google-cloud-alloydb-connector; extra == 'alloydb'
Provides-Extra: asyncmy
Requires-Dist: asyncmy; extra == 'asyncmy'
Provides-Extra: asyncpg
Requires-Dist: asyncpg; extra == 'asyncpg'
Provides-Extra: attrs
Requires-Dist: attrs; extra == 'attrs'
Requires-Dist: cattrs; extra == 'attrs'
Provides-Extra: bigquery
Requires-Dist: google-cloud-bigquery; extra == 'bigquery'
Requires-Dist: google-cloud-storage; extra == 'bigquery'
Provides-Extra: cloud-sql
Requires-Dist: cloud-sql-python-connector; extra == 'cloud-sql'
Provides-Extra: cockroachdb
Requires-Dist: asyncpg; extra == 'cockroachdb'
Requires-Dist: psycopg[binary,pool]; extra == 'cockroachdb'
Provides-Extra: duckdb
Requires-Dist: duckdb; extra == 'duckdb'
Provides-Extra: fastapi
Requires-Dist: fastapi; extra == 'fastapi'
Provides-Extra: flask
Requires-Dist: flask; extra == 'flask'
Provides-Extra: fsspec
Requires-Dist: fsspec; extra == 'fsspec'
Provides-Extra: litestar
Requires-Dist: litestar; extra == 'litestar'
Provides-Extra: msgspec
Requires-Dist: msgspec; extra == 'msgspec'
Provides-Extra: mypyc
Provides-Extra: mysql-connector
Requires-Dist: mysql-connector-python; extra == 'mysql-connector'
Provides-Extra: nanoid
Requires-Dist: fastnanoid>=0.4.1; extra == 'nanoid'
Provides-Extra: obstore
Requires-Dist: obstore; extra == 'obstore'
Provides-Extra: opentelemetry
Requires-Dist: opentelemetry-instrumentation; extra == 'opentelemetry'
Provides-Extra: oracledb
Requires-Dist: oracledb; extra == 'oracledb'
Provides-Extra: orjson
Requires-Dist: orjson; extra == 'orjson'
Provides-Extra: pandas
Requires-Dist: pandas; extra == 'pandas'
Requires-Dist: pyarrow; extra == 'pandas'
Provides-Extra: performance
Requires-Dist: msgspec; extra == 'performance'
Requires-Dist: sqlglot[rs]; extra == 'performance'
Provides-Extra: polars
Requires-Dist: polars; extra == 'polars'
Requires-Dist: pyarrow; extra == 'polars'
Provides-Extra: prometheus
Requires-Dist: prometheus-client; extra == 'prometheus'
Provides-Extra: psqlpy
Requires-Dist: psqlpy; extra == 'psqlpy'
Provides-Extra: psycopg
Requires-Dist: psycopg[binary,pool]; extra == 'psycopg'
Provides-Extra: pydantic
Requires-Dist: pydantic; extra == 'pydantic'
Requires-Dist: pydantic-extra-types; extra == 'pydantic'
Provides-Extra: pymssql
Requires-Dist: pymssql; extra == 'pymssql'
Provides-Extra: pymysql
Requires-Dist: pymysql; extra == 'pymysql'
Provides-Extra: spanner
Requires-Dist: google-cloud-spanner; extra == 'spanner'
Provides-Extra: uuid
Requires-Dist: uuid-utils; extra == 'uuid'
Description-Content-Type: text/markdown

# SQLSpec

**Type-safe SQL execution layer for Python.**

SQLSpec handles database connectivity and result mapping so you can focus on SQL. Write raw queries when you need precision, use the builder API when you need composability, or load SQL from files when you need organization. Every statement passes through a [sqlglot](https://github.com/tobymao/sqlglot)-powered AST pipeline for validation, dialect conversion, and optimization before execution. Export results as Python objects, Arrow tables, Polars or pandas DataFrames.

It's not an ORM. It's the connectivity and processing layer between your application and your database that provides the right abstraction for each situation without dictating how you write SQL.

## Status

SQLSpec is currently in active development. The public API may change. Follow the [docs](https://sqlspec.dev/) and changelog for updates.

## What You Get

**Connection Management**

- Connection pooling with configurable size, timeout, and lifecycle hooks
- Sync and async support with a unified API surface
- Adapters for PostgreSQL (psycopg, asyncpg, psqlpy), SQLite (sqlite3, aiosqlite), DuckDB, MySQL (asyncmy, mysql-connector, pymysql), Oracle, BigQuery, and ADBC-compatible databases

**Query Execution**

- Raw SQL strings with automatic parameter binding and dialect translation
- SQL AST parsing via sqlglot for validation, optimization, and dialect conversion
- Builder API for programmatic query construction without string concatenation
- SQL file loading to keep queries organized alongside your code (named SQL queries)
- Statement stacks for batching multiple operations with transaction control

**Result Handling**

- Type-safe result mapping to Pydantic, msgspec, attrs, or dataclasses
- Apache Arrow export for zero-copy integration with pandas, Polars, and analytical tools
- Result iteration, single-row fetch, or bulk retrieval based on your use case

**Framework Integration**

- Litestar plugin with dependency injection for connections, sessions, and pools
- Starlette/FastAPI middleware for automatic transaction management
- Flask extension with sync/async portal support

**Production Features**

- SQL validation and caching via sqlglot AST parsing
- OpenTelemetry and Prometheus instrumentation hooks
- Database event channels with native LISTEN/NOTIFY, Oracle AQ, and a portable queue fallback
- Structured logging with correlation ID support
- Migration CLI for schema versioning

## Quick Start

### Install

```bash
pip install "sqlspec"
```

### Run your first query

```python
from pydantic import BaseModel
from sqlspec import SQLSpec
from sqlspec.adapters.sqlite import SqliteConfig

class Greeting(BaseModel):
    message: str

spec = SQLSpec()
db = spec.add_config(SqliteConfig(connection_config={"database": ":memory:"}))

with spec.provide_session(db) as session:
    greeting = session.select_one(
        "SELECT 'Hello, SQLSpec!' AS message",
        schema_type=Greeting,
    )
    print(greeting.message)  # Output: Hello, SQLSpec!
```

That's it. Write SQL, define a schema, get typed objects back. Connection pooling, parameter binding, and result mapping are handled automatically.

See the [Getting Started guide](https://sqlspec.dev/getting_started/) for installation variants, adapter selection, and advanced result mapping options.

## Documentation

- [Getting Started](https://sqlspec.dev/getting_started/)
- [Usage Guides](https://sqlspec.dev/usage/)
- [Examples Gallery](https://sqlspec.dev/examples/)
- [API Reference](https://sqlspec.dev/reference/)
- [CLI Reference](https://sqlspec.dev/usage/cli.html)

## Reference Applications

- **[PostgreSQL + Vertex AI Demo](https://github.com/cofin/postgres-vertexai-demo)** - Vector search with pgvector and real-time chat using Litestar and Google ADK. Shows connection pooling, migrations, type-safe result mapping, vector embeddings, and response caching.
- **[Oracle + Vertex AI Demo](https://github.com/cofin/oracledb-vertexai-demo)** - Oracle 23ai vector search with semantic similarity using HNSW indexes. Demonstrates NumPy array conversion, large object (CLOB) handling, and real-time performance metrics.

See the [usage docs](https://sqlspec.dev/usage/) for detailed guides on adapters, configuration patterns, and features like the [SQL file loader](https://sqlspec.dev/usage/loader.html).

## Built With

- **[sqlglot](https://github.com/tobymao/sqlglot)** - SQL parser, transpiler, and optimizer powering SQLSpec's AST pipeline

## Contributing

Contributions, issue reports, and adapter ideas are welcome. Review the
[contributor guide](https://sqlspec.dev/contributing/) and follow the project
coding standards before opening a pull request.

## License

SQLSpec is distributed under the MIT License.
