diff --git a/docs/src/index.md b/docs/src/index.md index 901c2e3d..48f38cdd 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -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) ``` diff --git a/src/statements.jl b/src/statements.jl index 832b8cd2..d3db8da0 100644 --- a/src/statements.jl +++ b/src/statements.jl @@ -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 @@ -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 \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 6e3c8812..59bd2352 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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