sql

package module
v1.2.84 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: Apache-2.0 Imports: 27 Imported by: 10

README

SQL

One sql service for fns.

Features

  • Global transaction
  • Support prepared statement
  • Support master slaver kind
  • Support cluster kind

Install

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

Usage

Config

Standalone:

sql:
  kind: "standalone"
  isolation: 2
  transactionMaxAge: 10
  debugLog: true
  options:
    driver: "postgres"
    dsn: "username:password@tcp(ip:port)/databases"
    maxIdles: 0
    maxOpens: 0
    statements:
      enable: true
      cacheSize: 256
      evictTimeoutSeconds: 10

MasterSlave:

sql:
  kind: "masterSlave"
  isolation: 2
  transactionMaxAge: 10
  options:
    driver: "postgres"
    master: "username:password@tcp(ip:port)/databases"
    slavers:
      - "username:password@tcp(ip:port)/databases"
      - "username:password@tcp(ip:port)/databases"
    maxIdles: 0
    maxOpens: 0
    statements:
      enable: true
      cacheSize: 256
      evictTimeoutSeconds: 10

Cluster:

sql:
  kind: "cluster"
  isolation: 2
  transactionMaxAge: 10
  options:
    driver: "postgres"
    dsn:
      - "username:password@tcp(ip:port)/databases"
      - "username:password@tcp(ip:port)/databases"
    maxIdles: 0
    maxOpens: 0
    statements:
      enable: true
      cacheSize: 256
      evictTimeoutSeconds: 10

Note: when use some driver like pgx, then disable statements, cause driver has handled statements.

Isolation:

  • Default: 0
  • ReadUncommitted: 1
  • ReadCommitted: 2
  • WriteCommitted: 3
  • RepeatableRead: 4
  • Snapshot: 5
  • Serializable: 6
  • Linearizable: 7
Import driver
import _ "github.com/go-sql-driver/mysql"
Deploy
app.Deply(sql.New())
Proxy usage
// begin transaction 
sql.Begin(ctx)
// commit transaction
sql.Commit(ctx)
// rollback transaction
sql.Rollback(ctx)
// query
sql.Query(ctx, querySQL, ...)
// execute
sql.Execute(ctx, executeSQL, ...)
Code generator in fn

Add annotation code writer

generates.New(generates.WithAnnotations(sql.FAG()...))

Use @sql:transaction annotation. params are readonly and isolation.

  • readonly: set the transaction to be readonly.
  • isolation: use spec isolation. default is use isolation of config.
    • ReadCommitted
    • ReadUncommitted
    • WriteCommitted
    • RepeatableRead
    • Snapshot
    • Serializable
    • Linearizable
// @fn some
// ... some func use transaction
// @sql:transaction
func some(ctx context.Context, param Param) (result Result, err error) {
	// ...
	return
}

Use @sql:use annotation to switch datasource service. param is service name.

// @fn some
// ... some func use transaction
// @sql:use postgres1
func some(ctx context.Context, param Param) (result Result, err error) {
	// ...
	return
}
ORM
Multi sources

use multi database service to implements

Config:

postgres1:
  kind: "standalone"

mysql1:
  kind: "standalone"

Deploy:

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

Proxy

sql.Query(sql.Use(ctx, "postgres1"), querySQL, ...)
sql.Query(sql.Use(ctx, "mysql1"), querySQL, ...)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Begin added in v1.2.1

func Begin(ctx context.Context, options ...databases.TransactionOption) (err error)

func Commit added in v1.2.1

func Commit(ctx context.Context) (err error)

func Dialect added in v1.0.0

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

func Disuse added in v1.2.1

func Disuse(ctx context.Context) context.Context

func Execute

func Execute(ctx context.Context, query []byte, arguments ...interface{}) (result databases.Result, err error)

func FAG added in v1.2.1

func ForceDialect added in v1.2.1

func ForceDialect(ctx context.Context, dialect string) context.Context

