Skip to content

Commit

Permalink
📝 Improve DuckDB documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdesousa committed Oct 22, 2024
1 parent 4e30621 commit 280146a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
58 changes: 58 additions & 0 deletions lib/ayesql/runner/duckdb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,64 @@ if Code.ensure_loaded?(Duckdbex) do
iex> MyQueries.get_user([id: id], conn: connection)
{:ok, ...}
```
## Example
Given the following AyeSQL file:
```sql
-- name: get_player_by_last_name
-- docs: Get players by their last name
SELECT *
FROM atp
WHERE name_last = :last_name
```
And the following AyeSQL module:
```elixir
defmodule ATP do
use AyeSQL,
runner: AyeSQL.Runner.Duckdbex
defqueries "./atp.sql"
end
```
Then we can query a CSV file as follows:
```elixir
# Open a DuckDB connection
{:ok, db} = Duckdbex.open()
{:ok, conn} = Duckdbex.connection(db)
# Fetch a remote CSV and copy its contents to the table `atp`
url = "https://raw.githubusercontent.com/duckdb-in-action/examples/refs/heads/main/ch05/atp/atp_players.csv"
{:ok, _res} = Duckdbex.query(conn, "INSTALL httpfs")
{:ok, _res} = Duckdbex.query(conn, "LOAD httpfs")
{:ok, _res} = Duckdbex.query(conn, "CREATE OR REPLACE TABLE atp AS FROM '\#{url}'")
# Run our query to find a player by last name
ATP.get_player_by_last_name([last_name: "Federer"], conn: conn)
```
This should get the following result:
```elixir
{:ok,
[
%{
player_id: 103819,
name_first: "Roger",
name_last: "Federer",
hand: "R",
dob: "19810808",
ioc: "SUI",
height: 185,
wikidata_id: "Q1426"
}
]}
```
"""
use AyeSQL.Runner

Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ defmodule AyeSQL.MixProject do
"AyeSQL runners": [
AyeSQL.Runner,
AyeSQL.Runner.Ecto,
AyeSQL.Runner.Postgrex
AyeSQL.Runner.Postgrex,
AyeSQL.Runner.Duckdbex
]
]
end
Expand Down

0 comments on commit 280146a

Please sign in to comment.