# andialbrecht/sqlparse architecture

> Architecture diagram of the GitHub repository andialbrecht/sqlparse, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/andialbrecht/sqlparse
- Repository: https://github.com/andialbrecht/sqlparse

## Overview

sqlparse is a non-validating SQL parsing library. The main workflow tokenizes SQL, splits it into statements, optionally groups tokens into a navigable syntax tree, and returns statements for inspection or formatting. Formatting uses configurable filters; the command-line tool reads SQL and invokes the same formatting API. The graph preserves the documented split, parse, tree-inspection, formatting, and CLI capabilities. Some internal links rely on the SOURCE INDEX or README; unsampled implementation details are not inferred.

Main entry points: [`sqlparse/formatter.py`](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/formatter.py) (Format Options), [`sqlparse/engine/filter_stack.py`](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/engine/filter_stack.py) (Filter Stack), [`sqlparse/__init__.py`](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/__init__.py) (API Functions), [`sqlparse/lexer.py`](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/lexer.py) (SQL Lexer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public API"]
  node_api["API Functions<br/>[__init__.py]"]
end

subgraph group_parse["Parsing Engine"]
  node_lexer["SQL Lexer<br/>[lexer.py]"]
  node_keywords["Keyword Rules<br/>[keywords.py]"]
  node_splitter["Statement Splitter"]
  node_grouping["Token Grouping<br/>[grouping.py]"]
  node_stack["Filter Stack<br/>[filter_stack.py]"]
end

subgraph group_model["Token Model"]
  node_tokens["Token Types<br/>[tokens.py]"]
  node_tree["SQL Token Tree<br/>[sql.py]"]
end

subgraph group_format["Formatting"]
  node_formatter["Format Options<br/>[formatter.py]"]
  node_reindent["Reindent Filter<br/>[reindent.py]"]
  node_aligned["Aligned Indent<br/>[aligned_indent.py]"]
  node_otherfilters["Text Filters<br/>[others.py]"]
  node_casefilters["Token Filters<br/>[tokens.py]"]
  node_outputfilters["Output Filters<br/>[output.py]"]
end

subgraph group_cli["Command Line"]
  node_cli["sqlformat CLI<br/>[cli.py]"]
end

node_user(("SQL User"))

node_user -->|"submits SQL"| node_api
node_api -->|"parses and splits"| node_stack
node_stack -->|"tokenizes"| node_lexer
node_lexer -->|"uses rules"| node_keywords
node_lexer -->|"emits types"| node_tokens
node_stack -->|"splits stream"| node_splitter
node_stack -->|"groups optionally"| node_grouping
node_grouping -->|"builds groups"| node_tree
node_tree -->|"uses types"| node_tokens
node_api -->|"formats SQL"| node_formatter
node_formatter -->|"configures filter"| node_reindent
node_formatter -->|"configures filter"| node_aligned
node_formatter -->|"configures filters"| node_otherfilters
node_formatter -->|"configures filters"| node_casefilters
node_formatter -->|"configures filters"| node_outputfilters
node_user -->|"runs command"| node_cli
node_cli -->|"formats input"| node_api

click node_api "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/__init__.py"
click node_lexer "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/lexer.py"
click node_keywords "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/keywords.py"
click node_tokens "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/tokens.py"
click node_splitter "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/engine/statement_splitter.py"
click node_grouping "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/engine/grouping.py"
click node_tree "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/sql.py"
click node_stack "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/engine/filter_stack.py"
click node_formatter "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/formatter.py"
click node_reindent "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/reindent.py"
click node_aligned "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/aligned_indent.py"
click node_otherfilters "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/others.py"
click node_casefilters "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/tokens.py"
click node_outputfilters "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/output.py"
click node_cli "https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/cli.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_user toneBlue
class node_lexer,node_keywords,node_splitter,node_grouping,node_stack toneAmber
class node_tokens,node_tree toneMint
class node_formatter,node_reindent,node_aligned,node_otherfilters,node_casefilters,node_outputfilters toneRose
class node_cli toneIndigo
```

## Components

### Public API

- [API Functions](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/__init__.py): `sqlparse/__init__.py`

### Parsing Engine

- [SQL Lexer](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/lexer.py): `sqlparse/lexer.py`
- [Keyword Rules](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/keywords.py): `sqlparse/keywords.py`
- [Statement Splitter](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/engine/statement_splitter.py): `sqlparse/engine/statement_splitter.py`
- [Token Grouping](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/engine/grouping.py): `sqlparse/engine/grouping.py`
- [Filter Stack](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/engine/filter_stack.py): `sqlparse/engine/filter_stack.py`

### Token Model

- [Token Types](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/tokens.py): `sqlparse/tokens.py`
- [SQL Token Tree](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/sql.py): `sqlparse/sql.py`

### Formatting

- [Format Options](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/formatter.py): `sqlparse/formatter.py`
- [Reindent Filter](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/reindent.py): `sqlparse/filters/reindent.py`
- [Aligned Indent](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/aligned_indent.py): `sqlparse/filters/aligned_indent.py`
- [Text Filters](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/others.py): `sqlparse/filters/others.py`
- [Token Filters](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/tokens.py): `sqlparse/filters/tokens.py`
- [Output Filters](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/filters/output.py): `sqlparse/filters/output.py`

### Command Line

- [sqlformat CLI](https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/cli.py): `sqlparse/cli.py`

### Other components

- **SQL User**

## Connections

- SQL User → API Functions: submits SQL (evidence: `README.md`)
- API Functions → Filter Stack: parses and splits (evidence: `sqlparse/__init__.py`)
- Filter Stack → SQL Lexer: tokenizes (evidence: `sqlparse/engine/filter_stack.py`)
- SQL Lexer → Keyword Rules: uses rules (evidence: `sqlparse/lexer.py`)
- SQL Lexer → Token Types: emits types (evidence: `sqlparse/lexer.py`)
- Filter Stack → Statement Splitter: splits stream (evidence: `sqlparse/engine/filter_stack.py`)
- Filter Stack → Token Grouping: groups optionally (evidence: `sqlparse/engine/filter_stack.py`)
- Token Grouping → SQL Token Tree: builds groups (evidence: `sqlparse/engine/grouping.py`)
- SQL Token Tree → Token Types: uses types (evidence: `sqlparse/sql.py`)
- API Functions → Format Options: formats SQL (evidence: `sqlparse/__init__.py`)
- Format Options → Reindent Filter: configures filter (evidence: `sqlparse/formatter.py`)
- Format Options → Aligned Indent: configures filter (evidence: `sqlparse/formatter.py`)
- Format Options → Text Filters: configures filters (evidence: `sqlparse/formatter.py`)
- Format Options → Token Filters: configures filters (evidence: `sqlparse/formatter.py`)
- Format Options → Output Filters: configures filters (evidence: `sqlparse/formatter.py`)
- SQL User → sqlformat CLI: runs command (evidence: `README.md`)
- sqlformat CLI → API Functions: formats input (evidence: `sqlparse/cli.py`)
