diff --git a/docs/make.jl b/docs/make.jl index f8f8f8b..38198fe 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,3 +1,4 @@ +using DBConnector using Documenter makedocs(; diff --git a/docs/src/Tutorials/postgreSQL.md b/docs/src/Tutorials/postgreSQL.md index 0dfd19f..c427fae 100644 --- a/docs/src/Tutorials/postgreSQL.md +++ b/docs/src/Tutorials/postgreSQL.md @@ -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 @@ -50,7 +49,7 @@ 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: @@ -58,14 +57,26 @@ 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: diff --git a/src/mysql.jl b/src/mysql.jl index 0075238..70a0e17 100644 --- a/src/mysql.jl +++ b/src/mysql.jl @@ -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 +""" \ No newline at end of file diff --git a/src/postgresql.jl b/src/postgresql.jl index a856133..9be12e3 100644 --- a/src/postgresql.jl +++ b/src/postgresql.jl @@ -1,54 +1,107 @@ +""" +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) @@ -56,19 +109,4 @@ function _dbconnect(conn_obj::Type{LibPQ.Connection}; host::String, user::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...) - - +""" \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index afe68d0..8795fc0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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