Skip to content

Commit 4c8d8b3

Browse files
chris-b1TheCedarPrince
authored andcommitted
DBInterface integration
1 parent 3e1a6c9 commit 4c8d8b3

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
.vscode
23
*.jl.cov
34
*.jl.*.cov
45
*.jl.mem

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "1.15.1"
55

66
[deps]
77
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
8+
DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965"
89
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
910
Decimals = "abce61dc-4473-55a0-ba07-351d65e31d42"
1011
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
@@ -25,6 +26,7 @@ UTCDateTimes = "0f7cfa37-7abf-4834-b969-a8aa512401c2"
2526
[compat]
2627
CEnum = "0.2, 0.3, 0.4"
2728
DataFrames = "0.20, 0.21, 0.22, 1"
29+
DBInterface = "2"
2830
Decimals = "0.4.1"
2931
DocStringExtensions = "0.8.0, 0.9.1"
3032
Infinity = "0.2"

docs/src/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,16 @@ execute(conn, copyin)
9999

100100
close(conn)
101101
```
102+
103+
### `DBInterface Integration`
104+
105+
LibPQ types can also be used with the generic [DBInterface.jl](https://github.com/JuliaDatabases/DBInterface.jl)
106+
package to connect to and query Postgres databases.
107+
108+
```julia
109+
using LibPQ, DBInterface
110+
111+
conn = DBInterface.connect(LibPQ.Connection, "dbname=postgres")
112+
res = DBInterface.execute(con, "SELECT * FROM table")
113+
DBInterface.close!(conn)
114+
```

src/LibPQ.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ using OffsetArrays
2929
using SQLStrings
3030
using TimeZones
3131
using UTCDateTimes
32+
using DBInterface
3233

3334
const Parameter = Union{String,Missing}
3435
const LOGGER = getlogger(@__MODULE__)
@@ -95,6 +96,7 @@ include("exceptions.jl")
9596
include("parsing.jl")
9697
include("copy.jl")
9798
include("tables.jl")
99+
include("dbinterface.jl")
98100

99101
include("asyncresults.jl")
100102

src/dbinterface.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DBInterface.connect(::Type{Connection}, args...; kws...) =
2+
LibPQ.Connection(args...; kws...)
3+
4+
DBInterface.prepare(conn::Connection, args...; kws...) =
5+
LibPQ.prepare(conn, args...; kws...)
6+
7+
DBInterface.execute(conn::Union{Connection, Statement}, args...; kws...) =
8+
LibPQ.execute(conn, args...; kws...)
9+
10+
DBInterface.close!(conn::Connection) = LibPQ.close(conn)

test/runtests.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ using Memento
1111
using Memento.TestUtils
1212
using OffsetArrays
1313
using SQLStrings
14+
using DBInterface
1415
using TimeZones
1516
using Tables
1617
using UTCDateTimes
@@ -1893,6 +1894,31 @@ end
18931894

18941895
close(conn)
18951896
end
1897+
1898+
@testset "DBInterface integration" begin
1899+
conn = DBInterface.connect("dbname=postgres user=$DATABASE_USER")
1900+
@test conn isa LibPQ.Connection
1901+
1902+
result = DBInterface.execute(
1903+
conn,
1904+
"SELECT typname FROM pg_type WHERE oid = 16";
1905+
)
1906+
@test result isa LibPQ.Result
1907+
@test status(result) == LibPQ.libpq_c.PGRES_TUPLES_OK
1908+
@test isopen(result)
1909+
@test LibPQ.num_columns(result) == 1
1910+
@test LibPQ.num_rows(result) == 1
1911+
@test LibPQ.column_name(result, 1) == "typname"
1912+
@test LibPQ.column_number(result, "typname") == 1
1913+
1914+
data = columntable(result)
1915+
1916+
@test data[:typname][1] == "bool"
1917+
1918+
DBInterface.close!(result)
1919+
@test !isopen(result)
1920+
1921+
end
18961922
end
18971923
end
18981924

0 commit comments

Comments
 (0)