Giter Site home page Giter Site logo

sqlg's Introduction

Security Status

Go Report Card GoDoc

SQLg

An easy way to generate SQL statements for Go. ๐Ÿค—

Install

go get github.com/wwwangxc/sqlg

Quick Start

Select

package main

import (
        "fmt"

        "github.com/com/wwwangxc/sqlg"
)

func main () {
        // SELECT * FROM `user` WHERE `id`=? AND `deleted_at` IS NULL
        // [666]
        g := sqlg.NewGenerator("user",
                sqlg.WithAnd("id", sqlg.EQ(666)),
                sqlg.WithAnd("deleted_at", sqlg.Null()))
        _, _ = g.Select()
    
        // SELECT * FROM `user` WHERE `id`!=? AND `deleted_at` IS NOT NULL
        // [666]
        g = sqlg.NewGenerator("user",
                sqlg.WithAnd("id", sqlg.EQ(666)),
                sqlg.WithAnd("deleted_at", sqlg.NNull()))
        _, _ = g.Select()
    
        // SELECT `id`, `name` FROM `user` WHERE `id`>=? OR `name`=? ORDER BY `id` DESC LIMIT 10
        // [666 tom]
        g = sqlg.NewGenerator("user",
                sqlg.WithAnd("id", sqlg.GTE(666)),
                sqlg.WithOr("name", sqlg.EQ("tom")),
                sqlg.WithOrderByDESC("id"), sqlg.WithLimit(10))
        _, _ = g.Select("id", "name")
    
        type User struct {
                ID   uint64 `db:"id"`
                Name string `db:"name"`
                Age    uint8
                Height uint8 `db:"-"`
        }
    
        // compound expression
        m := sqlg.NewCompExpr()
        m.Put("name", sqlg.EQ("tom"))
        m.Put("id", sqlg.EQ(666))
        
        // SELECT `id`, `name` FROM `user` WHERE `deleted_at` IS NULL AND (`name`=? OR `id`=?)
        // [tom 666]
        // <nil>
        g = sqlg.NewGenerator("user",
                sqlg.WithAnd("deleted_at", sqlg.Null()),
                sqlg.WithAndExprs(m))
        _, _, _ = g.SelectByStruct(User{})
}

Update

package main

import (
        "fmt"

        "github.com/com/wwwangxc/sqlg"
)

func main () {
        // create generator
        g := sqlg.NewGenerator("user", sqlg.WithAnd("id", sqlg.EQ(666)), sqlg.WithLimit(1))
        
        // assignment expression
        assExpr := sqlg.NewAssExpr()
        assExpr.Put("name", "jerry")
        assExpr.Put("age", 3)
        
        // UPDATE `user` SET `name`=?, `age`=? WHERE `id`=? LIMIT 1
        // [jerry 3 666]
        _, _ = g.Update(assExpr)
}

Delete

package main

import (
        "fmt"

        "github.com/com/wwwangxc/sqlg"
)

func main () {
        // create generator
        g := sqlg.NewGenerator("user", sqlg.WithAnd("id", sqlg.EQ(666)), sqlg.WithLimit(1))
        
        // DELETE FROM `user` WHERE `id`=? LIMIT 1
        // [666]
        _, _ = g.Delete(assExpr)
}

Insert

package main

import (
        "fmt"

        "github.com/com/wwwangxc/sqlg"
)

func main () {
        // create generator
        g := sqlg.NewGenerator("user")
        columns := []string{"name", "age"}
        records := [][]interface{}{{"tom", 5}, {"jerry", 3}}

        // INSERT INTO `user` (`name`, `age`) VALUES (?,?), (?,?)
        // [tom 5 jerry 3]
        _, _ = g.Insert(columns, records)
}

Insert Not Exist

package main

import (
        "fmt"

        "github.com/com/wwwangxc/sqlg"
)

func main () {
        // compound expression
        m := sqlg.NewCompExpr()
        m.Put("name", sqlg.EQ("jerry"))
        m.Put("age", sqlg.EQ(3))

        // create generator
        g := sqlg.NewGenerator("user", sqlg.WithNExists("user", m))
        columns := []string{"name", "age"}
        records := [][]interface{}{{"tom", 5}}

        // INSERT INTO `user` (`name`, `age`) SELECT ?,? FROM dual WHERE NOT EXISTS (SELECT * FROM `user` WHERE `name`=? AND `age`=?)
        // [tom 5 jerry 3]
        _, _ = g.Insert(columns, records)
}

Insert On Duplicate Key Update

package main

import (
        "fmt"

        "github.com/com/wwwangxc/sqlg"
)

func main () {
        // assignment expression
        m := sqlg.NewAssExpr()
        m.Put("name", "jerry")
        m.Put("age", 3)

        // create generator
        g := sqlg.NewGenerator("user", sqlg.OnDuplicateKeyUpdate(m))
        columns := []string{"name", "age"}
        records := [][]interface{}{{"tom", 5}}

        // INSERT INTO user (`name`, `age`) VALUES (?,?) ON DUPLICATE KEY UPDATE `name`=?, `age`=?
        // [tom 5 jerry 3]
        _, _ = g.Insert(columns, records)
}

sqlg's People

Contributors

wwwangxc avatar

Stargazers

 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.