Skip to content

Commit 2c0547d

Browse files
authored
Merge pull request #274 from TheCedarPrince/tcp-dbinterface-final
Add Support for DBInterface
2 parents f6e30b4 + e7ab2ac commit 2c0547d

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

.gitignore

+1
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

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "1.16.0"
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

+13
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

+2
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

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

test/runtests.jl

+39
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,44 @@ end
18931894

18941895
close(conn)
18951896
end
1897+
1898+
@testset "DBInterface integration" begin
1899+
conn = DBInterface.connect(LibPQ.Connection, "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+
data = columntable(result)
1914+
@test data[:typname][1] == "bool"
1915+
1916+
qstr = "SELECT \$1::double precision as foo, typname FROM pg_type WHERE oid = \$2"
1917+
stmt = DBInterface.prepare(conn, qstr)
1918+
result = DBInterface.execute(
1919+
conn,
1920+
qstr,
1921+
(1.0, 16);
1922+
)
1923+
@test result isa LibPQ.Result
1924+
@test status(result) == LibPQ.libpq_c.PGRES_TUPLES_OK
1925+
@test isopen(result)
1926+
@test LibPQ.num_columns(result) == 2
1927+
@test LibPQ.num_rows(result) == 1
1928+
@test LibPQ.column_name(result, 1) == "foo"
1929+
@test LibPQ.column_name(result, 2) == "typname"
1930+
1931+
DBInterface.close!(conn)
1932+
@test !isopen(conn)
1933+
1934+
end
18961935
end
18971936
end
18981937

0 commit comments

Comments
 (0)