Coder Social home page Coder Social logo

go-mysql-orm's Introduction

Base on go-sql-driver/mysql.

Get Started

import (
    "database/sql"
    "github.com/folospace/go-mysql-orm/orm"
)

//connect mysql db
var db, _ = orm.OpenMysql("user:password@tcp(127.0.0.1:3306)/mydb?parseTime=true&charset=utf8mb4&loc=Asia%2FShanghai")

//user table
var UserTable = new(User)

type User struct {
    Id        int       `json:"id"`
    Email     string    `json:"email" orm:"email,unique"`
    Name      string    `json:"name" default:"jack"`
    Avatar    string    `json:"avatar" comment:"head image"`
    CreatedAt time.Time `json:"created_at"`
    UpdatedAt time.Time `json:"updated_at"`
}

func (*User) Connections() []*sql.DB {
    return []*sql.DB{db}
}
func (*User) DatabaseName() string {
    return "mydb"
}
func (*User) TableName() string {
    return "user"
}
func (u *User) Query() *orm.Query[*User] {
    return orm.NewQuery(UserTable).WherePrimaryIfNotZero(u.Id)
}

func main() {
    //create db table, add new columns if table already exist.
    UserTable.Query().CreateTable()  
    
    //create struct from db table
    UserTable.Query().CreateStruct()
}

select

    //select * from user where id = 1 //to struct
    user, _ := UserTable.Query().Get(1)
    fmt.Println(user) //User{Id:1}

    //select * from user where name='john' //to struct slice
    users, _ := UserTable.Query().Where(&UserTable.Name, "john").Gets()
    fmt.Println(users) //User{Id:1}, User{Id:2}, ...
    
    //select email from user //to slice
    emails, _ := UserTable.Query().Select(&UserTable.Email).Limit(10).GetSliceString()
    fmt.Println(emails) //a**@gmail.com, b**@gmail.com, ...
    
    //select user info to slice, group by id
    var userInfoMap map[int][]string
    UserTable.Query().Select(&UserTable.Id, &UserTable.Email, &UserTable.Name).Limit(10).GetTo(&userInfoMap)
    fmt.Println(userInfoMap) //{1:[a**@gmail.com, a**], 2:[b**@gmail.com, b**], ...}
    
    //select user id to slice, group by name
    var sameNameUsers map[string][]int
    UserTable.Query().Select(&UserTable.Name, &UserTable.Id).Limit(10).GetTo(&sameNameUsers)
    fmt.Println(sameNameUsers) //{a**:[1,3], b**:[2,4], ...}
    

update | delete | insert

    //update user set name="john 2" where id = 1
    UserTable.Query().WherePrimary(1).Update(&UserTable.Name, "john 2")
    
    //delete
    UserTable.Query().Delete(1, 2, 3)
    
    //insert
    _ = UserTable.Query().Insert(&User{Name: "han"})   
    
    //update users with different names
    _ = UserTable.Query().OnConflictUpdate(&UserTable.Name, &UserTable.Name).
    Insert(&User{Id: 1, Name: "han"}, &User{Id: 2, Name: "join"})

join

    //query join 
    UserTable.Query().Join(OrderTable, func (query *orm.Query[*User]) *orm.Query[*User] {
            return query.Where(&UserTable.Id, &OrderTable.UserId)
    }).Select(UserTable).Gets()

transaction

    //transaction
    _ = UserTable.Query().Transaction(func (query *orm.Query[*User]) error {
        newId := query.Insert(&User{Name: "john"}).LastInsertId //insert
        //newId := orm.NewQuery(UserTable).UseTx(query.Tx()).Insert(User{Name: "john"}).LastInsertId
        fmt.Println(newId)
        return errors.New("I want rollback") //rollback
    })

subquery

    //subquery
    subquery := UserTable.Query().WherePrimary(1).Select(&UserTable.Id).SubQuery()
    
    //where in suquery
    UserTable.Query().Where(&UserTable.Id, orm.WhereIn, subquery).Gets()
    
    //insert subquery
    UserTable.Query().Select(&UserTable.Id).InsertSubquery(subquery)
    
    //join subquery
    UserTable.Query().Join(subquery, func (query *orm.Query[*User]) *orm.Query[*User] {
        return query.Where(&UserTable.Id, orm.Raw("sub.id"))
    }).Gets()
    

go-mysql-orm's People

Contributors

folospace avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-mysql-orm's Issues

Has any Plan to support other database?

The most convenient ORM that written in golang for me, if it can using in sqlite that was great.

对我来说用 golang 编写的最方便的 ORM,如果它能在 sqlite 中使用那就太好了。

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.