Documentation
¶
Index ¶
- Constants
- func GetPlaceholder(dbType string, index int) string
- func SetEasyDb(edb *EasyDb)
- type DsnConf
- func (cf *DsnConf) AddDsnTpl(driverName, dsnTpl string) error
- func (cf DsnConf) CheckAvailable() bool
- func (cf DsnConf) GetAvailableDsnTplMap() map[string]string
- func (cf DsnConf) GetDsn() (string, error)
- func (cf DsnConf) GetDsnTpl() (string, error)
- func (cf *DsnConf) UpdateDsnTpl(driverName, dsnTpl string) error
- type EasyDb
- func (d *EasyDb) CloseDb() error
- func (d EasyDb) DecodeInterface(data map[string]interface{}) map[string]interface{}
- func (d *EasyDb) Exec(query string, args ...interface{}) (sql.Result, error)
- func (d *EasyDb) ExecByFile(filepath string, args ...interface{}) (sql.Result, error)
- func (d *EasyDb) ExecInsert(tableName string, columns []string, values []interface{}) error
- func (d *EasyDb) ExecSqlWithTransaction(sqlStatements []string) error
- func (d *EasyDb) ExecUpdateByValues(tableName string, columns []string, values []interface{}, whereClause string, ...) error
- func (d *EasyDb) GetMany(querySQL string, dest interface{}, args ...interface{}) error
- func (d *EasyDb) GetOne(querySQL string, dest []interface{}, args ...interface{}) error
- func (d *EasyDb) GetOneData(querySQL string, dest interface{}, args ...interface{}) error
- func (d *EasyDb) GetSqlDB() *sql.DB
- func (d *EasyDb) Ping() error
- func (d *EasyDb) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (d *EasyDb) QueryRow(query string, args ...interface{}) *sql.Row
- func (d *EasyDb) SowLog(level int)
Constants ¶
const DSN_TPL_MYSQL = "DB_USER:DB_PASSWORD@tcp(DB_HOST:DB_PORT)/DB_NAME"
const DSN_TPL_POSTGRES = "user=DB_USER password=DB_PASSWORD dbname=DB_NAME host=DB_HOST port=DB_PORT sslmode=disable"
const DSN_TPL_SQLITE = "DB_NAME.db"
Variables ¶
This section is empty.
Functions ¶
func GetPlaceholder ¶
getPlaceholder 生成参数占位符 mysql, sqlite 的参数占位符是?. postgres则是 $1, $2, $3 ...
Types ¶
type DsnConf ¶ added in v0.1.0
type DsnConf struct {
DriverName string
DbHost, DbUser, DbPassword, DbName string
DbPort int
// contains filtered or unexported fields
}
func NewDsnConf ¶ added in v0.1.0
NewDsnConf 创建DsnConf实例,包含常用数据库的dsn模板。 If you need to add more database drivers, please use AddDsnTpl method to add dsn template after creating DsnConf instance. 示例:
import (
"github.com/iotames/easydb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
cf1 := easydb.NewDsnConf("mysql", "127.0.0.1", "root", "password", "testdb", 3306)
// 可选:cf1.UpdateDsnTpl("mysql", "DB_USER:DB_PASSWORD@tcp(DB_HOST:DB_PORT)/DB_NAME?charset=utf8mb4&parseTime=True&loc=Local")
dbmysql := easydb.NewEasyDbByConf(*cf1)
cf2 := easydb.NewDsnConf("postgres", "127.0.0.1", "username", "password", "testdb", 5432)
// 可选:cf2.UpdateDsnTpl("postgres", "user=DB_USER password=DB_PASSWORD dbname=DB_NAME host=DB_HOST port=DB_PORT sslmode=disable search_path=public")
dbpg := easydb.NewEasyDbByConf(*cf2)
func (*DsnConf) AddDsnTpl ¶ added in v0.1.0
AddDsnTpl 添加新的dsn模板 If the driverName already exists, it returns an error. 示例:
// test.db是dbName参数,填sqlite3的文件名,可以换成绝对路径
cf := easydb.NewDsnConf("sqlite3", "", "", "", "testdb", 0)
err := cf.AddDsnTpl("sqlite3", "DB_NAME.db")
func (DsnConf) CheckAvailable ¶ added in v0.1.0
CheckAvailable 检查当前系统是否支持该数据库驱动
func (DsnConf) GetAvailableDsnTplMap ¶ added in v0.1.0
GetAvailableDsnTplMap 获取当前系统支持的数据库驱动及其对应的dsn模板
func (*DsnConf) UpdateDsnTpl ¶ added in v0.1.0
UpdateDsnTpl 更新已存在的dsn模板 If the driverName does not exist, it returns an error. 示例:
cf := easydb.NewDsnConf("postgres", "127.0.0.1", "username", "password", "testdb", 5432)
cf.UpdateDsnTpl("postgres", "user=DB_USER password=DB_PASSWORD dbname=DB_NAME host=DB_HOST port=DB_PORT sslmode=disable search_path=public")
type EasyDb ¶
type EasyDb struct {
// contains filtered or unexported fields
}
func NewEasyDb ¶
NewEasyDb 初始化EasyDb数据库连接实例。 driverName See https://golang.org/s/sqldrivers for a list of third-party drivers.
示例:
import (
"github.com/iotames/easydb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
d := easydb.NewEasyDb("mysql", "127.0.0.1", "root", "password", "testdb", 3306)
d1 := easydb.NewEasyDb("postgres", "127.0.0.1", "username", "password", "testdb", 5432)
func NewEasyDbByConf ¶ added in v0.1.0
NewEasyDbByConf 使用DsnConf参数初始化EasyDb实例。 示例:
import (
"github.com/iotames/easydb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
cf := easydb.NewDsnConf("mysql", "127.0.0.1", "root", "password", "testdb", 3306)
// 可选:cf.UpdateDsnTpl("mysql", "DB_USER:DB_PASSWORD@tcp(DB_HOST:DB_PORT)/DB_NAME?charset=utf8mb4&parseTime=True&loc=Local")
dbmysql := easydb.NewEasyDbByConf(*cf)
cfpg := easydb.NewDsnConf("postgres", "127.0.0.1", "username", "password", "testdb", 5432)
// 可选:cfpg.UpdateDsnTpl("postgres", "user=DB_USER password=DB_PASSWORD dbname=DB_NAME host=DB_HOST port=DB_PORT sslmode=disable search_path=public")
dbpg := easydb.NewEasyDbByConf(*cfpg)
func NewEasyDbBySqlDB ¶
NewEasyDbBySqlDB 使用sqldb *sql.DB参数初始化EasyDb实例。
各数据库驱动:https://golang.org/s/sqldrivers
import (
"github.com/iotames/easydb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
sqldb, err = sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/debugdb")
// sqldb, err = sql.Open("postgres", "user=postgres password=postgres dbname=postgres host=127.0.0.1 port=5432 sslmode=disable search_path=public")
// sqldb, err := sql.Open("sqlite3", "./mydb.sqlite")
d := NewEasyDbBySqlDB(sqldb)
func (EasyDb) DecodeInterface ¶
DecodeInterface 用于解码GetOneData方法返回的结果 data 参数类型实际是 map[string][]byte 示例:
data := make(map[string]interface{}, 3)
d.GetOneData("SELECT id, name, age, wallet_balance FROM $1", &data, "users")
fmt.Printf("-----GetOneData--result(%+v)----\n", d.DecodeInterface(data))
func (*EasyDb) ExecByFile ¶
ExecByFile 从文件执行SQL脚本
func (*EasyDb) ExecInsert ¶
ExecInsert 执行单条插入语句
func (*EasyDb) ExecSqlWithTransaction ¶
ExecSqlWithTransaction 在事务中执行多条SQL语句
func (*EasyDb) ExecUpdateByValues ¶
func (d *EasyDb) ExecUpdateByValues(tableName string, columns []string, values []interface{}, whereClause string, whereValues []interface{}) error
ExecUpdate 执行更新语句 tableName: 表名 columns: 要更新的列名 values: 要更新的值 whereClause: WHERE子句。例如:"id = $1" whereValues: WHERE子句中的参数值。例如:[]interface{}{1}
func (*EasyDb) GetMany ¶
GetMany 根据where条件查询多条数据 querySQL SQL查询语句 dest: 用于接收结果的切片的指针 args: SQL参数 示例:
var datalist []map[string]interface{}
d.GetMany("SELECT id, name, age, wallet_balance FROM users", &datalist)
func (*EasyDb) GetOne ¶
GetOne 根据where条件查询单条数据 querySQL SQL查询语句 args: SQL参数 dest: 用于接收结果的结构体指针 示例:
var qrid *int
var qrToUrl *string
GetOne("select id, to_url from qr_list where code = $1", []interface{}{qrid, qrToUrl}, "codexxx")
func (*EasyDb) GetOneData ¶
GetOneData 根据where条件查询单条数据,支持结构体指针或map接收结果 querySQL SQL查询语句 例:select field1, field2 from table1 where name = $1 and status = $2 dest: 用于接收结果的结构体指针或map[string]any, *map[string]any args: SQL参数 示例:
data := make(map[string]interface{}, 3)
d.GetOneData("SELECT id, name, age, wallet_balance FROM $1", data, "users")
// 传指针亦可 d.GetOneData("SELECT id, name, age, wallet_balance FROM $1", &data, "users")
fmt.Printf("-----GetOneData--result(%+v)----\n", data)