GithubHelp home page GithubHelp logo

tds_ecto's Introduction

Tds.Ecto

MSSQL / TDS Adapter for Ecto

Example

# In your config/config.exs file add MSSQL connection options
# By default it use port 1433, different port can be specified with
# port: 1434
# option and named instance with
# instance: "instance_name"
config :my_app, Repo,
  database: "ecto_simple",
  username: "mssql",
  password: "mssql",
  hostname: "localhost"

# In your application code
defmodule Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Tds.Ecto
end

defmodule Weather do
  use Ecto.Model

  schema "weather" do
    field :city     # Defaults to type :string
    field :temp_lo, :integer
    field :temp_hi, :integer
    field :prcp,    :float, default: 0.0
  end
end

defmodule Simple do
  import Ecto.Query

  def sample_query do
    query = from w in Weather,
          where: w.prcp > 0 or is_nil(w.prcp),
         select: w
    Repo.all(query)
  end
end

Usage

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

def deps do
  [{:tds_ecto, "~> 1.0.2"}]
end

You should also update your applications list to include both projects:

def application do
  [applications: [:logger, :tds_ecto, :ecto]]
end

To use the adapter in your repo:

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Tds.Ecto
end

tds_ecto relies on pattern matching against error messages to extract constraint names. If your SQL server is not configured to English as its default language you may add an after_connect hook to your repo and set English as the language for Ecto connections.

def after_connect(conn) do
  Tds.Ecto.Connection.query(conn, "SET LANGUAGE English", [], [])
end

For additional information on usage please see the documentation for Ecto.

Data Type Mapping

MSSQL             	Ecto
----------        	------
nvarchar          	:string
varchar			  	:binary
char              	:binary
varbinary		  	:binary
float             	:float
decimal           	:decimal
integer 		  	:integer
bit 			  	:boolean
uniqueidentifier  	:uuid
datetime		  	:datetime
date			  	:date
time 			  	:time

Contributing

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

$ git clone https://github.com/livehelpnow/tds_ecto.git
$ cd tds_ecto
$ mix test

The tests require an addition to your hosts file to connect to your sql server database.

mssql.local

Additionally SQL authentication needs to be used for connecting and testing. Add the user test_user with access to the database test_db. See one of the test files for the connection information and port number.

License

Copyright 2014, 2015 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

   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.

tds_ecto's People

Contributors

mobileoverlord avatar danielicardopcnt avatar sldab avatar nubunto avatar josevalim avatar tica2 avatar amarsahinovic avatar

Watchers

Javi Mollà avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.