Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
drowzy authored Dec 1, 2017
2 parents b1991bc + 25ffb8a commit 6f548a1
Show file tree
Hide file tree
Showing 19 changed files with 532 additions and 203 deletions.
146 changes: 146 additions & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# This file contains the configuration for Credo and you are probably reading
# this after creating it with `mix credo.gen.config`.
#
# If you find anything wrong or unclear in this file, please report an
# issue on GitHub: https://github.com/rrrene/credo/issues
#
%{
#
# You can have as many configs as you like in the `configs:` field.
configs: [
%{
#
# Run any exec using `mix credo -C <name>`. If no exec name is given
# "default" is used.
#
name: "default",
#
# These are the files included in the analysis:
files: %{
#
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
#
included: ["lib/", "src/", "web/", "apps/"],
excluded: [~r"/_build/", ~r"/deps/"]
},
#
# If you create your own checks, you must specify the source files for
# them here, so they can be loaded by Credo before running the analysis.
#
requires: [],
#
# If you want to enforce a style guide and need a more traditional linting
# experience, you can change `strict` to `true` below:
#
strict: true,
#
# If you want to use uncolored output by default, you can change `color`
# to `false` below:
#
color: true,
#
# You can customize the parameters of any check by adding a second element
# to the tuple.
#
# To disable a check put `false` as second element:
#
# {Credo.Check.Design.DuplicatedCode, false}
#
checks: [
{Credo.Check.Consistency.ExceptionNames},
{Credo.Check.Consistency.LineEndings},
{Credo.Check.Consistency.ParameterPatternMatching},
{Credo.Check.Consistency.SpaceAroundOperators},
{Credo.Check.Consistency.SpaceInParentheses},
{Credo.Check.Consistency.TabsOrSpaces},

# You can customize the priority of any check
# Priority values are: `low, normal, high, higher`
#
{Credo.Check.Design.AliasUsage, priority: :low},

# For some checks, you can also set other parameters
#
# If you don't want the `setup` and `test` macro calls in ExUnit tests
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
#
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},

# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
#
{Credo.Check.Design.TagTODO, exit_status: 2},
{Credo.Check.Design.TagFIXME},

{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
{Credo.Check.Readability.ModuleAttributeNames},
{Credo.Check.Readability.ModuleDoc},
{Credo.Check.Readability.ModuleNames},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs},
{Credo.Check.Readability.ParenthesesInCondition},
{Credo.Check.Readability.PredicateFunctionNames},
{Credo.Check.Readability.PreferImplicitTry},
{Credo.Check.Readability.RedundantBlankLines},
{Credo.Check.Readability.StringSigils},
{Credo.Check.Readability.TrailingBlankLine},
{Credo.Check.Readability.TrailingWhiteSpace},
{Credo.Check.Readability.VariableNames},
{Credo.Check.Readability.Semicolons},
{Credo.Check.Readability.SpaceAfterCommas},

{Credo.Check.Refactor.DoubleBooleanNegation},
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.FunctionArity},
{Credo.Check.Refactor.LongQuoteBlocks},
{Credo.Check.Refactor.MatchInCondition},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.PipeChainStart},
{Credo.Check.Refactor.UnlessWithElse},

{Credo.Check.Warning.BoolOperationOnSameValues},
{Credo.Check.Warning.ExpensiveEmptyEnumCheck},
{Credo.Check.Warning.IExPry},
{Credo.Check.Warning.IoInspect},
{Credo.Check.Warning.LazyLogging},
{Credo.Check.Warning.OperationOnSameValues},
{Credo.Check.Warning.OperationWithConstantResult},
{Credo.Check.Warning.UnusedEnumOperation},
{Credo.Check.Warning.UnusedFileOperation},
{Credo.Check.Warning.UnusedKeywordOperation},
{Credo.Check.Warning.UnusedListOperation},
{Credo.Check.Warning.UnusedPathOperation},
{Credo.Check.Warning.UnusedRegexOperation},
{Credo.Check.Warning.UnusedStringOperation},
{Credo.Check.Warning.UnusedTupleOperation},
{Credo.Check.Warning.RaiseInsideRescue},

# Controversial and experimental checks (opt-in, just remove `, false`)
#
{Credo.Check.Refactor.ABCSize, false},
{Credo.Check.Refactor.AppendSingleItem, false},
{Credo.Check.Refactor.VariableRebinding, false},
{Credo.Check.Warning.MapGetUnsafePass, false},
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},

