|
| 1 | +# LQL (Lambda Query Language) Current Functionality Specification |
| 2 | + |
| 3 | +## Overview |
| 4 | +LQL is a functional pipeline-style DSL that transpiles to procedural SQL, providing an intuitive and composable way to write database queries using lambda expressions and pipeline operators. While it provides a cross platform way to write SQL queries, it really shines as a way of writing procedural SQL code in a more functional way. |
| 5 | + |
| 6 | +## Core Features |
| 7 | + |
| 8 | +### 1. Pipeline Syntax |
| 9 | +- **Pipeline Operator (`|>`)**: Chain operations in a functional style |
| 10 | +- **Composition**: Build complex queries from simple operations |
| 11 | +- **Readability**: Linear flow matches thought process |
| 12 | +- **Immutability**: Each operation returns a new query state |
| 13 | + |
| 14 | +### 2. Database Support |
| 15 | +- **SQLite**: Full transpilation support with C# function generation |
| 16 | +- **SQL Server**: Complete dialect implementation |
| 17 | +- **PostgreSQL**: Full support with PostgreSQL-specific features |
| 18 | +- **Cross-Database**: Write once, transpile to multiple SQL dialects |
| 19 | + |
| 20 | +### 3. Query Operations |
| 21 | + |
| 22 | +#### Basic Operations |
| 23 | +- **select()**: Column selection with aliasing support |
| 24 | +- **filter()**: WHERE clause with lambda expressions |
| 25 | +- **distinct()**: Remove duplicate rows |
| 26 | +- **limit()**: Row limiting |
| 27 | +- **offset()**: Row skipping for pagination |
| 28 | + |
| 29 | +#### Join Operations |
| 30 | +- **join()**: Inner joins with ON conditions |
| 31 | +- **left_join()**: Left outer joins |
| 32 | +- **right_join()**: Right outer joins (dialect-specific) |
| 33 | +- **cross_join()**: Cartesian product |
| 34 | + |
| 35 | +#### Grouping & Aggregation |
| 36 | +- **group_by()**: Group rows by columns |
| 37 | +- **having()**: Filter grouped results |
| 38 | +- **Aggregate Functions**: COUNT, SUM, AVG, MIN, MAX |
| 39 | + |
| 40 | +#### Set Operations |
| 41 | +- **union()**: Combine queries (distinct) |
| 42 | +- **union_all()**: Combine queries (with duplicates) |
| 43 | +- **intersect()**: Common rows between queries |
| 44 | +- **except()**: Rows in first query not in second |
| 45 | + |
| 46 | +#### Ordering |
| 47 | +- **order_by()**: Sort results (ASC/DESC) |
| 48 | +- **Multiple columns**: Support for complex sorting |
| 49 | + |
| 50 | +### 4. Lambda Expression Support |
| 51 | +- **Filter Predicates**: `fn(row) => row.column = value` |
| 52 | +- **Complex Conditions**: AND, OR, NOT operators |
| 53 | +- **Comparison Operators**: =, !=, <, >, <=, >= |
| 54 | +- **Pattern Matching**: LIKE, NOT LIKE |
| 55 | +- **IN Operator**: Check membership in lists |
| 56 | +- **NULL Handling**: IS NULL, IS NOT NULL |
| 57 | + |
| 58 | +### 5. Function Support |
| 59 | + |
| 60 | +#### String Functions |
| 61 | +- UPPER(), LOWER() |
| 62 | +- LENGTH(), CONCAT() |
| 63 | +- SUBSTRING(), TRIM() |
| 64 | + |
| 65 | +#### Date Functions |
| 66 | +- NOW(), DATE() |
| 67 | +- YEAR(), MONTH(), DAY() |
| 68 | +- Date arithmetic |
| 69 | + |
| 70 | +#### Mathematical Functions |
| 71 | +- Basic arithmetic: +, -, *, / |
| 72 | +- Modulo operator: % |
| 73 | +- Mathematical functions per dialect |
| 74 | + |
| 75 | +#### Conditional Functions |
| 76 | +- CASE WHEN expressions |
| 77 | +- COALESCE() |
| 78 | +- NULLIF() |
| 79 | + |
| 80 | +### 6. Advanced Features |
| 81 | + |
| 82 | +#### Subqueries |
| 83 | +- **IN subqueries**: Filter based on subquery results |
| 84 | +- **Scalar subqueries**: Single value subqueries |
| 85 | +- **Correlated subqueries**: Reference outer query |
| 86 | + |
| 87 | +#### Variables & Let Bindings |
| 88 | +- **let statements**: Define reusable query fragments |
| 89 | +- **Variable references**: Use defined queries in expressions |
| 90 | + |
| 91 | +#### CTEs (Common Table Expressions) |
| 92 | +- **WITH clause**: Define temporary named result sets |
| 93 | +- **Recursive CTEs**: Planned feature |
| 94 | + |
| 95 | +### 7. Tooling |
| 96 | + |
| 97 | +#### CLI Tool (LqlCli) |
| 98 | +- **Transpilation**: Convert .lql to .sql files |
| 99 | +- **Validation**: Syntax checking without transpilation |
| 100 | +- **Console Output**: Quick testing of queries |
| 101 | +- **File Processing**: Batch processing of LQL files |
| 102 | + |
| 103 | +#### VS Code Extension |
| 104 | +- **Syntax Highlighting**: LQL-specific syntax colors |
| 105 | +- **IntelliSense**: Auto-completion for keywords |
| 106 | +- **Error Diagnostics**: Real-time syntax validation |
| 107 | +- **Snippets**: Common query patterns |
| 108 | +- **Format on Save**: Code formatting |
| 109 | + |
| 110 | +#### Browser Playground |
| 111 | +- **Interactive Editor**: Write and test LQL in browser |
| 112 | +- **Live Transpilation**: See SQL output in real-time |
| 113 | +- **Multiple Dialects**: Switch between SQL targets |
| 114 | +- **Schema Explorer**: Browse database structure |
| 115 | +- **Example Queries**: Learn from examples |
| 116 | + |
| 117 | +### 8. Parser Implementation |
| 118 | + |
| 119 | +#### ANTLR Grammar |
| 120 | +- **Lexer Rules**: Token definitions |
| 121 | +- **Parser Rules**: Grammar structure |
| 122 | +- **Visitor Pattern**: AST traversal and transformation |
| 123 | + |
| 124 | +#### AST (Abstract Syntax Tree) |
| 125 | +- **Statement Types**: Query, expression, operation nodes |
| 126 | +- **Type System**: Basic type inference |
| 127 | +- **Validation**: Semantic analysis during parsing |
| 128 | + |
| 129 | +### 9. Transpilation Pipeline |
| 130 | +1. **Lexical Analysis**: Tokenize LQL input |
| 131 | +2. **Parsing**: Build AST from tokens |
| 132 | +3. **Semantic Analysis**: Validate and type-check |
| 133 | +4. **Optimization**: Query optimization passes |
| 134 | +5. **SQL Generation**: Dialect-specific SQL output |
| 135 | + |
| 136 | +### 10. Integration with DataProvider |
| 137 | +- **Automatic Processing**: .lql files processed during build |
| 138 | +- **Code Generation**: Extension methods from LQL queries |
| 139 | +- **Type Safety**: Compile-time validation |
| 140 | +- **Parameter Binding**: Automatic parameter detection |
| 141 | + |
| 142 | +## Implementation Architecture |
| 143 | + |
| 144 | +### Core Components |
| 145 | +``` |
| 146 | +Lql/ |
| 147 | +├── Parsing/ |
| 148 | +│ ├── Lql.g4 (ANTLR grammar) |
| 149 | +│ ├── LqlLexer.cs |
| 150 | +│ ├── LqlParser.cs |
| 151 | +│ └── LqlToAstVisitor.cs |
| 152 | +├── AST/ |
| 153 | +│ ├── Statement types |
| 154 | +│ └── Expression types |
| 155 | +├── PipelineProcessor.cs |
| 156 | +└── Dialect implementations |
| 157 | +``` |
| 158 | + |
| 159 | +### Dialect Structure |
| 160 | +Each dialect (SQLite, SQL Server, PostgreSQL) provides: |
| 161 | +- SQL generation context |
| 162 | +- Function mappings |
| 163 | +- Type mappings |
| 164 | +- Dialect-specific features |
| 165 | + |
| 166 | +## Current Limitations |
| 167 | +- No window functions |
| 168 | +- Limited CTE support |
| 169 | +- No stored procedure generation |
| 170 | +- No trigger generation (planned) |
| 171 | +- No database-specific optimizations |
| 172 | +- Limited type inference |
| 173 | + |
| 174 | +## Usage Examples |
| 175 | + |
| 176 | +### Simple Query |
| 177 | +```lql |
| 178 | +users |> select(id, name, email) |
| 179 | +``` |
| 180 | + |
| 181 | +### Complex Query |
| 182 | +```lql |
| 183 | +let active_orders = Order |
| 184 | +|> filter(fn(row) => row.status = 'active') |
| 185 | +|> select(*) |
| 186 | +
|
| 187 | +Customer |
| 188 | +|> join(active_orders, on = Customer.Id = active_orders.CustomerId) |
| 189 | +|> group_by(Customer.Id, Customer.Name) |
| 190 | +|> having(fn(row) => COUNT(*) > 5) |
| 191 | +|> select(Customer.Name, COUNT(*) AS OrderCount) |
| 192 | +|> order_by(OrderCount DESC) |
| 193 | +``` |
| 194 | + |
| 195 | +## Transpilation Output |
| 196 | +LQL queries are transpiled to optimized, dialect-specific SQL maintaining semantic equivalence while leveraging database-specific features where appropriate. |
0 commit comments