jankensqlhub 1.4.0

A high-performance, modular Rust library for parameterizable SQL query management with support for SQLite and PostgreSQL
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# Janken SQL Hub - Database Query Management Library

A Rust library for parameterizable SQL query management that prevents SQL injection through prepared statements and supports multiple database backends (SQLite and PostgreSQL).

## Table of Contents

- [Overview]#-overview
- [Claude Code Skill]#-claude-code-skill
- [Quick Start]#-quick-start
- [Parameter Syntax Reference]#-parameter-syntax-reference
- [Usage Guide]#-usage-guide
- [Advanced Features]#-advanced-features
- [Error Handling]#-error-handling
- [PostgreSQL Support]#-postgresql-support
- [Installation]#-installation
- [Architecture]#architecture
- [Acknowledgments]#-acknowledgments

---

## 🎯 Overview

**Janken SQL Hub** enables developers to define SQL queries with parameters in a database-agnostic way, automatically generating prepared statements for different database backends while preventing SQL injection attacks.

### Why JSON-Configured Queries?

Common CRUD operations often become scattered across codebases, mixed with business logic, making them hard to audit and maintain. **Janken SQL Hub** solves this by:

- **Centralizing query definitions** - All SQL in portable JSON files, not buried in code
- **Co-locating SQL with constraints** - Query logic and validation rules live together
- **Enabling easy auditing** - Review all database operations in one place
- **Simplifying maintenance** - Update queries without touching application code

```json
{
  "update_user_status": {
    "query": "UPDATE users SET status=@status WHERE id=@user_id",
    "args": {
      "user_id": {"type": "integer"},
      "status": {"enum": ["active", "inactive", "suspended"]}
    }
  }
}
```
*SQL and its constraints are cohesive, clear, and reviewable.*

### Non-Invasive Design

**Janken SQL Hub** is a focused utility, not a framework:

- **No coding restrictions** - Use it for what it's good at, use something else for the rest
- **Coexists with existing code** - Works alongside raw SQL, ORMs, or any other database access pattern
- **Simple utility functions** - `query_run_sqlite()` and `query_run_postgresql()` wrap your existing connections
- **Gradual adoption** - Start with a few queries, expand as needed

```rust
// JankenSQLHub handles configured queries
let result = query_run_sqlite(&mut conn, &queries, "get_user", &params)?;

// Your existing code continues to work unchanged
conn.execute("DROP TABLE temp_data", [])?;
```

### Core Capabilities

| Capability | Description |
|------------|-------------|
| **Parameterizable SQL** | `@param_name` syntax with automatic prepared statement generation |
| **Dynamic Identifiers** | `#[identifier]` syntax for safe table/column names |
| **List Parameters** | `:[list_param]` syntax for IN clauses |
| **Comma Lists** | `~[param]` syntax for comma-separated field lists |
| **Type Safety** | Parameter validation with constraints (range, pattern, enum) |
| **Multi-Backend** | SQLite and PostgreSQL support with identical API |

---

## 🤖 Claude Code Skill