func New added in v1.2.1

func New(options ...Option) (v services.Service)

func Readonly added in v1.2.1

func Readonly() databases.TransactionOption

func Rollback added in v1.2.1

func Rollback(ctx context.Context)

func Use added in v1.2.1

func Use(ctx context.Context, endpointName []byte) context.Context

func WithIsolation added in v1.2.1

func WithIsolation(isolation databases.Isolation) databases.TransactionOption

Types

type Argument added in v1.2.1

type Argument struct {
	Name  string          `json:"name" avro:"name"`
	Nil   bool            `json:"nil" avro:"nil"`
	Type  string          `json:"type" avro:"type"`
	Value avro.RawMessage `json:"value" avro:"value"`
}

func NewArgument added in v1.2.1

func NewArgument(v any) (argument Argument, err error)

func (Argument) Interface added in v1.2.1

func (argument Argument) Interface() (v any, err error)

type Arguments added in v1.0.0

type Arguments []any

func (Arguments) Len added in v1.2.1

func (arguments Arguments) Len() (n int)

func (Arguments) MarshalAvro added in v1.2.41

func (arguments Arguments) MarshalAvro() (p []byte, err error)

func (Arguments) MarshalJSON added in v1.0.0

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

func (*Arguments) UnmarshalAvro added in v1.2.41

func (arguments *Arguments) UnmarshalAvro(p []byte) (err error)

func (*Arguments) UnmarshalJSON added in v1.0.0

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

type Column

type Column struct {
	Valid bool            `json:"valid" avro:"valid"`
	Value avro.RawMessage `json:"value" avro:"value"`
}

func (*Column) Bool added in v1.2.1

func (c *Column) Bool() (v bool, err error)

func (*Column) Byte added in v1.2.1

func (c *Column) Byte() (v byte, err error)

func (*Column) Bytes added in v1.2.1

func (c *Column) Bytes() (v []byte, err error)

func (*Column) Date added in v1.2.1

func (c *Column) Date() (v times.Date, err error)

func (*Column) Datetime added in v1.2.1

func (c *Column) Datetime() (v time.Time, err error)

func (*Column) Float added in v1.2.1

func (c *Column) Float() (v float64, err error)

func (*Column) Int added in v1.2.1

func (c *Column) Int() (v int64, err error)

func (*Column) Reset added in v1.2.1

func (c *Column) Reset()

func (*Column) Scan

func (c *Column) Scan(src any) (err error)

func (*Column) String added in v1.2.1

func (c *Column) String() (v string, err error)

func (*Column) Time added in v1.2.1

func (c *Column) Time() (v times.Time, err error)

type ColumnType

type ColumnType struct {
	Name         string `json:"name" avro:"name"`
	DatabaseType string `json:"databaseType" avro:"databaseType"`
	Type         string `json:"type" avro:"type"`
}

func NewColumnType added in v1.2.1

func NewColumnType(name string, databaseType string, scanType reflect.Type) (v ColumnType)

func (ColumnType) ScanValue added in v1.2.1

func (ct ColumnType) ScanValue() (sv any)

type Columns added in v1.2.1

type Columns []any

type Config

type Config struct {
	Kind              string              `json:"kind"`
	Isolation         databases.Isolation `json:"isolation"`
	TransactionMaxAge int                 `json:"transactionMaxAge"`
	DebugLog          bool                `json:"debugLog"`
	SSL               SSLConfig           `json:"ssl"`
	Options           json.RawMessage     `json:"options"`
}

type NullBool added in v1.2.1

type NullBool struct {
	sql.NullBool
}

func NewNullBool added in v1.2.1

func NewNullBool(b bool) NullBool

func (NullBool) MarshalJSON added in v1.2.1

func (n NullBool) MarshalJSON() (p []byte, err error)

func (*NullBool) UnmarshalJSON added in v1.2.1

func (n *NullBool) UnmarshalJSON(p []byte) error

type NullByte added in v1.2.1

