Skip to content

Commit 3c8b9ec

Browse files
Ajit Pratap Singhclaude
authored andcommitted
docs: comprehensive documentation refresh for v1.5.1+ features
- USAGE_GUIDE.md: Add Simple API section, v1.5.1 features (GROUPING SETS, ROLLUP, CUBE, MERGE, Materialized Views, Expression Operators), SQL Injection Detection section with examples - CLEAN_ARCHITECTURE.md: Add security package, gosqlx high-level API, update AST types with new statement/expression types, version info - PRODUCTION_GUIDE.md: Add SQL Injection Detection section with code examples for production security scanning, version header - SECURITY.md (root): Add cross-reference note to docs/SECURITY.md - docs/SECURITY.md: Add cross-reference note to root SECURITY.md - Removed empty /docs/getting-started/ directory 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 98eb46d commit 3c8b9ec

5 files changed

Lines changed: 303 additions & 4 deletions

File tree

SECURITY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Security Policy
22

3+
> **Note**: This file covers security policies and vulnerability reporting. For comprehensive security analysis, threat modeling, and the SQL injection detection API, see [docs/SECURITY.md](docs/SECURITY.md).
4+
35
## Supported Versions
46

57
We release patches for security vulnerabilities. Currently supported versions:

docs/CLEAN_ARCHITECTURE.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# GoSQLX Clean Architecture Guide
22

3+
**Version**: v1.5.1+ | **Last Updated**: November 2025
4+
35
This document outlines the architectural principles and structure of the GoSQLX codebase after comprehensive cleanup and optimization.
46

57
## 📁 Directory Structure
@@ -28,12 +30,15 @@ GoSQLX/
2830
│ │
2931
│ ├── sql/ # SQL processing components
3032
│ │ ├── tokenizer/ # Lexical analysis
31-
│ │ ├── parser/ # Syntax analysis
33+
│ │ ├── parser/ # Syntax analysis (11 modular files)
3234
│ │ ├── ast/ # Abstract syntax trees
3335
│ │ ├── keywords/ # SQL keyword definitions
3436
│ │ ├── token/ # Token management
37+
│ │ ├── security/ # SQL injection detection (v1.4+)
3538
│ │ └── monitor/ # Performance monitoring
3639
│ │
40+
│ ├── gosqlx/ # Simple high-level API (v1.4+)
41+
│ │
3742
│ └── metrics/ # Performance metrics
3843
3944
├── testdata/ # Test data and fixtures
@@ -128,13 +133,42 @@ CLI Commands → Business Logic → Core Library → Models
128133
- Node manipulation and transformation
129134
- Memory management with object pooling
130135

136+
**Statement Types** (v1.5.1+):
137+
- `SelectStatement`, `InsertStatement`, `UpdateStatement`, `DeleteStatement`
138+
- `CreateStatement`, `AlterStatement`, `DropStatement`
139+
- `MergeStatement` (SQL:2003 F312)
140+
- `MaterializedViewStatement` (CREATE/DROP/REFRESH)
141+
- `WithStatement` (CTEs, recursive CTEs)
142+
143+
**Expression Types** (v1.5.1+):
144+
- `BetweenExpression`, `InExpression`, `LikeExpression`, `IsNullExpression`
145+
- `SubqueryExpression` (scalar, table, correlated, EXISTS)
146+
- `WindowExpression` (OVER clause with PARTITION BY, ORDER BY, frames)
147+
- `GroupingExpression` (GROUPING SETS, ROLLUP, CUBE)
148+
131149
### `pkg/sql/keywords/`
132150
**Purpose**: SQL keyword recognition and categorization
133151
- Multi-dialect keyword support
134152
- Keyword classification and context
135153
- Reserved word identification
136154
- Dialect-specific variations
137155

