From 420272a729239bc15130e4f29c8f93caeeb044cf Mon Sep 17 00:00:00 2001 From: Adrian Salceanu Date: Thu, 22 Sep 2016 11:34:28 +0200 Subject: [PATCH 01/21] updates for Julia v0.5 compatibility (mostly related to Strings warnings) --- deps/build.jl | 2 +- src/dbi_impl.jl | 10 ++++------ src/libpq_common.jl | 2 +- src/types.jl | 22 +++++----------------- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/deps/build.jl b/deps/build.jl index e75a4fb..3e8f08d 100644 --- a/deps/build.jl +++ b/deps/build.jl @@ -18,7 +18,7 @@ provides(Yum, "libpq5", libpq) provides(Yum, "postgresql-libs", libpq) provides(Pacman, "postgresql-libs", libpq) -@osx_only begin +@static if is_apple() using Homebrew provides(Homebrew.HB, "postgresql", libpq, os=:Darwin) end diff --git a/src/dbi_impl.jl b/src/dbi_impl.jl index 6c68149..bdbd31e 100644 --- a/src/dbi_impl.jl +++ b/src/dbi_impl.jl @@ -89,16 +89,15 @@ function checkerrclear(result::Ptr{PGresult}) end escapeliteral(db::PostgresDatabaseHandle, value) = value -escapeliteral(db::PostgresDatabaseHandle, value::AbstractString) = escapeliteral(db, bytestring(value)) -function escapeliteral(db::PostgresDatabaseHandle, value::Union{ASCIIString, UTF8String}) +function escapeliteral(db::PostgresDatabaseHandle, value::String) strptr = PQescapeLiteral(db.ptr, value, sizeof(value)) str = bytestring(strptr) PQfreemem(strptr) return str end -function escapeidentifier(db::PostgresDatabaseHandle, value::Union{ASCIIString, UTF8String}) +function escapeidentifier(db::PostgresDatabaseHandle, value::String) strptr = PQescapeIdentifier(db.ptr, value, sizeof(value)) str = bytestring(strptr) PQfreemem(strptr) @@ -115,8 +114,7 @@ function checkcopyreturnval(db::PostgresDatabaseHandle, returnval::Int32) end end -function copy_from(db::PostgresDatabaseHandle, table::AbstractString, - filename::AbstractString, format::AbstractString) +function copy_from(db::PostgresDatabaseHandle, table::AbstractString, filename::AbstractString, format::AbstractString) f = open(filename) try Base.run(db, string("COPY ", escapeidentifier(db, table), " FROM STDIN ", format)) @@ -138,7 +136,7 @@ function getparamtypes(result::Ptr{PGresult}) return @compat [pgtype(OID{Int(PQparamtype(result, i-1))}) for i = 1:nparams] end -LIBC = @windows ? "msvcrt.dll" : :libc +LIBC = @static is_windows() ? "msvcrt.dll" : :libc strlen(ptr::Ptr{UInt8}) = ccall((:strlen, LIBC), Csize_t, (Ptr{UInt8},), ptr) function getparams!(ptrs::Vector{Ptr{UInt8}}, params, types, sizes, lengths::Vector{Int32}, nulls) diff --git a/src/libpq_common.jl b/src/libpq_common.jl index 117808b..02fe849 100644 --- a/src/libpq_common.jl +++ b/src/libpq_common.jl @@ -1,5 +1,5 @@ macro c(ret_type, func, arg_types, lib) - local args_in = Any[ symbol(string('a',x)) for x in 1:length(arg_types.args) ] + local args_in = Any[ Symbol(string('a',x)) for x in 1:length(arg_types.args) ] quote $(esc(func))($(args_in...)) = ccall( ($(string(func)), $(Expr(:quote, lib)) ), $ret_type, $arg_types, $(args_in...) ) diff --git a/src/types.jl b/src/types.jl index 599d396..7d4f0f3 100644 --- a/src/types.jl +++ b/src/types.jl @@ -33,7 +33,7 @@ newpgtype(:int2, 21, (Int16,)) newpgtype(:float8, 701, (Float64,)) newpgtype(:float4, 700, (Float32,)) newpgtype(:bpchar, 1042, ()) -newpgtype(:varchar, 1043, (ASCIIString,UTF8String)) +newpgtype(:varchar, 1043, (String,String)) newpgtype(:text, 25, ()) newpgtype(:numeric, 1700, (BigInt,BigFloat)) newpgtype(:date, 1082, ()) @@ -50,8 +50,8 @@ newpgtype(:_int4, 1007, (Vector{Int32},)) newpgtype(:_int2, 1005, (Vector{Int16},)) newpgtype(:_float8, 1022, (Vector{Float64},)) newpgtype(:_float4, 1021, (Vector{Float32},)) -newpgtype(:_varchar, 1015, (Vector{ASCIIString}, Vector{UTF8String})) -newpgtype(:_text, 1009, (Vector{ASCIIString}, Vector{UTF8String})) +newpgtype(:_varchar, 1015, (Vector{String}, Vector{String})) +newpgtype(:_text, 1009, (Vector{String}, Vector{String})) typealias PGStringTypes Union{Type{PostgresType{:bpchar}}, @@ -151,10 +151,6 @@ function pgdata(::Type{PostgresType{:numeric}}, ptr::Ptr{UInt8}, data::Number) ptr = storestring!(ptr, string(data)) end -function pgdata(::PGStringTypes, ptr::Ptr{UInt8}, data::ByteString) - ptr = storestring!(ptr, data) -end - function pgdata(::PGStringTypes, ptr::Ptr{UInt8}, data::AbstractString) ptr = storestring!(ptr, bytestring(data)) end @@ -212,19 +208,11 @@ function pgdata(::Type{PostgresType{:_float4}}, ptr::Ptr{UInt8}, data::Vector{Fl ptr = storestring!(ptr, string("{", join(data, ','), "}")) end -function pgdata(::Type{PostgresType{:_varchar}}, ptr::Ptr{UInt8}, data::Vector{ASCIIString}) - ptr = storestring!(ptr, string("{", join(data, ','), "}")) -end - -function pgdata(::Type{PostgresType{:_varchar}}, ptr::Ptr{UInt8}, data::Vector{UTF8String}) - ptr = storestring!(ptr, string("{", join(data, ','), "}")) -end - -function pgdata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}, data::Vector{ASCIIString}) +function pgdata(::Type{PostgresType{:_varchar}}, ptr::Ptr{UInt8}, data::Vector{String}) ptr = storestring!(ptr, string("{", join(data, ','), "}")) end -function pgdata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}, data::Vector{UTF8String}) +function pgdata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}, data::Vector{String}) ptr = storestring!(ptr, string("{", join(data, ','), "}")) end From bdc04cc12bf2b6e1d9491f1a4b390a5033c6f59b Mon Sep 17 00:00:00 2001 From: Adrian Salceanu Date: Thu, 22 Sep 2016 11:57:24 +0200 Subject: [PATCH 02/21] updates for Julia v0.5 compatibility (mostly related to Strings warnings) --- src/dbi_impl.jl | 24 ++++++++++++------------ test/connection.jl | 12 ++++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/dbi_impl.jl b/src/dbi_impl.jl index bdbd31e..5038f21 100644 --- a/src/dbi_impl.jl +++ b/src/dbi_impl.jl @@ -10,7 +10,7 @@ function Base.connect(::Type{Postgres}, status = PQstatus(conn) if status != CONNECTION_OK - errmsg = bytestring(PQerrorMessage(conn)) + errmsg = unsafe_string(PQerrorMessage(conn)) PQfinish(conn) error(errmsg) end @@ -36,7 +36,7 @@ function Base.connect(::Type{Postgres}; conn = PQconnectdb(dsn) status = PQstatus(conn) if status != CONNECTION_OK - errmsg = bytestring(PQerrorMessage(conn)) + errmsg = unsafe_string(PQerrorMessage(conn)) PQfinish(conn) error(errmsg) end @@ -60,7 +60,7 @@ function DBI.errcode(db::PostgresDatabaseHandle) end function DBI.errstring(db::PostgresDatabaseHandle) - return bytestring(PQerrorMessage(db.ptr)) + return unsafe_string(PQerrorMessage(db.ptr)) end function DBI.errcode(res::PostgresResultHandle) @@ -68,7 +68,7 @@ function DBI.errcode(res::PostgresResultHandle) end function DBI.errstring(res::PostgresResultHandle) - return bytestring(PQresultErrorMessage(res.ptr)) + return unsafe_string(PQresultErrorMessage(res.ptr)) end DBI.errcode(stmt::PostgresStatementHandle) = DBI.errcode(stmt.result) @@ -79,8 +79,8 @@ function checkerrclear(result::Ptr{PGresult}) try if status == PGRES_FATAL_ERROR - statustext = bytestring(PQresStatus(status)) - errmsg = bytestring(PQresultErrorMessage(result)) + statustext = unsafe_string(PQresStatus(status)) + errmsg = unsafe_string(PQresultErrorMessage(result)) error("$statustext: $errmsg") end finally @@ -92,14 +92,14 @@ escapeliteral(db::PostgresDatabaseHandle, value) = value function escapeliteral(db::PostgresDatabaseHandle, value::String) strptr = PQescapeLiteral(db.ptr, value, sizeof(value)) - str = bytestring(strptr) + str = unsafe_string(strptr) PQfreemem(strptr) return str end function escapeidentifier(db::PostgresDatabaseHandle, value::String) strptr = PQescapeIdentifier(db.ptr, value, sizeof(value)) - str = bytestring(strptr) + str = unsafe_string(strptr) PQfreemem(strptr) return str end @@ -108,8 +108,8 @@ Base.run(db::PostgresDatabaseHandle, sql::AbstractString) = checkerrclear(PQexec function checkcopyreturnval(db::PostgresDatabaseHandle, returnval::Int32) if returnval == -1 - errcode = bytestring(DBI.errcode(db)) - errmsg = bytestring(DBI.errmsg(db)) + errcode = unsafe_string(DBI.errcode(db)) + errmsg = unsafe_string(DBI.errmsg(db)) error("Error $errcode: $errmsg") end end @@ -129,7 +129,7 @@ function copy_from(db::PostgresDatabaseHandle, table::AbstractString, filename:: return checkerrclear(PQgetResult(db.ptr)) end -hashsql(sql::AbstractString) = bytestring(string("__", hash(sql), "__")) +hashsql(sql::AbstractString) = unsafe_string(string("__", hash(sql), "__")) function getparamtypes(result::Ptr{PGresult}) nparams = PQnparams(result) @@ -291,7 +291,7 @@ end function DBI.fetchdf(result::PostgresResultHandle) df = DataFrame() for i = 0:(length(result.types)-1) - df[symbol(bytestring(PQfname(result.ptr, i)))] = unsafe_fetchcol_dataarray(result, i) + df[symbol(unsafe_string(PQfname(result.ptr, i)))] = unsafe_fetchcol_dataarray(result, i) end return df diff --git a/test/connection.jl b/test/connection.jl index 89e8162..74dd9d3 100644 --- a/test/connection.jl +++ b/test/connection.jl @@ -8,9 +8,9 @@ function test_connection() @test conn.status == PostgreSQL.CONNECTION_OK @test errcode(conn) == PostgreSQL.CONNECTION_OK @test !conn.closed - @test bytestring(libpq.PQdb(conn.ptr)) == "julia_test" - @test bytestring(libpq.PQuser(conn.ptr)) == "postgres" - @test bytestring(libpq.PQport(conn.ptr)) == "5432" + @test unsafe_string(libpq.PQdb(conn.ptr)) == "julia_test" + @test unsafe_string(libpq.PQuser(conn.ptr)) == "postgres" + @test unsafe_string(libpq.PQport(conn.ptr)) == "5432" disconnect(conn) @test conn.closed @@ -33,9 +33,9 @@ function test_connection() @test conn.status == PostgreSQL.CONNECTION_OK @test errcode(conn) == PostgreSQL.CONNECTION_OK @test !conn.closed - @test bytestring(libpq.PQdb(conn.ptr)) == "julia_test" - @test bytestring(libpq.PQuser(conn.ptr)) == "postgres" - @test bytestring(libpq.PQport(conn.ptr)) == "5432" + @test unsafe_string(libpq.PQdb(conn.ptr)) == "julia_test" + @test unsafe_string(libpq.PQuser(conn.ptr)) == "postgres" + @test unsafe_string(libpq.PQport(conn.ptr)) == "5432" disconnect(conn) @test conn.closed From 2c4ac457b1b7cec87fd420f8778af02d7844eed8 Mon Sep 17 00:00:00 2001 From: Adrian Salceanu Date: Thu, 22 Sep 2016 13:16:10 +0200 Subject: [PATCH 03/21] updates for Julia v0.5 compatibility (mostly related to Strings warnings) --- src/dbi_impl.jl | 2 +- src/types.jl | 58 ++++++++++++++++++++++++------------------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/dbi_impl.jl b/src/dbi_impl.jl index 5038f21..b96e9a2 100644 --- a/src/dbi_impl.jl +++ b/src/dbi_impl.jl @@ -291,7 +291,7 @@ end function DBI.fetchdf(result::PostgresResultHandle) df = DataFrame() for i = 0:(length(result.types)-1) - df[symbol(unsafe_string(PQfname(result.ptr, i)))] = unsafe_fetchcol_dataarray(result, i) + df[Symbol(unsafe_string(PQfname(result.ptr, i)))] = unsafe_fetchcol_dataarray(result, i) end return df diff --git a/src/types.jl b/src/types.jl index 7d4f0f3..e98917e 100644 --- a/src/types.jl +++ b/src/types.jl @@ -74,54 +74,54 @@ function decode_bytea_hex(s::AbstractString) return hex2bytes(s[3:end]) end -jldata(::Type{PostgresType{:date}}, ptr::Ptr{UInt8}) = bytestring(ptr) +jldata(::Type{PostgresType{:date}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) -jldata(::Type{PostgresType{:timestamp}}, ptr::Ptr{UInt8}) = bytestring(ptr) +jldata(::Type{PostgresType{:timestamp}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) -jldata(::Type{PostgresType{:timestamptz}}, ptr::Ptr{UInt8}) = bytestring(ptr) +jldata(::Type{PostgresType{:timestamptz}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) -jldata(::Type{PostgresType{:bool}}, ptr::Ptr{UInt8}) = bytestring(ptr) != "f" +jldata(::Type{PostgresType{:bool}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) != "f" -jldata(::Type{PostgresType{:int8}}, ptr::Ptr{UInt8}) = parse(Int64, bytestring(ptr)) +jldata(::Type{PostgresType{:int8}}, ptr::Ptr{UInt8}) = parse(Int64, unsafe_string(ptr)) -jldata(::Type{PostgresType{:int4}}, ptr::Ptr{UInt8}) = parse(Int32, bytestring(ptr)) +jldata(::Type{PostgresType{:int4}}, ptr::Ptr{UInt8}) = parse(Int32, unsafe_string(ptr)) -jldata(::Type{PostgresType{:int2}}, ptr::Ptr{UInt8}) = parse(Int16, bytestring(ptr)) +jldata(::Type{PostgresType{:int2}}, ptr::Ptr{UInt8}) = parse(Int16, unsafe_string(ptr)) -jldata(::Type{PostgresType{:float8}}, ptr::Ptr{UInt8}) = parse(Float64, bytestring(ptr)) +jldata(::Type{PostgresType{:float8}}, ptr::Ptr{UInt8}) = parse(Float64, unsafe_string(ptr)) -jldata(::Type{PostgresType{:float4}}, ptr::Ptr{UInt8}) = parse(Float32, bytestring(ptr)) +jldata(::Type{PostgresType{:float4}}, ptr::Ptr{UInt8}) = parse(Float32, unsafe_string(ptr)) function jldata(::Type{PostgresType{:numeric}}, ptr::Ptr{UInt8}) - s = bytestring(ptr) + s = unsafe_string(ptr) return parse(search(s, '.') == 0 ? BigInt : BigFloat, s) end -jldata(::PGStringTypes, ptr::Ptr{UInt8}) = bytestring(ptr) +jldata(::PGStringTypes, ptr::Ptr{UInt8}) = unsafe_string(ptr) -jldata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}) = bytestring(ptr) |> decode_bytea_hex +jldata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) |> decode_bytea_hex jldata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}) = Union{} -jldata(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}) = JSON.parse(bytestring(ptr)) +jldata(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}) = JSON.parse(unsafe_string(ptr)) -jldata(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}) = JSON.parse(bytestring(ptr)) +jldata(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}) = JSON.parse(unsafe_string(ptr)) -jldata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}) = map(x -> x != "f", split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}) = map(x -> x != "f", split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_int8}}, ptr::Ptr{UInt8}) = map(x -> parse(Int64, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_int8}}, ptr::Ptr{UInt8}) = map(x -> parse(Int64, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_int4}}, ptr::Ptr{UInt8}) = map(x -> parse(Int32, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_int4}}, ptr::Ptr{UInt8}) = map(x -> parse(Int32, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_int2}}, ptr::Ptr{UInt8}) = map(x -> parse(Int16, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_int2}}, ptr::Ptr{UInt8}) = map(x -> parse(Int16, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_float8}}, ptr::Ptr{UInt8}) = map(x -> parse(Float64, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_float8}}, ptr::Ptr{UInt8}) = map(x -> parse(Float64, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_float4}}, ptr::Ptr{UInt8}) = map(x -> parse(Float32, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_float4}}, ptr::Ptr{UInt8}) = map(x -> parse(Float32, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_varchar}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_varchar}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(unsafe_string(ptr)[2:end-1], ',')) function pgdata(::Type{PostgresType{:bool}}, ptr::Ptr{UInt8}, data::Bool) ptr = data ? storestring!(ptr, "TRUE") : storestring!(ptr, "FALSE") @@ -152,24 +152,24 @@ function pgdata(::Type{PostgresType{:numeric}}, ptr::Ptr{UInt8}, data::Number) end function pgdata(::PGStringTypes, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, bytestring(data)) + ptr = storestring!(ptr, unsafe_string(data)) end function pgdata(::PostgresType{:date}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, bytestring(data)) + ptr = storestring!(ptr, unsafe_string(data)) ptr = Dates.DateFormat(ptr) end function pgdata(::PostgresType{:timestamp}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, bytestring(data)) + ptr = storestring!(ptr, unsafe_string(data)) end function pgdata(::PostgresType{:timestamptz}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, bytestring(data)) + ptr = storestring!(ptr, unsafe_string(data)) end function pgdata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}, data::Vector{UInt8}) - ptr = storestring!(ptr, bytestring("\\x", bytes2hex(data))) + ptr = storestring!(ptr, unsafe_string("\\x", bytes2hex(data))) end function pgdata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}, data) @@ -177,11 +177,11 @@ function pgdata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}, data) end function pgdata{T<:AbstractString}(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}, data::Dict{T,Any}) - ptr = storestring!(ptr, bytestring(JSON.json(data))) + ptr = storestring!(ptr, unsafe_string(JSON.json(data))) end function pgdata{T<:AbstractString}(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}, data::Dict{T,Any}) - ptr = storestring!(ptr, bytestring(JSON.json(data))) + ptr = storestring!(ptr, unsafe_string(JSON.json(data))) end function pgdata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}, data::Vector{Bool}) From 6a20bfcbfd18dac7126261bc354291e021a521a2 Mon Sep 17 00:00:00 2001 From: Adrian Salceanu Date: Thu, 22 Sep 2016 14:11:42 +0200 Subject: [PATCH 04/21] updates for Julia v0.5 compatibility (mostly related to Strings warnings) --- src/types.jl | 58 ++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/types.jl b/src/types.jl index e98917e..7d4f0f3 100644 --- a/src/types.jl +++ b/src/types.jl @@ -74,54 +74,54 @@ function decode_bytea_hex(s::AbstractString) return hex2bytes(s[3:end]) end -jldata(::Type{PostgresType{:date}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) +jldata(::Type{PostgresType{:date}}, ptr::Ptr{UInt8}) = bytestring(ptr) -jldata(::Type{PostgresType{:timestamp}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) +jldata(::Type{PostgresType{:timestamp}}, ptr::Ptr{UInt8}) = bytestring(ptr) -jldata(::Type{PostgresType{:timestamptz}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) +jldata(::Type{PostgresType{:timestamptz}}, ptr::Ptr{UInt8}) = bytestring(ptr) -jldata(::Type{PostgresType{:bool}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) != "f" +jldata(::Type{PostgresType{:bool}}, ptr::Ptr{UInt8}) = bytestring(ptr) != "f" -jldata(::Type{PostgresType{:int8}}, ptr::Ptr{UInt8}) = parse(Int64, unsafe_string(ptr)) +jldata(::Type{PostgresType{:int8}}, ptr::Ptr{UInt8}) = parse(Int64, bytestring(ptr)) -jldata(::Type{PostgresType{:int4}}, ptr::Ptr{UInt8}) = parse(Int32, unsafe_string(ptr)) +jldata(::Type{PostgresType{:int4}}, ptr::Ptr{UInt8}) = parse(Int32, bytestring(ptr)) -jldata(::Type{PostgresType{:int2}}, ptr::Ptr{UInt8}) = parse(Int16, unsafe_string(ptr)) +jldata(::Type{PostgresType{:int2}}, ptr::Ptr{UInt8}) = parse(Int16, bytestring(ptr)) -jldata(::Type{PostgresType{:float8}}, ptr::Ptr{UInt8}) = parse(Float64, unsafe_string(ptr)) +jldata(::Type{PostgresType{:float8}}, ptr::Ptr{UInt8}) = parse(Float64, bytestring(ptr)) -jldata(::Type{PostgresType{:float4}}, ptr::Ptr{UInt8}) = parse(Float32, unsafe_string(ptr)) +jldata(::Type{PostgresType{:float4}}, ptr::Ptr{UInt8}) = parse(Float32, bytestring(ptr)) function jldata(::Type{PostgresType{:numeric}}, ptr::Ptr{UInt8}) - s = unsafe_string(ptr) + s = bytestring(ptr) return parse(search(s, '.') == 0 ? BigInt : BigFloat, s) end -jldata(::PGStringTypes, ptr::Ptr{UInt8}) = unsafe_string(ptr) +jldata(::PGStringTypes, ptr::Ptr{UInt8}) = bytestring(ptr) -jldata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) |> decode_bytea_hex +jldata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}) = bytestring(ptr) |> decode_bytea_hex jldata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}) = Union{} -jldata(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}) = JSON.parse(unsafe_string(ptr)) +jldata(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}) = JSON.parse(bytestring(ptr)) -jldata(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}) = JSON.parse(unsafe_string(ptr)) +jldata(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}) = JSON.parse(bytestring(ptr)) -jldata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}) = map(x -> x != "f", split(unsafe_string(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}) = map(x -> x != "f", split(bytestring(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_int8}}, ptr::Ptr{UInt8}) = map(x -> parse(Int64, x), split(unsafe_string(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_int8}}, ptr::Ptr{UInt8}) = map(x -> parse(Int64, x), split(bytestring(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_int4}}, ptr::Ptr{UInt8}) = map(x -> parse(Int32, x), split(unsafe_string(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_int4}}, ptr::Ptr{UInt8}) = map(x -> parse(Int32, x), split(bytestring(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_int2}}, ptr::Ptr{UInt8}) = map(x -> parse(Int16, x), split(unsafe_string(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_int2}}, ptr::Ptr{UInt8}) = map(x -> parse(Int16, x), split(bytestring(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_float8}}, ptr::Ptr{UInt8}) = map(x -> parse(Float64, x), split(unsafe_string(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_float8}}, ptr::Ptr{UInt8}) = map(x -> parse(Float64, x), split(bytestring(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_float4}}, ptr::Ptr{UInt8}) = map(x -> parse(Float32, x), split(unsafe_string(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_float4}}, ptr::Ptr{UInt8}) = map(x -> parse(Float32, x), split(bytestring(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_varchar}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(unsafe_string(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_varchar}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(bytestring(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(unsafe_string(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(bytestring(ptr)[2:end-1], ',')) function pgdata(::Type{PostgresType{:bool}}, ptr::Ptr{UInt8}, data::Bool) ptr = data ? storestring!(ptr, "TRUE") : storestring!(ptr, "FALSE") @@ -152,24 +152,24 @@ function pgdata(::Type{PostgresType{:numeric}}, ptr::Ptr{UInt8}, data::Number) end function pgdata(::PGStringTypes, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, unsafe_string(data)) + ptr = storestring!(ptr, bytestring(data)) end function pgdata(::PostgresType{:date}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, unsafe_string(data)) + ptr = storestring!(ptr, bytestring(data)) ptr = Dates.DateFormat(ptr) end function pgdata(::PostgresType{:timestamp}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, unsafe_string(data)) + ptr = storestring!(ptr, bytestring(data)) end function pgdata(::PostgresType{:timestamptz}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, unsafe_string(data)) + ptr = storestring!(ptr, bytestring(data)) end function pgdata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}, data::Vector{UInt8}) - ptr = storestring!(ptr, unsafe_string("\\x", bytes2hex(data))) + ptr = storestring!(ptr, bytestring("\\x", bytes2hex(data))) end function pgdata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}, data) @@ -177,11 +177,11 @@ function pgdata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}, data) end function pgdata{T<:AbstractString}(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}, data::Dict{T,Any}) - ptr = storestring!(ptr, unsafe_string(JSON.json(data))) + ptr = storestring!(ptr, bytestring(JSON.json(data))) end function pgdata{T<:AbstractString}(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}, data::Dict{T,Any}) - ptr = storestring!(ptr, unsafe_string(JSON.json(data))) + ptr = storestring!(ptr, bytestring(JSON.json(data))) end function pgdata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}, data::Vector{Bool}) From 0fa9e58ad64b1856950f024f4c4a95dfaa4b2331 Mon Sep 17 00:00:00 2001 From: Adrian Salceanu Date: Thu, 22 Sep 2016 14:16:34 +0200 Subject: [PATCH 05/21] updates for Julia v0.5 compatibility (mostly related to Strings warnings) --- src/dbi_impl.jl | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/dbi_impl.jl b/src/dbi_impl.jl index b96e9a2..47f09a1 100644 --- a/src/dbi_impl.jl +++ b/src/dbi_impl.jl @@ -10,7 +10,7 @@ function Base.connect(::Type{Postgres}, status = PQstatus(conn) if status != CONNECTION_OK - errmsg = unsafe_string(PQerrorMessage(conn)) + errmsg = bytestring(PQerrorMessage(conn)) PQfinish(conn) error(errmsg) end @@ -36,7 +36,7 @@ function Base.connect(::Type{Postgres}; conn = PQconnectdb(dsn) status = PQstatus(conn) if status != CONNECTION_OK - errmsg = unsafe_string(PQerrorMessage(conn)) + errmsg = bytestring(PQerrorMessage(conn)) PQfinish(conn) error(errmsg) end @@ -60,7 +60,7 @@ function DBI.errcode(db::PostgresDatabaseHandle) end function DBI.errstring(db::PostgresDatabaseHandle) - return unsafe_string(PQerrorMessage(db.ptr)) + return bytestring(PQerrorMessage(db.ptr)) end function DBI.errcode(res::PostgresResultHandle) @@ -68,7 +68,7 @@ function DBI.errcode(res::PostgresResultHandle) end function DBI.errstring(res::PostgresResultHandle) - return unsafe_string(PQresultErrorMessage(res.ptr)) + return bytestring(PQresultErrorMessage(res.ptr)) end DBI.errcode(stmt::PostgresStatementHandle) = DBI.errcode(stmt.result) @@ -79,8 +79,8 @@ function checkerrclear(result::Ptr{PGresult}) try if status == PGRES_FATAL_ERROR - statustext = unsafe_string(PQresStatus(status)) - errmsg = unsafe_string(PQresultErrorMessage(result)) + statustext = bytestring(PQresStatus(status)) + errmsg = bytestring(PQresultErrorMessage(result)) error("$statustext: $errmsg") end finally @@ -92,14 +92,14 @@ escapeliteral(db::PostgresDatabaseHandle, value) = value function escapeliteral(db::PostgresDatabaseHandle, value::String) strptr = PQescapeLiteral(db.ptr, value, sizeof(value)) - str = unsafe_string(strptr) + str = bytestring(strptr) PQfreemem(strptr) return str end function escapeidentifier(db::PostgresDatabaseHandle, value::String) strptr = PQescapeIdentifier(db.ptr, value, sizeof(value)) - str = unsafe_string(strptr) + str = bytestring(strptr) PQfreemem(strptr) return str end @@ -108,8 +108,8 @@ Base.run(db::PostgresDatabaseHandle, sql::AbstractString) = checkerrclear(PQexec function checkcopyreturnval(db::PostgresDatabaseHandle, returnval::Int32) if returnval == -1 - errcode = unsafe_string(DBI.errcode(db)) - errmsg = unsafe_string(DBI.errmsg(db)) + errcode = bytestring(DBI.errcode(db)) + errmsg = bytestring(DBI.errmsg(db)) error("Error $errcode: $errmsg") end end @@ -129,7 +129,7 @@ function copy_from(db::PostgresDatabaseHandle, table::AbstractString, filename:: return checkerrclear(PQgetResult(db.ptr)) end -hashsql(sql::AbstractString) = unsafe_string(string("__", hash(sql), "__")) +hashsql(sql::AbstractString) = bytestring(string("__", hash(sql), "__")) function getparamtypes(result::Ptr{PGresult}) nparams = PQnparams(result) @@ -291,7 +291,7 @@ end function DBI.fetchdf(result::PostgresResultHandle) df = DataFrame() for i = 0:(length(result.types)-1) - df[Symbol(unsafe_string(PQfname(result.ptr, i)))] = unsafe_fetchcol_dataarray(result, i) + df[Symbol(bytestring(PQfname(result.ptr, i)))] = unsafe_fetchcol_dataarray(result, i) end return df From cf49c1c6394c593e4369f37c3b107fefd00bc746 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 14:50:12 +0200 Subject: [PATCH 06/21] Added missing types definition + updated deplreciated --- src/libpq_common.jl | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/libpq_common.jl b/src/libpq_common.jl index 02fe849..3b63742 100644 --- a/src/libpq_common.jl +++ b/src/libpq_common.jl @@ -50,14 +50,14 @@ const PGF_BINARY = 1 # Skipping MacroDefinition: PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME)PQsetdbLogin(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME,NULL,NULL) # Skipping MacroDefinition: PQfreeNotify(ptr)PQfreemem(ptr) const PQnoPasswordSupplied = "fe_sendauth: no password supplied\n" -typealias _IO_lock_t Union{} -typealias va_list Cint +const _IO_lock_t = Union{} +const va_list = Cint # typealias off_t __off_t # typealias ssize_t __ssize_t # typealias fpos_t _G_fpos_t -typealias Oid UInt32 +const Oid = UInt32 # begin enum ConnStatusType -typealias ConnStatusType UInt32 +const ConnStatusType = UInt32 const CONNECTION_OK = 0 const CONNECTION_BAD = 1 const CONNECTION_STARTED = 2 @@ -69,7 +69,7 @@ const CONNECTION_SSL_STARTUP = 7 const CONNECTION_NEEDED = 8 # end enum ConnStatusType # begin enum PostgresPollingStatusType -typealias PostgresPollingStatusType UInt32 +const PostgresPollingStatusType = UInt32 const PGRES_POLLING_FAILED = 0 const PGRES_POLLING_READING = 1 const PGRES_POLLING_WRITING = 2 @@ -77,7 +77,7 @@ const PGRES_POLLING_OK = 3 const PGRES_POLLING_ACTIVE = 4 # end enum PostgresPollingStatusType # begin enum ExecStatusType -typealias ExecStatusType UInt32 +const ExecStatusType = UInt32 const PGRES_EMPTY_QUERY = 0 const PGRES_COMMAND_OK = 1 const PGRES_TUPLES_OK = 2 @@ -90,7 +90,7 @@ const PGRES_COPY_BOTH = 8 const PGRES_SINGLE_TUPLE = 9 # end enum ExecStatusType # begin enum PGTransactionStatusType -typealias PGTransactionStatusType UInt32 +const PGTransactionStatusType = UInt32 const PQTRANS_IDLE = 0 const PQTRANS_ACTIVE = 1 const PQTRANS_INTRANS = 2 @@ -98,21 +98,26 @@ const PQTRANS_INERROR = 3 const PQTRANS_UNKNOWN = 4 # end enum PGTransactionStatusType # begin enum PGVerbosity -typealias PGVerbosity UInt32 +const PGVerbosity = UInt32 const PQERRORS_TERSE = 0 const PQERRORS_DEFAULT = 1 const PQERRORS_VERBOSE = 2 # end enum PGVerbosity # begin enum PGPing -typealias PGPing UInt32 +const PGPing = UInt32 const PQPING_OK = 0 const PQPING_REJECT = 1 const PQPING_NO_RESPONSE = 2 const PQPING_NO_ATTEMPT = 3 # end enum PGPing -typealias PQnoticeReceiver Ptr{Void} -typealias PQnoticeProcessor Ptr{Void} -typealias pqbool UInt8 -typealias pgthreadlock_t Ptr{Void} -typealias PGconn Void -typealias PGresult Void +const PQnoticeReceiver = Ptr{Void} +const PQnoticeProcessor = Ptr{Void} +const pqbool = UInt8 +const pgthreadlock_t = Ptr{Void} +const PGconn = Void +const PGresult = Void + +const _IO_FILE = Void +const FILE = Void +const PQconninfoOption = Void +const PGcancel = Void From a502becaee0c8e1fccdf62a6dbdb57b3ed6dec53 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 14:59:32 +0200 Subject: [PATCH 07/21] removed io seek unused functions + duplicate function --- src/libpq_interface.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libpq_interface.jl b/src/libpq_interface.jl index dfe0308..58b5790 100644 --- a/src/libpq_interface.jl +++ b/src/libpq_interface.jl @@ -94,8 +94,6 @@ module libpq_interface @c Cint _IO_vfprintf (Ptr{_IO_FILE}, Ptr{UInt8}, Cint) libpq # @c __ssize_t _IO_padn (Ptr{_IO_FILE}, Cint, __ssize_t) libpq @c Cint _IO_sgetn (Ptr{_IO_FILE}, Ptr{Void}, Cint) libpq - @c __off64_t _IO_seekoff (Ptr{_IO_FILE}, __off64_t, Cint, Cint) libpq - @c __off64_t _IO_seekpos (Ptr{_IO_FILE}, __off64_t, Cint) libpq @c Void _IO_free_backup_area (Ptr{_IO_FILE},) libpq @c Cint remove (Ptr{UInt8},) libpq @c Cint rename (Ptr{UInt8}, Ptr{UInt8}) libpq @@ -126,7 +124,6 @@ module libpq_interface @c Cint sscanf (Ptr{UInt8}, Ptr{UInt8}) libpq @c Cint fscanf (Ptr{FILE}, Ptr{UInt8}) libpq @c Cint scanf (Ptr{UInt8},) libpq - @c Cint sscanf (Ptr{UInt8}, Ptr{UInt8}) libpq @c Cint fgetc (Ptr{FILE},) libpq @c Cint getc (Ptr{FILE},) libpq @c Cint getchar () libpq From 9fedb2a5565bff842ba9ab5f9b15e8e17d967bd6 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 15:01:47 +0200 Subject: [PATCH 08/21] Removed duplicate function declarations --- src/libpq_interface.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libpq_interface.jl b/src/libpq_interface.jl index 58b5790..1c816b5 100644 --- a/src/libpq_interface.jl +++ b/src/libpq_interface.jl @@ -122,8 +122,6 @@ module libpq_interface @c Cint fscanf (Ptr{FILE}, Ptr{UInt8}) libpq @c Cint scanf (Ptr{UInt8},) libpq @c Cint sscanf (Ptr{UInt8}, Ptr{UInt8}) libpq - @c Cint fscanf (Ptr{FILE}, Ptr{UInt8}) libpq - @c Cint scanf (Ptr{UInt8},) libpq @c Cint fgetc (Ptr{FILE},) libpq @c Cint getc (Ptr{FILE},) libpq @c Cint getchar () libpq From 47517f40dccaa638e2915ca13f21cffa3f34ad7e Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 15:02:57 +0200 Subject: [PATCH 09/21] added missing type --- src/libpq_common.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libpq_common.jl b/src/libpq_common.jl index 3b63742..592da29 100644 --- a/src/libpq_common.jl +++ b/src/libpq_common.jl @@ -121,3 +121,4 @@ const _IO_FILE = Void const FILE = Void const PQconninfoOption = Void const PGcancel = Void +const PGnotify = Void From 3ac14f76ba17a8f7b50e335680d2cc42222c9896 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 15:17:56 +0200 Subject: [PATCH 10/21] Added missing type --- src/libpq_common.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libpq_common.jl b/src/libpq_common.jl index 592da29..85fe82c 100644 --- a/src/libpq_common.jl +++ b/src/libpq_common.jl @@ -122,3 +122,4 @@ const FILE = Void const PQconninfoOption = Void const PGcancel = Void const PGnotify = Void +const PQArgBlock = Void From 7c35b113dd10e0f9467eadacad8f775bf70b8fd6 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 15:32:46 +0200 Subject: [PATCH 11/21] added more missing types --- src/libpq_common.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libpq_common.jl b/src/libpq_common.jl index 85fe82c..407a93d 100644 --- a/src/libpq_common.jl +++ b/src/libpq_common.jl @@ -123,3 +123,5 @@ const PQconninfoOption = Void const PGcancel = Void const PGnotify = Void const PQArgBlock = Void +const PGresAttDesc = Void +const PQprintOpt = Void From b778a76451710ad5ed644f10c09a898201063f55 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 15:38:08 +0200 Subject: [PATCH 12/21] Fixed type alias declaration --- src/types.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.jl b/src/types.jl index 7d4f0f3..9bb7290 100644 --- a/src/types.jl +++ b/src/types.jl @@ -54,7 +54,7 @@ newpgtype(:_varchar, 1015, (Vector{String}, Vector{String})) newpgtype(:_text, 1009, (Vector{String}, Vector{String})) -typealias PGStringTypes Union{Type{PostgresType{:bpchar}}, +const PGStringTypes = Union{Type{PostgresType{:bpchar}}, Type{PostgresType{:varchar}}, Type{PostgresType{:text}}, Type{PostgresType{:date}}} From 2c706261f47c33cf736c8ac4ef509186c38781eb Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 15:53:39 +0200 Subject: [PATCH 13/21] Changed bytestring to unsafe_string --- src/types.jl | 58 ++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/types.jl b/src/types.jl index 9bb7290..f931e46 100644 --- a/src/types.jl +++ b/src/types.jl @@ -74,54 +74,54 @@ function decode_bytea_hex(s::AbstractString) return hex2bytes(s[3:end]) end -jldata(::Type{PostgresType{:date}}, ptr::Ptr{UInt8}) = bytestring(ptr) +jldata(::Type{PostgresType{:date}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) -jldata(::Type{PostgresType{:timestamp}}, ptr::Ptr{UInt8}) = bytestring(ptr) +jldata(::Type{PostgresType{:timestamp}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) -jldata(::Type{PostgresType{:timestamptz}}, ptr::Ptr{UInt8}) = bytestring(ptr) +jldata(::Type{PostgresType{:timestamptz}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) -jldata(::Type{PostgresType{:bool}}, ptr::Ptr{UInt8}) = bytestring(ptr) != "f" +jldata(::Type{PostgresType{:bool}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) != "f" -jldata(::Type{PostgresType{:int8}}, ptr::Ptr{UInt8}) = parse(Int64, bytestring(ptr)) +jldata(::Type{PostgresType{:int8}}, ptr::Ptr{UInt8}) = parse(Int64, unsafe_string(ptr)) -jldata(::Type{PostgresType{:int4}}, ptr::Ptr{UInt8}) = parse(Int32, bytestring(ptr)) +jldata(::Type{PostgresType{:int4}}, ptr::Ptr{UInt8}) = parse(Int32, unsafe_string(ptr)) -jldata(::Type{PostgresType{:int2}}, ptr::Ptr{UInt8}) = parse(Int16, bytestring(ptr)) +jldata(::Type{PostgresType{:int2}}, ptr::Ptr{UInt8}) = parse(Int16, unsafe_string(ptr)) -jldata(::Type{PostgresType{:float8}}, ptr::Ptr{UInt8}) = parse(Float64, bytestring(ptr)) +jldata(::Type{PostgresType{:float8}}, ptr::Ptr{UInt8}) = parse(Float64, unsafe_string(ptr)) -jldata(::Type{PostgresType{:float4}}, ptr::Ptr{UInt8}) = parse(Float32, bytestring(ptr)) +jldata(::Type{PostgresType{:float4}}, ptr::Ptr{UInt8}) = parse(Float32, unsafe_string(ptr)) function jldata(::Type{PostgresType{:numeric}}, ptr::Ptr{UInt8}) - s = bytestring(ptr) + s = unsafe_string(ptr) return parse(search(s, '.') == 0 ? BigInt : BigFloat, s) end -jldata(::PGStringTypes, ptr::Ptr{UInt8}) = bytestring(ptr) +jldata(::PGStringTypes, ptr::Ptr{UInt8}) = unsafe_string(ptr) -jldata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}) = bytestring(ptr) |> decode_bytea_hex +jldata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}) = unsafe_string(ptr) |> decode_bytea_hex jldata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}) = Union{} -jldata(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}) = JSON.parse(bytestring(ptr)) +jldata(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}) = JSON.parse(unsafe_string(ptr)) -jldata(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}) = JSON.parse(bytestring(ptr)) +jldata(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}) = JSON.parse(unsafe_string(ptr)) -jldata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}) = map(x -> x != "f", split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}) = map(x -> x != "f", split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_int8}}, ptr::Ptr{UInt8}) = map(x -> parse(Int64, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_int8}}, ptr::Ptr{UInt8}) = map(x -> parse(Int64, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_int4}}, ptr::Ptr{UInt8}) = map(x -> parse(Int32, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_int4}}, ptr::Ptr{UInt8}) = map(x -> parse(Int32, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_int2}}, ptr::Ptr{UInt8}) = map(x -> parse(Int16, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_int2}}, ptr::Ptr{UInt8}) = map(x -> parse(Int16, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_float8}}, ptr::Ptr{UInt8}) = map(x -> parse(Float64, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_float8}}, ptr::Ptr{UInt8}) = map(x -> parse(Float64, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_float4}}, ptr::Ptr{UInt8}) = map(x -> parse(Float32, x), split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_float4}}, ptr::Ptr{UInt8}) = map(x -> parse(Float32, x), split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_varchar}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_varchar}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(unsafe_string(ptr)[2:end-1], ',')) -jldata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(bytestring(ptr)[2:end-1], ',')) +jldata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}) = convert(Vector{AbstractString}, split(unsafe_string(ptr)[2:end-1], ',')) function pgdata(::Type{PostgresType{:bool}}, ptr::Ptr{UInt8}, data::Bool) ptr = data ? storestring!(ptr, "TRUE") : storestring!(ptr, "FALSE") @@ -152,24 +152,24 @@ function pgdata(::Type{PostgresType{:numeric}}, ptr::Ptr{UInt8}, data::Number) end function pgdata(::PGStringTypes, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, bytestring(data)) + ptr = storestring!(ptr, unsafe_string(data)) end function pgdata(::PostgresType{:date}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, bytestring(data)) + ptr = storestring!(ptr, unsafe_string(data)) ptr = Dates.DateFormat(ptr) end function pgdata(::PostgresType{:timestamp}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, bytestring(data)) + ptr = storestring!(ptr, unsafe_string(data)) end function pgdata(::PostgresType{:timestamptz}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, bytestring(data)) + ptr = storestring!(ptr, unsafe_string(data)) end function pgdata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}, data::Vector{UInt8}) - ptr = storestring!(ptr, bytestring("\\x", bytes2hex(data))) + ptr = storestring!(ptr, unsafe_string("\\x", bytes2hex(data))) end function pgdata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}, data) @@ -177,11 +177,11 @@ function pgdata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}, data) end function pgdata{T<:AbstractString}(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}, data::Dict{T,Any}) - ptr = storestring!(ptr, bytestring(JSON.json(data))) + ptr = storestring!(ptr, unsafe_string(JSON.json(data))) end function pgdata{T<:AbstractString}(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}, data::Dict{T,Any}) - ptr = storestring!(ptr, bytestring(JSON.json(data))) + ptr = storestring!(ptr, unsafe_string(JSON.json(data))) end function pgdata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}, data::Vector{Bool}) From 57850c109084fac49978ebf1e76f4be77e4552a1 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 16:01:27 +0200 Subject: [PATCH 14/21] changed bytestring to unsafe_string --- src/dbi_impl.jl | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/dbi_impl.jl b/src/dbi_impl.jl index 47f09a1..b96e9a2 100644 --- a/src/dbi_impl.jl +++ b/src/dbi_impl.jl @@ -10,7 +10,7 @@ function Base.connect(::Type{Postgres}, status = PQstatus(conn) if status != CONNECTION_OK - errmsg = bytestring(PQerrorMessage(conn)) + errmsg = unsafe_string(PQerrorMessage(conn)) PQfinish(conn) error(errmsg) end @@ -36,7 +36,7 @@ function Base.connect(::Type{Postgres}; conn = PQconnectdb(dsn) status = PQstatus(conn) if status != CONNECTION_OK - errmsg = bytestring(PQerrorMessage(conn)) + errmsg = unsafe_string(PQerrorMessage(conn)) PQfinish(conn) error(errmsg) end @@ -60,7 +60,7 @@ function DBI.errcode(db::PostgresDatabaseHandle) end function DBI.errstring(db::PostgresDatabaseHandle) - return bytestring(PQerrorMessage(db.ptr)) + return unsafe_string(PQerrorMessage(db.ptr)) end function DBI.errcode(res::PostgresResultHandle) @@ -68,7 +68,7 @@ function DBI.errcode(res::PostgresResultHandle) end function DBI.errstring(res::PostgresResultHandle) - return bytestring(PQresultErrorMessage(res.ptr)) + return unsafe_string(PQresultErrorMessage(res.ptr)) end DBI.errcode(stmt::PostgresStatementHandle) = DBI.errcode(stmt.result) @@ -79,8 +79,8 @@ function checkerrclear(result::Ptr{PGresult}) try if status == PGRES_FATAL_ERROR - statustext = bytestring(PQresStatus(status)) - errmsg = bytestring(PQresultErrorMessage(result)) + statustext = unsafe_string(PQresStatus(status)) + errmsg = unsafe_string(PQresultErrorMessage(result)) error("$statustext: $errmsg") end finally @@ -92,14 +92,14 @@ escapeliteral(db::PostgresDatabaseHandle, value) = value function escapeliteral(db::PostgresDatabaseHandle, value::String) strptr = PQescapeLiteral(db.ptr, value, sizeof(value)) - str = bytestring(strptr) + str = unsafe_string(strptr) PQfreemem(strptr) return str end function escapeidentifier(db::PostgresDatabaseHandle, value::String) strptr = PQescapeIdentifier(db.ptr, value, sizeof(value)) - str = bytestring(strptr) + str = unsafe_string(strptr) PQfreemem(strptr) return str end @@ -108,8 +108,8 @@ Base.run(db::PostgresDatabaseHandle, sql::AbstractString) = checkerrclear(PQexec function checkcopyreturnval(db::PostgresDatabaseHandle, returnval::Int32) if returnval == -1 - errcode = bytestring(DBI.errcode(db)) - errmsg = bytestring(DBI.errmsg(db)) + errcode = unsafe_string(DBI.errcode(db)) + errmsg = unsafe_string(DBI.errmsg(db)) error("Error $errcode: $errmsg") end end @@ -129,7 +129,7 @@ function copy_from(db::PostgresDatabaseHandle, table::AbstractString, filename:: return checkerrclear(PQgetResult(db.ptr)) end -hashsql(sql::AbstractString) = bytestring(string("__", hash(sql), "__")) +hashsql(sql::AbstractString) = unsafe_string(string("__", hash(sql), "__")) function getparamtypes(result::Ptr{PGresult}) nparams = PQnparams(result) @@ -291,7 +291,7 @@ end function DBI.fetchdf(result::PostgresResultHandle) df = DataFrame() for i = 0:(length(result.types)-1) - df[Symbol(bytestring(PQfname(result.ptr, i)))] = unsafe_fetchcol_dataarray(result, i) + df[Symbol(unsafe_string(PQfname(result.ptr, i)))] = unsafe_fetchcol_dataarray(result, i) end return df From 38f5064965e14f66b53379f9cd0d482a0cecf933 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 16:04:52 +0200 Subject: [PATCH 15/21] Fixed String to be AbstractString --- src/dbi_impl.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dbi_impl.jl b/src/dbi_impl.jl index b96e9a2..ce6feab 100644 --- a/src/dbi_impl.jl +++ b/src/dbi_impl.jl @@ -90,14 +90,14 @@ end escapeliteral(db::PostgresDatabaseHandle, value) = value -function escapeliteral(db::PostgresDatabaseHandle, value::String) +function escapeliteral(db::PostgresDatabaseHandle, value::AbstractString) strptr = PQescapeLiteral(db.ptr, value, sizeof(value)) str = unsafe_string(strptr) PQfreemem(strptr) return str end -function escapeidentifier(db::PostgresDatabaseHandle, value::String) +function escapeidentifier(db::PostgresDatabaseHandle, value::AbstractString) strptr = PQescapeIdentifier(db.ptr, value, sizeof(value)) str = unsafe_string(strptr) PQfreemem(strptr) From 9cb502be5c00d09fb9849ceb7cfea5922b8bcaf3 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 16:12:19 +0200 Subject: [PATCH 16/21] Fixed convert function redefinition warning --- src/types.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/types.jl b/src/types.jl index f931e46..ce4960e 100644 --- a/src/types.jl +++ b/src/types.jl @@ -18,6 +18,7 @@ function newpgtype(pgtypename, oid, jltypes) Base.convert(::Type{PostgresType}, ::Type{OID{oid}}) = PostgresType{pgtypename} for t in jltypes + pgtypename in [:jsonb, :_text] && continue Base.convert(::Type{PostgresType}, ::Type{t}) = PostgresType{pgtypename} end end From 55aa2656039dc5b7811436b6b4b0809a3c594e37 Mon Sep 17 00:00:00 2001 From: Amgad Naiem Date: Wed, 12 Jul 2017 16:19:33 +0200 Subject: [PATCH 17/21] Fixed multiple type declarations --- src/types.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.jl b/src/types.jl index ce4960e..0d00bc5 100644 --- a/src/types.jl +++ b/src/types.jl @@ -34,7 +34,7 @@ newpgtype(:int2, 21, (Int16,)) newpgtype(:float8, 701, (Float64,)) newpgtype(:float4, 700, (Float32,)) newpgtype(:bpchar, 1042, ()) -newpgtype(:varchar, 1043, (String,String)) +newpgtype(:varchar, 1043, (String,)) newpgtype(:text, 25, ()) newpgtype(:numeric, 1700, (BigInt,BigFloat)) newpgtype(:date, 1082, ()) @@ -51,7 +51,7 @@ newpgtype(:_int4, 1007, (Vector{Int32},)) newpgtype(:_int2, 1005, (Vector{Int16},)) newpgtype(:_float8, 1022, (Vector{Float64},)) newpgtype(:_float4, 1021, (Vector{Float32},)) -newpgtype(:_varchar, 1015, (Vector{String}, Vector{String})) +newpgtype(:_varchar, 1015, (Vector{String},)) newpgtype(:_text, 1009, (Vector{String}, Vector{String})) From d73923cc0b9c0b13ae69b040e0d4ad674f84896e Mon Sep 17 00:00:00 2001 From: Amgad Date: Thu, 13 Jul 2017 01:03:59 +0200 Subject: [PATCH 18/21] fixed getting byte array from strings --- src/dbi_impl.jl | 2 +- src/types.jl | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/dbi_impl.jl b/src/dbi_impl.jl index ce6feab..3c641eb 100644 --- a/src/dbi_impl.jl +++ b/src/dbi_impl.jl @@ -129,7 +129,7 @@ function copy_from(db::PostgresDatabaseHandle, table::AbstractString, filename:: return checkerrclear(PQgetResult(db.ptr)) end -hashsql(sql::AbstractString) = unsafe_string(string("__", hash(sql), "__")) +hashsql(sql::AbstractString) = String(string("__", hash(sql), "__")) function getparamtypes(result::Ptr{PGresult}) nparams = PQnparams(result) diff --git a/src/types.jl b/src/types.jl index 0d00bc5..0784b52 100644 --- a/src/types.jl +++ b/src/types.jl @@ -153,24 +153,24 @@ function pgdata(::Type{PostgresType{:numeric}}, ptr::Ptr{UInt8}, data::Number) end function pgdata(::PGStringTypes, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, unsafe_string(data)) + ptr = storestring!(ptr, String(data)) end function pgdata(::PostgresType{:date}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, unsafe_string(data)) + ptr = storestring!(ptr, String(data)) ptr = Dates.DateFormat(ptr) end function pgdata(::PostgresType{:timestamp}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, unsafe_string(data)) + ptr = storestring!(ptr, String(data)) end function pgdata(::PostgresType{:timestamptz}, ptr::Ptr{UInt8}, data::AbstractString) - ptr = storestring!(ptr, unsafe_string(data)) + ptr = storestring!(ptr, String(data)) end function pgdata(::Type{PostgresType{:bytea}}, ptr::Ptr{UInt8}, data::Vector{UInt8}) - ptr = storestring!(ptr, unsafe_string("\\x", bytes2hex(data))) + ptr = storestring!(ptr, String("\\x", bytes2hex(data))) end function pgdata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}, data) @@ -178,11 +178,11 @@ function pgdata(::Type{PostgresType{:unknown}}, ptr::Ptr{UInt8}, data) end function pgdata{T<:AbstractString}(::Type{PostgresType{:json}}, ptr::Ptr{UInt8}, data::Dict{T,Any}) - ptr = storestring!(ptr, unsafe_string(JSON.json(data))) + ptr = storestring!(ptr, String(JSON.json(data))) end function pgdata{T<:AbstractString}(::Type{PostgresType{:jsonb}}, ptr::Ptr{UInt8}, data::Dict{T,Any}) - ptr = storestring!(ptr, unsafe_string(JSON.json(data))) + ptr = storestring!(ptr, String(JSON.json(data))) end function pgdata(::Type{PostgresType{:_bool}}, ptr::Ptr{UInt8}, data::Vector{Bool}) From 31cb7f157d6a0bbb0f396d9747d6cf298809c834 Mon Sep 17 00:00:00 2001 From: Amgad Date: Thu, 13 Jul 2017 08:57:57 +0200 Subject: [PATCH 19/21] fixed depreciated bytestring in test --- test/testutils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testutils.jl b/test/testutils.jl index 27fb02c..2058a5c 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -12,7 +12,7 @@ function testdberror(dbobj, expected) @test errcode(dbobj) == expected catch println(errstring(dbobj)) - println(bytestring(PostgreSQL.PQresStatus(expected))) + println(unsafe_string(PostgreSQL.PQresStatus(expected))) rethrow() end end From 5e528f6006076f4e4baabc9c0f77980a91b24361 Mon Sep 17 00:00:00 2001 From: Amgad Date: Thu, 13 Jul 2017 09:04:23 +0200 Subject: [PATCH 20/21] Fixed formatting --- src/dbi_impl.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dbi_impl.jl b/src/dbi_impl.jl index 290d940..3a95734 100644 --- a/src/dbi_impl.jl +++ b/src/dbi_impl.jl @@ -114,7 +114,8 @@ function checkcopyreturnval(db::PostgresDatabaseHandle, returnval::Int32) end end -function copy_from(db::PostgresDatabaseHandle, table::AbstractString, filename::AbstractString, format::AbstractString) +function copy_from(db::PostgresDatabaseHandle, table::AbstractString, + filename::AbstractString, format::AbstractString) f = open(filename) try Base.run(db, string("COPY ", escapeidentifier(db, table), " FROM STDIN ", format)) From 82441cda5ace6df401cc20a16dc7f724c4fc78fc Mon Sep 17 00:00:00 2001 From: Amgad Date: Thu, 13 Jul 2017 09:28:46 +0200 Subject: [PATCH 21/21] added abstact type for julia 0.6 (with compat support) --- src/types.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/types.jl b/src/types.jl index 94c1d6c..c8d5f3a 100644 --- a/src/types.jl +++ b/src/types.jl @@ -2,10 +2,10 @@ import DataArrays: NAtype import JSON import Compat: Libc, unsafe_convert, parse, @compat, String, unsafe_string -abstract AbstractPostgresType +@compat abstract type AbstractPostgresType end type PostgresType{Name} <: AbstractPostgresType end -abstract AbstractOID +@compat abstract type AbstractOID end type OID{N} <: AbstractOID end oid{T<:AbstractPostgresType}(t::Type{T}) = convert(OID, t) @@ -218,7 +218,7 @@ function pgdata(::Type{PostgresType{:_text}}, ptr::Ptr{UInt8}, data::Vector{Stri end # dbi -abstract Postgres <: DBI.DatabaseSystem +@compat abstract type Postgres<:DBI.DatabaseSystem end type PostgresDatabaseHandle <: DBI.DatabaseHandle ptr::Ptr{PGconn}