codeint ships a built-in sql Language plugin that measures every .sql file with no configuration required.
It reads SQL in two layers using sqlparser (ADR 0018): a tokenizer layer for statements, functions, complexity, and Duplication, and a per-statement parser layer for Flagged lines, Hazard lines, and SQL schemas.
The sql plugin claims the .sql extension only; SQL inside string literals of another language stays with that language.
With no Dialect configuration, codeint reads every .sql file as generic SQL and measures Erosion, Cognitive complexity, Duplication, Verbosity, and Hazards.
- Statements as functions: each top-level statement counts as one function spanning its first and last source line. Blank lines and comment-only lines stay out of source lines, as in every other language.
- Object names: a
CREATEstatement for aTABLE,VIEW,INDEX,FUNCTION,PROCEDURE,TRIGGER,TYPE, orMACROtakes the object's name, keeping schema prefixes such asanalytics.monthly_totals. Other statements, such as standaloneSELECTorUPDATEstatements, have no name. - Decision points: each
WHENarm of aCASE, eachANDandORin a condition (excludingBETWEEN ... AND, transactionAND CHAIN, andCREATE OR REPLACE), eachJOIN(includingSTRAIGHT_JOIN), each subquery ((SELECT ...)or(WITH ...)), and each set operation (UNION,INTERSECT, orEXCEPT, excluding wildcardEXCEPT) adds1. Window functions (OVER (...)) andGROUP BYadd0. - Cognitive complexity: nesting depth is subquery depth plus block depth inside a stored routine.
Each decision point adds
1 + nesting depth. - Duplication: exact and near-clone detection uses SQL tokens without comments, normalizing identifiers and literals while preserving SQL keywords.
- Flagged line rules: two AST-only rules feed Verbosity:
or-chain-collapses-to-in: three or moreORequality comparisons of the same column against constant literals.having-without-aggregate: aHAVINGcondition that contains no aggregate function.
After checking any source_sets globs in codeint.jsonc or codeint.json (which always win, in the order vendored, generated, test, production) and core Vendored and Generated rules, the sql plugin classifies each .sql file as follows:
- Generated:
- A database dump header in the first 20 lines:
-- PostgreSQL database dump,-- Dumped by pg_dump,-- MySQL dump(followed by a space),-- MariaDB database dump, or-- MariaDB dump(followed by a space). - A
-- Code generatedheader withDO NOT EDIT.(case-insensitive) in the first 20 lines. - A migration tool marker anywhere in the file:
--> statement-breakpoint,-- atlas:txmode, or-- atlas:nolint. - Any file inside a
prisma/migrations/ordrizzle/folder.
- A database dump header in the first 20 lines:
- Test: any file under a
test,tests,fixtures, ortestdatafolder at any depth, or any file named*.test.sqlortest_*.sql. - Production: every other
.sqlfile, including hand-written migrations.
Hand-written migrations are measured as Production so complex or destructive migrations receive every Metric and every Hazard line rule.
Generated SQL shows its Metrics and Hazards under the generated Source set so dumps and tool-written migrations never distort Production ratios.
- Templated SQL: codeint skips any
.sqlfile containing{{,{%,%},<%, or%>outside SQL strings and comments. The file appears underskippedwith reasontemplated SQL, so template files such as dbt models are not measured as raw SQL. - Rejected statements: because the parser reads one statement at a time, a single statement that the parser cannot parse never removes the rest of the file.
A rejected statement still counts in every token-layer Metric (source lines, Erosion, Cognitive complexity, and Duplication), receives no Flagged line or Hazard line rules, and is listed under
rejected_statementswith itspath, startingline, anddialect.
SQL has no runtime imports, package manifests, or entry points.
Because the sql Language plugin reports no imports, Import cycles, Orphan files, Import boundaries, and Dependency hygiene are absent for SQL (never 0), and reports include a one-line import_metrics_note explaining why.
In a monorepo with Workspace packages defined by other languages, each .sql file belongs to the Workspace package of its enclosing folder and counts in that package's SQL and repository totals.
Use the languages setting in codeint.jsonc or codeint.json to restrict sql to specific folders when needed.
Without configuration, every .sql file uses generic SQL.
Map repository-relative folders or file globs to a Dialect under sql.dialects in codeint.jsonc or codeint.json:
| Dialect | Covers |
|---|---|
postgresql |
PostgreSQL and PL/pgSQL routines |
mysql |
MySQL and MariaDB |
sqlite |
SQLite |
duckdb |
DuckDB |
Files matching no sql.dialects entry stay generic SQL.
ClickHouse, SQL Server, Oracle, BigQuery, and Snowflake stay generic SQL.
An unknown Dialect name, or one file matched by two entries with different Dialects, stops the run with exit code 2 and names the configuration key or conflicting patterns.
When codeint init detects .sql files, it adds a commented sql.dialects hint to the starter codeint.jsonc.
In a monorepo that mixes databases, assign each folder its own Dialect:
- Stored routines: a stored function, procedure, or trigger counts as one function including its whole body.
Inside a routine, each
IF,ELSIForELSEIF, loop (WHILE,FOR,REPEAT,LOOP), and error handler (EXCEPTION,HANDLER) adds1decision point and deepens Cognitive complexity block nesting. - PostgreSQL: dollar-quoted bodies (
$$ ... $$or$tag$ ... $tag$) on SQL and PL/pgSQL routines andDOblocks are tokenized again as SQL for complexity and Duplication.psqlbackslash commands (lines starting with\, outsideCOPY ... FROM STDIN;payloads) count as source lines and are not parsed as statements. - MySQL and MariaDB:
DELIMITERdirectives change the active statement delimiter until the nextDELIMITERdirective so routines with inner semicolons stay one function. TheDELIMITERlines themselves are not statements. - SQLite:
CREATE TRIGGER ... BEGIN ... END;stays one function spanning its full body. - DuckDB:
CREATE MACROcounts as one function named after the macro. DuckDB CLI dot-commands (lines starting with.followed by a letter, such as.modeor.read) count as source lines and are not parsed as statements.
codeint builds an in-memory schema automatically from the repository's own SQL files, with no live database connection and no schema-path configuration.
- Included statements:
CREATE TABLE(excludingCREATE TEMP/CREATE TEMPORARY TABLE),CREATE INDEX,ALTER TABLE(including added, modified, renamed, and dropped columns, constraints, and table renames),DROP TABLE, andDROP INDEX. - Included Source sets: Production, Test, and Generated SQL (including tool-written migrations), never Vendored SQL; rollback down-migration files (
down.sqlor*.down.sql) are skipped from schema construction while still checked as SQL files. - Ordering: files are applied in repository-relative path order, and statements in file order, so sequential migrations produce the final table, column, primary key, and index definitions.
- Scopes: codeint builds one schema per Workspace package and Dialect, plus one root schema per Dialect for
.sqlfiles outside every Workspace package. Generic SQL is its own Dialect scope.
Every codeint check report lists each scope's table count under sql_schemas (or under ## SQL schemas in Markdown).
A scope showing tables: 0 tells you that no CREATE TABLE definitions were found in that package and Dialect.
Five of the 32 SQL Hazard line rules use the built schema:
filter-on-unindexed-columnorder-by-unindexed-columnjoin-on-unindexed-columnunknown-columntext-compared-to-number
A schema rule matches only when the schema knows the table and the column resolves unambiguously to one table. When a table is not in the schema (such as an external table, CTE, or derived table) or when an unqualified column could belong to more than one table in scope, the schema rules stay silent so an incomplete schema never creates false Findings.
See Hazards for how Hazards works and docs/reference.md#sql for the complete 32-rule table.
Calibrated SQL Reference ranges come from a panel of 20 pre-2023 public SQL repositories (ADR 0022, source codeint panel of 20 SQL repositories, ADR 0022, codeint 0.6.0):
| Metric | Reference range |
|---|---|
| Erosion | 9.4% to 97.6% |
| Duplication | 4.2% to 92.1% |
| Verbosity | 4.2% to 92.1% |
| Import cycles | N/A |
codeint check displays these Reference ranges next to SQL values for Erosion, Duplication, and Verbosity; Import cycles stays absent because SQL has no runtime imports.
{ "sql": { "dialects": [ { "dialect": "postgresql", "include": ["services/billing/migrations", "services/billing/queries"], }, { "dialect": "duckdb", "include": ["analytics/**/*.sql"], }, ], }, }