156+
### `pkg/sql/security/` (v1.4+)
157+
**Purpose**: SQL injection detection and security analysis
158+
- Pattern-based injection detection
159+
- Tautology recognition (`1=1`, `'a'='a'`)
160+
- UNION-based injection detection
161+
- Time-based blind injection detection
162+
- Comment bypass detection
163+
- Severity classification (Critical, High, Medium, Low)
164+
165+
### `pkg/gosqlx/` (v1.4+)
166+
**Purpose**: Simple high-level API for common use cases
167+
- One-line parsing: `gosqlx.Parse(sql)`
168+
- Validation: `gosqlx.Validate(sql)`
169+
- Batch processing: `gosqlx.ParseMultiple(queries)`
170+
- Timeout support: `gosqlx.ParseWithTimeout(sql, timeout)`
171+
138172
## 🔄 Development Workflow
139173

140174
### 1. **Adding New Features**

docs/PRODUCTION_GUIDE.md

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# GoSQLX Production Deployment Guide
22

3+
**Version**: v1.5.1+ | **Last Updated**: November 2025
4+
35
Comprehensive guide for deploying GoSQLX in production environments.
46

57
## Executive Summary
@@ -317,20 +319,65 @@ func (p *SQLProcessor) ProcessWithSecurity(sql []byte) ([]interface{}, error) {
317319
// Acquire resource
318320
p.semaphore <- struct{}{}
319321
defer func() { <-p.semaphore }()
320-
322+
321323
// Validate input
322324
if err := validateSQLInput(sql); err != nil {
323325
return nil, fmt.Errorf("input validation failed: %w", err)
324326
}
325-
327+
326328
// Process with timeout
327329
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
328330
defer cancel()
329-
331+
330332
return p.processWithContext(ctx, sql)
331333
}
332334
```
333335

336+
### 4. SQL Injection Detection (v1.4+)
337+
338+
GoSQLX includes a built-in security scanner for detecting SQL injection patterns:
339+
340+
```go
341+
import "github.com/ajitpratap0/GoSQLX/pkg/sql/security"
342+
343+
func (p *SQLProcessor) ScanForInjection(sql []byte) error {
344+
// Parse SQL first
345+
tkz := tokenizer.GetTokenizer()
346+
defer tokenizer.PutTokenizer(tkz)
347+
348+
tokens, err := tkz.Tokenize(sql)
349+
if err != nil {
350+
return err
351+
}
352+
353+
// Parse to AST
354+
parser := parser.NewParser()
355+
defer parser.Release()
356+
ast, err := parser.Parse(tokens)
357+
if err != nil {
358+
return err
359+
}
360+
361+
// Scan for injection patterns
362+
scanner := security.NewScanner()
363+
result := scanner.Scan(ast)
364+
365+
if result.HasCritical() || result.HasHigh() {
366+
return fmt.Errorf("potential SQL injection detected: %d issues",
367+
result.CriticalCount + result.HighCount)
368+
}
369+
370+
return nil
371+
}
372+
```
373+
374+
**Detected Patterns:**
375+
- Tautology attacks (`1=1`, `'a'='a'`)
376+
- UNION-based injection
377+
- Time-based blind injection (`SLEEP`, `WAITFOR DELAY`)
378+
- Comment bypass (`--`, `/**/`)
379+
- Dangerous functions (`xp_cmdshell`, `LOAD_FILE`)
380+
334381
## Monitoring & Observability
335382

336383
### 1. Performance Metrics

docs/SECURITY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# GoSQLX Security Analysis Report
22

3+
> **Note**: This document provides comprehensive security analysis and the SQL injection detection API. For security policies and vulnerability reporting, see [SECURITY.md](../SECURITY.md) in the project root.
4+
35
## 🛡️ Comprehensive Security Assessment
46

57
**Analysis Date**: November 2025

docs/USAGE_GUIDE.md

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# GoSQLX Usage Guide
22

3+
**Version**: v1.5.1+ | **Last Updated**: November 2025
4+
35
## Table of Contents
46
- [Getting Started](#getting-started)
7+
- [Simple API (Recommended)](#simple-api-recommended)
58
- [Basic Usage](#basic-usage)
9+
- [Advanced SQL Features (v1.4+)](#advanced-sql-features-v14)
10+
- [SQL Injection Detection](#sql-injection-detection)
611
- [Advanced Patterns](#advanced-patterns)
712
- [Real-World Examples](#real-world-examples)
813
- [SQL Dialect Support](#sql-dialect-support)
@@ -31,6 +36,55 @@ import (
3136
)
3237
```
3338

