pdb

package module
v0.0.0-...-d2c2cd5 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2022 License: MIT Imports: 4 Imported by: 0

README

pdb

介绍

DB 相关工具方法

软件架构

软件架构说明

安装教程
  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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseWhere

func ParseWhere(dbType DBType, params []WhereParam) (where string, values []any)

ParseWhere 将 params 中的查询参数,转换为适用 gorm 的 where 条件

参数
 - dbType:数据库类型:oracle、mssql、sqlserver、mysql
 - params:查询参数 WhereParam

返回值
 - where:带 ? 占位符的字符串,可用于 gorm.DB.Where(where, values...)
Example
package main

import (
	"fmt"
	"gorm.io/driver/sqlserver"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"os"
	"time"
)

var (
	newLogger = logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold: time.Second * 10, // 慢 SQL 阈值
			LogLevel:      logger.Error,     // Log level
			Colorful:      true,             // 彩色打印
		},
	)

	conf = &gorm.Config{
		Logger:      newLogger,
		PrepareStmt: true, // 执行任何 SQL 时都创建并缓存预编译语句,可以提高后续的调用速度
	}
	db, _ = gorm.Open(sqlserver.Open(fmt.Sprintf("sqlserver://%v:%v@%v:%v?database=%v", "PT", "agft7g", "192.168.20.105", "1433", "PT")), conf)
)

func main() {
	f, p := ParseWhere(Mssql, []WhereParam{
		{
			Field: "user_id",
			Type:  String,
			Value: "asdf|>adsg|5?86*",
		},
		{
			Field: "age",
			Type:  Number,
			Value: "!=",
		},
		{
			Field: "score",
			Type:  Number,
			Value: "50:200|>300",
		},
		{
			Field: "birthday",
			Type:  Date,
			Value: "2022-09-08 18:25:36.8546:2023-09-08 18:25:36",
		},
	})

	var a struct{}

	fmt.Println(db.ToSQL(func(tx *gorm.DB) *gorm.DB {
		return tx.Table(`aaa`).Where(f, p...).Find(&a)
	}))

}
Output:

123

Types

type DBType

type DBType int
const (
	Mssql DBType = iota
	SqlServer
	Oracle
	Mysql
)

type FieldType

type FieldType int
const (
	String FieldType = iota
	Number
	Date
)

type WhereParam

type WhereParam struct {
	Field     string                    // 字段名称
	Type      FieldType                 // 字段类型
	Value     string                    // 查询的条件值
	Format    string                    // format:date类型,time.Parse(value, format);其它,fmt.Sprintf(format, value)
	Formatter func(value string) string // 自定义格式化方法(优先于format)
}

Jump to

Keyboard shortcuts

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