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

Changes purpose of package to be just a wrapper for libPQ.jl that supports DBInterface.jl #59

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

11 changes: 11 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "PostgreSQL"
uuid = "1b57947a-4062-48bc-bd42-99f27a2f9af8"
license = "MIT"
version = "0.1.0"

[deps]
DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965"
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"

[compat]
julia = "1"
88 changes: 15 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,74 +1,16 @@
# PostgreSQL.jl

[![Build Status](https://travis-ci.org/JuliaDB/PostgreSQL.jl.svg)](https://travis-ci.org/JuliaDB/PostgreSQL.jl) [![Coverage Status](https://img.shields.io/coveralls/JuliaDB/PostgreSQL.jl.svg)](https://coveralls.io/r/JuliaDB/PostgreSQL.jl) [![codecov.io](http://codecov.io/github/JuliaDB/PostgreSQL.jl/coverage.svg)](http://codecov.io/github/JuliaDB/PostgreSQL.jl)

An interface to PostgreSQL from Julia. Uses libpq (the C PostgreSQL API) and obeys the [DBI.jl protocol](https://github.com/JuliaDatabases/DBI.jl).


## Maintenance Notice

I can no longer spend work time on this so this project is in maintenance mode (accepting PRs and attempting to resolve issues). New code on the `dbapi` branch (https://github.com/JuliaDatabases/DBAPI.jl) represents the most recent work, which I will continue if I am in a position to do so again.


## Usage

```julia
using DBI
using PostgreSQL

conn = connect(Postgres, "localhost", "username", "password", "dbname", 5432)

stmt = prepare(conn, "SELECT 1::bigint, 2.0::double precision, 'foo'::character varying, " *
"'foo'::character(10);")
result = execute(stmt)
for row in result
# code
end

finish(stmt)

disconnect(conn)
```

### Block Syntax

```julia
using DBI
using PostgreSQL

connect(Postgres, "localhost", "username", "password", "dbname", 5432) do conn
#code
end
```


## Requirements

* [DBI.jl](https://github.com/JuliaDatabases/DBI.jl)
* [DataFrames.jl](https://github.com/JuliaStats/DataFrames.jl) >= v0.5.7
* [DataArrays.jl](https://github.com/JuliaStats/DataArrays.jl) >= v0.1.2
* libpq shared library (comes with a standard PostgreSQL client installation)
* Julia 0.4

Tests require a local PostgreSQL server with a postgres user/database (installed by default with PostgreSQL server installations) with trusted authentication from localhost.


## Systems

* Tested on Funtoo Linux and Windows 8
* Should work on other systems provided libpq is avaiable (please file an issue if this is not the case)


## TODO (soon)

* Implement more default PostgreSQL type handling
* Test type handling overrides
* More comprehensive error handling and tests
* Support for COPY


## TODO (not soon)

* Asynchronous connection support
* Asynchronous Julia for handling asynchronous connections
* Testing and compatibility with multiple versions of PostgreSQL and libpq
A wrapper for PostgreSQL databases using `libPQ.jl` supporting `DBInterface.jl` syntax.

## Supported
- `conn = DBInterface.connect(T, args...; kw...)`
- `stmt = DBInterface.prepare(conn, "INSERT INTO test_table VALUES(?, ?)")`
- `DBInterface.execute(stmt, [1, 3.14])`
- `stmt = DBInterface.prepare(conn, "INSERT INTO test_table VALUES(:col1, :col2)")`
- `DBInterface.executemany(stmt, (col1=[1,2,3,4,5], col2=[3.14, 1.23, 2.34 3.45, 4.56]))`
- `DBInterface.close!(conn)`

## Not Supported Yet
- `DBInterface.Cursor`
- `close!(stmt::Statement)`
- `close!(x::Cursor)`
- `DBInterface.execute(stmt, (col1=1, col2=3.14))`
7 changes: 0 additions & 7 deletions REQUIRE

This file was deleted.

41 changes: 0 additions & 41 deletions deps/build.jl

This file was deleted.

51 changes: 35 additions & 16 deletions src/PostgreSQL.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
module PostgreSQL
export Postgres,
executemany,
copy_from,
escapeliteral

using BinDeps
@BinDeps.load_dependencies

include("libpq_interface.jl")
using .libpq_interface
using DBI
using DataFrames
using DataArrays

include("types.jl")
include("dbi_impl.jl")

import LibPQ, DBInterface

struct Connection <: DBInterface.Connection
inner::LibPQ.Connection
end

struct Stmt <: DBInterface.Statement
inner::LibPQ.Statement
end

DBInterface.connect(::Type{Connection}, args...; kw...) = Connection(LibPQ.Connection(args...; kw...))

DBInterface.close!(conn::Connection) = LibPQ.close(conn.inner)

function DBInterface.prepare(conn::Connection, sql::AbstractString)
# The definition of the sql is different for `DBInterface` and `libPQ`.

sql_new = sql
i = 1
while true
r = match(r"(:[\w]+)|([\?])", sql_new)
if isnothing(r)
break
end
sql_new = sql_new[1:r.offset - 1] * "\$$i" * sql_new[r.offset + length(r.match):end]
i += 1
end

return Stmt(LibPQ.prepare(conn.inner, sql_new))
end

DBInterface.execute(stmt::Stmt, params=()) = LibPQ.execute(stmt.inner, params)


end
Loading