z1db

package
v0.10.6 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2023 License: MIT Imports: 18 Imported by: 0

README

gotool

介绍

golang 工具集

软件架构

软件架构说明

安装教程
  1. bash z1db/scripts/z1db-install.sh
  2. xxxx
  3. xxxx
使用说明
  1. bash z1db/scripts/z1db-install.sh 安装 z1db
  2. bash z1db/scripts/z1sql2grom.sh 把sql转为struct,包含gorm、json、bson标签和DBType实现
  3. xxxx
  4. 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/
z1db

sql2mongo支持的sql类型

sql类型 实例 说明
insert INSERT INTO role (name, des, CreatedAt) VALUES ('z1', 'myzero1', 1684654029),('z12', 'myzero12',1684654033) 可以单条和多条插入
delete DELETE FROM user WHERE name = 'z1' 删除满足条件的所有记录
drop table DROP TABLE user 删除user集合
select SELECT user.username,user.id FROM user WHERE username LIKE "myzero1%" 字段名称前面都加上表名称
join SELECT user.username,role.des FROM user JOIN role ON user.rid=role.id WHERE user.username LIKE "myzero1%" 关联查询支持join和left join
join3 SELECT user.username,role.des FROM user_role JOIN role ON user_role.rid=role.id JOIN role ON user_role.uid=user.id WHERE user.username LIKE "myzero1%" 三个表关联查询,主表必须包含其它2个表的关联字段

z1db使用gorm操作mongodb数据库的方法列表

安装说明:使用z1db前需要先运行go\pkg\mod\gitee.com\myzero1\gotool@v0.9.0\z1db.sh脚本替换grom的包,否则mongodb无效

需要使用mongdb,需要让对应的model实现DBType方法,请参考MongoUser go\pkg\mod\gitee.com\myzero1\gotool@v0.9.0\z1db\model.go

详情参考 go\pkg\mod\gitee.com\myzero1\gotool@v0.9.0\test\z1db_test.go

  • create

实例:
		user := z1db.MongoUser{
			Username: `gorm_username`,
			Email:    `gorm_email@qq.com`,
		}
		tx := db.Model(&z1db.MongoUser{}).Create(&user)
		// tx := db.Create(&user)
		if tx.Error != nil {
			t.Error(tx.Error)
		} else {
			t.Log(`----RowsAffected--create--1---`, tx.RowsAffected)
		}

对应sql:
INSERT INTO `mongo_users` (
	`created_at`,
	`updated_at`,
	`deleted_at`,
	`username`,
	`email`,
	`mobile_phone`,
	`avatar`,
	`password`,
	`api_token`,
	`status`,
	`rid`
)
VALUES
	(
		1684986904,
		1684986904,
		NULL,
		'gorm_username',
		'gorm_email@qq.com',
		'',
		'',
		'',
		'',
		1,
		1
	)

说明:


  • delete

实例:
		tx := db.Model(&z1db.MongoUser{}).Where("username = ?", `gorm_username2`).Delete(&z1db.MongoUser{})
		// tx := db.Where("username = ?", `gorm_username2`).Delete(&z1db.MongoUser{})

		if tx.Error != nil {
			t.Error(tx.Error)
		} else {
			t.Log(`----RowsAffected--Delete-----`, tx.RowsAffected)
		}

对应sql:
UPDATE `mongo_users`
SET `deleted_at` = '2023-05-25 11:55:04.669'
WHERE
	username = 'gorm_username2'
AND `mongo_users`.`deleted_at` IS NULL

说明:


  • update

实例:
		tx := db.Model(&z1db.MongoUser{}).Where(`username=?`, `gorm_username`).Updates(z1db.MongoUser{Username: `gorm_username-updated`})

		if tx.Error != nil {
			t.Error(tx.Error)
		} else {
			t.Log(`----RowsAffected--update-----`, tx.RowsAffected)
		}

对应sql:
UPDATE `mongo_users`
SET `updated_at` = 1684986904,
 `username` = 'gorm_username-updated'
WHERE
	username = 'gorm_username'
AND `mongo_users`.`deleted_at` IS NULL

说明:


  • select one

实例:
		user := z1db.MongoUser{}
		tx := db.Model(&z1db.MongoUser{}).Where("username = ?", `gorm_username-updated`).Find(&user)
		// tx := db.Model(&z1db.MongoUser{}).Select("username").Where("username = ?", `gorm_username-updated`).Find(&user)

		if tx.Error != nil {
			t.Error(tx.Error)
		} else {
			t.Log(`----RowsAffected---Select----one-----`, tx.RowsAffected)
			t.Log(`----Result---Select-----one----`, user)
		}

