orm

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: BSD-3-Clause Imports: 8 Imported by: 1

README

go-orm

Coverage Status Release Go Report Card Build Status

Introduction

The library provides a nice and simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" that is used to interact with this table. Models allow you to query data in tables, as well as insert new records in the table.

Examples

Init connection

package main

import (
	"context"
	"database/sql"

	"github.com/deweppro/go-orm"
	"github.com/deweppro/go-orm/plugins"
	"github.com/deweppro/go-orm/schema/mysql"
)

func main() {
	conn := mysql.New(&mysql.Config{
		Pool: []mysql.Item{
			{
				Name:     "main_db",
				Host:     "127.0.0.1",
				Port:     3306,
				Schema:   "test_table",
				User:     "demo",
				Password: "1234",
			},
		}})
	defer conn.Close()
	if err := conn.Reconnect(); err != nil {
		panic(err.Error())
	}

	db := orm.NewDB(conn, orm.Plugins{Logger: plugins.StdOutLog, Metrics: plugins.StdOutMetric})
	pool := db.Pool("main_db")

	if err := pool.Ping(); err != nil {
		panic(err.Error())
	}

	// use pool[main_db] here
	err := pool.CallContext("query name", context.Background(), func(ctx context.Context, db *sql.DB) error {...}
	err := pool.TxContext("query name", context.Background(), func(context.Context, *sql.Tx) error) error {...}
}

Basic query

package main

import (
	"context"
	"database/sql"

	"github.com/deweppro/go-orm"
	"github.com/deweppro/go-orm/plugins"
	"github.com/deweppro/go-orm/schema/mysql"
)

func main() {
	...

	var userName string
	err := pool.CallContext("user_name", context.Background(), func(ctx context.Context, db *sql.DB) error {
		rows, err := db.QueryContext(ctx, "select `name` from `users` where `id`=?", 10)
		if err != nil {
			return err
		}
		defer rows.Close()

		for rows.Next() {
			if err = rows.Scan(&userName); err != nil {
				return err
			}
		}
		if err = rows.Close(); err != nil {
			return err
		}
		if err = rows.Err(); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		panic(err.Error())
	}
}

Lite query

package main

import (
	"context"
	"fmt"

	"github.com/deweppro/go-orm"
	"github.com/deweppro/go-orm/plugins"
	"github.com/deweppro/go-orm/schema/mysql"
)

func main() {
	...

	var userName string
	err := pool.QueryContext("user_name", context.Background(), func(q orm.Querier) {
		q.SQL("select `name` from `users` limit 1")
		q.Bind(func(bind orm.Scanner) error {
			return bind.Scan(&userName)
		})
	})

	err = pool.ExecContext("user_name", context.Background(), func(e orm.Executor) {
		e.SQL("insert into `users` (`id`, `name`) values (?, ?);")
		e.Params(3, "cccc")

		e.Bind(func(result orm.Result) error {
			fmt.Printf("RowsAffected=%d LastInsertId=%d", result.RowsAffected, result.LastInsertId)
			return nil
		})
	})

	err = pool.TransactionContext("", context.Background(), func(v orm.Tx) {
		v.Exec(func(e orm.Executor) {
			e.SQL("insert into `users` (`id`, `name`) values (?, ?);")
			e.Params(3, "cccc")

			e.Bind(func(result orm.Result) error {
				fmt.Printf("RowsAffected=%d LastInsertId=%d", result.RowsAffected, result.LastInsertId)
				return nil
			})
		})
		v.Query(func(q orm.Querier) {
			q.SQL("select `name` from `users` limit 1")
			q.Bind(func(bind orm.Scanner) error {
				return bind.Scan(&userName)
			})
		})
	})
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrInvalidModelPool if sync pool has invalid model type
	ErrInvalidModelPool = errors.New("invalid internal model pool")
)

Functions

This section is empty.

Types

type DB

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

DB connection storage

func NewDB

func NewDB(c schema.Connector, plug Plugins) *DB

NewDB init database connections

func (*DB) Pool

func (d *DB) Pool(name string) *Stmt

Pool getting pool connections by name

type Executor added in v1.1.0

type Executor interface {
	SQL(query string, args ...interface{})
	Params(args ...interface{})
	Bind(call func(result Result) error)
}

Executor interface for generate execute query

type Plugins

type Plugins struct {
	Logger  logger.Logger
	Metrics plugins.MetricGetter
}

Plugins storage

type Querier added in v1.1.0

type Querier interface {
	SQL(query string, args ...interface{})
	Bind(call func(bind Scanner) error)
}

Querier interface for generate query

type Result

type Result struct {
	RowsAffected int64
	LastInsertId int64
}

Result exec result model

type Scanner added in v1.1.0

type Scanner interface {
	Scan(args ...interface{}) error
}

Scanner interface for bind data

type Stmt

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

Stmt statement model

func (*Stmt) CallContext added in v1.1.0

func (s *Stmt) CallContext(name string, ctx context.Context, callFunc func(context.Context, *sql.DB) error) error

CallContext basic query execution

func (*Stmt) ExecContext added in v1.1.0

func (s *Stmt) ExecContext(name string, ctx context.Context, call func(q Executor)) error

ExecContext ...

func (*Stmt) Ping

func (s *Stmt) Ping() error

Ping database ping

func (*Stmt) QueryContext added in v1.1.0

func (s *Stmt) QueryContext(name string, ctx context.Context, call func(q Querier)) error

QueryContext ...

func (*Stmt) TransactionContext added in v1.1.0

func (s *Stmt) TransactionContext(name string, ctx context.Context, call func(v Tx)) error

func (*Stmt) TxContext added in v1.1.0

func (s *Stmt) TxContext(name string, ctx context.Context, callFunc func(context.Context, *sql.Tx) error) error

TxContext the basic execution of a query in a transaction

type Tx added in v1.1.0

type Tx interface {
	Exec(vv ...func(e Executor))
	Query(vv ...func(q Querier))
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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