Skip to content

Commit

Permalink
kwargs for mysql and postgresql
Browse files Browse the repository at this point in the history
Almost the secrets issue is because the PR has no access to the environment secrets of the base repo
  • Loading branch information
Farreeda committed Aug 24, 2023
1 parent 666bc10 commit 503bbe9
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 60 deletions.
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DBConnector
using Documenter

makedocs(;
Expand Down
25 changes: 18 additions & 7 deletions docs/src/Tutorials/postgreSQL.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ Get your database's :

1- username
2- password
3- host
5- Database name

optional:
- host
- Database name
- port (5432 by default)
- unix_socket
- client_flag
- opts=opts

Note: If you have additional keys that are necessary to be added, Jump to Method 2:

## Environment Set-Up

Expand Down Expand Up @@ -50,22 +49,34 @@ TUTORIAL> add DBConnector
```
using DBConnector
conn= _dbconnect(LibPQ.Connection, host, user, password, db=db)
conn= _dbconnect(LibPQ.Connection; host = host, user = user, password = password, db=db)
```
In case you want to use the optional strings:

```
using DBConnector
conn= _dbconnect(LibPQ.Connection, host, user, password, db="the database name", port = 5432, unix_socket=unix_socket, client_flag=client_flag, opts=opts )
conn= _dbconnect(LibPQ.Connection; host = host, user = user, password = password, db="the database name", port = 5432)
```



Now you are connected!

Note: It produces error only in case the path is incorrect credentials

# Method 2

create your own connection string as this example:

```
connection_string = "postgresql://username:password@unix:/path/to/socket/directory/database_name"
conn= _dbconnect(LibPQ.Connection, connection_string)
```

### Packages Used in Analysis

Package descriptions:
Expand Down
87 changes: 83 additions & 4 deletions src/mysql.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,87 @@
function _dbconnect(conn_obj::Type{MySQL.Connection}, host::String, user::String, password::String; db::String="", port::Integer=3306, unix_socket::Union{Nothing,String}=nothing, client_flag=API.CLIENT_MULTI_STATEMENTS, opts = Dict())
function _dbconnect(conn_obj::Type{MySQL.Connection}; kwargs...)

if unix_socket == nothing
unix_socket = API.MYSQL_DEFAULT_SOCKET

if haskey(kwargs, :host) && haskey(kwargs, :user)
host = kwargs[:host]
kwargs = filter(kvp -> first(kvp) != :host, kwargs)
user = kwargs[:user]
kwargs = filter(kvp -> first(kvp) != :user, kwargs)
if isempty(kwargs)
return DBInterface.connect(conn_obj,host, user)
elseif haskey(kwargs, :password)
password=kwargs[:password]
kwargs = filter(kvp -> first(kvp) != :password, kwargs)
if isempty(kwargs)
return DBInterface.connect(conn_obj,host, user, password)
elseif haskey(kwargs, :db)
db = kwargs[:db]
kwargs = filter(kvp -> first(kvp) != :db, kwargs)
if isempty(kwargs)
return DBInterface.connect(conn_obj,host, user, password; kwargs)
elseif haskey(kwargs, :port)
db = kwargs[:port]
kwargs = filter(kvp -> first(kvp) != :port, kwargs)
if isempty(kwargs)
return DBInterface.connect(conn_obj,host, user; kwargs)
end
end
end
elseif haskey(kwargs, :db)
db = kwargs[:db]
kwargs = filter(kvp -> first(kvp) != :db, kwargs)
if isempty(kwargs)
return DBInterface.connect(conn_obj,host, user; kwargs)
elseif haskey(kwargs, :port)
db = kwargs[:port]
kwargs = filter(kvp -> first(kvp) != :port, kwargs)
if isempty(kwargs)
return DBInterface.connect(conn_obj,host, user; kwargs)
end
end
end

else
error("Invalid arguments, make sure that keyword user and host exists")
end
end

# if !haskey(kwargs, "unix_socket")
# unix_socket = API.MYSQL_DEFAULT_SOCKET
# kwargs.push(unix_socket)
#end
"""
if haskey(kwargs, :host)
host = kwargs[:host]
kwargs = filter(kvp -> first(kvp) != :host, kwargs)
if haskey(kwargs, :user)
user = kwargs[:user]
kwargs = filter(kvp -> first(kvp) != :user, kwargs)
elseif haskey(kwargs, :username)
user = kwargs[:username]
kwargs = filter(kvp -> first(kvp) != :username, kwargs)
else
error("Invalid arguments, make sure that keyword user exists")
end
else
error("Invalid arguments, make sure that keyword host exists")
end
if haskey(kwargs, :password)
password=kwargs[:password]
kwargs = filter(kvp -> first(kvp) != :password, kwargs)
elseif isempty(kwargs)
return DBInterface.connect(conn_obj,host, user)
else
return DBInterface.connect(conn_obj,host, user; kwargs)
end
if isempty(kwargs)
return DBInterface.connect(conn_obj,host, user, password)
else
return DBInterface.connect(conn_obj,host, user, password; kwargs)
return DBInterface.connect(conn_obj,host, user, password, db=db, port=port, unix_socket=unix_socket, client_flag=client_flag, opts=opts )
end
return DBInterface.connect(conn_obj; kwargs...)
#return DBInterface.connect(conn_obj,host, user, password, db=db, port=port, unix_socket=unix_socket, client_flag=client_flag, opts=opts )
end
"""
134 changes: 86 additions & 48 deletions src/postgresql.jl
Original file line number Diff line number Diff line change
@@ -1,74 +1,112 @@
"""
Workaround for LibPQ interface to DBInterface's `prepare` function; not supported in LibPQ.jl package currently
"""
DBInterface.prepare(conn::LibPQ.Connection, args...; kws...) =
LibPQ.prepare(conn, args...; kws...)


"""
Dispatch for LibPQ interface to DBInterface `connect` function; not supported in LibPQ.jl package currently
"""
DBInterface.connect(::Type{LibPQ.Connection}, args...; kws...) =
LibPQ.Connection(args...; kws...)

"""
This LibPQ connection function creates a connection string using
1- username (user)
2- hostname (host)
3- password
4- database (db)
5- port
Cases handles:
1- user and host
2- user, host, db
3- user, host, password
4- user, host, password, db
5- user, host, password, db, port
6- user, host, db, port
adding other keywords can't be handled yet
"""

function _dbconnect(conn_obj::Type{LibPQ.Connection}; kwargs...)
conn_string = ""
for k in kwargs
conn_string = conn_string * "$(string(k.first))=$(k.second) "
end

conn_string = strip(conn_string)

return DBInterface.connect(conn_obj, conn_string)
if haskey(kwargs, :host) && haskey(kwargs, :user)
host = kwargs[:host]
kwargs = filter(kvp -> first(kvp) != :host, kwargs)
user = kwargs[:user]
kwargs = filter(kvp -> first(kvp) != :user, kwargs)
if isempty(kwargs)
conn_string = "postgresql://$user@$host"
return DBInterface.connect(conn_obj, conn_string)
elseif haskey(kwargs, :password)
password=kwargs[:password]
kwargs = filter(kvp -> first(kvp) != :password, kwargs)
if isempty(kwargs)
conn_string = "postgresql://$user:$password@$host"
return DBInterface.connect(conn_obj, conn_string)
elseif haskey(kwargs, :db)
db = kwargs[:db]
kwargs = filter(kvp -> first(kvp) != :db, kwargs)
if isempty(kwargs)
conn_string = "postgresql://$(user):$(password)@$(host)/$(db)?user=$(user)"
return DBInterface.connect(conn_obj, conn_string)
elseif haskey(kwargs, :port)
db = kwargs[:port]
kwargs = filter(kvp -> first(kvp) != :port, kwargs)
if isempty(kwargs)
conn_string = "postgresql://$user:$password@$host:$port/$db"
return DBInterface.connect(conn_obj, conn_string)
end
end
end
elseif haskey(kwargs, :db)
db = kwargs[:db]
kwargs = filter(kvp -> first(kvp) != :db, kwargs)
if isempty(kwargs)
conn_string = "postgresql://$user@$host/$db"
return DBInterface.connect(conn_obj, conn_string)
elseif haskey(kwargs, :port)
db = kwargs[:port]
kwargs = filter(kvp -> first(kvp) != :port, kwargs)
if isempty(kwargs)
conn_string = "postgresql://$user@$host:$port/$db"
return DBInterface.connect(conn_obj, conn_string)
end
end

else
error("Invalid arguments, make sure that keyword user and host exist")
end
end

end

function _dbconnect(conn_obj::Type{LibPQ.Connection}, conn_string :: String)

return DBInterface.connect(conn_obj, conn_string)

end
"""
Workaround for LibPQ interface to DBInterface's `execute` function; not supported in LibPQ.jl package currently
"""
DBInterface.execute(conn::Union{LibPQ.Connection, LibPQ.Statement}, args...; kws...) =
LibPQ.execute(conn, args...; kws...)

function _dbconnect(conn_obj::Type{LibPQ.Connection}, host::String, user::String, password::String, db::String; port::Integer=5432)

conn_string = "postgresql://$(user):$(password)@$(host)/$(db)?user=$(user)"

function _dbconnect(conn_obj::Type{LibPQ.Connection}, conn_string :: String)

return DBInterface.connect(conn_obj, conn_string)

end
#=
function _dbconnect(conn_obj::Type{LibPQ.Connection}; host::String, user::String, password::String; db::String="", port::Integer=5432, unix_socket::Union{Nothing,String}=nothing, client_flag=API.CLIENT_MULTI_STATEMENTS, opts = Dict())
"""
BUG DESCRIPTION:
As of now, this function does not work. the conn_string is not built correctly and you'd probably need to build it manually with string interpolations.
What this function would do is construct a connection string that looks like:
"host = /var/run/postgresql user = user password = password dbname = mimiciii"
However, the problem with the postgresql connection string is that the keywords in the string are actually positionally dependent. You can't just put them in any order.
"""
"""
function _dbconnect(conn_obj::Type{LibPQ.Connection}; kwargs...)
conn_string = ""
for k in kws
conn_string = conn_string * "$(string(k.first))=$(k.second) "
for k in kwargs
conn_string = conn_string * "(string(k.first))=(k.second) "
end
conn_string = strip(conn_string)
return DBInterface.connect(conn_obj, conn_string)
end
=#

"""
Workaround for LibPQ interface to DBInterface's `prepare` function; not supported in LibPQ.jl package currently
"""
DBInterface.prepare(conn::LibPQ.Connection, args...; kws...) =
LibPQ.prepare(conn, args...; kws...)

"""
Workaround for LibPQ interface to DBInterface's `execute` function; not supported in LibPQ.jl package currently
"""
DBInterface.execute(conn::Union{LibPQ.Connection, LibPQ.Statement}, args...; kws...) =
LibPQ.execute(conn, args...; kws...)


"""
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ end

@testset "_dbconnect function for LibPQ" begin

conn= DBConnector._dbconnect(LibPQ.Connection, ENV["POSTGRES_HOST"],ENV["POSTGRES_USER"], ENV["POSTGRES_PASSWORD"], "omop")
conn= DBConnector._dbconnect(LibPQ.Connection, host = ENV["POSTGRES_HOST"],user = ENV["POSTGRES_USER"], password = ENV["POSTGRES_PASSWORD"], db = "omop")

@test @isdefined conn

end
Expand Down

0 comments on commit 503bbe9

Please sign in to comment.