Categorygithub.com/ankorstore/yokai/sql
modulepackage
1.1.0
Repository: https://github.com/ankorstore/yokai.git
Documentation: pkg.go.dev

# README

SQL Module

ci go report codecov Deps PkgGoDev

SQL module based on database/sql.

Installation

go get github.com/ankorstore/yokai/sql

Documentation

This module provides a Driver, decorating database/sql compatible drivers, with a hooking mechanism.

Usage

The following database systems are supported:

To create a *sql.DB with the tracing and logging hooks:

package main

import (
	"database/sql"
	
	yokaisql "github.com/ankorstore/yokai/sql"
	"github.com/ankorstore/yokai/sql/hook/log"
	"github.com/ankorstore/yokai/sql/hook/trace"
)

func main() {
	// MySQL
	driver, _ := yokaisql.Register("mysql", trace.NewTraceHook(), log.NewLogHook())
	db, _ := sql.Open(driver, "user:password@tcp(localhost:3306)/db?parseTime=true")

	// Postgres
	driver, _ := yokaisql.Register("postgres", trace.NewTraceHook(), log.NewLogHook())
	db, _ := sql.Open(driver, "host=host port=5432 user=user password=password dbname=db sslmode=disable")

	// SQLite
	driver, _ := yokaisql.Register("sqlite", trace.NewTraceHook(), log.NewLogHook())
	db, _ := sql.Open(driver, ":memory:")
}

See database/sql documentation for more details.

Hooks

This module provides a hooking mechanism to add logic around the SQL operations.

Log hook

This module provides an LogHook, that you can use to automatically log the SQL operations:

package main

import (
	"database/sql"

	yokaisql "github.com/ankorstore/yokai/sql"
	"github.com/ankorstore/yokai/sql/hook/log"
	"github.com/rs/zerolog"
)

func main() {

	logHook := log.NewLogHook(
		log.WithLevel(zerolog.DebugLevel),        // SQL logs level, debug by default
		log.WithArguments(true),                  // SQL logs with SQL arguments, false by default
		log.WithExcludedOperations(               // SQL operations to exclude from logging, empty by default
			yokaisql.ConnectionPingOperation,
			yokaisql.ConnectionResetSessionOperation,
		),
	)

	driver, _ := yokaisql.Register("sqlite", logHook)
	db, _ := sql.Open(driver, ":memory:")
}

Trace hook

This module provides an TraceHook, that you can use to automatically trace the SQL operations:

package main

import (
	"database/sql"

	yokaisql "github.com/ankorstore/yokai/sql"
	"github.com/ankorstore/yokai/sql/hook/log"
	"github.com/ankorstore/yokai/sql/hook/trace"
	"github.com/rs/zerolog"
)

func main() {

	traceHook := trace.NewTraceHook(
		trace.WithArguments(true),                  // SQL traces with SQL arguments, false by default
		trace.WithExcludedOperations(               // SQL operations to exclude from tracing, empty by default
			yokaisql.ConnectionPingOperation,
			yokaisql.ConnectionResetSessionOperation,
		),
	)

	driver, _ := yokaisql.Register("sqlite", traceHook)
	db, _ := sql.Open(driver, ":memory:")
}

Custom hook

This module provides a Hook interface, that you can implement to extend the logic around SQL operations:

package main

import (
	"context"
	"database/sql"

	yokaisql "github.com/ankorstore/yokai/sql"
	"github.com/ankorstore/yokai/sql/hook/log"
	"github.com/ankorstore/yokai/sql/hook/trace"
	"github.com/rs/zerolog"
)

type CustomHook struct{}

func (h *CustomHook) Before(ctx context.Context, event *yokaisql.HookEvent) context.Context {
	// your custom logic before SQL operation
	
	return ctx
}

func (h *CustomHook) After(ctx context.Context, event *yokaisql.HookEvent) {
	// your custom logic after SQL operation
}

func main() {
	driver, _ := yokaisql.Register("sqlite", &CustomHook{})
	db, _ := sql.Open(driver, ":memory:")
}

Healthcheck

This module provides an SQLProbe, compatible with the healthcheck module:

package main

import (
	"context"

	yokaihc "github.com/ankorstore/yokai/healthcheck"
	yokaisql "github.com/ankorstore/yokai/sql"
	"github.com/ankorstore/yokai/sql/healthcheck"
)

func main() {
	driver, _ := yokaisql.Register("sqlite")
	db, _ := sql.Open(driver, ":memory:")

	checker, _ := yokaihc.NewDefaultCheckerFactory().Create(
		yokaihc.WithProbe(healthcheck.NewSQLProbe(db)),
	)

	checker.Check(context.Background(), yokaihc.Readiness)
}

This probe performs a ping to the configured database connection.

# Packages

No description provided by the author
No description provided by the author

# Functions

ContainsOperation returns true if a given Operation item is contained id a list of Operation.
ConvertNamedValuesToValues converts a list of driver.NamedValue into a list of driver.Value.
FetchOperation returns an Operation for a given name.
FetchOperations returns a list of Operation for a given list of names.
FetchSystem returns a System for a given name.
NewConfiguration returns a new Configuration.
NewConnection returns a new Connection.
NewConnector returns a new Connector.
NewDefaultDriverFactory returns a new DefaultDriverFactory.
NewDefaultDriverRegistry returns a new DefaultDriverRegistry.
NewDriver returns a new Driver.
NewHookEvent returns a new HookEvent.
NewStatement returns a new Statement.
NewTransaction returns a new Transaction.
Register registers a new Driver for a given name and an optional list of Hook.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author

# Structs

Configuration is the SQL components (driver, connector, connection, etc) configuration.
Connection is a SQL driver connection wrapping a driver.Conn.
Connector is a SQL driver connector wrapping a driver.Connector.
DefaultDriverFactory is the default DriverFactory implementation.
DefaultDriverRegistry is the default DriverRegistry implementation.
Driver is a SQL driver wrapping a driver.Driver.
HookEvent is representing an event provided to a database Hook.
Statement is a SQL driver statement wrapping a driver.Stmt.
Transaction is a SQL driver transaction wrapping a driver.Tx.

# Interfaces

DriverFactory is the interface for Driver factories.
DriverRegistry is the interface for Driver registries.
Hook is the interface for database hooks.

# Type aliases

Operation is an enum for the supported database operations.
System is an enum for the supported database systems.