Home

Awesome

mysql - MySQL connector for Tarantool

fast-testing status publish status

Getting Started

Prerequisites

If you prefer to install the connector using a system package manager you don't need to manually install dependencies.

Installation

Build from sources

Clone repository and then build it using CMake:

git clone https://github.com/tarantool/mysql.git tarantool-mysql
cd tarantool-mysql && cmake . -DCMAKE_BUILD_TYPE=RelWithDebInfo
make
make install

Run tests

To run the tests, the following preparatory steps must be completed:

The tests can now be run by make check.

tt rocks

You can also use tt rocks:

tt rocks install mysql

Install a package

Enable tarantool repository and install tarantool-mysql package:

apt-get install tarantool-mysql # Debian or Ubuntu
yum install tarantool-mysql     # CentOS
dnf install tarantool-mysql     # Fedora

Usage

local mysql = require('mysql')
local pool = mysql.pool_create({ host = '127.0.0.1', user = 'user', password = 'password', db = 'db', size = 5 })
local conn = pool:get()
local tuples, status  = conn:execute("SELECT ? AS a, 'xx' AS b, NULL as c", 42))
conn:begin()
conn:execute("INSERT INTO test VALUES(1, 2, 3)")
conn:commit()
pool:put(conn)

API Documentation

conn = mysql.connect(opts)

Connect to a database.

Options:

Throws an error on failure.

Returns:

conn:execute(statement, ...)

Execute a statement with arguments in the current transaction.

Throws an error on failure.

Returns:

(when use_numeric_result = false or is not set on a pool/connection creation)

{
    { -- result set
        {column1 = r1c1val, column2 = r1c2val, ...}, -- row
        {column1 = r2c1val, column2 = r2c2val, ...}, -- row
        ...
    },
    ...
}

(when use_numeric_result = true on a pool/connection creation)

{
    { -- result set
        rows = {
            {r1c1val, r1c2val, ...}, -- row
            {r2c1val, r2c2val, ...}, -- row
            ...
        },
        metadata = {
            {type = 'long', name = 'col1'}, -- column meta
            {type = 'long', name = 'col2'}, -- column meta
            ...
        },
    },
    ...
}

Example:

(when keep_null = false or is not set on a pool/connection creation)

tarantool> conn:execute("SELECT ? AS a, 'xx' AS b", NULL AS c , 42)
---
- - - a: 42
      b: xx
- true
...

(when keep_null = true on a pool/connection creation)

tarantool> conn:execute("SELECT ? AS a, 'xx' AS b", NULL AS c, 42)
---
- - - a: 42
      b: xx
      c: null
- true
...

conn:begin()

Begin a transaction.

Returns: true

conn:commit()

Commit current transaction.

Returns: true

conn:rollback()

Rollback current transaction.

Returns: true

conn:ping()

Execute a dummy statement to check that connection is alive.

Returns:

conn:quote()

Quote a query string.

Throws an error on failure.

Returns:

conn:reset(user, pass, db)

Update the connection authentication settings.

Options:

Throws an error on failure.

conn:close()

Close the individual connection or return it to a pool.

Throws an error on failure.

Returns: true

pool = mysql.pool_create(opts)

Create a connection pool with count of size established connections.

Options:

Throws an error on failure.

Returns

conn = pool:get(opts)

Get a connection from pool. Reset connection before returning it. If connection is broken then it will be reestablished. If there is no free connections and timeout is not specified then calling fiber will sleep until another fiber returns some connection to pool. If timeout is specified, and there is no free connections for the duration of the timeout, then the return value is nil.

Options:

Returns:

pool:put(conn)

Return a connection to connection pool.

Options

pool:close()

Close all connections in pool.

Returns: true

Comments

All calls to connections api will be serialized, so it should to be safe to use one connection from some count of fibers. But you should understand, that you can have some unwanted behavior across db calls, for example if another fiber 'injects' some sql between two your calls.

See Also