# Deprecated checks (these will be deleted after a grace period)
#
{Credo.Check.Readability.Specs, false},
{Credo.Check.Warning.NameRedeclarationByAssignment, false},
{Credo.Check.Warning.NameRedeclarationByCase, false},
{Credo.Check.Warning.NameRedeclarationByDef, false},
{Credo.Check.Warning.NameRedeclarationByFn, false},

# Custom checks can be created using `mix credo.gen.check`.
#
]
}
]
}
7 changes: 7 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
inputs: [
"mix.exs",
"{config,lib,test}/**/*.{ex,exs}"
],
line_length: 80
]
115 changes: 69 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,96 +1,119 @@
# Tds

[![Hex.pm](https://img.shields.io/hexpm/v/tds.svg)](https://hex.pm/packages/tds) [![Build status](https://ci.appveyor.com/api/projects/status/aibnqbukppa3kcpt?svg=true)](https://ci.appveyor.com/project/mjaric/tds)

MSSQL / TDS Database driver for Elixir.

This is an alpha version that currently supports Ecto 2.0. It has implemented the [db_connection](https://github.com/elixir-ecto/db_connection) behaviour, and added support for transactions and prepared queries.
Please check out the issues for a more complete overview. This branch should not yet be considered stable or used in production.
Supports Ecto 2.0. It (mostly) implements the [db_connection](https://github.com/elixir-ecto/db_connection) behaviour and has support for transactions and prepared queries.

Please check out the issues for a more complete overview. This branch should not be considered stable or ready for production yet.

## Usage

Add Tds as a dependency in your `mix.exs` file.

```elixir
def deps do
[{:tds, "~> 1.0.4"} ]
[{:tds, "~> 1.0"} ]
end
```

After you are done, run `mix deps.get` in your shell to fetch and compile Tds. Start an interactive Elixir shell with `iex -S mix`.
When you are done, run `mix deps.get` in your shell to fetch and compile Tds. Start an interactive Elixir shell with `iex -S mix`.

```iex
iex> {:ok, pid} = Tds.start_link([hostname: "localhost", username: "test_user", password: "test_password", database: "test_db", port: 4000])
{:ok, #PID<0.69.0>}
iex> Tds.query!(pid, "SELECT 'Some Awesome Text' AS MyColumn", [])
%Tds.Result{columns: ["MyColumn"], rows: [{"Some Awesome Text"}], num_rows: 1}}
iex> Tds.query!(pid, "INSERT INTO MyTable (MyColumn) VALUES (@my_value)", [%Tds.Parameter{name: "@my_value", value: "My Actual Value"}])
iex> Tds.query!(pid, "INSERT INTO MyTable (MyColumn) VALUES (@my_value)",
...> [%Tds.Parameter{name: "@my_value", value: "My Actual Value"}])
%Tds.Result{columns: nil, rows: nil, num_rows: 1}}
```

## Features

* Automatic decoding and encoding of Elixir values to and from MSSQL's binary format
* Supports TDS Version 7.3, 7.4
* Automatic decoding and encoding of Elixir values to and from MSSQL's binary format
* Support of TDS Versions 7.3, 7.4

## Connecting to SQL Instances
Tds Supports sql instances by passing ```instance: "instancename"``` to the connection options.


Tds supports SQL instances by passing `instance: "instancename"` to the connection options.

## Data representation

TDS Elixir
---------- ------
NULL nil
bool true | false
char "é"
int 42
float 42.0
text "text"
binary <<42>>
numeric #Decimal<42.0> *
date {2013, 10, 12}
time {0, 37, 14}
datetime {{2013, 10, 12}, {0, 37, 14}}
uuid <<160,238,188,153,156,11,78,248,187,109,107,185,189,56,10,17>>

All data types will be supported, unsupported types currently are *User Defined Types*, xml
| TDS | Elixir |
| -------- | -------------------------------------------------------------- |
| NULL | nil |
| bool | true / false |
| char | "é" |
| int | 42 |
| float | 42.0 |
| text | "text" |
| binary | <<42>> |
| numeric | #Decimal<42.0> * |
| date | {2013, 10, 12} |
| time | {0, 37, 14} |
| datetime | {{2013, 10, 12}, {0, 37, 14}} |
| uuid | <<160,238,188,153,156,11,78,248,187,109,107,185,189,56,10,17>> |

Currently unsupported: [User-Defined Types](https://docs.microsoft.com/en-us/sql/relational-databases/clr-integration-database-objects-user-defined-types/working-with-user-defined-types-in-sql-server), XML

## Contributing

To contribute you need to compile Tds from source and test it:
Clone and compile Tds with:

```
$ git clone https://github.com/livehelpnow/tds.git
$ cd tds
$ mix test
```bash
git clone https://github.com/livehelpnow/tds.git
cd tds
mix deps.get
```

The tests require your sql server database to be present on localhost. In case you are unable to run local instance of SQL server either using local installation on [windows](https://docs.microsoft.com/en-us/sql/database-engine/install-windows/install-sql-server-from-the-installation-wizard-setup) or [linix](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup), docker image for [linux](https://hub.docker.com/r/microsoft/mssql-server-linux/)
You can test the library with `mix test`. Use `mix credo` for linting and
`mix dialyzer` for static code analysis. Dialyzer will take a while when you
use it for the first time.

### SQL Server Setup

The tests require an sql server database to be available on localhost.

If you have Docker installed, you can use the official [SQL Server Docker image](https://hub.docker.com/r/microsoft/mssql-server-linux).
To start the container, run:

```bash
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=some!Password' -p 1433:1433 -d microsoft/mssql-server-linux:latest
```

If you prefer to install SQL Server directly on your computer, you can find
installation instructions here:

Additionally SQL authentication needs to be used for connecting and testing. Check config/test.exs file for credentials used in unit testing.
* [Windows](https://docs.microsoft.com/en-us/sql/database-engine/install-windows/install-sql-server-from-the-installation-wizard-setup)
* [Linux](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup)

##Special Thanks
Make sure your SQL server accepts the credentials defined in `config/test.exs`.

Thanks to ericmj, this driver takes a lot of inspiration from postgrex.
https://github.com/ericmj/
You also will need to have the *sqlcmd* command line tools installed. Setup
instructions can be found [here](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-tools).

## Special Thanks

Also thanks to everyone in the Elixir Google group and on the Elixir IRC Channel
Thanks to [ericmj](https://github.com/ericmj), this driver takes a lot of inspiration from postgrex.

Also thanks to everyone in the Elixir Google group and on the Elixir IRC Channel.

## License

Copyright 2014, 2015, 2017 LiveHelpNow
Copyright 2014, 2015, 2017 LiveHelpNow

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
6 changes: 3 additions & 3 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ config :logger, level: :info

config :tds,
opts: [
hostname: "localhost",
username: "sa",
password: "some!Password",
hostname: System.get_env("SQL_HOSTNAME") || "127.0.0.1",
username: System.get_env("SQL_USERNAME") || "sa",
password: System.get_env("SQL_PASSWORD") || "some!Password",
database: "test"
]

15 changes: 8 additions & 7 deletions lib/tds.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Tds do

alias Tds.Query

@timeout 5000
Expand Down Expand Up @@ -41,6 +40,7 @@ defmodule Tds do
{:error, err} -> {:error, err}
end
end

def prepare!(pid, statement, opts \\ []) do
query = %Query{statement: statement}

Expand All @@ -56,6 +56,7 @@ defmodule Tds do
{:error, err} -> {:error, err}
end
end

def execute!(pid, query, params, opts \\ []) do
case DBConnection.execute(pid, query, params, opts) do
{:ok, result} -> result
Expand All @@ -69,6 +70,7 @@ defmodule Tds do
{:error, err} -> {:error, err}
end
end

def close!(pid, query, opts \\ []) do
case DBConnection.close(pid, query, opts) do
{:ok, result} -> result
Expand All @@ -77,10 +79,10 @@ defmodule Tds do
end

def transaction(pid, fun, opts \\ []) do
case DBConnection.transaction(pid, fun, opts) do
{:ok, result} -> result
err -> err
end
case DBConnection.transaction(pid, fun, opts) do
{:ok, result} -> result
err -> err
end
end

defdelegate rollback(conn, any), to: DBConnection
Expand All @@ -90,7 +92,6 @@ defmodule Tds do
end

defp default(opts) do
opts
|> Keyword.put_new(:idle_timeout, @timeout)
Keyword.put_new(opts, :idle_timeout, @timeout)
end
end
Loading

0 comments on commit 6f548a1

Please sign in to comment.