mysql

package module
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 8 Imported by: 5

README

mysql

GoDoc Tesst Go Report Card codecov Gitter chat

MySQL adapter for REL.

Example

package main

import (
	"context"

	_ "github.com/go-sql-driver/mysql"
	"github.com/go-rel/mysql"
	"github.com/go-rel/rel"
)

func main() {
	// open mysql connection.
	// note: `clientFoundRows=true` is required for update and delete to works correctly.
	adapter, err := mysql.Open("root@(127.0.0.1:3306)/rel_test?clientFoundRows=true&charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		panic(err)
	}
	defer adapter.Close()

	// initialize REL's repo.
	repo := rel.New(adapter)
	repo.Ping(context.TODO())
}

Example Replication (Source/Replica)

package main

import (
	"context"

	_ "github.com/go-sql-driver/mysql"
	"github.com/go-rel/primaryreplica"
	"github.com/go-rel/mysql"
	"github.com/go-rel/rel"
)

func main() {
	// open mysql connection.
	// note: `clientFoundRows=true` is required for update and delete to works correctly.
	adapter := primaryreplica.New(
		mysql.MustOpen("root@(source:23306)/rel_test?charset=utf8&parseTime=True&loc=Local"),
		mysql.MustOpen("root@(replica:23307)/rel_test?charset=utf8&parseTime=True&loc=Local"),
	)
	defer adapter.Close()

	// initialize REL's repo.
	repo := rel.New(adapter)
	repo.Ping(context.TODO())
}

Supported Driver

  • github.com/go-sql-driver/mysql

Supported Database

  • MySQL 5 and 8
  • MariaDB 10

Testing

Start MariaDB server in Docker
docker run -it --rm -p 3307:3306 -e "MARIADB_ROOT_PASSWORD=test" -e "MARIADB_DATABASE=rel_test" mariadb:10
Run tests
MYSQL_DATABASE="root:test@tcp(localhost:3307)/rel_test" go test ./...

Documentation

Overview

Package mysql wraps mysql driver as an adapter for REL.

Usage:

// open mysql connection.
// note: `clientFoundRows=true` is required for update and delete to works correctly.
adapter, err := mysql.Open("root@(127.0.0.1:3306)/rel_test?clientFoundRows=true&charset=utf8&parseTime=True&loc=Local")
if err != nil {
	panic(err)
}
defer adapter.Close()

// initialize REL's repo.
repo := rel.New(adapter)

Index

Constants

View Source
const Name string = "mysql"

Name of database type this adapter implements.

Variables

This section is empty.

Functions

func MustOpen added in v0.4.0

func MustOpen(dsn string) rel.Adapter

MustOpen mysql connection using dsn.

func New

func New(database *db.DB) rel.Adapter

New mysql adapter using existing connection. Existing connection needs to be created with `clientFoundRows=true` options for update and delete to works correctly.

func Open

func Open(dsn string) (rel.Adapter, error)

Open mysql connection using dsn.

Types

type MySQL added in v0.11.0

type MySQL struct {
	sql.SQL
}

MySQL adapter.

func (MySQL) Name added in v0.11.0

func (MySQL) Name() string

Name of database adapter.

type Quote added in v0.2.0

type Quote struct{}

Quote MySQL identifiers and literals.

func (Quote) ID added in v0.2.0

func (q Quote) ID(name string) string

func (Quote) Value added in v0.2.0

func (q Quote) Value(v interface{}) string

type ValueConvert added in v0.2.0

type ValueConvert struct{}

ValueConvert converts values to MySQL literals.

func (ValueConvert) ConvertValue added in v0.2.0

func (c ValueConvert) ConvertValue(v interface{}) (driver.Value, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL