sql

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2023 License: Apache-2.0 Imports: 15 Imported by: 10

README

SQL

One sql service for fns.

Features

  • Global transaction
  • Proxy
  • Support master slaver kind
  • Support cluster kind

Install

go get github.com/aacfactory/fns-contrib/databases/sql

Usage

Config
  • Standalone type
    • masterSlaverMode = false
    • dsn size is one
  • Master slaver type
    • masterSlaverMode = true
    • first of dsn is master, afters are slavers
  • Cluster type
    • masterSlaverMode = false
    • all in dsn is members

Example:

sql:
  masterSlaverMode: false,
  driver: "postgres",
  dsn:
    - "username:password@tcp(ip:port)/databases"
  maxIdles: 0
  maxOpens: 0
  enableDebugLog: true
  gtmCleanUpSecond: 120
  isolation: 2
  dialect: "postgres"
Import driver
import _ "github.com/go-sql-driver/mysql"
Deploy
app.Deply(sql.Service())
Proxy usage

See proxy.go

// begin transaction 
sql.BeginTransaction(ctx)
// commit transaction
sql.CommitTransaction(ctx)
// query
sql.Query(ctx, querySQL, ...)
// execute
sql.Execute(ctx, executeSQL, ...)
ORM usage
Multi database source

use multi database service to implements

Config:

postgres1:
  masterSlaverMode: false,
  driver: "postgres",
  dsn:
    - "username:password@tcp(ip:port)/databases"

mysql1:
  masterSlaverMode: false,
  driver: "mysql",
  dsn:
    - "username:password@tcp(ip:port)/databases"

Deploy:

app.Deploy(sql.Service(sql.Name("postgres1")))
app.Deploy(sql.Service(sql.Name("mysql1")))

Proxy

sql.Query(sql.WithOptions(ctx, sql.Database("postgres1")), querySQL, ...)
sql.Query(sql.WithOptions(ctx, sql.Database("mysql1")), querySQL, ...)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeginTransaction added in v0.8.1

func BeginTransaction(ctx context.Context) (err errors.CodeError)

func CommitTransaction added in v0.8.1

func CommitTransaction(ctx context.Context) (err errors.CodeError)

func Dialect added in v1.0.0

func Dialect(ctx context.Context) (dialect string, err errors.CodeError)

func Execute

func Execute(ctx context.Context, query string, args ...interface{}) (affected int64, lastInsertId int64, err errors.CodeError)

func RegisterType added in v1.0.0

func RegisterType(vt ValueType)

func RollbackTransaction added in v0.8.1

func RollbackTransaction(ctx context.Context) (err errors.CodeError)

func Service

func Service(databases ...string) service.Service

func WithOptions added in v1.0.0

func WithOptions(ctx context.Context, options ...ProxyOption) context.Context

Types

type Arguments added in v1.0.0

type Arguments struct {
	// contains filtered or unexported fields
}

func NewArguments added in v1.0.0

func NewArguments() *Arguments

func (*Arguments) Append added in v1.0.0

func (t *Arguments) Append(values ...interface{}) *Arguments

func (*Arguments) MarshalJSON added in v1.0.0

func (t *Arguments) MarshalJSON() (p []byte, err error)

func (*Arguments) Merge added in v1.0.0

func (t *Arguments) Merge(v *Arguments) *Arguments

func (*Arguments) Size added in v1.0.0

func (t *Arguments) Size() (n int)

func (*Arguments) UnmarshalJSON added in v1.0.0

func (t *Arguments) UnmarshalJSON(p []byte) (err error)

func (*Arguments) Values added in v1.0.0

func (t *Arguments) Values() (args []interface{})

type Column

type Column interface {
	Type() (typ string)
	DatabaseType() (dbt string)
	Name() (v string)
	IsNil() (ok bool)
	Get(v interface{}) (err error)
	Value() (value any, err error)
}

type Date added in v1.0.0

type Date struct {
	Year  int
	Month time.Month
	Day   int
}

func DateNow added in v1.0.0

func DateNow() Date

func NewDate added in v1.0.0

func NewDate(year int, month time.Month, day int) Date

func NewDateFromJsonDate added in v1.0.0

func NewDateFromJsonDate(d json.Date) Date

func NewDateFromTime added in v1.0.0

func NewDateFromTime(t time.Time) Date

