Skip to content

Commit 4e4102c

Browse files
authored
Merge pull request #48 from woylie/refactor/tds-error
Rewrite error module
2 parents 0214bd6 + 47be70e commit 4e4102c

10 files changed

Lines changed: 380 additions & 96 deletions

File tree

.credo.exs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# This file contains the configuration for Credo and you are probably reading
2+
# this after creating it with `mix credo.gen.config`.
3+
#
4+
# If you find anything wrong or unclear in this file, please report an
5+
# issue on GitHub: https://github.com/rrrene/credo/issues
6+
#
7+
%{
8+
#
9+
# You can have as many configs as you like in the `configs:` field.
10+
configs: [
11+
%{
12+
#
13+
# Run any exec using `mix credo -C <name>`. If no exec name is given
14+
# "default" is used.
15+
#
16+
name: "default",
17+
#
18+
# These are the files included in the analysis:
19+
files: %{
20+
#
21+
# You can give explicit globs or simply directories.
22+
# In the latter case `**/*.{ex,exs}` will be used.
23+
#
24+
included: ["lib/", "src/", "web/", "apps/"],
25+
excluded: [~r"/_build/", ~r"/deps/"]
26+
},
27+
#
28+
# If you create your own checks, you must specify the source files for
29+
# them here, so they can be loaded by Credo before running the analysis.
30+
#
31+
requires: [],
32+
#
33+
# If you want to enforce a style guide and need a more traditional linting
34+
# experience, you can change `strict` to `true` below:
35+
#
36+
strict: true,
37+
#
38+
# If you want to use uncolored output by default, you can change `color`
39+
# to `false` below:
40+
#
41+
color: true,
42+
#
43+
# You can customize the parameters of any check by adding a second element
44+
# to the tuple.
45+
#
46+
# To disable a check put `false` as second element:
47+
#
48+
# {Credo.Check.Design.DuplicatedCode, false}
49+
#
50+
checks: [
51+
{Credo.Check.Consistency.ExceptionNames},
52+
{Credo.Check.Consistency.LineEndings},
53+
{Credo.Check.Consistency.ParameterPatternMatching},
54+
{Credo.Check.Consistency.SpaceAroundOperators},
55+
{Credo.Check.Consistency.SpaceInParentheses},
56+
{Credo.Check.Consistency.TabsOrSpaces},
57+
58+
# You can customize the priority of any check
59+
# Priority values are: `low, normal, high, higher`
60+
#
61+
{Credo.Check.Design.AliasUsage, priority: :low},
62+
63+
# For some checks, you can also set other parameters
64+
#
65+
# If you don't want the `setup` and `test` macro calls in ExUnit tests
66+
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
67+
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
68+
#
69+
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},
70+
71+
# You can also customize the exit_status of each check.
72+
# If you don't want TODO comments to cause `mix credo` to fail, just
73+
# set this value to 0 (zero).
74+
#
75+
{Credo.Check.Design.TagTODO, exit_status: 2},
76+
{Credo.Check.Design.TagFIXME},
77+
78+
{Credo.Check.Readability.FunctionNames},
79+
{Credo.Check.Readability.LargeNumbers},
80+
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
81+
{Credo.Check.Readability.ModuleAttributeNames},
82+
{Credo.Check.Readability.ModuleDoc},
83+
{Credo.Check.Readability.ModuleNames},
84+
{Credo.Check.Readability.ParenthesesOnZeroArityDefs},
85+
{Credo.Check.Readability.ParenthesesInCondition},
86+
{Credo.Check.Readability.PredicateFunctionNames},
87+
{Credo.Check.Readability.PreferImplicitTry},
88+
{Credo.Check.Readability.RedundantBlankLines},
89+
{Credo.Check.Readability.StringSigils},
90+
{Credo.Check.Readability.TrailingBlankLine},
91+
{Credo.Check.Readability.TrailingWhiteSpace},
92+
{Credo.Check.Readability.VariableNames},
93+
{Credo.Check.Readability.Semicolons},
94+
{Credo.Check.Readability.SpaceAfterCommas},
95+
96+
{Credo.Check.Refactor.DoubleBooleanNegation},
97+
{Credo.Check.Refactor.CondStatements},
98+
{Credo.Check.Refactor.CyclomaticComplexity},
99+
{Credo.Check.Refactor.FunctionArity},
100+
{Credo.Check.Refactor.LongQuoteBlocks},
101+
{Credo.Check.Refactor.MatchInCondition},
102+
{Credo.Check.Refactor.NegatedConditionsInUnless},
103+
{Credo.Check.Refactor.NegatedConditionsWithElse},
104+
{Credo.Check.Refactor.Nesting},
105+
{Credo.Check.Refactor.PipeChainStart},
106+
{Credo.Check.Refactor.UnlessWithElse},
107+
108+
{Credo.Check.Warning.BoolOperationOnSameValues},
109+
{Credo.Check.Warning.ExpensiveEmptyEnumCheck},
110+
{Credo.Check.Warning.IExPry},
111+
{Credo.Check.Warning.IoInspect},
112+
{Credo.Check.Warning.LazyLogging},
113+
{Credo.Check.Warning.OperationOnSameValues},
114+
{Credo.Check.Warning.OperationWithConstantResult},
115+
{Credo.Check.Warning.UnusedEnumOperation},
116+
{Credo.Check.Warning.UnusedFileOperation},
117+
{Credo.Check.Warning.UnusedKeywordOperation},
118+
{Credo.Check.Warning.UnusedListOperation},
119+
{Credo.Check.Warning.UnusedPathOperation},
120+
{Credo.Check.Warning.UnusedRegexOperation},
121+
{Credo.Check.Warning.UnusedStringOperation},
122+
{Credo.Check.Warning.UnusedTupleOperation},
123+
{Credo.Check.Warning.RaiseInsideRescue},
124+
125+
# Controversial and experimental checks (opt-in, just remove `, false`)
126+
#
127+
{Credo.Check.Refactor.ABCSize, false},
128+
{Credo.Check.Refactor.AppendSingleItem, false},
129+
{Credo.Check.Refactor.VariableRebinding, false},
130+
{Credo.Check.Warning.MapGetUnsafePass, false},
131+
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},
132+
133+
# Deprecated checks (these will be deleted after a grace period)
134+
#
135+
{Credo.Check.Readability.Specs, false},
136+
{Credo.Check.Warning.NameRedeclarationByAssignment, false},
137+
{Credo.Check.Warning.NameRedeclarationByCase, false},
138+
{Credo.Check.Warning.NameRedeclarationByDef, false},
139+
{Credo.Check.Warning.NameRedeclarationByFn, false},
140+
141+
# Custom checks can be created using `mix credo.gen.check`.
142+
#
143+
]
144+
}
145+
]
146+
}