39+
## Simple API (Recommended)
40+
41+
The simplest way to use GoSQLX is through the high-level API that handles all complexity for you:
42+
43+
```go
44+
package main
45+
46+
import (
47+
"fmt"
48+
"log"
49+
50+
"github.com/ajitpratap0/GoSQLX/pkg/gosqlx"
51+
)
52+
53+
func main() {
54+
// Parse SQL in one line - that's it!
55+
ast, err := gosqlx.Parse("SELECT * FROM users WHERE active = true")
56+
if err != nil {
57+
log.Fatal(err)
58+
}
59+
60+
fmt.Printf("Successfully parsed %d statement(s)\n", len(ast.Statements))
61+
}
62+
```
63+
64+
### More Simple API Examples
65+
66+
```go
67+
// Validate SQL without full parsing
68+
if err := gosqlx.Validate("SELECT * FROM users"); err != nil {
69+
fmt.Println("Invalid SQL:", err)
70+
}
71+
72+
// Parse multiple queries efficiently
73+
queries := []string{
74+
"SELECT * FROM users",
75+
"SELECT * FROM orders",
76+
}
77+
asts, err := gosqlx.ParseMultiple(queries)
78+
79+
// Parse with timeout for long queries
80+
ast, err := gosqlx.ParseWithTimeout(sql, 5*time.Second)
81+
82+
// Parse from byte slice (zero-copy)
83+
ast, err := gosqlx.ParseBytes([]byte("SELECT * FROM users"))
84+
```
85+
86+
> **Note:** The simple API has < 1% performance overhead compared to the low-level API. Use the simple API unless you need fine-grained control.
87+
3488
## Basic Usage
3589

