From b86213838651d1b45802a8a6975a69d95e61d94a Mon Sep 17 00:00:00 2001 From: quildtide <42811940+quildtide@users.noreply.github.com> Date: Tue, 11 May 2021 18:33:14 -0400 Subject: [PATCH 1/3] Allow a Statement to be called like a function --- docs/src/index.md | 8 ++++++++ src/statements.jl | 6 ++++++ test/runtests.jl | 42 +++++++++++++++++++++++++++--------------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 901c2e3d..13ce8f0c 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..8dca4f34 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,7 @@ function _execute_prepared( zero(Cint), # return result in text format ) end + +function (stmt::Statement)(args...; kwargs...) + execute(stmt, args...; kwargs...) +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 6e3c8812..a34ace37 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,25 @@ 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) close(conn) end From 2758e68e1205c9e35e440038463ce5d1acbfc2d0 Mon Sep 17 00:00:00 2001 From: quildtide <42811940+quildtide@users.noreply.github.com> Date: Mon, 24 May 2021 23:42:14 -0400 Subject: [PATCH 2/3] Improve Statement functor API --- docs/src/index.md | 2 +- src/statements.jl | 12 ++++++++++-- test/runtests.jl | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 13ce8f0c..48f38cdd 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -31,7 +31,7 @@ stmt = prepare(conn, "SELECT typname FROM pg_type WHERE oid = \$1") result = execute(stmt, ["16"]) data = columntable(result) # or -result = stmt(["16"]) +result = stmt("16") data = columntable(result) close(conn) diff --git a/src/statements.jl b/src/statements.jl index 8dca4f34..80cbfc76 100644 --- a/src/statements.jl +++ b/src/statements.jl @@ -162,6 +162,14 @@ function _execute_prepared( ) end -function (stmt::Statement)(args...; kwargs...) - execute(stmt, args...; kwargs...) +function (stmt::LibPQ.Statement)(; kwargs...) + LibPQ.execute(stmt; kwargs...) +end + +function (stmt::LibPQ.Statement)(parameters::Union{AbstractVector, Tuple}; kwargs...) + LibPQ.execute(stmt, parameters; kwargs...) +end + +function (stmt::LibPQ.Statement)(parameters...; kwargs...) + LibPQ.execute(stmt, parameters; kwargs...) end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index a34ace37..59bd2352 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1534,6 +1534,9 @@ end 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 end From e0666cd2aa3ee46d560c9cfe65286390a51cfff2 Mon Sep 17 00:00:00 2001 From: quildtide <42811940+quildtide@users.noreply.github.com> Date: Tue, 25 May 2021 00:04:27 -0400 Subject: [PATCH 3/3] Clean up unnecessary LibPQ scoping --- src/statements.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/statements.jl b/src/statements.jl index 80cbfc76..d3db8da0 100644 --- a/src/statements.jl +++ b/src/statements.jl @@ -162,14 +162,14 @@ function _execute_prepared( ) end -function (stmt::LibPQ.Statement)(; kwargs...) - LibPQ.execute(stmt; kwargs...) +function (stmt::Statement)(; kwargs...) + execute(stmt; kwargs...) end -function (stmt::LibPQ.Statement)(parameters::Union{AbstractVector, Tuple}; kwargs...) - LibPQ.execute(stmt, parameters; kwargs...) +function (stmt::Statement)(parameters::Union{AbstractVector, Tuple}; kwargs...) + execute(stmt, parameters; kwargs...) end -function (stmt::LibPQ.Statement)(parameters...; kwargs...) - LibPQ.execute(stmt, parameters; kwargs...) +function (stmt::Statement)(parameters...; kwargs...) + execute(stmt, parameters; kwargs...) end \ No newline at end of file