func NewTimeFromTime added in v1.0.0

func NewTimeFromTime(t time.Time) Date

func TimeNow added in v1.0.0

func TimeNow() Date

func (*Date) IsZero added in v1.0.0

func (d *Date) IsZero() (ok bool)

func (*Date) MarshalJSON added in v1.0.0

func (d *Date) MarshalJSON() ([]byte, error)

func (*Date) Scan added in v1.0.0

func (d *Date) Scan(src interface{}) error

func (*Date) String added in v1.0.0

func (d *Date) String() string

func (*Date) ToTime added in v1.0.0

func (d *Date) ToTime() time.Time

func (*Date) UnmarshalJSON added in v1.0.0

func (d *Date) UnmarshalJSON(p []byte) error

type NullDate added in v1.0.0

type NullDate struct {
	Valid bool
	Value Date
}

func (*NullDate) Scan added in v1.0.0

func (t *NullDate) Scan(src interface{}) error

type NullJson

type NullJson struct {
	Json  json.RawMessage
	Valid bool
}

func (*NullJson) Scan

func (v *NullJson) Scan(src interface{}) error

type NullRawBytes added in v1.0.0

type NullRawBytes struct {
	Raw   stdsql.RawBytes
	Valid bool
}

func (*NullRawBytes) Scan added in v1.0.0

func (v *NullRawBytes) Scan(src interface{}) error

type NullTime added in v1.0.0

type NullTime struct {
	Valid bool
	Value Time
}

func (*NullTime) Scan added in v1.0.0

func (t *NullTime) Scan(src interface{}) error

type ProxyOption added in v1.0.0

type ProxyOption func(*ProxyOptions)

func Database added in v1.0.0

func Database(name string) ProxyOption

type ProxyOptions added in v1.0.0

type ProxyOptions struct {
	// contains filtered or unexported fields
}

type Row

type Row interface {
	json.Marshaler
	json.Unmarshaler
	Empty() (ok bool)
	Columns() (columns []Column)
	Column(name string, value interface{}) (has bool, err error)
}

type Rows

type Rows interface {
	json.Marshaler
	json.Unmarshaler
	Empty() (ok bool)
	Size() int
	Next() (v Row, has bool)
}

func Query

func Query(ctx context.Context, query string, args ...interface{}) (v Rows, err errors.CodeError)

type Time added in v1.0.0

type Time struct {
	Hour    int
	Minutes int
	Second  int
}

func NewTime added in v1.0.0

func NewTime(hour int, min int, sec int) Time

func (*Time) IsZero added in v1.0.0

func (t *Time) IsZero() (ok bool)

func (*Time) MarshalJSON added in v1.0.0

func (t *Time) MarshalJSON() ([]byte, error)

func (*Time) Scan added in v1.0.0

func (t *Time) Scan(src interface{}) error

func (*Time) String added in v1.0.0

func (t *Time) String() string

func (*Time) ToTime added in v1.0.0

func (t *Time) ToTime() time.Time

func (*Time) UnmarshalJSON added in v1.0.0

func (t *Time) UnmarshalJSON(p []byte) error

type ValueScanner added in v1.0.0

type ValueScanner interface {
	stdsql.Scanner
	Value() (value any)
	Reset()
}

type ValueType added in v1.0.0

type ValueType interface {
	Type() (typ reflect.Type)
	ColumnType() (ct string)
	DatabaseTypes() (types []string)
	Scanner() (scanner ValueScanner)
	Encode(src any) (p []byte, err error)
	Decode(p []byte) (v any, err error)
}

func BoolValueType added in v1.0.0

func BoolValueType() ValueType

func BytesValueType added in v1.0.0

func BytesValueType() ValueType

func DateValueType added in v1.0.0

func DateValueType() ValueType

func DatetimeValueType added in v1.0.0

func DatetimeValueType() ValueType

func FloatValueType added in v1.0.0

func FloatValueType() ValueType

func IntValueType added in v1.0.0

func IntValueType() ValueType

func JsonValueType added in v1.0.0

func JsonValueType() ValueType

func StringValueType added in v1.0.0

func StringValueType() ValueType

func TimeValueType added in v1.0.0

func TimeValueType() ValueType

Directories

Path Synopsis
dal

Jump to

Keyboard shortcuts

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