Skip to content

Commit

Permalink
Fixing allowing Named Instances, adding it as an option (#13)
Browse files Browse the repository at this point in the history
* adding a docker instance that tests mssqlex with custom ports

* don't forget to build custom port container

* allow for named instances + ports with new opts. tests for both

* simplify and fix travis docker tests

* oops, wrong port number

* updating docs to reflect options changes
  • Loading branch information
Steven Blowers committed Jul 21, 2017
1 parent b8e4ffa commit 3b8c0d3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ services:
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=TestPa$$word123
- MSSQL_TCP_PORT=9204

mssqlex:
build: .
environment:
- MIX_ENV=test
- MSSQL_UID=sa
- MSSQL_PWD=TestPa$$word123
- MSSQL_HST=tcp:sql_server,1433
- MSSQL_HST=sql_server
- MSSQL_IN=MSSQLSERVER
- MSSQL_PRT=9204
- TRAVIS_JOB_ID=$TRAVIS_JOB_ID
depends_on:
- sql_server
command: ["./wait-for-it.sh", "sql_server:1433", "-s", "--", "mix", "coveralls.travis"]
command: ["./wait-for-it.sh", "sql_server:9204", "-s", "--", "mix", "coveralls.travis"]
24 changes: 16 additions & 8 deletions lib/mssqlex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ defmodule Mssqlex do
`opts` expects a keyword list with zero or more of:
* `:odbc_driver` - The driver the adapter will use
(default: {ODBC Driver 13 for SQL Server})
* `:hostname` - The server hostname (default: localhost)
* `:port` - The server port number (default: 1433)
* `:database` - The name of the database
(default: MSSQL_DB environment variable)
* `:username` - Username (default: MSSQL_UID environment variable)
* `:password` - User password (default: MSSQL_PWD environment variable)
* `:odbc_driver` - The driver the adapter will use.
* environment variable: `MSSQL_DVR`
* default value: {ODBC Driver 13 for SQL Server}
* `:hostname` - The server hostname.
* environment variable: `MSSQL_HST`
* default value: localhost
* `:instance_name` - OPTIONAL. The name of the instance, if using named instances.
* environment variable: `MSSQL_IN`
* `:port` - OPTIONAL. The server port number.
* environment variable: `MSSQL_PRT`
* `:database` - The name of the database.
* environment variable: `MSSQL_DB`
* `:username` - Username.
* environment variable: `MSSQL_UID`
* `:password` - User's password.
* environment variable: `MSSQL_PWD`
`Mssqlex` uses the `DBConnection` framework and supports all `DBConnection`
options like `:idle`, `:after_connect` etc.
Expand Down
19 changes: 16 additions & 3 deletions lib/mssqlex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ defmodule Mssqlex.Protocol do
@spec connect(opts :: Keyword.t) :: {:ok, state}
| {:error, Exception.t}
def connect(opts) do

server_address = opts[:hostname] || System.get_env("MSSQL_HST") || "localhost"
instance_name = opts[:instance_name] || System.get_env("MSSQL_IN")
port = opts[:port] || System.get_env("MSSQL_PRT")

conn_opts = [
{"DRIVER", opts[:odbc_driver] || "{ODBC Driver 13 for SQL Server}"},
{"SERVER", (opts[:hostname] || System.get_env("MSSQL_HST") || "localhost") <> "," <> (opts[:port] || "1433")},
{"DATABASE", opts[:database] || System.get_env("MSSQL_DB")},
{"Driver", opts[:odbc_driver] || System.get_env("MSSQL_DVR") || "{ODBC Driver 13 for SQL Server}"},
{"Server", build_server_address(server_address, instance_name, port)},
{"Database", opts[:database] || System.get_env("MSSQL_DB")},
{"UID", opts[:username] || System.get_env("MSSQL_UID")},
{"PWD", opts[:password] || System.get_env("MSSQL_PWD")}
]
Expand All @@ -62,6 +67,14 @@ defmodule Mssqlex.Protocol do
end
end

@spec build_server_address(String.t, String.t, String.t) :: String.t
defp build_server_address(server_address, instance_name, port)

defp build_server_address(server_address, nil, nil), do: server_address
defp build_server_address(server_address, instance_name, nil), do: "#{server_address}\\#{instance_name}"
defp build_server_address(server_address, nil, port), do: "#{server_address},#{port}"
defp build_server_address(server_address, instance_name, port), do: "#{server_address}\\#{instance_name},#{port}"

@doc false
@spec disconnect(err :: Exception.t, state) :: :ok
def disconnect(_err, %{pid: pid} = state) do
Expand Down

0 comments on commit 3b8c0d3

Please sign in to comment.