This repository includes a [Claude Code skill](https://code.claude.com/docs/en/skills) at `.claude/skills/using-jankensqlhub/SKILL.md` that provides AI-assisted guidance when working with JankenSQLHub. When using [Claude Code](https://code.claude.com), the skill is automatically discovered and gives Claude knowledge of:

- Parameter syntax and query definition structure
- Type system and constraint configuration
- SQLite and PostgreSQL execution patterns
- Structured error handling with `JankenError`

This enables Claude to generate correct JankenSQLHub code, debug parameter validation issues, and follow library conventions without needing to re-read the documentation each time.

---

## 🚀 Quick Start

### 1. Define a Query (JSON)

```json
{
  "get_user": {
    "query": "SELECT id, name, email FROM users WHERE id=@user_id",
    "returns": ["id", "name", "email"],
    "args": {
      "user_id": {"type": "integer"}
    }
  }
}
```

### 2. Execute the Query (Rust)

```rust
use janken_sql_hub::{QueryDefinitions, query_run_sqlite};
use rusqlite::Connection;

// Load queries and connect to database
let queries = QueryDefinitions::from_file("queries.json")?;
let mut conn = Connection::open("mydb.sqlite")?;

// Execute with JSON parameters
let params = serde_json::json!({"user_id": 42});
let result = query_run_sqlite(&mut conn, &queries, "get_user", &params)?;
// result.data contains the JSON response
```

That's it! The library handles prepared statements and SQL injection prevention automatically.

---

## 📖 Parameter Syntax Reference

| Syntax | Type | Description | Example |
|--------|------|-------------|---------|
| `@param` | string (default) | Basic parameter placeholder | `WHERE name=@user_name` |
| `@param` | any type | Override type in args | `"user_id": {"type": "integer"}` |
| `#[param]` | table_name | Dynamic identifier (validated) | `SELECT * FROM #[table_name]` |
| `:[param]` | list | Array for IN clauses | `WHERE id IN :[user_ids]` |
| `~[param]` | comma_list | Comma-separated values | `SELECT ~[fields] FROM users` |

### Quick Examples

```sql
-- Basic parameters (default to string, can override type)
SELECT * FROM users WHERE id=@user_id AND name=@user_name

-- Dynamic table/column names (always validated against enum)
SELECT * FROM #[table_name] WHERE id=@user_id

-- List parameters for IN clauses
SELECT * FROM users WHERE id IN :[user_ids]

-- Comma list for dynamic field selection
SELECT ~[fields] FROM users WHERE status='active'
-- With {"fields": ["name", "email"]} becomes: SELECT name,email FROM users
```

---

## 📚 Usage Guide

### Query Definition Structure

Each query definition supports these fields:

| Field | Required | Description |
|-------|----------|-------------|
| `query` || SQL statement with parameter placeholders |
| `returns` | Optional | Column names for SELECT queries (JSON response structure) |
| `args` | Optional | Parameter type overrides and constraints |

### Basic Examples

**SELECT with parameters:**
```json
{
  "search_users": {
    "query": "SELECT id, name FROM users WHERE age > @min_age",
    "returns": ["id", "name"],
    "args": {
      "min_age": {"type": "integer"}
    }
  }
}
```

**INSERT:**
```json
{
  "create_user": {
    "query": "INSERT INTO users (name, email) VALUES (@name, @email)"
  }
}
```
*Note: `@name` and `@email` default to string type, so args can be omitted.*

**Dynamic table:**
```json
{
  "query_from_table": {
    "query": "SELECT * FROM #[source] WHERE id=@id",
    "returns": ["id", "name"],
    "args": {
      "source": {"enum": ["users", "accounts"]},
      "id": {"type": "integer"}
    }
  }
}
```

**List parameter (IN clause):**
```json
{
  "get_users_by_ids": {
    "query": "SELECT id, name FROM users WHERE id IN :[user_ids]",
    "returns": ["id", "name"],
    "args": {
      "user_ids": {"itemtype": "integer"}
    }
  }
}
```

### Executing Queries

```rust
use janken_sql_hub::{QueryDefinitions, query_run_sqlite};
use rusqlite::Connection;

let queries = QueryDefinitions::from_file("queries.json")?;
let mut conn = Connection::open_in_memory()?;

// Basic parameter
let params = serde_json::json!({"user_id": 42});
let result = query_run_sqlite(&mut conn, &queries, "get_user", &params)?;

// Dynamic table
let params = serde_json::json!({"source": "accounts", "id": 1});
let result = query_run_sqlite(&mut conn, &queries, "query_from_table", &params)?;

// List parameter
let params = serde_json::json!({"user_ids": [1, 2, 3, 4, 5]});
let result = query_run_sqlite(&mut conn, &queries, "get_users_by_ids", &params)?;
```

### Important: Null Values Not Supported

**JSON null values are rejected.** All parameter values must be non-null (strings, numbers, booleans, arrays, objects).

*Rationale: null acts as a super-passport that circumvents type validation, leading to weaker type safety and potential security issues.*

---

## ⚙️ Advanced Features

### Parameter Types and Constraints

**Supported Types:**

| Type | Description | Constraint Options |
|------|-------------|-------------------|
| `string` | Text (default for `@param`) | `pattern`, `enum`, `range` (char count) |
| `integer` | Whole numbers | `range`, `enum` |
| `float` | Decimal numbers | `range`, `enum` |
| `boolean` | true/false | `enum` |
| `blob` | Binary data | `range` (size in bytes) |
| `table_name` | Auto-assigned to `#[param]` | `enum` (required), `range` (char count) |
| `list` | Auto-assigned to `:[param]` | `itemtype`, `range` (array size) |
| `comma_list` | Auto-assigned to `~[param]` | `enum`, `range` (array size) |

**Constraint Examples:**

```json
{
  "args": {
    "age": {"type": "integer", "range": [0, 150]},
    "email": {"pattern": "\\S+@\\S+\\.\\S+"},
    "status": {"enum": ["active", "inactive", "pending"]},
    "data": {"type": "blob", "range": [1, 1048576]},
    "user_ids": {"itemtype": "integer", "range": [1, 100]},
    "table": {"enum": ["users", "accounts"]},
    "fields": {"enum": ["name", "email", "age"], "range": [1, 3]},
    "username": {"type": "string", "range": [3, 50]}
  }
}
```

**Range Constraint Semantics:**

| Type | Range Meaning |
|------|---------------|
| `integer`, `float` | Value must be within [min, max] |
| `string`, `table_name` | Character count must be within [min, max] |
| `blob` | Size in bytes must be within [min, max] |
| `list`, `comma_list` | Array size (element count) must be within [min, max] |
| `boolean` | Range not supported |

### Dynamic Returns

Map return columns dynamically using the same comma_list parameter:

```json
{
  "dynamic_select": {
    "query": "SELECT ~[fields] FROM users",
    "returns": "~[fields]",
    "args": {
      "fields": {"enum": ["name", "email", "age"]}
    }
  }
}
```

### Conditional Enum Constraints (`enumif`)

Validate parameter values based on other parameters:

```json
{
  "args": {
    "media_source": {
      "enumif": {
        "media_type": {
          "song": ["artist", "album"],
          "show": ["channel", "episodes"]
        }
      }
    }
  }
}
```

With `media_type: "song"`, `media_source` must be "artist" or "album".

**Fuzzy Matching Patterns:**

| Pattern | Description | Example |
|---------|-------------|---------|
| `"value"` | Exact match | `"admin"` matches only "admin" |
| `"start:prefix"` | Starts with | `"start:admin"` matches "admin_user" |
| `"end:suffix"` | Ends with | `"end:txt"` matches "readme.txt" |
| `"contain:str"` | Contains | `"contain:error"` matches "system_error" |

```json
{
  "permission": {
    "enumif": {
      "role": {
        "start:admin": ["read_all", "write_all", "delete_all"],
        "start:user": ["read_own", "write_own"],
        "contain:guest": ["read_public"]
      }
    }
  }
}
```

*Note: When multiple patterns could match, the first alphabetically is used.*

---

## 🛡️ Error Handling

JankenSQLHub provides structured errors with unique codes and JSON metadata.

### Basic Usage

```rust
use jankensqlhub::{JankenError, get_error_data, get_error_info};

if let Some(janken_err) = error.downcast_ref::<JankenError>() {
    let data = get_error_data(janken_err);
    
    if let Some(info) = get_error_info(data.code) {
        eprintln!("{} ({}) - {}", info.name, data.code, info.description);
    }
}
```

### Error Code Reference

| Code | Error Type | Description |
|------|------------|-------------|
| 2000 | QUERY_NOT_FOUND | Query definition not found |
| 2010 | PARAMETER_NOT_PROVIDED | Required parameter missing |
| 2020 | PARAMETER_TYPE_MISMATCH | Value doesn't match expected type |
| 2030 | PARAMETER_NAME_CONFLICT | Parameter name conflicts with table name |

### Extracting Metadata

```rust
use jankensqlhub::{error_meta, M_EXPECTED, M_GOT, M_PARAM_NAME, M_QUERY_NAME};

match janken_err {
    JankenError::ParameterTypeMismatch { .. } => {
        let expected = error_meta(data, M_EXPECTED)?;
        let got = error_meta(data, M_GOT)?;
        eprintln!("Type mismatch: expected {}, got {}", expected, got);
    }
    JankenError::ParameterNotProvided { .. } => {
        let param_name = error_meta(data, M_PARAM_NAME)?;
        eprintln!("Missing parameter: {}", param_name);
    }
    _ => {}
}
```

---

## 🐘 PostgreSQL Support

PostgreSQL support shares the same API with async execution:

```rust
use jankensqlhub::{QueryDefinitions, query_run_postgresql};
use tokio_postgres::NoTls;

// Setup connection
let (client, connection) = tokio_postgres::connect(&connection_string, NoTls).await?;
tokio::spawn(async move { 
    if let Err(e) = connection.await { 
        eprintln!("connection error: {}", e); 
    } 
});

// Execute queries (same API as SQLite)
let params = serde_json::json!({"user_id": 42});
let result = query_run_postgresql(&mut client, &queries, "get_user", &params).await?;
```

### PostgreSQL Features

- **Async Execution**: Leverages tokio-postgres for high-performance operations
- **ACID Transactions**: Automatic transaction wrapping with rollback on failure
- **Prepared Statements**: Auto-conversion to `$1, $2, ...` format
- **JSON/JSONB Support**: Direct querying with automatic serde_json conversion

See the [operational guide](op.md) for testing setup.

---

## 📦 Installation

```bash
cargo add jankensqlhub
```

### Feature Flags

| Flag | Description |
|------|-------------|
| `all` (default) | Both SQLite and PostgreSQL |
| `sqlite` | SQLite only |
| `postgresql` | PostgreSQL only |

```bash
# SQLite only
cargo add jankensqlhub --features sqlite

# PostgreSQL only
cargo add jankensqlhub --features postgresql
```

### Links

- [📦 Crates.io]https://crates.io/crates/jankensqlhub
- [📚 Documentation]https://docs.rs/jankensqlhub
- [🏠 Repository]https://github.com/pandazy/jankensqlhub

---

## Architecture

**Janken SQL Hub** serves as a **server-side query adapter**, bridging web API endpoints and database operations:

```
Client JSON → QueryDef (predefined) → Prepared Statement → Database → JSON Response
```

- **No ORM**: Direct SQL usage avoids query builder overhead
- **Security First**: Query templates prevent SQL injection
- **Type Safety**: Compile-time parameter validation

---

## 🙏 Acknowledgments

This project was developed with significant assistance from [Cline](https://cline.bot/) - an autonomous AI coding agent for VS Code that handles complex software engineering tasks.

---

**Built with ❤️ in Rust for type-safe, performant database query management.**