Code Monkey home page Code Monkey logo

myxql's Introduction

MyXQL

Build Status

MySQL driver for Elixir.

Documentation: http://hexdocs.pm/myxql

Features

  • Automatic decoding and encoding of Elixir values to and from MySQL text and binary protocols
  • Supports transactions, prepared queries, streaming, pooling and more via DBConnection
  • Supports MySQL 5.5+, 8.0, and MariaDB 10.3
  • Supports mysql_native_password, sha256_password (*), and caching_sha2_password (*) authentication plugins

* These authentication methods require either an SSL connection or the server must exchange its public key during handshake.

Usage

Add :myxql to your dependencies:

def deps() do
  [
    {:myxql, "~> 0.2.0"}
  ]
end

Make sure you are using the latest version!

iex> {:ok, pid} = MyXQL.start_link(username: "root")
iex> MyXQL.query!(pid, "CREATE DATABASE IF NOT EXISTS blog")

iex> {:ok, pid} = MyXQL.start_link(username: "root", database: "blog")
iex> MyXQL.query!(pid, "CREATE TABLE posts IF NOT EXISTS (id serial primary key, title text)")

iex> MyXQL.query!(pid, "INSERT INTO posts (`title`) VALUES ('Post 1')")
%MyXQL.Result{columns: nil, connection_id: 11204,, last_insert_id: 1, num_rows: 1, num_warnings: 0, rows: nil}

iex> MyXQL.query(pid, "INSERT INTO posts (`title`) VALUES (?), (?)", ["Post 2", "Post 3"])
%MyXQL.Result{columns: nil, connection_id: 11204, last_insert_id: 2, num_rows: 2, num_warnings: 0, rows: nil}

iex> MyXQL.query(pid, "SELECT * FROM posts")
{:ok,
 %MyXQL.Result{
   columns: ["id", "title"],
   connection_id: 11204,
   last_insert_id: nil,
   num_rows: 3,
   num_warnings: 0,
   rows: [[1, "Post 1"], [2, "Post 2"], [3, "Post 3"]]
 }}

It's recommended to start MyXQL under supervision tree:

defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    children = [
      {MyXQL, username: "root", name: :myxql}
    ]

    Supervisor.start_link(children, opts)
  end
end

and then we can refer to it by its :name:

iex> MyXQL.query!(:myxql, "SELECT NOW()").rows
[[~N[2018-12-28 13:42:31]]]

Mariaex Compatibility

See Mariaex Compatibility page for transition between drivers.

Data representation

MySQL           Elixir
-----           ------
NULL            nil
bool            1 | 0
int             42
float           42.0
decimal         #Decimal<42.0> *
date            ~D[2013-10-12] **
time            ~T[00:37:14]
datetime        ~N[2013-10-12 00:37:14] **, ***
timestamp       #DateTime<2013-10-12 00:37:14Z> ***
json            %{"foo" => "bar"} ****
char            "é"
text            "myxql"
binary          <<1, 2, 3>>
bit             <<1::size(1), 0::size(1)>>

* See Decimal

** When using SQL mode that allows them, MySQL "zero" dates and datetimes are represented as :zero_date and :zero_datetime respectively.

*** Datetime fields are represented as NaiveDateTime, however a UTC DateTime can be used for encoding as well

**** MySQL added a native JSON type in version 5.7.8, if you're using earlier versions, remember to use TEXT column for your JSON field.

JSON support

MyXQL comes with JSON support out of the box via the Jason library. To use it, add :jason to your dependencies:

{:jason, "~> 1.0"}

You can customize it to use another library via the :json_library configuration:

config :myxql, :json_library, SomeJSONModule

Contributing

Run tests:

git clone [email protected]:elixir-ecto/myxql.git
cd myxql
mix deps.get
mix test

See scripts/ci.sh and scripts/test-versions.sh for scripts used to test against different server versions.

License

The source code is under Apache License 2.0.

Copyright (c) 2018 Plataformatec

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.

myxql's People

Contributors

dbii avatar farhadi avatar hkrutzer avatar qrede avatar schultzer avatar sebastiw avatar seeekr avatar wojtekmach 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.