aorm

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

README

arom

介绍

基于 go-sqlx封装的orm库 (就是将原来的orm库与sqlx合并)

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Constants

View Source
const (
	InfoLevel = iota
	ErrorLevel
	Disabled
)

log levels 新增

Variables

View Source
var (
	Error  = errorLog.Println
	Errorf = errorLog.Printf
	Info   = infoLog.Println
	Infof  = infoLog.Printf
)

log methods 新增

View Source
var (
	TagKey string
	Ignore string
)

Functions

func DefaultLogger

func DefaultLogger() func(e *Engine)

DefaultLogger 默认日志记录器。

func SetLevel

func SetLevel(level int)

新增 函数

func StructForScan

func StructForScan(pointer interface{}) []interface{}

输入一个结构体,返回这个结构体所有字段。

func StructToMap

func StructToMap(structObj interface{}) map[string]interface{}

StructToMap ...参数<structObj>是结构体对象

Types

type Clause

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

func (*Clause) Build

func (c *Clause) Build(orders ...Type) (string, []interface{})

Build 方法根据传入的 Type 的顺序,构造出最终的 SQL 语句。

func (*Clause) Set

func (c *Clause) Set(name Type, vars ...interface{})

Set 方法根据 Type 调用对应的 generator,生成该子句对应的 SQL 语句。

type Config

type Config struct {
	Driver string `json:"driver"` // 驱动: mysql/sqlite3/oracle/mssql/postgres/clickhouse, 如果集群配置了驱动, 这里可以省略
	// mysql 示例:
	// root:root@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=true
	Dsn            string `json:"dsn"`            // 数据库链接
	SetMaxOpenConn int    `json:"setMaxOpenConn"` // (连接池)最大打开的连接数,默认值为0表示不限制
	SetMaxIdleConn int    `json:"setMaxIdleConn"` // (连接池)闲置的连接数, 默认0
	Prefix         string `json:"prefix"`         // 表前缀, 如果集群配置了前缀, 这里可以省略
}

Config 单点配置

type ConfigCluster

type ConfigCluster struct {
	Master []Config // 主数据库配置节点
	Slave  []Config // 从数据库配置节点
	Driver string   // 驱动
	Prefix string   // 前缀
}

ConfigCluster 集群配置

type Engine

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

Engine 全局配置 (初始化配置模块, 可以全局保存并复用。)

func New

func New(config interface{}) (e *Engine, err error)

New : 初始化 Engine 结构体对象指针

func (*Engine) GetDriver

func (c *Engine) GetDriver() string

GetDriver 获取驱动。

func (*Engine) GetLogger

func (c *Engine) GetLogger() ILogger

GetLogger 获取一个logger实例。

func (*Engine) GetMasterDB

func (c *Engine) GetMasterDB() *sqlx.DB

GetMasterDB : 获取一个主库用来做查询之外的操作

func (*Engine) GetPrefix

func (c *Engine) GetPrefix() string

GetPrefix 获取前缀。

func (*Engine) GetSlaveDB

func (c *Engine) GetSlaveDB() *sqlx.DB

GetSlaveDB : 获取一个从库用来做查询操作

func (*Engine) Ping

func (c *Engine) Ping() error

Ping ...

func (*Engine) SetIgnoreName

func (c *Engine) SetIgnoreName(arg string)

SetIgnoreName 自定义结构体对应的orm忽略字段名字,默认-

func (*Engine) SetLogger

func (c *Engine) SetLogger(logger ILogger)

SetLogger ...

func (*Engine) SetPrefix

func (c *Engine) SetPrefix(prefix string)

SetPrefix 设置表前缀

func (*Engine) SetTagKey

func (c *Engine) SetTagKey(arg string)

SetTagKey 自定义结构体对应的orm字段,默认orm

func (*Engine) Use

func (c *Engine) Use(closers ...func(e *Engine))

Use 使用自定义插件,可以修改 Engine 的配置。

type Field

type Field struct {
	Name string // 字段名
	Type string // 字段类型
	Tag  string // 字段约束,例如: primary key,unique ...
}

表中的字段

type IEngine

type IEngine interface {
	GetMasterDB() *sql.DB
	GetSlaveDB() *sql.DB
	GetPrefix() (prefix string)
	SetLogger(logger ILogger)
	GetLogger() ILogger
	GetDriver() string
}

IEngine Engine实体的抽象接口,主要是用于与其他模块通信,便于传递 Engine 实例。

type ILogger

type ILogger interface {
	Sql(sqlStr string, runtime time.Duration)
	Slow(sqlStr string, runtime time.Duration)
	Error(msg string)
	SqlLog() bool
	ErrorLog() bool
	SlowLog() float64
}

ILogger ...

type LogLevel

type LogLevel uint
const (
	// LogSQL ...
	LogSQL LogLevel = iota
	// LogSlow ...
	LogSlow // 慢日志
	// LogError ...
	LogError
)

func (LogLevel) String

func (l LogLevel) String() string

String ...

type LogOption

type LogOption struct {
	FilePath       string
	EnableSqlLog   bool
	EnableSlowLog  float64 // 是否记录慢查询, 默认0s, 不记录, 设置记录的时间阀值, 比如 1, 则表示超过1s的都记录
	EnableErrorLog bool
}

