Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up connections a bit #277

Merged
merged 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,15 @@ function SQLDriverConnect(dbc::Ptr{Cvoid},connstr)
# and subsequent library calls behave properly
c = transcode(sqlwcharsize(), connstr)
push!(c, sqlwcharsize()(0))
@odbc(:SQLDriverConnectW,
ret = @odbc(:SQLDriverConnectW,
(Ptr{Cvoid},Ptr{Cvoid},Ptr{SQLWCHAR},SQLSMALLINT,Ptr{SQLCHAR},SQLSMALLINT,Ptr{SQLSMALLINT},SQLUSMALLINT),
dbc,C_NULL,c,SQL_NTS,C_NULL,0,C_NULL,SQL_DRIVER_NOPROMPT)
if ret == SQL_ERROR
ret = @odbc(:SQLDriverConnect,
(Ptr{Cvoid},Ptr{Cvoid},Ptr{SQLCHAR},SQLSMALLINT,Ptr{SQLCHAR},SQLSMALLINT,Ptr{SQLSMALLINT},SQLUSMALLINT),
dbc,C_NULL,connstr,SQL_NTS,C_NULL,0,C_NULL,SQL_DRIVER_NOPROMPT)
end
return ret
end

function driverconnect(connstr)
Expand Down
11 changes: 5 additions & 6 deletions src/dbinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ function clear!(conn::Connection)
end

"""
ODBC.Connection(dsn_or_connectionstring, user, password; connectionstring::Bool=false)
ODBC.Connection(dsn_or_connectionstring, user, password)

Construct a `Connection` type by connecting to a valid ODBC Connection or by specifying a datasource name or valid connection string.
Takes optional 2nd and 3rd arguments for named datasources `username` and `password`, respectively.
1st argument `dsn` can be either the name of a pre-defined ODBC Connection or a valid connection string.
If passing a connection string, the `connectionstring=true` keyword argument must also be passed.
The `user` and `pwd` arguments are ignored if `connectionstring=true`.
The `user` and `pwd` arguments are ignored if the first argument is a connection string.
A great resource for building valid connection strings is [http://www.connectionstrings.com/](http://www.connectionstrings.com/).
"""
function Connection(dsn::AbstractString, usr=nothing, pwd=nothing; connectionstring::Bool=false)
function Connection(dsn::AbstractString, usr=nothing, pwd=nothing)
connectionstring = occursin('=', dsn)
return Connection(connectionstring ? API.driverconnect(dsn) : API.connect(dsn, usr, pwd), dsn, nothing)
end

Expand All @@ -35,8 +35,7 @@ end
Construct a `Connection` type by connecting to a valid ODBC Connection or by specifying a datasource name or valid connection string.
Takes optional 2nd and 3rd arguments for named datasources `username` and `password`, respectively.
1st argument `dsn` can be either the name of a pre-defined ODBC Connection or a valid connection string.
If passing a connection string, the `connectionstring=true` keyword argument must also be passed.
The `user` and `pwd` arguments are ignored if `connectionstring=true`.
The `user` and `pwd` arguments are ignored if the first argument is a connection string.
A great resource for building valid connection strings is [http://www.connectionstrings.com/](http://www.connectionstrings.com/).
"""
DBInterface.connect(::Type{Connection}, args...; kw...) = Connection(args...; kw...)
Expand Down
Empty file added src/load.jl
Empty file.
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ else
libpath = MariaDB_Connector_ODBC_jll.libmaodbc_path
end
ODBC.adddriver("ODBC_Test_MariaDB", libpath)
ODBC.adddsn("ODBC_Test_DSN_MariaDB", "ODBC_Test_MariaDB"; SERVER="localhost", PLUGIN_DIR=PLUGIN_DIR, Option=67108864, CHARSET="utf8mb4")
ODBC.adddsn("ODBC_Test_DSN_MariaDB", "ODBC_Test_MariaDB"; SERVER="localhost", UID="root", PLUGIN_DIR=PLUGIN_DIR, Option=67108864, CHARSET="utf8mb4")

conn = DBInterface.connect(ODBC.Connection, "ODBC_Test_DSN_MariaDB", "root")
conn = DBInterface.connect(ODBC.Connection, "ODBC_Test_DSN_MariaDB")
DBInterface.close!(conn)
conn = DBInterface.connect(ODBC.Connection, "ODBC_Test_DSN_MariaDB", "root")
conn = DBInterface.connect(ODBC.Connection, "Driver={ODBC_Test_MariaDB};SERVER=localhost;PLUGIN_DIR=$PLUGIN_DIR;Option=67108864;CHARSET=utf8mb4;USER=root")

DBInterface.execute(conn, "DROP DATABASE if exists mysqltest")
DBInterface.execute(conn, "CREATE DATABASE mysqltest")
Expand Down