Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a prepared Statement to be called like a function #220

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ async_result = async_execute(conn, "SELECT typname FROM pg_type WHERE oid = \$1"
result = fetch(async_result)
data = columntable(result)

# the same but with prepared statements
stmt = prepare(conn, "SELECT typname FROM pg_type WHERE oid = \$1")
result = execute(stmt, ["16"])
data = columntable(result)
# or
result = stmt("16")
data = columntable(result)

close(conn)
```

Expand Down
14 changes: 14 additions & 0 deletions src/statements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Base.broadcastable(stmt::Statement) = Ref(stmt)
Create a prepared statement on the PostgreSQL server using libpq.
The statement is given an generated unique name using [`unique_id`](@ref).

Statements are executable either via `execute(stmt, args...; kwargs...)` or `stmt(args...; kwargs...)`

!!! note

Currently the statement is not explicitly deallocated, but it is deallocated at the end
Expand Down Expand Up @@ -159,3 +161,15 @@ function _execute_prepared(
zero(Cint), # return result in text format
)
end

function (stmt::Statement)(; kwargs...)
execute(stmt; kwargs...)
end

function (stmt::Statement)(parameters::Union{AbstractVector, Tuple}; kwargs...)
execute(stmt, parameters; kwargs...)
end

function (stmt::Statement)(parameters...; kwargs...)
execute(stmt, parameters; kwargs...)
end
45 changes: 30 additions & 15 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1487,15 +1487,21 @@ end
@test LibPQ.num_columns(stmt) == 2
@test LibPQ.num_params(stmt) == 0
@test LibPQ.column_names(stmt) == ["oid", "typname"]

function _test_stmt_exec_1(result)
@test LibPQ.num_columns(result) == 2
@test LibPQ.column_names(result) == ["oid", "typname"]
@test LibPQ.column_types(result) == [LibPQ.Oid, String]
@test LibPQ.num_rows(result) > 0

result = execute(stmt; throw_error=true)
close(result)
end

@test LibPQ.num_columns(result) == 2
@test LibPQ.column_names(result) == ["oid", "typname"]
@test LibPQ.column_types(result) == [LibPQ.Oid, String]
@test LibPQ.num_rows(result) > 0
result = execute(stmt; throw_error=true)
_test_stmt_exec_1(result)

close(result)
result = stmt(; throw_error=true)
_test_stmt_exec_1(result)

close(conn)
end
Expand All @@ -1508,19 +1514,28 @@ end
@test LibPQ.num_columns(stmt) == 2
@test LibPQ.num_params(stmt) == 1
@test LibPQ.column_names(stmt) == ["oid", "typname"]

function _test_stmt_exec_2(result)
@test LibPQ.num_columns(result) == 2
@test LibPQ.column_names(result) == ["oid", "typname"]
@test LibPQ.column_types(result) == [LibPQ.Oid, String]
@test LibPQ.num_rows(result) == 1

result = execute(stmt, [16]; throw_error=true)
data = columntable(result)
@test data[:oid][1] == 16
@test data[:typname][1] == "bool"

@test LibPQ.num_columns(result) == 2
@test LibPQ.column_names(result) == ["oid", "typname"]
@test LibPQ.column_types(result) == [LibPQ.Oid, String]
@test LibPQ.num_rows(result) == 1
close(result)
end

data = columntable(result)
@test data[:oid][1] == 16
@test data[:typname][1] == "bool"
result = execute(stmt, [16]; throw_error=true)
_test_stmt_exec_2(result)

close(result)
result = stmt([16]; throw_error=true)
_test_stmt_exec_2(result)

result = stmt(16; throw_error=true)
_test_stmt_exec_2(result)

close(conn)
end
Expand Down