bur

package module
v0.0.0-...-97724e5 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

bur

An ORM framework implementation in Go(based on sg).

Go Report Card GoDoc

Quickstart
package main

import (
  "database/sql"
  "fmt"
  . "github.com/go-the-way/bur"
  "github.com/go-the-way/sg"
  _ "github.com/go-sql-driver/mysql"
)

const (
  testDriverName = "mysql"
  testDSN        = "root:oGMyxw4auP6F6Sn1ENxMVTa1kCc=@tcp(192.168.0.252:3310)/test"
)

var (
  testDB, _ = sql.Open(testDriverName, testDSN)
)

type userModel struct {
  ID         int    `orm:"pk{T} column{id} insertIgnore{T} definition{id int not null auto_increment comment 'ID'}"`
  Name       string `orm:"pk{F} column{name} definition{name varchar(50) not null default 'hello world' comment 'Name'}"`
  Age        int    `orm:"pk{F} column{age} definition{age int not null default '20' comment 'Age'}"`
  Address    string `orm:"pk{F} column{address} definition{address varchar(100) not null comment 'Address'}"`
  Phone      string `orm:"pk{F} column{phone} definition{phone varchar(11) not null default '13900000000' comment 'Phone'}"`
  CreateTime string `orm:"pk{F} column{create_time} insertIgnore{T} updateIgnore{T} definition{create_time datetime not null default current_timestamp comment 'CreateTime'}"`
  XYZ        string `orm:"pk{F} column{xyz} definition{xyz varchar(50) not null default 'xyz' comment 'XYZ'}"`
}

func (u *userModel) MetaData() *ModelMeta {
  return &ModelMeta{
    Migrate: true,
    Comment: "The userModel Table",
    IndexDefinitions: []sg.Ge{
      sg.IndexDefinition(false, sg.P("idx_name"), sg.C("name")),
    },
    InsertIgnores: []sg.C{"id", "create_time"},
  }   
}

func main() {
  DS(testDB)
  Register(new(userModel))
  o := New(new(userModel))
  count, err := o.Select().Count(&userModel{ID: 1})
  if err != nil {
    fmt.Println(err)
  } else {
    fmt.Println("count = ", count)
  }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Configuration = &Config{
		Migrate:            false,
		TableNameStrategy:  Underline,
		ColumnNameStrategy: Underline,
		Logger:             logrus.StandardLogger(),
		InsertHookers:      make([]ExecHooker, 0),
		DeleteHookers:      make([]ExecHooker, 0),
		UpdateHookers:      make([]ExecHooker, 0),
		SelectHookers:      make([]ExecHooker, 0),
	}
)
View Source
var (
	MySQLPagination = &mysqlPagination{}
)

Functions

func DS

func DS(db *sql.DB)

func DSWithName

func DSWithName(name string, db *sql.DB)

func New

func New(model Model) *orm

New defines return a new orm from Model model

func NewWithDS

func NewWithDS(model Model, ds string) *orm

NewWithDS defines return a new orm using named DS from Model model

func NewWithTx

func NewWithTx(model Model, tx *sql.Tx, autoCommit bool) *orm

NewWithTx defines return a new orm using Tx

func NullBool

func NullBool(b bool) sql.NullBool

func NullBoolPtr

func NullBoolPtr(b bool) *sql.NullBool

func NullByte

func NullByte(b byte) sql.NullByte

func NullBytePtr

func NullBytePtr(b byte) *sql.NullByte

func NullFloat64

func NullFloat64(f float64) sql.NullFloat64

func NullFloat64Ptr

func NullFloat64Ptr(f float64) *sql.NullFloat64

func NullInt16

func NullInt16(i int16) sql.NullInt16

func NullInt16Ptr

func NullInt16Ptr(i int16) *sql.NullInt16

func NullInt32

func NullInt32(i int32) sql.NullInt32

func NullInt32Ptr

func NullInt32Ptr(i int32) *sql.NullInt32

func NullInt64

func NullInt64(i int64) sql.NullInt64

func NullInt64Ptr

func NullInt64Ptr(i int64) *sql.NullInt64

func NullString

func NullString(str string) sql.NullString

func NullStringPtr

func NullStringPtr(str string) *sql.NullString

func NullTime

func NullTime(t time.Time) sql.NullTime

func NullTimePtr

func NullTimePtr(t time.Time) *sql.NullTime

func Register

func Register(model Model)

Register defines register a Model struct for bur

func SetConfig

func SetConfig(config *Config)

SetConfig define Set configuration for bur

Types

type Config

type Config struct {
	// Migrate defines migrate create or update table in the database
	Migrate bool
	// TableNameStrategy defines binding table name for Model struct
	TableNameStrategy Strategy
	// TableNameStrategy defines binding table's column name for Model's Fields
	ColumnNameStrategy Strategy
	// Logger defines logger for bur
	Logger        *logrus.Logger
	InsertHookers []ExecHooker
	DeleteHookers []ExecHooker
	UpdateHookers []ExecHooker
	SelectHookers []ExecHooker
}

Config defines configuration struct

type ExecHooker

type ExecHooker interface {
	BeforeExec(model Model, sqlStr *string, ps *[]interface{})
	AfterExec(model Model, sqlStr string, ps []interface{}, err error)
}

ExecHooker defines exec hooker before and after exec SQL

type Model

type Model interface {
	MetaData() *ModelMeta
}

Model defines standard Model interface and implements to marked a Model struct

type ModelMeta

type ModelMeta struct {
	// Migrate defines migrate create or update table in the database
	Migrate bool
	// Table defines table name for the Model
	// If Table is empty, set from Configuration's TableNameStrategy
	Table string
	// Comment defines table comment for the Model
	Comment string
	// Table defines column name Strategy for the Model's Fields
	// see strategy.Default, strategy.Underline, strategy.CamelCase,
	ColumnNameStrategy Strategy
	// DB defines DS name from dsMap, default: `_` or `master`
	DS string
	// PrimaryKeyColumns defines Table primary key columns
	PrimaryKeyColumns []sg.C
	// ColumnDefinitions defines Table column definitions
	// Use sg.ColumnDefinition to build
	ColumnDefinitions []sg.Ge
	// Indexes defines Table index definitions
	// Use sg.IndexDefinition to build
	IndexDefinitions []sg.Ge
	// InsertIgnores defines Table insert ignore columns
	InsertIgnores []sg.C
	// UpdateIgnores defines Table update ignore columns
	UpdateIgnores []sg.C
}

ModelMeta defines Model struct meta info

type Pagination

type Pagination interface {
	Page(originalSql string, offset, size int) (string, []interface{})
}

Pagination defines Pagination plugin

type Strategy

type Strategy int
const (
	// Default defines default not changed
	// Model:User  => Table:User
	// Field:UserName  => Column:UserName
	Default Strategy = 1 + iota
	// Underline defines to transform a underline style name
	// Model:UserModel => Table:user_model
	// Field:UserName => Column:user_name
	Underline
	// CamelCase defines to transform a camelcase style name
	// Model:UserModel => Table:userModel
	// Field:UserName => Column:userName
	CamelCase
)

Jump to

Keyboard shortcuts

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