db

package
v0.0.0-...-8f0e436 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(elem ...string) string

Format MySQL 格式化

Types

type MySQL

type MySQL struct {
	DB     *gorm.DB // 数据库任务入口
	DBUrl  string   // dbURL 数据库 URL
	DBUser string   // dbUser 数据库用户名
	DBPass string   // dbPass 数据库用户密码
	DBName string   // dbName 数据库名称
	// LogMode set log mode, `true` for detailed logs, `false` for no log, default, will only print error logs
	LogModeEnable   bool
	MaxIdleConnects int
	MaxOpenConnects int
	// contains filtered or unexported fields
}

MySQL sql 连接对象

func (*MySQL) Connect

func (s *MySQL) Connect(dbURL, dbUser, dbPass, dbName string, logModeEnable bool, maxIdleConns, maxOpenConns int) error

Connect 链接数据库服务

dbURL 数据库 URL

dbUser 数据库用户名

dbPass 数据库用户密码

dbName 数据库名称

logModeEnable set log mode, `true` for detailed logs, `false` for no log, default, will only print error logs

SetMaxIdleConns sets the maximum number of connections in the idle connection pool.

SetMaxOpenConns sets the maximum number of open connections to the database.

func (*MySQL) DisConnect

func (s *MySQL) DisConnect()

DisConnect 关闭连接

func (*MySQL) Exec

func (s *MySQL) Exec(f func(s *MySQL)) error

Exec 执行自定义 MySQL

func (*MySQL) ExecSQL

func (s *MySQL) ExecSQL(dest interface{}, sql string, values ...interface{})

ExecSQL 执行自定义 MySQL 语句,该方法是对 func Exec(f func(db *gorm.DB)) error 的实现

dest 期望通过该过程赋值的对象

sql 即将执行的 MySQL 语句,可以包含 "?" 来做通配符

values 上述 MySQL 语句中 "?" 通配符所表达的值

eg:在 db_user 表中根据用户编号和年龄查询用户基本信息,如下所示:

ExecSQL(&user, "select id,name,age from db_user where id=? and age=?", 1, 18)

Jump to

Keyboard shortcuts

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