Code Monkey home page Code Monkey logo

go-op's Introduction

Go Operation Build Status GoDoc License

Provide a common operation, such as Condition and Updater, supporting Go 1.18+.

Install

$ go get -u github.com/xgfone/go-op

Example

package main

import (
	"fmt"
	"strings"

	"github.com/xgfone/go-op"
)

func main() {
	// Manage the global op builders.
	builders := make(map[string]func(op.Op) string, 4)
	buildOper := func(op op.Oper) string { return builders[op.Op().Op](op.Op()) }
	register := func(op string, f func(op.Op) string) { builders[op] = f }

	// Register the Op builders.
	buildSignEq := func(op op.Op) string {
		if s, ok := op.Val.(string); ok {
			return fmt.Sprintf("`%s`='%s'", op.Key, s)
		}
		return fmt.Sprintf("`%s`=%v", op.Key, op.Val)
	}
	register(op.CondOpNotEqual, buildSignEq)
	register(op.CondOpEqual, buildSignEq)
	register(op.UpdateOpAdd, buildSignEq)
	register(op.UpdateOpSet, buildSignEq)

	// Define a UPDATE sql builder.
	buildUpdateSQL := func(table string, updaters []op.Updater, conds []op.Condition) string {
		sets := make([]string, len(updaters))
		for i, up := range updaters {
			sets[i] = buildOper(up)
		}

		wheres := make([]string, len(conds))
		for i, cond := range conds {
			wheres[i] = buildOper(cond)
		}

		return fmt.Sprintf("UPDATE `%s` SET %s WHERE %s",
			table, strings.Join(sets, ", "),
			strings.Join(wheres, " AND "))
	}

	// build the UPDATE sql.
	ColumnID := op.Key("id")
	ColumnAge := op.Key("age")
	sql := buildUpdateSQL("user",
		[]op.Updater{ColumnAge.Add(1), op.Set("name", "Aaron")},
		[]op.Condition{ColumnID.Eq(123), op.NotEq("is_deleted", false)},
	)

	fmt.Println(sql)

	// Output:
	// UPDATE `user` SET `age`=1, `name`='Aaron' WHERE `id`=123 AND `is_deleted`=false
}

go-op's People

Contributors

xgfone avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.