对应sql:
SELECT
	`username`
FROM
	`mongo_users`
WHERE
	username = 'gorm_username'
AND `mongo_users`.`deleted_at` IS NULL
LIMIT 1

说明:
user 必须声明为实例(user := z1db.MongoUser{}),不能只声明不实例(var user z1db.MongoUser)

  • select many

实例:
		users := []z1db.MongoUser{
			z1db.MongoUser{},
		}

		tx := db.Model(&z1db.MongoUser{}).Where("username like ?", `gorm_username%`).Offset(1).Limit(20).Find(&users)
		// tx := db.Model(&z1db.MongoUser{}).Select("username").Where("username like ?", `gorm_username-updated`).Offset(1).Limit(20).Find(&users)

		if tx.Error != nil {
			t.Error(tx.Error)
		} else {
			t.Log(`----RowsAffected---Select---many-----`, tx.RowsAffected)
			t.Log(`----Result---Select----many----`, users)
		}

对应sql:
SELECT
	`username`
FROM
	`mongo_users`
WHERE
	username = 'gorm_username'
AND `mongo_users`.`deleted_at` IS NULL

说明:
users 必须声明为实例(users := []z1db.MongoUser{z1db.MongoUser{}}),不能只声明不实例(var users []z1db.MongoUser)

  • select count

实例:
		var count int64

		tx := db.Model(&z1db.MongoUser{}).Where("username like ?", `gorm_username%`).Count(&count)
		// tx := db.Model(&z1db.MongoUser{}).Select("username").Where("username like ?", `gorm_username%`).Count(&count)

		if tx.Error != nil {
			t.Error(tx.Error)
		} else {
			t.Log(`----RowsAffected---Select---count-----`, tx.RowsAffected)
			t.Log(`----RowsAffected---Select---count-----`, count)
		}

对应sql:
SELECT
	count(*)
FROM
	`mongo_users`
WHERE
	(
		username LIKE 'gorm_username%'
		OR created_at > 0
	)
AND `mongo_users`.`deleted_at` IS NULL

说明:

log
0.10	mongodb for gorm without rewriting gorm, 
		and add transaction
		add callback function for user


Documentation

Overview

Code generated by github.com/cascax/sql2gorm

Index

Constants

This section is empty.

Variables

View Source
var Z1field2StatementWhere map[string][]Z1field

Functions

func ModifyStatementCreate added in v0.10.0

func ModifyStatementCreate(db *gorm.DB)

func ModifyStatementWhere added in v0.10.0

func ModifyStatementWhere(db *gorm.DB)

ModifyStatementWhereSettingDefault{On:false,ExcludedTables:`,user,role,`,FieldName:`place_id`,FieldValue:123456}

func ModifyStatementWhere2 added in v0.10.0

func ModifyStatementWhere2(stmt *gorm.Statement, fieldName string, fieldValue interface{})

func ModifyStatementWhere3 added in v0.10.0

func ModifyStatementWhere3(stmt *gorm.Statement)

Z1field2StatementWhere[`staff`]=[]Z1field{Z1field{Name:`palce_id`,Value:0}}

func NewDB

func NewDB(mongoCnf z1mongo.MongoConfig, tomongo gormplugin.ToMongo, addStatement gormplugin.AddStatement, gormConfig gorm.Config, dialectors ...gorm.Dialector) (err error)

use addStatement

 db = db.WithContext(context.WithValue(
	context.Background(),
	`z1AddStatement`,
	map[string]interface{}{
		`status`: 735,
	},

))

 use sql2gorm
	sql2gorm: $(go env GOMODCACHE)/$(go mod graph | grep 'gitee.com/myzero1/gotool' | head -n 2 | tail -n 1 | sed 's/ .*//')/z1db/scripts/z1sql2gorm.sh

	github.com/cloudquery/sqlite   // no cgo,go get github.com/cloudquery/sqlite@v1.0.1
	gorm.io/driver/sqlite
	gorm.io/driver/mysql
	gorm.io/driver/postgres

func Z1ParsingModel added in v0.10.0

func Z1ParsingModel(db *gorm.DB, model interface{}) (isMongo bool)

func Z1ParsingModelOld added in v0.10.0

func Z1ParsingModelOld(model interface{}) (isMongo, isSlice bool)

func Z1ToDryRun

func Z1ToDryRun(db *gorm.DB)

func Z1ToDryRun1 added in v0.10.0

func Z1ToDryRun1(db *gorm.DB, modelIsMongo bool)

func Z1ToMongo

func Z1ToMongo(db *gorm.DB)

func Z1ToMongo1 added in v0.10.0