README.md

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Tds
22

3-
MSSQL / TDS Database driver for Elixir.
3+
[![Hex.pm](https://img.shields.io/hexpm/v/tds.svg)](https://hex.pm/packages/tds)
44

5+
MSSQL / TDS Database driver for Elixir.
56
[![Build status](https://ci.appveyor.com/api/projects/status/aibnqbukppa3kcpt?svg=true)](https://ci.appveyor.com/project/mjaric/tds)
67

7-
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.
8-
Please check out the issues for a more complete overview. This branch should not yet be considered stable or used in production.
8+
This is an alpha version that currently 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.
9+
10+
Please check out the issues for a more complete overview. This branch should not be considered stable or ready for production yet.
911

1012
## Usage
1113

@@ -17,82 +19,102 @@ def deps do
1719
end
1820
```
1921

20-
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`.
22+
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`.
2123

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

3136
## Features
3237

33-
* Automatic decoding and encoding of Elixir values to and from MSSQL's binary format
34-
* Supports TDS Version 7.3, 7.4
38+
* Automatic decoding and encoding of Elixir values to and from MSSQL's binary format
39+
* Support of TDS Versions 7.3, 7.4
3540

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

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

4145
## Data representation
4246

43-
TDS Elixir
44-
---------- ------
45-
NULL nil
46-
bool true | false
47-
char "é"
48-
int 42
49-
float 42.0
50-
text "text"
51-
binary <<42>>
52-
numeric #Decimal<42.0> *
53-
date {2013, 10, 12}
54-
time {0, 37, 14}
55-
datetime {{2013, 10, 12}, {0, 37, 14}}
56-
uuid <<160,238,188,153,156,11,78,248,187,109,107,185,189,56,10,17>>
57-
58-
All data types will be supported, unsupported types currently are *User Defined Types*, xml
47+
| TDS | Elixir |
48+
| -------- | -------------------------------------------------------------- |
49+
| NULL | nil |
50+
| bool | true / false |
51+
| char | "é" |
52+
| int | 42 |
53+
| float | 42.0 |
54+
| text | "text" |
55+
| binary | <<42>> |
56+
| numeric | #Decimal<42.0> * |
57+
| date | {2013, 10, 12} |
58+
| time | {0, 37, 14} |
59+
| datetime | {{2013, 10, 12}, {0, 37, 14}} |
60+
| uuid | <<160,238,188,153,156,11,78,248,187,109,107,185,189,56,10,17>> |
61+
62+
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
5963

6064
## Contributing
6165

62-
To contribute you need to compile Tds from source and test it:
66+
Clone and compile Tds with:
6367

68+
```bash
69+
git clone https://github.com/livehelpnow/tds.git
70+
cd tds
71+
mix deps.get
6472
```
65-
$ git clone https://github.com/livehelpnow/tds.git
66-
$ cd tds
67-
$ mix test
73+
74+
You can test the library with `mix test`. Use `mix credo` for linting and
75+
`mix dialyzer` for static code analysis. Dialyzer will take a while when you
76+
use it for the first time.
77+
78+
### SQL Server Setup
79+
80+
The tests require an sql server database to be available on localhost.
81+
82+
If you have Docker installed, you can use the official [SQL Server Docker image](https://hub.docker.com/r/microsoft/mssql-server-linux).
83+
To start the container, run:
84+
85+
```bash
86+
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=some!Password' -p 1433:1433 -d microsoft/mssql-server-linux:latest
6887
```
6988

70-
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/)
71-
You will need sqlcmd command line tools for some operations in test. Setup instructions can be found [here](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-tools)
89+
If you prefer to install SQL Server directly on your computer, you can find
90+
installation instructions here:
7291

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

75-
##Special Thanks
95+
Make sure your SQL server accepts the credentials defined in `config/test.exs`.
7696

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

100+
## Special Thanks
80101

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

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

84106
## License
85107

86-
Copyright 2014, 2015, 2017 LiveHelpNow
108+
Copyright 2014, 2015, 2017 LiveHelpNow
87109

88-
Licensed under the Apache License, Version 2.0 (the "License");
89-
you may not use this file except in compliance with the License.
90-
You may obtain a copy of the License at
110+
Licensed under the Apache License, Version 2.0 (the "License");
111+
you may not use this file except in compliance with the License.
112+
You may obtain a copy of the License at
91113

92-
http://www.apache.org/licenses/LICENSE-2.0
114+
http://www.apache.org/licenses/LICENSE-2.0
93115

94-
Unless required by applicable law or agreed to in writing, software
95-
distributed under the License is distributed on an "AS IS" BASIS,
96-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
97-
See the License for the specific language governing permissions and
98-
limitations under the License.
116+
Unless required by applicable law or agreed to in writing, software
117+
distributed under the License is distributed on an "AS IS" BASIS,
118+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
119+
See the License for the specific language governing permissions and
120+
limitations under the License.

lib/tds.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule Tds do
2-
32
alias Tds.Query
43

54
@timeout 5000
@@ -17,6 +16,7 @@ defmodule Tds do
1716
{:error, err} -> {:error, err}
1817
end
1918
end
19+
2020
def query!(pid, statement, params, opts \\ []) do
2121
query = %Query{statement: statement}
2222
opts = Keyword.put_new(opts, :parameters, params)
@@ -35,6 +35,7 @@ defmodule Tds do
3535
{:error, err} -> {:error, err}
3636
end
3737
end
38+
3839
def prepare!(pid, statement, opts \\ []) do
3940
query = %Query{statement: statement}
4041

@@ -50,6 +51,7 @@ defmodule Tds do
5051
{:error, err} -> {:error, err}
5152
end
5253
end
54+
5355
def execute!(pid, query, params, opts \\ []) do
5456
case DBConnection.execute(pid, query, params, opts) do
5557
{:ok, result} -> result
@@ -63,6 +65,7 @@ defmodule Tds do
6365
{:error, err} -> {:error, err}
6466
end
6567
end
68+
6669
def close!(pid, query, opts \\ []) do
6770
case DBConnection.close(pid, query, opts) do
6871
{:ok, result} -> result
@@ -71,10 +74,10 @@ defmodule Tds do
7174
end
7275

7376
def transaction(pid, fun, opts \\ []) do
74-
case DBConnection.transaction(pid, fun, opts) do
75-
{:ok, result} -> result
76-
err -> err
77-
end
77+
case DBConnection.transaction(pid, fun, opts) do
78+
{:ok, result} -> result
79+
err -> err
80+
end
7881
end
7982

8083
defdelegate rollback(conn, any), to: DBConnection
@@ -84,7 +87,6 @@ defmodule Tds do
8487
end
8588

8689
defp default(opts) do
87-
opts
88-
|> Keyword.put_new(:idle_timeout, @timeout)
90+
Keyword.put_new(opts, :idle_timeout, @timeout)
8991
end
9092
end

0 commit comments

Comments
 (0)