DuckdbEx.Result (DuckdbEx v0.2.0)

View Source

DuckDB query result handling.

This module provides functionality for fetching and processing query results, mirroring the Python DuckDBPyResult class.

Reference: duckdb-python/src/duckdb_py/include/duckdb_python/pyresult.hpp

Overview

Results are returned as maps containing:

  • :rows - List of row tuples in column order
  • :row_count - Number of rows
  • :columns - List of column names (when available)

Examples

{:ok, result} = DuckdbEx.Connection.execute_result(conn, "SELECT 1 as num, 'hello' as text")
#=> {:ok, %{rows: [{1, "hello"}], row_count: 1, columns: ["num", "text"]}}

rows = DuckdbEx.Result.fetch_all(result)
#=> [{1, "hello"}]

Summary

Functions

Gets column names from the result.

Fetches all rows from a result.

Fetches multiple rows from a result.

Fetches one row from a result.

Returns the number of rows in the result.

Converts result to a list of tuples (for DB-API compatibility).

Types

row()

@type row() :: tuple() | map()

t()

@type t() :: %{
  rows: [row()],
  row_count: non_neg_integer(),
  columns: [String.t()] | nil
}

Functions

columns(arg1)

@spec columns(map()) :: [String.t()] | nil

Gets column names from the result.

Parameters

  • result - The result map from execute/3

Returns

  • List of column names, or nil if not available

Examples

{:ok, result} = DuckdbEx.Connection.execute(conn, "SELECT 1 as num, 'hello' as text")
columns = DuckdbEx.Result.columns(result)
#=> ["num", "text"] or nil

fetch_all(result)

@spec fetch_all(t()) :: [tuple()]

Fetches all rows from a result.

Parameters

  • result - The result map from execute/3

Returns

  • List of row tuples

Examples

{:ok, result} = DuckdbEx.Connection.execute_result(conn, "SELECT 1, 2, 3")
rows = DuckdbEx.Result.fetch_all(result)

Reference: DuckDBPyResult.fetchall() in Python

fetch_many(result, count)

@spec fetch_many(t(), non_neg_integer()) :: [tuple()]

Fetches multiple rows from a result.

Returns the first N rows from the result.

Parameters

  • result - The result map from execute/3
  • count - Number of rows to fetch

Returns

  • List of row tuples (up to count rows)

Examples

{:ok, result} = DuckdbEx.Connection.execute_result(conn, "SELECT * FROM range(10)")
rows = DuckdbEx.Result.fetch_many(result, 3)
#=> [{0}, {1}, {2}]

Reference: DuckDBPyResult.fetchmany() in Python

fetch_one(result)

@spec fetch_one(t()) :: tuple() | nil

Fetches one row from a result.

Returns the first row from the result, or nil if no rows.

Parameters

  • result - The result map from execute/3

Returns

  • Row tuple or nil

Examples

{:ok, result} = DuckdbEx.Connection.execute_result(conn, "SELECT 1 as num")
row = DuckdbEx.Result.fetch_one(result)
#=> {1}

Reference: DuckDBPyResult.fetchone() in Python

row_count(arg1)

@spec row_count(t()) :: non_neg_integer()

Returns the number of rows in the result.

Parameters

  • result - The result map from execute/3

Returns

  • Number of rows

Examples

{:ok, result} = DuckdbEx.Connection.execute(conn, "SELECT * FROM range(100)")
count = DuckdbEx.Result.row_count(result)
#=> 100

to_tuples(result)

@spec to_tuples(t()) :: [tuple()]

Converts result to a list of tuples (for DB-API compatibility).

Parameters

  • result - The result map from execute/3

Returns

  • List of tuples (row values in order)

Examples

{:ok, result} = DuckdbEx.Connection.execute_result(conn, "SELECT 1, 'hello'")
tuples = DuckdbEx.Result.to_tuples(result)
#=> [{1, "hello"}]