gsql

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildSQLUpdate

func BuildSQLUpdate(update any) (map[string]any, error)

BuildSQLUpdate build Update struct into sql update map

💡 HINT:

⚠️ WARNING: fields of update must be pointers

🚀 example:

    // model of table_abc
    type TableAbc struct {
        ID   int64  `gorm:"column:id"`
        Name string `gorm:"column:name"`
        Age  int    `gorm:"column:p_age"`
    }

    func (TableAbc) TableName() string {
    	return "table_abc"
    }

    // fields need to update.
    type TableAbcUpdate struct {
        Name *string `sql_field:"name"`
        Age  *int    `sql_field:"p_age"`
    }

    var name = "byte-er"
    attrs = TableAbcUpdate{
        Name: &name
    }

    // SQL: update table_abc set name="byte-er" where id = 1
	attrs, err := BuildSQLUpdate(attrs)
    if err != nil{
        // do something
    }
    if err := db.Model(TableAbc{}).Where("id = ?", id).Updates(attrs).Error; err != nil {
        logs.Error("update table abc failed: %s", err)
    }

func BuildSQLWhereExpr added in v0.1.8

func BuildSQLWhereExpr(where any) (clause.Expression, error)

BuildSQLWhereExpr build Where model struct into query & args in SQL

💡 HINT:

⚠️ WARNING:

🚀 example:

// model table
type TableAbc struct {
	ID   int64  `gorm:"column:id"`
	Name string `gorm:"column:name"`
	Age  int    `gorm:"column:p_age"`
}

func (TableAbc) TableName() string {
	return "table_abc"
}

// fields to be updated
type TableAbcWhere struct {
	Name *string `sql_field:"name" sql_operator:"like"`
	Age  *int    `sql_field:"p_age"`
}

func example() {
	var name = "name%"
	var age = 20
	attrs := TableAbcWhere{
		Name: &name,
		Age:  &age,
	}

	gwhere, err := BuildSQLWhereExpr(attrs)
	if err != nil {
		// handle error
	}

	// SQL: update table_abc set name="byte-er" where id = 1
	if err := db.Find(&pos).Where(gwhere).Error; err != nil {
		logs.Error("fins table abc failed: %s", err)
	}
}

func JSONContains added in v0.1.8

func JSONContains(column string, data any) clause.Expression

func JSONContainsExprs added in v0.1.8

func JSONContainsExprs(column string, data any) ([]clause.Expression, error)

func JSONSetExpr added in v0.1.8

func JSONSetExpr(column string, data any) clause.Expr

Types

type SQLUpdater

type SQLUpdater func(column string, data any) clause.Expr

SQLUpdater update SQL generator

type SQLWhereExprBuilder added in v0.1.8

type SQLWhereExprBuilder func(column string, data any) (clause.Expression, error)

SQLWhereExprBuilder where SQL generator

func GetWhereExpr added in v0.1.8

func GetWhereExpr(operator string) (SQLWhereExprBuilder, error)

Jump to

Keyboard shortcuts

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