Skip to content

Commit

Permalink
Merge pull request #274 from TheCedarPrince/tcp-dbinterface-final
Browse files Browse the repository at this point in the history
Add Support for DBInterface
  • Loading branch information
iamed2 committed Jul 25, 2023
2 parents f6e30b4 + e7ab2ac commit 2c0547d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
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.16.0"

[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...)

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!(conn)
@test !isopen(conn)

end
end
end

Expand Down

2 comments on commit 2c0547d

@iamed2
Copy link
Collaborator Author

@iamed2 iamed2 commented on 2c0547d Jul 25, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/88247

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.16.0 -m "<description of version>" 2c0547daef005e160aa60cb529d4969a670befbc
git push origin v1.16.0

Please sign in to comment.