3690
### Simple Tokenization
@@ -214,6 +268,166 @@ func HandleTokenizerError(sql string) {
214268
}
215269
```
216270

271+
## Advanced SQL Features (v1.4+)
272+
273+
### GROUPING SETS, ROLLUP, CUBE (SQL-99 T431)
274+
275+
```go
276+
// GROUPING SETS - explicit grouping combinations
277+
sql := `SELECT region, product, SUM(sales)
278+
FROM orders
279+
GROUP BY GROUPING SETS ((region), (product), (region, product), ())`
280+
ast, err := gosqlx.Parse(sql)
281+
282+
// ROLLUP - hierarchical subtotals
283+
sql := `SELECT year, quarter, month, SUM(revenue)
284+
FROM sales
285+
GROUP BY ROLLUP (year, quarter, month)`
286+
ast, err := gosqlx.Parse(sql)
287+
288+
// CUBE - all possible combinations
289+
sql := `SELECT region, product, SUM(amount)
290+
FROM sales
291+
GROUP BY CUBE (region, product)`
292+
ast, err := gosqlx.Parse(sql)
293+
```
294+
295+
### MERGE Statements (SQL:2003 F312)
296+
297+
```go
298+
sql := `
299+
MERGE INTO target_table t
300+
USING source_table s ON t.id = s.id
301+
WHEN MATCHED THEN
302+
UPDATE SET t.name = s.name, t.value = s.value
303+
WHEN NOT MATCHED THEN
304+
INSERT (id, name, value) VALUES (s.id, s.name, s.value)
305+
`
306+
ast, err := gosqlx.Parse(sql)
307+
```
308+
309+
### Materialized Views
310+
311+
```go
312+
// Create materialized view
313+
sql := `CREATE MATERIALIZED VIEW sales_summary AS
314+
SELECT region, SUM(amount) as total
315+
FROM sales GROUP BY region`
316+
ast, err := gosqlx.Parse(sql)
317+
318+
// Refresh materialized view
319+
sql := `REFRESH MATERIALIZED VIEW CONCURRENTLY sales_summary`
320+
ast, err := gosqlx.Parse(sql)
321+
322+
// Drop materialized view
323+
sql := `DROP MATERIALIZED VIEW IF EXISTS sales_summary`
324+
ast, err := gosqlx.Parse(sql)
325+
```
326+
327+
### Expression Operators (BETWEEN, IN, LIKE, IS NULL)
328+
329+
```go
330+
// BETWEEN with expressions
331+
sql := `SELECT * FROM orders WHERE amount BETWEEN 100 AND 500`
332+
333+
// IN with subquery
334+
sql := `SELECT * FROM users WHERE id IN (SELECT user_id FROM admins)`
335+
336+
// LIKE with pattern matching
337+
sql := `SELECT * FROM products WHERE name LIKE '%widget%'`
338+
339+
// IS NULL / IS NOT NULL
340+
sql := `SELECT * FROM users WHERE deleted_at IS NULL`
341+
342+
// NULLS FIRST/LAST ordering (SQL-99 F851)
343+
sql := `SELECT * FROM users ORDER BY last_login DESC NULLS LAST`
344+
```
345+
346+
### Subqueries
347+
348+
```go
349+
// Scalar subquery
350+
sql := `SELECT name, (SELECT MAX(salary) FROM employees) as max_sal FROM users`
351+
352+
// EXISTS subquery
353+
sql := `SELECT * FROM orders o
354+
WHERE EXISTS (SELECT 1 FROM customers c WHERE c.id = o.customer_id)`
355+
356+
// Correlated subquery
357+
sql := `SELECT * FROM employees e
358+
WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept = e.dept)`
359+
```
360+
361+
## SQL Injection Detection
362+
363+
GoSQLX includes a built-in security scanner (`pkg/sql/security`) for detecting SQL injection patterns:
364+
365+
```go
366+
import (
367+
"github.com/ajitpratap0/GoSQLX/pkg/gosqlx"
368+
"github.com/ajitpratap0/GoSQLX/pkg/sql/security"
369+
)
370+
371+
func CheckForInjection(sql string) {
372+
// Parse the SQL first
373+
ast, err := gosqlx.Parse(sql)
374+
if err != nil {
375+
fmt.Println("Parse error:", err)
376+
return
377+
}
378+
379+
// Create scanner and scan for injection patterns
380+
scanner := security.NewScanner()
381+
result := scanner.Scan(ast)
382+
383+
// Check results
384+
if result.HasCritical() {
385+
fmt.Printf("CRITICAL: Found %d critical security issues!\n", result.CriticalCount)
386+
}
387+
if result.HasHigh() {
388+
fmt.Printf("HIGH: Found %d high-severity issues\n", result.HighCount)
389+
}
390+
391+
// Print all findings
392+
for _, finding := range result.Findings {
393+
fmt.Printf("[%s] %s: %s\n",
394+
finding.Severity,
395+
finding.Pattern,
396+
finding.Description)
397+
}
398+
}
399+
```
400+
401+
### Detected Injection Patterns
402+
403+
The security scanner detects:
404+
- **Tautology patterns**: `1=1`, `'a'='a'`, always-true conditions
405+
- **UNION-based injection**: Unauthorized UNION statements
406+
- **Time-based blind injection**: `SLEEP()`, `WAITFOR DELAY`
407+
- **Comment bypass**: `--`, `/**/` comment abuse
408+
- **Stacked queries**: Multiple statement injection
409+
- **Dangerous functions**: `xp_cmdshell`, `LOAD_FILE`, `INTO OUTFILE`
410+
411+
```go
412+
// Example: Check user input for injection
413+
func ValidateUserQuery(userInput string) error {
414+
ast, err := gosqlx.Parse(userInput)
415+
if err != nil {
416+
return fmt.Errorf("invalid SQL syntax: %w", err)
417+
}
418+
419+
scanner := security.NewScanner()
420+
result := scanner.Scan(ast)
421+
422+
if result.HasCritical() || result.HasHigh() {
423+
return fmt.Errorf("potential SQL injection detected: %d issues found",
424+
result.CriticalCount + result.HighCount)
425+
}
426+
427+
return nil
428+
}
429+
```
430+
217431
## Real-World Examples
218432

219433
### SQL Validator

0 commit comments

Comments
 (0)