rds

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Example (Delete)
package main

import (
	"context"
	"fmt"
	"github.com/sqjian/go-kit/rds"
)

var db *rds.Rds

func main() {
	ctx := context.Background()

	fmt.Println(db.Delete(ctx, "test", map[string]any{"age": 5}))
}
Output:

Example (Init)
package main

import (
	"github.com/sqjian/go-kit/log"
	"github.com/sqjian/go-kit/rds"
	"time"
)

var db *rds.Rds

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	_db, dbErr := rds.NewRds(
		rds.Mysql,
		rds.WithIp("192.168.6.6"),
		rds.WithPort("3306"),
		rds.WithUserName("root"),
		rds.WithPassWord("xylx1.t!@#"),
		rds.WithMaxLifeTime(time.Second),
		rds.WithMaxIdleConns(3),
		rds.WithDbName("test"),
		rds.WithLogger(func() log.Log { inst, _ := log.NewLogger(log.WithLevel("dummy")); return inst }()),
	)
	checkErr(dbErr)

	db = _db
}
Output:

Example (Insert)
package main

import (
	"context"
	"fmt"
	"github.com/sqjian/go-kit/rds"
)

var db *rds.Rds

func main() {
	ctx := context.WithValue(context.Background(), "id", 1)
	fmt.Println(db.Insert(ctx, "test", map[string]any{"age": 1}))
}
Output:

Example (MysqlQuery)
package main

import (
	"context"
	"fmt"
	"github.com/sqjian/go-kit/rds"
)

var db *rds.Rds

func main() {
	ctx := context.WithValue(context.Background(), "id", 1)
	rst, err := db.Query(ctx, []string{"test"}, nil)
	if err != nil {
		panic(err)
	}
	for k, v := range rst {
		fmt.Printf("k:%v,v:%v", k, v)
	}
	fmt.Println(string((rst[0]["create_time"]).([]byte)))
}
Output:

Example (Update)
package main

import (
	"context"
	"fmt"
	"github.com/sqjian/go-kit/rds"
)

var db *rds.Rds

func main() {
	ctx := context.WithValue(context.Background(), "id", 1)

	fmt.Println(db.Update(ctx, "test", map[string]any{"age": 5}, map[string]any{"age": 1}))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithPlaceholder

func WithPlaceholder(placeholder string) instructOptionFunc

Types

type ConfigOptionFunc

type ConfigOptionFunc func(*config)

func WithDbName

func WithDbName(dbName string) ConfigOptionFunc

func WithIp

func WithIp(ip string) ConfigOptionFunc

func WithLogger

func WithLogger(logger log.Log) ConfigOptionFunc

func WithMaxIdleConns

func WithMaxIdleConns(MaxIdleConns int) ConfigOptionFunc

func WithMaxLifeTime

func WithMaxLifeTime(MaxLifeTime time.Duration) ConfigOptionFunc

func WithPassWord

func WithPassWord(PassWord string) ConfigOptionFunc

func WithPort

func WithPort(port string) ConfigOptionFunc

func WithUserName

func WithUserName(UserName string) ConfigOptionFunc

type Err

type Err int
const (
	UnknownErrCode Err = iota
	IllegalParams
)

func (Err) String

func (i Err) String() string

type Instruct

type Instruct struct {
	Sql  string
	Args []any
}

type QueryOption

type QueryOption interface {
	// contains filtered or unexported methods
}

type QueryOptionFunc

type QueryOptionFunc func(*SqlOption)

func WithColumn

func WithColumn(column []string) QueryOptionFunc

func WithWhere

func WithWhere(where map[string]any) QueryOptionFunc

type Rds added in v0.1.6

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

func NewRds added in v0.1.6

func NewRds(dbType Type, opts ...ConfigOptionFunc) (*Rds, error)

func (*Rds) Delete added in v0.1.9

func (r *Rds) Delete(ctx context.Context, table string, where map[string]any, opts ...QueryOptionFunc) (map[string]int64, error)

func (*Rds) Insert added in v0.1.9

func (r *Rds) Insert(ctx context.Context, table string, data map[string]any, opts ...QueryOptionFunc) (map[string]int64, error)

func (*Rds) Query added in v0.1.9

func (r *Rds) Query(ctx context.Context, table []string, opts ...QueryOptionFunc) ([]map[string]any, error)

func (Rds) Stub added in v0.1.9

func (c Rds) Stub() *Rds

func (*Rds) Update added in v0.1.9

func (r *Rds) Update(ctx context.Context, table string, data map[string]any, where map[string]any, opts ...QueryOptionFunc) (map[string]int64, error)

type SqlOption

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

type Type

type Type int64
const (
	UnknownType Type = iota
	Mysql
	Sqlite
)

func (Type) String

func (i Type) String() string

Jump to

Keyboard shortcuts

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