eorm

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2021 License: MIT Imports: 8 Imported by: 0

README

eorm

一个orm

实例

import (
	"context"
	"log"
	"testing"

	_ "github.com/go-sql-driver/mysql"
)

func Newclient() (client *Client, err error) {
	setting := Settings{
		DriverName: "mysql",
		User:       "root",
		Password:   "123456",
		Database:   "sql_demo",
		Host:       "127.0.0.1:3306",
		Options:    map[string]string{"charset": "utf8mb4"},
	}
	return NewClient(setting)
}

type User struct {
	Id       int64  `eorm:"id"`
	User_id  int64  `eorm:"user_id"`
	Username string `eorm:"username"`
	Password string `eorm:"password"`
}

func TestEorm_Insert(t *testing.T) {
	user := &User{
		User_id:  7345893745987349850,
		Username: "songzhichao",
		Password: "xxx",
	}

	statement := NewStatement()
	statement = statement.SetTableName("user").InsertStruct(user)

	client, _ := Newclient()
	client.Insert(context.Background(), statement)
}

func TestSession_FindOne(t *testing.T) {
	statement := NewStatement()
	statement = statement.SetTableName("user").
		AndEqual("username", "yixingwei").
		Select("user_id,username,password")

	client, err := Newclient()
	if err != nil {
		log.Println(err)
		return
	}

	user := &User{}
	_ = client.FindOne(context.Background(), statement, user)

	log.Println(user)

}

func TestSession_Delete(t *testing.T) {
	statement := NewStatement()
	statement = statement.SetTableName("user").
		AndEqual("username", "yixingwei")
	client, _ := Newclient()
	client.Delete(context.Background(), statement)
}

func TestSession_Update(t *testing.T) {
	user := &User{
		User_id:  7345893745987349850,
		Username: "songzhichao",
		Password: "szcdmm",
	}

	statement := NewStatement()
	statement = statement.SetTableName("user").
		AndEqual("username", "songzhichao").
		UpdateStruct(user)

	client, _ := Newclient()
	client.Update(context.Background(), statement)
}

func TestSession_FindAll(t *testing.T) {
	statement := NewStatement()
	statement = statement.SetTableName("user").
		AndLessThan("id", "6").
		Select("id,user_id,username,password")

	client, err := Newclient()
	if err != nil {
		log.Println(err)
		return
	}

	var user []User
	_ = client.FindAll(context.Background(), statement, &user)
	log.Println(user)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clause

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

Clause 条款、子句

func (*Clause) Build

func (this *Clause) Build(orders ...Type)

拼接各个sql语句

func (*Clause) Set

func (this *Clause) Set(operation Type, param ...interface{})

根据关键字构建sql语句

func (*Clause) SetCondition

func (this *Clause) SetCondition(name Type, sql string, vars []interface{})

func (*Clause) SetTableName

func (this *Clause) SetTableName(tablename string) *Clause

type Client

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

func NewClient

func NewClient(setting Settings) (c *Client, err error)

实例化一个客户端(新建一个数据库链接)

func NewClientWithDBconn

func NewClientWithDBconn(db *sql.DB) (c *Client, err error)

func (*Client) Close

func (c *Client) Close()

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, statement *Statement) (int64, error)

func (*Client) FindAll

func (c *Client) FindAll(ctx context.Context, statement *Statement, dest interface{}) (err error)

func (*Client) FindOne

func (c *Client) FindOne(ctx context.Context, statement *Statement, dest interface{}) (err error)

func (*Client) Insert

func (c *Client) Insert(ctx context.Context, statement *Statement) (int64, error)

func (*Client) Transaction

func (c *Client) Transaction(f TxFunc) (result interface{}, err error)

事务支持

func (*Client) Update

func (c *Client) Update(ctx context.Context, statement *Statement) (int64, error)

type CommonDB

type CommonDB interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	Exec(query string, args ...interface{}) (sql.Result, error)
}

CommonDB is a minimal function set of db

type Field

type Field struct {
	Name string

	Type        string
	TableColumn string
	Tag         string
	// contains filtered or unexported fields
}

type Operation

type Operation int

type Schema

type Schema struct {
	Fields     []*Field          //字段属性组合
	FieldNames []string          //字段名称
	FieldTags  []string          //tag名称
	FieldMap   map[string]*Field // key:value
}

func StructForType

func StructForType(t reflect.Type) *Schema

func (*Schema) RecordValues

func (this *Schema) RecordValues(dest interface{}) []interface{}

func (*Schema) UpdateParam

func (this *Schema) UpdateParam(dest interface{}) map[string]interface{}

type Session

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

func NewSession

func NewSession(db *sql.DB) *Session

func (*Session) Begin

func (s *Session) Begin() (err error)

func (*Session) Clear

func (s *Session) Clear()

func (*Session) Commit

func (s *Session) Commit() (err error)

func (*Session) DB

func (s *Session) DB() CommonDB

DB return tx if a tx begins otherwise return *sql.DB

func (*Session) Exec

func (s *Session) Exec() (result sql.Result, err error)

func (*Session) Query

func (s *Session) Query() (rows *sql.Rows, err error)

func (*Session) QueryRow

func (s *Session) QueryRow() *sql.Row

func (*Session) Raw

func (s *Session) Raw(sql string, values ...interface{}) *Session

func (*Session) Rollback

func (s *Session) Rollback() (err error)

type Settings

type Settings struct {
	DriverName string

	User     string
	Password string
	Database string
	Host     string

	Options map[string]string

	MaxOpenConns int
	MaxIdleConns int

	LoggingEnabled bool
}

func (*Settings) DataSourceName

func (s *Settings) DataSourceName() string

type Statement

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

条件组装 用户API层

func NewStatement

func NewStatement() *Statement

func (*Statement) AndEqual

func (s *Statement) AndEqual(field string, value interface{}) *Statement

where条件

func (*Statement) AndGreaterThan

func (s *Statement) AndGreaterThan(field string, value interface{}) *Statement

func (*Statement) AndLessThan

func (s *Statement) AndLessThan(field string, value interface{}) *Statement

func (*Statement) AndLike

func (s *Statement) AndLike(field string, value interface{}) *Statement

func (*Statement) InsertStruct

func (s *Statement) InsertStruct(vars interface{}) *Statement

新增数据API

func (*Statement) OrEqual

func (s *Statement) OrEqual(field string, value interface{}) *Statement

func (*Statement) OrGreaterThan

func (s *Statement) OrGreaterThan(field string, value interface{}) *Statement

func (*Statement) OrLessThan

func (s *Statement) OrLessThan(field string, value interface{}) *Statement

func (*Statement) OrLike

func (s *Statement) OrLike(field string, value interface{}) *Statement

func (*Statement) Select

func (s *Statement) Select(field ...string) *Statement

Select

func (*Statement) SetTableName

func (s *Statement) SetTableName(tableName string) *Statement

SetTableName 设置表名

func (*Statement) UpdateStruct

func (s *Statement) UpdateStruct(vars interface{}) *Statement

修改数据API

type TxFunc

type TxFunc func(ctx context.Context, client *Client) (interface{}, error)

type Type

type Type int
const (
	Insert Type = iota
	Value
	Update
	Delete
	Limit
	Select
	Where
	Condition
)

Jump to

Keyboard shortcuts

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