Skip to content

Commit 6f84c85

Browse files
committed
update readme
1 parent d0d1919 commit 6f84c85

File tree

2 files changed

+62
-37
lines changed

2 files changed

+62
-37
lines changed

README.md

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# SqlKit
22

3-
[Hex](https://hex.pm/packages/sql_kit) | [Documentation](https://hexdocs.pm/sql_kit)
3+
[Hex](https://hex.pm/packages/sql_kit) | [GitHub](https://github.com/tylerbarker/sql_kit) | [Documentation](https://hexdocs.pm/sql_kit)
44

5-
A SQL toolkit for Elixir with automatic result transformation to maps and structs.
5+
Execute raw SQL using files or strings, automatically get maps and structs back. Built on top of ecto_sql.
66

7-
SqlKit provides two ways to execute raw SQL with results automatically transformed into maps or structs:
7+
SqlKit provides two ways to execute raw SQL with automatic result transformation:
88

99
1. **Direct SQL execution** - Execute SQL strings directly with any Ecto repo
1010
2. **File-based SQL** - Keep SQL in dedicated files with compile-time embedding
@@ -19,6 +19,7 @@ For file-based SQL, keeping queries in dedicated `.sql` files brings practical b
1919

2020
## Features
2121

22+
- **Just SQL**: No DSL or special syntax to learn.
2223
- **Automatic result transformation**: Query results returned as maps or structs, not raw columns/rows
2324
- **Two APIs**: Execute SQL strings directly or load from files
2425
- **Compile-time embedding**: File-based SQL read once at compile time and stored as module attributes
@@ -82,7 +83,7 @@ For larger queries or better organization, keep SQL in dedicated files:
8283

8384
#### 1. Create SQL files
8485

85-
SQL files are housed in subdirectories under the root SQL directory. This is `priv/repo/sql` by default but is configurable via `:root_sql_dir` config option.
86+
SQL files are housed in subdirectories under the root SQL directory. This is `priv/repo/sql` by default but is configurable via `:root_sql_dir` config option. The `priv/` directory is recommended because these files are included in Mix releases by default.
8687

8788
```sql
8889
-- priv/repo/sql/reports/stats.sql
@@ -143,6 +144,61 @@ config :my_app, SqlKit,
143144
load_sql: :compiled # use compile-time embedded SQL
144145
```
145146

147+
## Parameter Syntax by Database
148+
149+
Each database uses different parameter placeholder syntax:
150+
151+
| Database | Syntax | Example |
152+
|------------|-------------------|--------------------------------------------|
153+
| PostgreSQL | `$1`, `$2`, ... | `WHERE id = $1 AND age > $2` |
154+
| MySQL | `?` | `WHERE id = ? AND age > ?` |
155+
| SQLite | `?` | `WHERE id = ? AND age > ?` |
156+
| SQL Server | `@1`, `@2`, ... | `WHERE id = @1 AND age > @2` |
157+
| ClickHouse | `{name:Type}` | `WHERE id = {id:UInt32} AND age > {age:UInt32}` |
158+
159+
### ClickHouse Named Parameters
160+
161+
ClickHouse uses named parameters with explicit types. Pass parameters as a map:
162+
163+
```elixir
164+
# SQL file: user_by_id.sql
165+
# SELECT * FROM users WHERE id = {id:UInt32}
166+
167+
ClickHouseSQL.query_one!("user_by_id.sql", %{id: 1})
168+
```
169+
170+
### Named Parameters for Other Databases
171+
172+
For databases using positional parameters, wrap SqlKit calls in functions to get named parameter ergonomics:
173+
174+
```elixir
175+
defmodule MyApp.Users do
176+
def get_active_users(company_id, min_age) do
177+
SqlKit.query_all!(MyApp.Repo, """
178+
SELECT id, name, email, age
179+
FROM users
180+
WHERE company_id = $1
181+
AND age >= $2
182+
AND active = true
183+
ORDER BY name
184+
""", [company_id, min_age], as: User)
185+
end
186+
end
187+
188+
# Usage
189+
MyApp.Users.get_active_users(123, 21)
190+
# => [%User{id: 1, name: "Alice", email: "alice@example.com", age: 30}, ...]
191+
```
192+
193+
This pattern gives you named parameters through Elixir function arguments while keeping queries as plain SQL.
194+
195+
## Use SqlKit Options
196+
197+
- `:otp_app` (required) - Your application name
198+
- `:repo` (required) - The Ecto repo module to use for queries
199+
- `:dirname` (required) - Subdirectory within root_sql_dir for this module's SQL files
200+
- `:files` (required) - List of SQL filenames to load
201+
146202
## API Reference
147203

148204
### Standalone Functions
@@ -318,36 +374,6 @@ SQL.load("users.sql")
318374
- `:unsafe_atoms` - If `true`, uses `String.to_atom/1` instead of `String.to_existing_atom/1` for column names. Default: `false`
319375
- `:query_name` - Custom identifier for exceptions (standalone API only; defaults to truncated SQL)
320376

321-
## Parameter Syntax by Database
322-
323-
Each database uses different parameter placeholder syntax:
324-
325-
| Database | Syntax | Example |
326-
|------------|-------------------|--------------------------------------------|
327-
| PostgreSQL | `$1`, `$2`, ... | `WHERE id = $1 AND age > $2` |
328-
| MySQL | `?` | `WHERE id = ? AND age > ?` |
329-
| SQLite | `?` | `WHERE id = ? AND age > ?` |
330-
| SQL Server | `@1`, `@2`, ... | `WHERE id = @1 AND age > @2` |
331-
| ClickHouse | `{name:Type}` | `WHERE id = {id:UInt32} AND age > {age:UInt32}` |
332-
333-
### ClickHouse Named Parameters
334-
335-
ClickHouse uses named parameters with explicit types. Pass parameters as a map:
336-
337-
```elixir
338-
# SQL file: user_by_id.sql
339-
# SELECT * FROM users WHERE id = {id:UInt32}
340-
341-
ClickHouseSQL.query_one!("user_by_id.sql", %{id: 1})
342-
```
343-
344-
## Use SqlKit Options
345-
346-
- `:otp_app` (required) - Your application name
347-
- `:repo` (required) - The Ecto repo module to use for queries
348-
- `:dirname` (required) - Subdirectory within root_sql_dir for this module's SQL files
349-
- `:files` (required) - List of SQL filenames to load
350-
351377
## Contributing
352378

353379
### Prerequisites

lib/sql_kit.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# credo:disable-for-this-file Credo.Check.Refactor.LongQuoteBlocks
22
defmodule SqlKit do
33
@moduledoc """
4-
A SQL toolkit for Elixir with automatic result transformation.
4+
Execute raw SQL using files or strings, automatically get maps and structs back. Built on top of ecto_sql.
55
6-
SqlKit provides two ways to execute raw SQL with results automatically
7-
transformed into maps or structs:
6+
SqlKit provides two ways to execute raw SQL with automatic result transformation:
87
98
## Direct SQL Execution
109

0 commit comments

Comments
 (0)