Skip to content

Commit

Permalink
adds connect
Browse files Browse the repository at this point in the history
  • Loading branch information
drizk1 committed Apr 25, 2024
1 parent ccd63ed commit 59d4dc0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/examples/UserGuide/getting_started.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@

# Alternatively, `using Tidier` will import TidierDB in the above manner for you, where TidierDB functions and macros will be available as `DB.@mutate()` and so on, and the TidierData equivalent would be `@mutate()`.

# There are two ways to connect to the database. you can use `connect` without any need to load any additional packages.

# for example

# ```julia
# Connect to MySQL
# conn = connect(:mysql; host="localhost", user="root", password="password", db="mydb")
# versus
# Connect to DuckDB
# julia> conn = connect(:duckdb)
# ```

# Alternatively, you can use the packages outlined below and establish a connection directly through their respective methods.

# The associated databased packages used to set up connections are currently as follows

# - ClickHouse: ClickHouse.jl
Expand Down
42 changes: 41 additions & 1 deletion src/TidierDB.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import DuckDB: connect as duckdb_connect
export db_table, set_sql_mode, @arrange, @group_by, @filter, @select, @mutate, @summarize, @summarise,
@distinct, @left_join, @right_join, @inner_join, @count, @window_order, @window_frame, @show_query, @collect, @slice_max,
@slice_min, @slice_sample, @rename, copy_to, add_interp_parameter!, duckdb_open, duckdb_connect, @semi_join, @full_join,
@anti_join
@anti_join, connect

include("docstrings.jl")
include("structs.jl")
Expand Down Expand Up @@ -289,4 +289,44 @@ function copy_to(conn, df_or_path::Union{DataFrame, AbstractString}, name::Strin
end
end


"""
$docstring_connect
"""
function connect(backend::Symbol; kwargs...)
if backend == :MySQL || backend == :mysql
# Required parameters by MySQL.jl: host and user
host = get(kwargs, :host, "localhost")
user = get(kwargs, :user, "")
password = get(kwargs, :password, "")
# Extract other optional parameters
db = get(kwargs, :db, nothing)
port = get(kwargs, :port, nothing)
return DBInterface.connect(MySQL.Connection, host, user, password; db=db, port=port)
elseif backend == :LibPQ || backend == :libpq
# Construct a connection string from kwargs for LibPQ
conn_str = join(["$(k)=$(v)" for (k, v) in kwargs], " ")
return LibPQ.Connection(conn_str)
elseif backend == :MsSQL || backend == :mssql
# Construct a connection string for ODBC if required for MsSQL
conn_str = join(["$(k)=$(v)" for (k, v) in kwargs], ";")
return ODBC.Connection(conn_str)
elseif backend == :Clickhouse || backend == :clickhouse
# Ensure host and port are specified for ClickHouse
if haskey(kwargs, :host) && haskey(kwargs, :port)
return ClickHouse.connect(kwargs[:host], kwargs[:port]; (k => v for (k, v) in kwargs if k [:host, :port])...)
else
throw(ArgumentError("Missing required positional arguments 'host' and 'port' for ClickHouse."))
end
elseif backend == :SQLite || backend == :lite
db_path = get(kwargs, :db, ":memory:") # Default to in-memory database if not specified
return SQLite.DB(db_path)
elseif backend == :DuckDB || backend == :duckdb
mem = DuckDB.open(":memory:")
return DuckDB.connect(mem)
else
throw(ArgumentError("Unsupported backend: $backend"))
end
end

end
35 changes: 35 additions & 0 deletions src/docstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -994,4 +994,39 @@ julia> db = duckdb_connect(mem);
julia> copy_to(db, df, "df_mem");
```
"""

const docstring_connect =
"""
connect(backend::Symbol; kwargs...)
This function establishes a database connection based on the specified backend and connection parameters.
# Arguments
- `backend`: A symbol specifying the database backend to connect to. Supported backends are:
- `:duckdb`, `:lite`(SQLite), `:mssql`, `mysql`(for MariaDB and MySQL), `:clickhouse`, `:postgres`
- `kwargs`: Keyword arguments specifying the connection parameters for the selected backend. The required parameters vary depending on the backend:
- MySQL:
- `host`: The host name or IP address of the MySQL server. Default is "localhost".
- `user`: The username for authentication. Default is an empty string.
- `password`: The password for authentication.
- `db`: The name of the database to connect to (optional).
- `port`: The port number of the MySQL server (optional).
# Returns
- A database connection object based on the selected backend.
# Examples
```julia
# Connect to MySQL
# conn = connect(:mysql; host="localhost", user="root", password="password", db="mydb")
# Connect to PostgreSQL using LibPQ
# conn = connect(:libpq; host="localhost", dbname="mydb", user="postgres", password="password")
# Connect to ClickHouse
# conn = connect(:clickhouse; host="localhost", port=9000, database="mydb", user="default", password="")
# Connect to SQLite
# conn = connect(:lite)
# Connect to DuckDB
julia> conn = connect(:duckdb)
DuckDB.Connection(":memory:")
"""

0 comments on commit 59d4dc0

Please sign in to comment.