type Logger

type Logger struct {
	log.Logger
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(o *LogOption) *Logger

NewLogger ...

func (*Logger) Error

func (l *Logger) Error(msg string)

Error ...

func (*Logger) ErrorLog

func (l *Logger) ErrorLog() bool

EnableErrorLog ...

func (*Logger) Slow

func (l *Logger) Slow(sqlStr string, runtime time.Duration)

Slow ...

func (*Logger) SlowLog

func (l *Logger) SlowLog() float64

EnableSlowLog ...

func (*Logger) Sql

func (l *Logger) Sql(sqlStr string, runtime time.Duration)

Sql ...

func (*Logger) SqlLog

func (l *Logger) SqlLog() bool

EnableSqlLog ...

type Orm

type Orm struct {
}

type Schema

type Schema struct {
	Model      interface{} // 模型对象
	Name       string      // 表名
	Fields     []*Field    // 表中所有字段
	FieldNames []string    // 表中所有字段的名字
	// contains filtered or unexported fields
}

对象和表的映射

func (*Schema) FieldValues

func (s *Schema) FieldValues(dest interface{}) []interface{}

FieldValues 获取结构体中所有字段的值,参数<dest>为 struct/*struct。

func (*Schema) GetField

func (s *Schema) GetField(name string) *Field

func (*Schema) Parse

func (s *Schema) Parse(dest interface{}, d dialect.Dialect) *Schema

Parse 将任意的对象解析为 Schema 实例,TagName 定义在engine.go 文件里。

type Session

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

Session 真正操作数据库底层模块, 所有的操作, 最终都会走到这里来获取或修改数据。(Session 的核心功能是与数据库进行交互)

func InitSession

func InitSession(db *sqlx.DB, dialect dialect.Dialect) *Session

func (*Session) Begin added in v0.1.1

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

Begin 开启事务,如果 master节点为nil,则通过slave节点开启事务。

func (Session) Clear

func (s Session) Clear()

Clear 清空SQL语句

func (*Session) Commit added in v0.1.1

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

Commit 提交事务

func (*Session) CreateTable

func (s *Session) CreateTable() error

CreateTable create a table in database with a model

func (*Session) Delete

func (s *Session) Delete(where ...interface{}) (int64, error)

len(where) == 0 则是删除整条记录,否则根据 where条件删除对应的记录。

func (*Session) DropTable

func (s *Session) DropTable() error

DropTable drops a table with the name of model

func (*Session) Exec

func (s *Session) Exec(sqlStr string, values ...interface{}) (result sql.Result, err error)

Exec 原生增、删、改语句,master(主库)用来做 增、删、改操作。

func (*Session) Find

func (s *Session) Find(sliceType interface{}) error

Find 调用方式:

var users []User
s.Find(&users)

func (*Session) HasTable

func (s *Session) HasTable() bool

HasTable returns true of the table exists

func (*Session) Insert

func (s *Session) Insert(values ...interface{}) (int64, error)

Insert 对应的 SQL 语句一般是这样的:

INSERT INTO table_name(col1, col2, col3, ...) VALUES (A1, A2, A3, ...),(B1, B2, B3, ...),...
例如:
	u1 := &User{Name: "Tom", Age: 18}
	u2 := &User{Name: "Sam", Age: 25}
	s.Insert(u1, u2, ...)

func (*Session) LastSql

func (s *Session) LastSql() string

LastSql 输出执行完的SQL语句

func (*Session) Limit

func (s *Session) Limit(num int) *Session

Limit adds limit condition to clause

func (*Session) Model

func (s *Session) Model(value interface{}) *Session

Model assigns refTable

func (*Session) Order

func (s *Session) Order(desc string) *Session

OrderBy adds order by condition to clause

func (*Session) Query

func (s *Session) Query(sqlStr string, values ...interface{}) (rows *sqlx.Rows, err error)

Query 原生查询语句。slave(从库)用来做查询操作。

func (*Session) QueryRow

func (s *Session) QueryRow(sqlStr string, values ...interface{}) (row *sqlx.Row, err error)

func (*Session) Raw

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

Raw 拼接 SQL语句,将 sql 和 value 保存到 lastSql

func (*Session) RefTable

func (s *Session) RefTable() *Schema

func (*Session) Rollback added in v0.1.1

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

Rollback 事务回滚

func (*Session) SetTX added in v0.1.1

func (s *Session) SetTX(b bool)

SetTX 设置事务的状态,true:开启事务,false:禁用事务

func (*Session) Take

func (s *Session) Take(structType interface{}) error

func (*Session) Update

func (s *Session) Update(kv ...interface{}) (int64, error)

Update 参数<kv[0]>是待更新的键值对,<kv[1]> 是 Where 的SQL语句,<kv[2:]> 是 Where SQL语句 ? 占位符的具体值。

func (*Session) Where

func (s *Session) Where(desc string, args ...interface{}) *Session

Where <desc>是具体的SQL语句,例如: "id=? and name=?",<args>是 ? 这个占位符的具体值,例如: 2,小明...

type Type

type Type int
const (
	Insert Type = iota
	Values
	Select
	Limit
	Where
	OrderBy
	Update
	Delete
	Count
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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