type NullByte struct {
	sql.NullByte
}

func NewNullByte added in v1.2.1

func NewNullByte(b byte) NullByte

func (NullByte) MarshalJSON added in v1.2.1

func (n NullByte) MarshalJSON() (p []byte, err error)

func (*NullByte) UnmarshalJSON added in v1.2.1

func (n *NullByte) UnmarshalJSON(p []byte) error

type NullBytes added in v1.2.1

type NullBytes struct {
	Valid bool
	Bytes []byte
}

func NewNullBytes added in v1.2.1

func NewNullBytes(p []byte) NullBytes

func (NullBytes) MarshalJSON added in v1.2.1

func (n NullBytes) MarshalJSON() ([]byte, error)

func (*NullBytes) Scan added in v1.2.1

func (n *NullBytes) Scan(src any) error

func (*NullBytes) UnmarshalJSON added in v1.2.1

func (n *NullBytes) UnmarshalJSON(p []byte) error

func (NullBytes) Value added in v1.2.1

func (n NullBytes) Value() (driver.Value, error)

type NullDate added in v1.0.0

type NullDate struct {
	Valid bool
	Date  times.Date
}

func NewNullDate added in v1.2.1

func NewNullDate(v times.Date) NullDate

func (NullDate) MarshalJSON added in v1.2.1

func (n NullDate) MarshalJSON() (p []byte, err error)

func (*NullDate) Scan added in v1.0.0

func (n *NullDate) Scan(src any) error

func (*NullDate) UnmarshalJSON added in v1.2.1

func (n *NullDate) UnmarshalJSON(p []byte) error

func (NullDate) Value added in v1.0.0

func (n NullDate) Value() (driver.Value, error)

type NullDatetime added in v1.2.1

type NullDatetime struct {
	sql.NullTime
}

func NewNullDatetime added in v1.2.1

func NewNullDatetime(t time.Time) NullDatetime

func (NullDatetime) MarshalJSON added in v1.2.1

func (n NullDatetime) MarshalJSON() (p []byte, err error)

func (*NullDatetime) UnmarshalJSON added in v1.2.1

func (n *NullDatetime) UnmarshalJSON(p []byte) error

type NullFloat64 added in v1.2.1

type NullFloat64 struct {
	sql.NullFloat64
}

func NewNullFloat64 added in v1.2.1

func NewNullFloat64(n float64) NullFloat64

func (NullFloat64) MarshalJSON added in v1.2.1

func (n NullFloat64) MarshalJSON() (p []byte, err error)

func (*NullFloat64) UnmarshalJSON added in v1.2.1

func (n *NullFloat64) UnmarshalJSON(p []byte) error

type NullInt16 added in v1.2.1

type NullInt16 struct {
	sql.NullInt16
}

func NewNullInt16 added in v1.2.1

func NewNullInt16(n int16) NullInt16

func (NullInt16) MarshalJSON added in v1.2.1

func (n NullInt16) MarshalJSON() (p []byte, err error)

func (*NullInt16) UnmarshalJSON added in v1.2.1

func (n *NullInt16) UnmarshalJSON(p []byte) error

type NullInt32 added in v1.2.1

type NullInt32 struct {
	sql.NullInt32
}

func NewNullInt32 added in v1.2.1

func NewNullInt32(n int32) NullInt32

func (NullInt32) MarshalJSON added in v1.2.1

func (n NullInt32) MarshalJSON() (p []byte, err error)

func (*NullInt32) UnmarshalJSON added in v1.2.1

func (n *NullInt32) UnmarshalJSON(p []byte) error

type NullInt64 added in v1.2.1

type NullInt64 struct {
	sql.NullInt64
}

func NewNullInt64 added in v1.2.1

func NewNullInt64(n int64) NullInt64

func (NullInt64) MarshalJSON added in v1.2.1

func (n NullInt64) MarshalJSON() (p []byte, err error)

func (*NullInt64) UnmarshalJSON added in v1.2.1