func Z1ToMongo1(db *gorm.DB, model interface{}, stmt *gorm.Statement, modelIsMongo bool)

Types

type DBs

type DBs struct {
	Gorm       *gorm.DB
	GormOrigin *gorm.DB
	Mongo      *mongo.Database
}
var DB *DBs

type ModifyStatementWhereSetting added in v0.10.0

type ModifyStatementWhereSetting struct {
	On             bool
	ExcludedTables string
	FieldName      string
	FieldValue     interface{}
}
var ModifyStatementWhereSettingDefault ModifyStatementWhereSetting

type MongoRole added in v0.9.1

type MongoRole struct {
	Z1Model
	Name   string `gorm:"column:name" json:"name" bson:"name"`
	Des    string `gorm:"column:des" json:"des" bson:"des"`
	Action string `gorm:"column:action" json:"action" bson:"action"`
	Status int64  `gorm:"column:status;default:1" json:"status" bson:"status"`
}

type MongoUser added in v0.9.1

type MongoUser struct {
	Z1Model
	Username    string `gorm:"column:username" json:"username" bson:"username"`
	Email       string `gorm:"column:email" json:"email" bson:"email"`
	MobilePhone string `gorm:"column:mobile_phone" json:"mobile_phone" bson:"mobile_phone"`
	Avatar      string `gorm:"column:avatar" json:"avatar" bson:"avatar"`
	Password    string `gorm:"column:password" json:"password" bson:"password"`
	ApiToken    string `gorm:"column:api_token" json:"api_token" bson:"api_token"`
	Status      int64  `gorm:"column:status;default:1" json:"status" bson:"status"`
	Rid         int64  `gorm:"column:rid;default:1" json:"rid" bson:"rid"`
}

type MongoUserRole added in v0.9.1

type MongoUserRole struct {
	Z1Model
	Uid    int64 `gorm:"column:uid" json:"uid" bson:"uid"`
	Rid    int64 `gorm:"column:rid" json:"rid" bson:"rid"`
	Status int64 `gorm:"column:status;default:1" json:"status" bson:"status"`
}

type Role added in v0.9.1

type Role struct {
	Z1Model
	Name   string `gorm:"column:name" json:"name" bson:"name"`
	Des    string `gorm:"column:des" json:"des" bson:"des"`
	Action string `gorm:"column:action" json:"action" bson:"action"`
	Status int64  `gorm:"column:status;default:1" json:"status" bson:"status"`
}

type User added in v0.9.1

type User struct {
	Z1Model
	Username    string `gorm:"column:username" json:"username" bson:"username"`
	Email       string `gorm:"column:email" json:"email" bson:"email"`
	MobilePhone string `gorm:"column:mobile_phone" json:"mobile_phone" bson:"mobile_phone"`
	Avatar      string `gorm:"column:avatar" json:"avatar" bson:"avatar"`
	Password    string `gorm:"column:password" json:"password" bson:"password"`
	ApiToken    string `gorm:"column:api_token" json:"api_token" bson:"api_token"`
	Status      int64  `gorm:"column:status;default:1" json:"status" bson:"status"`
	Rid         int64  `gorm:"column:rid;default:1" json:"rid" bson:"rid"`
}

type UserRole added in v0.9.1

type UserRole struct {
	Z1Model
	Uid    int64 `gorm:"column:uid" json:"uid" bson:"uid"`
	Rid    int64 `gorm:"column:rid" json:"rid" bson:"rid"`
	Status int64 `gorm:"column:status;default:1" json:"status" bson:"status"`
}

type Z1Model

type Z1Model struct {
	ID        int64                 `gorm:"column:id;primarykey" json:"id" bson:"id"`                                       // 默认自增,可以人为设置sonyflake machineid max 65536 2^16
	CreatedAt int64                 `gorm:"column:created_at;not null" json:"created_at" bson:"created_at"`                 // 创建时间戳
	UpdatedAt int64                 `gorm:"column:updated_at;not null" json:"updated_at" bson:"updated_at"`                 // 更新时间戳
	DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at;index;not null;default:0" json:"deleted_at" bson:"deleted_at"` // 删除时间戳 已经改为了int64

	ID_ primitive.ObjectID `gorm:"-:all" json:"_id" bson:"_id"` // for mongodb _id 这个字段是标识,是否使用MongoDB的
}

func (*Z1Model) DBType

func (m *Z1Model) DBType() string

type Z1Modeli added in v0.10.0

type Z1Modeli interface {
	DBType() string
}

type Z1field added in v0.10.0

type Z1field struct {
	Name  string
	Value interface{}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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