qail 0.5.1-alpha

The Horizontal Query Language β€” Stop writing strings. Hook your data.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# πŸͺ QAIL β€” The Horizontal Query Language

> **Stop writing strings. Hook your data.**

[![Crates.io](https://img.shields.io/badge/crates.io-qail-orange)](https://crates.io/crates/qail)
[![License](https://img.shields.io/badge/license-MIT-blue)](LICENSE)
[![Rust](https://img.shields.io/badge/rust-1.75+-blueviolet)](https://www.rust-lang.org/)

---

## The Manifesto

SQL is **vertical**, verbose, and clunky inside modern codebases.

QAIL is **horizontal**, dense, and composable. It treats database queries like a pipeline, using symbols to **hook** data and pull it into your application.

```sql
-- The Old Way (SQL)
SELECT id, email, role FROM users WHERE active = true LIMIT 1;
```

```bash
# The QAIL Way
get::usersβ€’@id@email@role[active=true][lim=1]
```

One line. Zero ceremony. **Maximum velocity.**

### The Philosophy

1.  **Constraint**: Vertical space is precious. SQL blocks interrupt the flow of code reading. QAIL flows *with* your logic.
2.  **Density**: Symbols (`@`, `β€’`, `[]`) convey more information per pixel than keywords (`SELECT`, `FROM`, `WHERE`).
3.  **The Star Rule**: If you need 50 columns, fetch the struct (`@*`). If you need 3, list them. Listing 20 columns manually is an anti-pattern. QAIL encourages "all or nothing" density.

**Is it still Horizontal?**
Yes. The *language* itself is horizontal because it uses symbols instead of keywords. But we give you the **Vertical Escape Hatch** (tabs/newlines) so you can organize complex logic however you see fit, without fighting the parser. Horizontal is the *identity*; Vertical is the *layout*.

---

## πŸ“– Quick Reference

| Symbol | Name       | Function                | SQL Equivalent           |
|--------|------------|-------------------------|--------------------------|
| `::`   | The Gate   | Defines the action      | `SELECT`, `INSERT`, `UPDATE` |
| `!`    | The Unique | Distinct modifier       | `SELECT DISTINCT`        |
| `β€’`    | The Pivot  | Connects action to table| `FROM table`             |
| `@`    | The Hook   | Selects specific columns| `col1, col2`             |
| `[]`   | The Cage   | Constraints & Filters   | `WHERE`, `LIMIT`, `SET`  |
| `->`   | The Link   | Inner Join              | `INNER JOIN`             |
| `<-`   | The Left   | Left Join               | `LEFT JOIN`              |
| `->>`  | The Right  | Right Join              | `RIGHT JOIN`             |
| `~`    | The Fuse   | Fuzzy / Partial Match   | `ILIKE '%val%'`          |
| `\|`   | The Split  | Logical OR              | `OR`                     |
| `&`    | The Bind   | Logical AND             | `AND`                    |
| `^!`   | The Peak   | Sort Descending         | `ORDER BY ... DESC`      |
| `^`    | The Rise   | Sort Ascending          | `ORDER BY ... ASC`       |
| `*`    | The Star   | All / Wildcard          | `*`                      |
| `[*]`  | The Deep   | Array Unnest            | `UNNEST(arr)`            |
| `$`    | The Var    | Parameter Injection     | `$1`, `$2`               |
| `lim=` | The Limit  | Row limit               | `LIMIT n`                |
| `off=` | The Skip   | Offset for pagination   | `OFFSET n`               |

---

## πŸš€ Installation

### CLI (Recommended)

```bash
cargo install qail
```

### As a Library

```toml
# Cargo.toml
[dependencies]
qail = "0.5.0-alpha"
```

---

## πŸ’‘ Usage

### CLI β€” The `qail` Command

```bash
# Fetch all users
qail 'get::usersβ€’@*'

# Get specific columns with filter
qail 'get::ordersβ€’@id@total@status[user_id=$1][lim=10]' --bind 42

# Update a record
qail 'set::usersβ€’[verified=true][id=$1]' --bind 7

# Delete with condition
qail 'del::sessionsβ€’[expired_at<now]'

# Transpile only (don't execute)
qail 'get::usersβ€’@*[active=true]' --dry-run
```

### As a Library

```rust
use qail::prelude::*;

#[tokio::main]
async fn main() -> Result<(), QailError> {
    let db = QailDB::connect("postgres://localhost/mydb").await?;

    // Parse and execute
    let users: Vec<User> = db
        .query("get::usersβ€’@id@email@role[active=true][lim=10]")
        .fetch_all()
        .await?;

    // Or use the builder for type-safe composition
    let query = qail::get("users")
        .hook(&["id", "email", "role"])
        .cage("active", true)
        .limit(10);

    let users: Vec<User> = db.run(query).fetch_all().await?;

    Ok(())
}
```

---

## πŸ“š Syntax Deep Dive

### A. Simple Fetch (`get::`)

```sql
-- SQL
SELECT id, email, role FROM users WHERE active = true LIMIT 1;
```

```bash
# QAIL
get::usersβ€’@id@email@role[active=true][lim=1]
```

---

### B. Mutation (`set::`)

```sql
-- SQL
UPDATE user_verifications SET consumed_at = now() WHERE id = $1;
```

```bash
# QAIL
set::user_verificationsβ€’[consumed_at=now][id=$1]
```

> **Note:** In `set::` mode, the **first `[]`** is the payload (SET), the **second `[]`** is the filter (WHERE).

---

### C. Deletion (`del::`)

```sql
-- SQL
DELETE FROM sessions WHERE expired_at < now();
```

```bash
# QAIL
del::sessionsβ€’[expired_at<now]
```

---

### D. Complex Search with Fuzzy Match

```sql
-- SQL
SELECT * FROM ai_knowledge_base 
WHERE active = true 
AND (topic ILIKE $1 OR question ILIKE $1 OR EXISTS (SELECT 1 FROM unnest(keywords) k WHERE k ILIKE $1))
ORDER BY created_at DESC
LIMIT 5;
```

```bash
# QAIL
get::ai_knowledge_baseβ€’@*[active=true][topic~$1|question~$1|keywords[*]~$1][^!created_at][lim=5]

# Or multi-line for readability:
get::ai_knowledge_baseβ€’@*
  [active=true]
  [topic~$1 | question~$1 | keywords[*]~$1]
  [^!created_at]
  [lim=5]
```

---

### E. Joins

```bash
# Inner join (default)
get::users->ordersβ€’@name@total
# β†’ SELECT name, total FROM users INNER JOIN orders ON orders.user_id = users.id

# Left join (include users without orders)
get::users<-ordersβ€’@name@total
# β†’ SELECT name, total FROM users LEFT JOIN orders ON orders.user_id = users.id

# Right join
get::orders->>customersβ€’@*
# β†’ SELECT * FROM orders RIGHT JOIN customers ON customers.order_id = orders.id
```

---

### F. DISTINCT Queries (v0.5+)

```bash
# Get unique roles
get!::usersβ€’@role
# β†’ SELECT DISTINCT role FROM users

# Distinct with filter
get!::ordersβ€’@status[created_at>'2024-01-01']
# β†’ SELECT DISTINCT status FROM orders WHERE created_at > '2024-01-01'
```

---

### G. Pagination (OFFSET)

```bash
# Page 3 (20 items per page)
get::productsβ€’@*[lim=20][off=40]
# β†’ SELECT * FROM products LIMIT 20 OFFSET 40
```

---

## βš™οΈ Configuration

Create a `.qailrc` or `qail.toml` in your project root:

```toml
[connection]
driver = "postgres"           # postgres | mysql | sqlite
url = "postgres://localhost/mydb"

[output]
format = "table"              # table | json | csv
color = true

[safety]
confirm_mutations = true      # Prompt before UPDATE/DELETE
dry_run_default = false
```

---

## πŸ—οΈ Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      QAIL Pipeline                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                             β”‚
β”‚   "get::usersβ€’@*[active=true]"                              β”‚
β”‚              β”‚                                              β”‚
β”‚              β–Ό                                              β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                   β”‚
β”‚   β”‚   Parser (nom)      β”‚  β†’ Tokenize symbols               β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                   β”‚
β”‚             β”‚                                               β”‚
β”‚             β–Ό                                               β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                   β”‚
β”‚   β”‚   AST (QailCmd)     β”‚  β†’ Structured representation      β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                   β”‚
β”‚             β”‚                                               β”‚
β”‚             β–Ό                                               β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                   β”‚
β”‚   β”‚ Transpiler (SQL)    β”‚  β†’ Generate valid SQL             β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                   β”‚
β”‚             β”‚                                               β”‚
β”‚             β–Ό                                               β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                   β”‚
β”‚   β”‚ Engine (sqlx)       β”‚  β†’ Execute against DB             β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                   β”‚
β”‚                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Core Structs

```rust
pub struct QailCmd {
    pub action: Action,         // GET, SET, DEL, ADD
    pub table: String,
    pub columns: Vec<Column>,
    pub cages: Vec<Cage>,       // Filters, limits, sorts
    pub bindings: Vec<Value>,
}

pub enum Action {
    Get,    // SELECT
    Set,    // UPDATE
    Del,    // DELETE
    Add,    // INSERT
}

pub struct Cage {
    pub kind: CageKind,         // Filter, Limit, Sort, Payload
    pub conditions: Vec<Condition>,
}

pub struct Condition {
    pub column: String,
    pub op: Operator,           // Eq, Ne, Gt, Lt, Fuzzy, In
    pub value: Value,
}
```

---

## πŸ—ΊοΈ Roadmap

### Phase 1: Parser βœ…
- [x] Lexer for QAIL symbols
- [x] `nom` parser combinators
- [x] AST generation

### Phase 2: Transpiler βœ…
- [x] PostgreSQL codegen
- [x] MySQL codegen
- [x] SQLite codegen
- [x] JOINs (INNER, LEFT, RIGHT)
- [x] DISTINCT, OFFSET, RETURNING

### Phase 3: Engine βœ…
- [x] Async execution (sqlx)
- [x] Connection pooling
- [x] Multi-driver support (Postgres/MySQL/SQLite)
- [ ] Transaction support
- [ ] Prepared statement caching

### Phase 4: Ecosystem βœ…
- [x] VS Code extension (syntax highlighting)
- [x] `qail!` compile-time macro
- [x] Struct generation (`gen::`)
- [ ] Language server (LSP)
- [ ] REPL mode

### E. The Flagship Comparison (Complex Joins)

**Scenario**: Find verified users who joined after 2024 and booked under the 'SUMMER' campaign.

```sql
-- SQL (7 lines, cognitive load high)
SELECT u.* 
FROM users u
JOIN bookings b ON b.user_id = u.id
WHERE u.created_at >= '2024-01-01'
  AND u.email_verified = true
  AND b.campaign_code ILIKE '%SUMMER%'
ORDER BY u.created_at DESC
LIMIT 50;
```

```bash
# QAIL (1 line, cognitive load low)
get::users->bookingsβ€’@*[created_at>='2024-01-01'][email_verified=true][bookings.campaign_code~'SUMMER'][^!created_at][lim=50]
```

---

## 🀝 Contributing

We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.

```bash
# Clone the repo
git clone https://github.com/your-username/qail.git
cd qail

# Run tests
cargo test

# Run with example
cargo run -- "get::usersβ€’@*[lim=5]" --dry-run
```

---

## πŸ“„ License

MIT Β© 2025 QAIL Contributors

---

<p align="center">
  <strong>Built with πŸ¦€ Rust and β˜• caffeine</strong><br>
  <a href="https://qail.rs">qail.rs</a>
</p>