database

package module
v0.0.0-...-462de11 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 13 Imported by: 13

README

Database

Go Reference Go codecov Go Report Card Mit License

Package database provides a database abstraction client.

This package is based on gorm

Installation

go get -u github.com/gopi-frame/database

Import

import "github.com/gopi-frame/database"

Usage

package main

import (
    "github.com/gopi-frame/database"
    
    _ "github.com/gopi-frame/database/sqlite"
    // _ "github.com/gopi-frame/database/mysql"
    // _ "github.com/gopi-frame/database/postgres"
    // _ "github.com/gopi-frame/database/sqlserver"
)

func main() {
    db, err := database.Connect("sqlite", map[string]any{
        "dsn": "file:test.db",
    })
    if err!= nil {
        panic(err)
    }
}

Drivers

How to create a custom driver

To create a custom driver, just implement the database.Driver interface and register it using database.Register.

Example
package main

import (
    "github.com/gopi-frame/database"
    "gorm.io/gorm"
)

var driverName = "custom"

func init() {
    database.Register(driverName, &CustomDriver{})
}

type CustomDriver struct{}

func (d *CustomDriver) Open(options map[string]any) (gorm.Dialector, error) {
    var d gorm.Dialector
    // implement your driver here
    return d, nil
}

Documentation

Overview

Package database is a package for managing database drivers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(driverName string, options map[string]any) (*gorm.DB, error)

Connect connects to a database using the given driver name and options.

func Drivers

func Drivers() []string

Drivers returns a list of registered database drivers.

func ExpandSliceWithEnvHook

func ExpandSliceWithEnvHook(f reflect.Type, _ reflect.Type, data any) (any, error)

func ExpandStringKeyMapWithEnvHook

func ExpandStringKeyMapWithEnvHook(f reflect.Type, _ reflect.Type, data any) (any, error)

func NamingStrategyParseHookFunc

func NamingStrategyParseHookFunc() func(f reflect.Type, t reflect.Type, data any) (any, error)

func Open

func Open(driverName string, options map[string]any) (gorm.Dialector, error)

Open opens a new database connector using the given driver name and options. If the driver with the given name doesn't exist, it panics.

func Register

func Register(driverName string, driver database.Driver)

Register registers a new database driver. If a driver with the same name already exists, it panics.

func StringToLocationHookFunc

func StringToLocationHookFunc() func(f reflect.Type, t reflect.Type, data any) (any, error)

Types

type DatabaseManager

type DatabaseManager struct {
	*gorm.DB
	// contains filtered or unexported fields
}

func NewDatabaseManager

func NewDatabaseManager() *DatabaseManager

func (*DatabaseManager) AddConnection

func (m *DatabaseManager) AddConnection(name string, db *gorm.DB)

func (*DatabaseManager) AddDeferConnection

func (m *DatabaseManager) AddDeferConnection(name string, config map[string]any)

func (*DatabaseManager) GetConnection

func (m *DatabaseManager) GetConnection(name string) *gorm.DB

func (*DatabaseManager) HasConnection

func (m *DatabaseManager) HasConnection(name string) bool

func (*DatabaseManager) SetDefaultConnection

func (m *DatabaseManager) SetDefaultConnection(connection string)

type DuplicateConnectionException

type DuplicateConnectionException struct {
	Throwable
}

func NewDuplicateDriverException

func NewDuplicateDriverException(name string) *DuplicateConnectionException

type NotConfiguredException

type NotConfiguredException struct {
	Throwable
}

func NewNotConfiguredException

func NewNotConfiguredException(name string) *NotConfiguredException

type UnregisteredDriverException

type UnregisteredDriverException struct {
	Throwable
}

func NewUnregisteredDriverException

func NewUnregisteredDriverException(name string) *UnregisteredDriverException

Directories

Path Synopsis
driver
mysql Module
postgres Module
sqlite Module
sqlserver Module
mysql module
postgres module
resolver
mysql Module
postgres Module
sqlite Module
sqlserver Module
sqlite module
sqlserver module

Jump to

Keyboard shortcuts

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