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

DBinterface.jl Support #271

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.vscode
*.jl.cov
*.jl.*.cov
*.jl.mem
Expand Down
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "1.15.1"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Decimals = "abce61dc-4473-55a0-ba07-351d65e31d42"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand All @@ -25,6 +26,7 @@ UTCDateTimes = "0f7cfa37-7abf-4834-b969-a8aa512401c2"
[compat]
CEnum = "0.2, 0.3, 0.4"
DataFrames = "0.20, 0.21, 0.22, 1"
DBInterface = "2"
Decimals = "0.4.1"
DocStringExtensions = "0.8.0, 0.9.1"
Infinity = "0.2"
Expand Down
13 changes: 13 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,16 @@ execute(conn, copyin)

close(conn)
```

### `DBInterface Integration`

LibPQ types can also be used with the generic [DBInterface.jl](https://github.com/JuliaDatabases/DBInterface.jl)
package to connect to and query Postgres databases.

```julia
using LibPQ, DBInterface

conn = DBInterface.connect(LibPQ.Connection, "dbname=postgres")
res = DBInterface.execute(con, "SELECT * FROM table")
DBInterface.close!(conn)
```
2 changes: 2 additions & 0 deletions src/LibPQ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ using OffsetArrays
using SQLStrings
using TimeZones
using UTCDateTimes
using DBInterface

const Parameter = Union{String,Missing}
const LOGGER = getlogger(@__MODULE__)
Expand Down Expand Up @@ -95,6 +96,7 @@ include("exceptions.jl")
include("parsing.jl")
include("copy.jl")
include("tables.jl")
include("dbinterface.jl")

include("asyncresults.jl")

Expand Down
9 changes: 9 additions & 0 deletions src/dbinterface.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DBInterface.connect(::Type{Connection}, args...; kws...) = Connection(args...; kws...)

DBInterface.prepare(conn::Connection, args...; kws...) = prepare(conn, args...; kws...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this function needs to have separate, identically named bindings with identical method signatures in DBInterface and LibPQ? If a dependency on DBInterface is added anyway, it seems like the current definition of prepare could just extend then reexport the binding from DBInterface. Same for execute.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LibPQ.jl is not currently fully compliant with the interface. The solution presented here avoids bringing it into compliance in favour of providing partial compliance. The future solution to bring it into compliance is either to augment LibPQ.jl's interface to bring it to compliance, or to wrap that interface with a compliant one, and this strategy allows both options.

An example of partial compliance: prepare does not have a do-block form. In LibPQ.jl, I think it would make the most sense if that form actually used the function to operate on the statement and then deallocate the statement at the end, rather than the DBInterface method which says the function should be f()::DBInterface.Connection (I don't see why that would be particularly useful). Having separate dispatches for these two functions allows both to be implemented in the future.


function DBInterface.execute(conn::Union{Connection,Statement}, args...; kws...)
return execute(conn, args...; kws...)
end

DBInterface.close!(conn::Connection) = close(conn)
39 changes: 39 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using Memento
using Memento.TestUtils
using OffsetArrays
using SQLStrings
using DBInterface
using TimeZones
using Tables
using UTCDateTimes
Expand Down Expand Up @@ -1893,6 +1894,44 @@ end

close(conn)
end

@testset "DBInterface integration" begin
conn = DBInterface.connect(LibPQ.Connection, "dbname=postgres user=$DATABASE_USER")
@test conn isa LibPQ.Connection

result = DBInterface.execute(
conn,
"SELECT typname FROM pg_type WHERE oid = 16";
)
@test result isa LibPQ.Result
@test status(result) == LibPQ.libpq_c.PGRES_TUPLES_OK
@test isopen(result)
@test LibPQ.num_columns(result) == 1
@test LibPQ.num_rows(result) == 1
@test LibPQ.column_name(result, 1) == "typname"
@test LibPQ.column_number(result, "typname") == 1
data = columntable(result)
@test data[:typname][1] == "bool"

qstr = "SELECT \$1::double precision as foo, typname FROM pg_type WHERE oid = \$2"
stmt = DBInterface.prepare(conn, qstr)
result = DBInterface.execute(
conn,
qstr,
(1.0, 16);
)
@test result isa LibPQ.Result
@test status(result) == LibPQ.libpq_c.PGRES_TUPLES_OK
@test isopen(result)
@test LibPQ.num_columns(result) == 2
@test LibPQ.num_rows(result) == 1
@test LibPQ.column_name(result, 1) == "foo"
@test LibPQ.column_name(result, 2) == "typname"

DBInterface.close!(result)
@test !isopen(result)
chris-b1 marked this conversation as resolved.
Show resolved Hide resolved

end
end
end

Expand Down