diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e978134..f38aa2c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,10 +46,37 @@ jobs: ${{ runner.os }}- - uses: julia-actions/julia-buildpkg@v1 - uses: julia-actions/julia-runtest@v1 + - uses: julia-actions/julia-processcoverage@v1 - uses: codecov/codecov-action@v1 with: file: lcov.info + job1: + ## The type of runner that the job will run on, + ## here it runs on ubuntu latest + runs-on: ubuntu-latest + steps: + - name: step 1 + ## Reference your environment variables + run: echo "The mysqlport is:${{secrets.MYSQL_PORT}}mysqluser:${{secrets.MYSQL_USER}} + mysqlhost:${{secrets.MYSQL_HOST}} + mysqlpassword:${{secrets.MYSQL_PASSWORD}} + postgresuser:${{secrets.POSTGRES_USER}} + postgreshost:${{secrets.POSTGRES_HOST}} + postgresport:${{secrets.POSTGRES_PORT}} + postgrespassword:${{secrets.POSTGRES_PASSWORD}}" + job2: + runs-on: ubuntu-latest + steps: + - name: step 1 + ## Another way reference your environment variables + run: echo "The mysqlport is:${{secrets.MYSQL_PORT}}mysqluser:${{secrets.MYSQL_USER}} + mysqlhost:${{secrets.MYSQL_HOST}} + mysqlpassword:${{secrets.MYSQL_PASSWORD}} + postgresuser:${{secrets.POSTGRES_USER}} + postgreshost:${{secrets.POSTGRES_HOST}} + postgresport:${{secrets.POSTGRES_PORT}} + postgrespassword:${{secrets.POSTGRES_PASSWORD}}" docs: name: Documentation runs-on: ubuntu-latest @@ -60,3 +87,4 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} + \ No newline at end of file diff --git a/.gitignore b/.gitignore deleted file mode 100644 index b48b360..0000000 --- a/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -*.jl.cov -*.jl.*.cov -*.jl.mem -.DS_Store -/docs/build/ -/docs/Manifest.toml -/Manifest.toml \ No newline at end of file diff --git a/docs/make.jl b/docs/make.jl index fd29b00..3c7736b 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,5 +1,23 @@ using Documenter, Example -makedocs(modules = [Example], sitename = "Example.jl") +makedocs(; + modules = [DBConnector], + authors = "Jacob Zelko (aka TheCedarPrince) and Fareeda", + repo = "https://github.com/JuliaDatabases/DBConnector.jl", + sitename = "DBConnector.jl", + format = Documenter.HTML(; + prettyurls = get(ENV, "CI", "false") == "true", + canonical = "https://github.com/JuliaDatabases/DBConnector.jl" + edit_link = "dev", + footer = "Created by [Jacob Zelko](https://jacobzelko.com) & [Georgia Tech Research Institute](https://www.gtri.gatech.edu). [License](https://github.com/JuliaHealth/OMOPCDMCohortCreator.jl/blob/main/LICENSE)" + ), + pages = [ + "Home" => "index.md", + "Functions" => "Functions.md" + ], -deploydocs(repo = "github.com/quinnj/Example.jl.git", push_preview = true) +) +deploydocs(repo = "github.com/quinnj/Example.jl.git", + push_preview = true, + + ) diff --git a/docs/src/Functions.md b/docs/src/Functions.md new file mode 100644 index 0000000..19bebe4 --- /dev/null +++ b/docs/src/Functions.md @@ -0,0 +1,54 @@ +# Functions + +```@contents +Pages = ["Functions.md"] +``` + +## _dbconnect() + +This Function is dedicated to expand the range of connection between The [OMOPCDMCohortCreator](https://github.com/JuliaHealth/OMOPCDMCohortCreator.jl) with databases. +Databases exist are SQLite, MySQL, PostgreSQL. +In the upcoming sections, we will learn about these tools. + +### SQLite + +Assuming your data exists on a SQLite database and you want to connect it to the OMOPCDMCohortCreator. + +calling the function + +``` _dbconnect(SQLite.Connection,"path/to/database.db") ``` + + + +### MySQL + +Assuming your data exists on a MySQL database and you want to connect it to the OMOPCDMCohortCreator. + +Make sure that the server is running and using `MySQL.API`: + +``` systemctl status mysql ``` + +calling the function + +``` _dbconnect(MySQL.Connection, "host_name", "username", "password"; db = "database_name" , port = port_num, unix_socket = "unix_socket_data_path" ) ``` + +those parameters must exist: + +- host_name : if you are working on same machine server it will be "local host" + +- username +- password + +Can be known by openning all the `.cnf` files until you find them. I wasn't lucky and opened around 5 till I found the right one. Don't give up! + +If unix_socket is not inserted, the default one is given + + + +For any struggles MySQL related, [Read this](https://dev.mysql.com/doc/refman/8.0/en/starting-server.html) + + + + + + diff --git a/docs/src/index.md b/docs/src/index.md index b1cee35..5d6f4db 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -5,3 +5,6 @@ Example Julia package repo. ```@autodocs Modules = [Example] ``` + + + diff --git a/src/jdbc.jl b/src/jdbc.jl index 1627904..51b6eb8 100644 --- a/src/jdbc.jl +++ b/src/jdbc.jl @@ -8,23 +8,33 @@ function DBInterface.connect(::Type{JDBC.Connection}, args...; connection_string catch println("JVM already initialized") end - JDBC.Connection(connection_string) + JDBC.DriverManager.getConnection(connection_string) end """ -Dispatch for LibPQ interface to DBInterface `prepare` function -TODO: Not fully implemented yet +Dispatch for JDBC interface to DBInterface `prepare` function +BUG: Doesn't seem to work for all JDBC versions yet """ -# DBInterface.prepare(conn::JDBC.Connection, args...; kws...) = -# JDBC.prepareStatement(conn, args...; kws...) +function DBInterface.prepare(conn::JDBC.JavaObject{Symbol("java.sql.Connection")}, args...; kws...) + stmt = JDBC.createStatement(conn) + result = executeQuery(stmt, args...) + return result +end + +""" +Workaround for JDBC interface to DBInterface's `execute` function +""" +function DBInterface.execute(conn::JDBC.JavaObject{Symbol("java.sql.ResultSet")}, args...; kws...) + JDBC.Source(conn) +end """ -Workaround for LibPQ interface to DBInterface's `execute` function +Workaround for JDBC interface to DBInterface's `execute` function """ -function DBInterface.execute(conn::Union{JDBC.Connection, JDBC.JPreparedStatement}, args...; kws...) - csr = JDBC.cursor(conn) - JDBC.execute!(csr, args..., kws...) - JDBC.Source(csr) +function DBInterface.execute(conn::JDBC.JavaObject{Symbol("java.sql.Connection")}, args...; kws...) + stmt = JDBC.createStatement(conn) + result = executeQuery(stmt, args...) + JDBC.Source(result) end diff --git a/src/mysql.jl b/src/mysql.jl index 151c87e..a477106 100644 --- a/src/mysql.jl +++ b/src/mysql.jl @@ -1,5 +1,10 @@ -function _dbconnect(conn_obj::Type{MySQL.Connection}, kwargs) - return DBInterface.connect(conn_obj; kwargs...) +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()) + if unix_socket == nothing + unix_socket = API.MYSQL_DEFAULT_SOCKET + end + + return DBInterface.connect(conn_obj,host, user, password, db=db, port=port, unix_socket=unix_socket, client_flag=client_flag, opts=opts ) end + diff --git a/src/postgresql.jl b/src/postgresql.jl index 7eb6293..d02d1b5 100644 --- a/src/postgresql.jl +++ b/src/postgresql.jl @@ -1,10 +1,12 @@ """ 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...) -function _dbconnect(conn_obj::Type{LibPQ.Connection}; kwargs...) +function _dbconnect(conn_obj::Type{LibPQ.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()) + conn_string = "" for k in kwargs conn_string = conn_string * "$(string(k.first))=$(k.second) " @@ -16,6 +18,14 @@ function _dbconnect(conn_obj::Type{LibPQ.Connection}; kwargs...) end +function _dbconnect(conn_obj::Type{LibPQ.Connection}, host::String, user::String, password::String; db::String="", port::Integer=3306, unix_socket::Union{Nothing,String}=nothing, opts = Dict()) + #conn = LibPQ.Connection("postgresql://postgres:postgres3@localhost:5432/mimic?user=postgres") + + return DBInterface.connect(conn_obj, "postgresql://$user:$password@$host:$port/$db?user=$user") + +end + + """ Workaround for LibPQ interface to DBInterface's `prepare` function; not supported in LibPQ.jl package currently """ @@ -27,5 +37,3 @@ Workaround for LibPQ interface to DBInterface's `execute` function; not supporte """ DBInterface.execute(conn::Union{LibPQ.Connection, LibPQ.Statement}, args...; kws...) = LibPQ.execute(conn, args...; kws...) - - diff --git a/src/sqlite.jl b/src/sqlite.jl index a35d5ad..db31dd4 100644 --- a/src/sqlite.jl +++ b/src/sqlite.jl @@ -1,5 +1,5 @@ -function _dbconnect(connector::Type{SQLite.DB}; file_path) +function _dbconnect(connector::Type{SQLite.DB}, file_path::String) - return connector(file_path.second) + return connector(file_path) end diff --git a/test/data/sqlite.db b/test/data/sqlite.db new file mode 100644 index 0000000..e69de29 diff --git a/test/runtests.jl b/test/runtests.jl index 35f99ea..ed658bb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,30 @@ -using Test, Example +using Test +using MySQL,SQLite, MySQL.API -@test hello("Julia") == "Hello, Julia" -@test domath(2.0) ≈ 7.0 + + + +@testset "_dbconnect function for SQLite" begin + + conn= _dbconnect(SQLite.DB, "DBConnector.jl/test/data/sqlite.db") + @test @isdefined conn + +end + """ +@testset "_dbconnect function for MySQL" begin + + conn = _dbconnect(MySQL.Connection, ENV['mysqlhost'], ENV['mysqluser'], ENV['mysqlpassword'], db="MySQL", port= 3306) + + @test typeof(conn) == MySQL.Connection + @test isopen(conn) + close(conn) + +end + +@testset "_dbconnect function for LibPQ" begin + + conn= _dbconnect(LibPQ.Connection, ENV['postgreshost'], ENV['postgresuser'], ENV['postgrespassword'], db = "mimic", port= 5432) + @test @isdefined conn + +end +""" \ No newline at end of file