func (n *NullInt64) UnmarshalJSON(p []byte) error

type NullJson

type NullJson[E any] struct {
	Valid bool
	E     E
}

func NewNullJson added in v1.2.1

func NewNullJson[E any](e E) NullJson[E]

func (NullJson[E]) MarshalJSON added in v1.2.1

func (n NullJson[E]) MarshalJSON() ([]byte, error)

func (*NullJson[E]) Scan

func (n *NullJson[E]) Scan(src any) error

func (*NullJson[E]) UnmarshalJSON added in v1.2.1

func (n *NullJson[E]) UnmarshalJSON(p []byte) error

func (NullJson[E]) Value added in v1.2.1

func (n NullJson[E]) Value() (driver.Value, error)

type NullString added in v1.2.1

type NullString struct {
	sql.NullString
}

func NewNullString added in v1.2.1

func NewNullString(s string) NullString

func (NullString) MarshalJSON added in v1.2.1

func (n NullString) MarshalJSON() (p []byte, err error)

func (*NullString) UnmarshalJSON added in v1.2.1

func (n *NullString) UnmarshalJSON(p []byte) error

type NullTime added in v1.0.0

type NullTime struct {
	Valid bool
	Time  times.Time
}

func NewNullTime added in v1.2.1

func NewNullTime(v times.Time) NullTime

func (NullTime) MarshalJSON added in v1.2.1

func (n NullTime) MarshalJSON() (p []byte, err error)

func (*NullTime) Scan added in v1.0.0

func (n *NullTime) Scan(src any) error

func (*NullTime) UnmarshalJSON added in v1.2.1

func (n *NullTime) UnmarshalJSON(p []byte) error

func (NullTime) Value added in v1.0.0

func (n NullTime) Value() (driver.Value, error)

type Option added in v1.2.1

type Option func(options *Options)

func WithDatabase added in v1.2.1

func WithDatabase(db databases.Database) Option

func WithDialect added in v1.2.1

func WithDialect(dialect string) Option

func WithName added in v1.2.1

func WithName(name string) Option

func WithTLS added in v1.2.11

func WithTLS(fn RegisterTLSFunc) Option

type Options added in v1.2.1

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

type RegisterTLSFunc added in v1.2.11

type RegisterTLSFunc func(config *tls.Config) (err error)

type Row

type Row []Column

type Rows

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

func NewRows

func NewRows(rows databases.Rows) (v Rows, err error)

func Query

func Query(ctx context.Context, query []byte, arguments ...interface{}) (v Rows, err error)

func (*Rows) Close added in v1.2.1

func (rows *Rows) Close() error

func (*Rows) Columns added in v1.2.1

func (rows *Rows) Columns() []ColumnType

func (Rows) MarshalAvro added in v1.2.41

func (rows Rows) MarshalAvro() (p []byte, err error)

func (Rows) MarshalJSON added in v0.9.6

func (rows Rows) MarshalJSON() (p []byte, err error)

func (*Rows) Next added in v0.9.6

func (rows *Rows) Next() (ok bool)

func (*Rows) Scan

func (rows *Rows) Scan(dst ...any) (err error)

Scan element of dst must be scanned. in dac case, when field is json kind and type does not implement sql.NullJson, then wrap field value by sql.NullJson

func (*Rows) UnmarshalAvro added in v1.2.41

func (rows *Rows) UnmarshalAvro(p []byte) (err error)

func (*Rows) UnmarshalJSON added in v0.9.6

func (rows *Rows) UnmarshalJSON(p []byte) (err error)

type SSLConfig added in v1.2.11

type SSLConfig struct {
	Enable bool `json:"enable"`
	configs.Client
}

type Scanner added in v1.2.1

type Scanner interface {
	sql.Scanner
	driver.Valuer
	json.Marshaler
	json.Unmarshaler
}

Directories

Path Synopsis
dac

Jump to

Keyboard shortcuts

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