Go to file
2024-06-13 16:13:13 +08:00
schema first commit 2023-04-12 15:58:25 +08:00
.gitignore first commit 2023-04-12 15:58:25 +08:00
builder_test.go [feat] 丰富错误提示信息 2023-07-30 19:12:56 +08:00
builder.go [feat] Reset 支持 join 2024-06-13 16:13:13 +08:00
config.go first commit 2023-04-12 15:58:25 +08:00
connection_factory.go first commit 2023-04-12 15:58:25 +08:00
connection.go first commit 2023-04-12 15:58:25 +08:00
db_manager.go first commit 2023-04-12 15:58:25 +08:00
db.go [fix] 多次调用 Open() 错误 2023-06-21 09:56:28 +08:00
go.mod first commit 2023-04-12 15:58:25 +08:00
go.sum first commit 2023-04-12 15:58:25 +08:00
gramar.go first commit 2023-04-12 15:58:25 +08:00
mysql_connector.go first commit 2023-04-12 15:58:25 +08:00
mysql_grammar.go first commit 2023-04-12 15:58:25 +08:00
raw.go first commit 2023-04-12 15:58:25 +08:00
README.md first commit 2023-04-12 15:58:25 +08:00
scan_test.go [fix] 修复 converting NULL to string is unsupported 错误 2023-06-21 13:12:09 +08:00
scan.go [feat] 丰富错误提示信息 2023-07-31 20:53:24 +08:00
sqlite3_connector.go first commit 2023-04-12 15:58:25 +08:00
sqlite3_grammar.go [feat] Sqlite3Grammar 调整 2023-06-15 11:46:17 +08:00
sqlite3_test.go [feat] Sqlite3Grammar 调整 2023-06-15 11:46:17 +08:00
transaction.go first commit 2023-04-12 15:58:25 +08:00
types.go first commit 2023-04-12 15:58:25 +08:00
util.go [feat] 新增 MysqlRealEscapeString 方法 2023-04-13 09:51:05 +08:00

WORK IN PROGRESS

Get Started

A golang ORM Framework like Laravel's Eloquent

Example

type User struct {
    Id        int64          `goelo:"column:id;primaryKey"`
    UserName  sql.NullString `goelo:"column:name"`
    Age       int            `goelo:"column:age"`
    Status    int            `goelo:"column:status"`
    Friends   []UserT        `goelo:"BelongsToMany:FriendsRelation"`
    Address   Address        `goelo:"HasOne:AddressRelation"`
    CreatedAt time.Time      `goelo:"column:created_at,timestatmp:create"`
    UpdatedAt sql.NullTime   `goelo:"column:updated_at,timestatmp:update"`
}

//Find/First/Get
var user User
DB.Table("users").Find(&user,1)
DB.Table("users").Where("name","john").First(&user)
DB.Table("users").Where("name","john").FirstOrCreate(&user)

//Column/Aggeregate
var age int
DB.Table("users").Where("id","=",20).Value(&age,"age")
DB.Table("users").Max(&age,"age")
var salary int
DB.Table("user").Where("age",">=",30).Avg(&salary,"salary")

//Find record to map
var m = make(map[string]interface{})
DB.Query().From("users").Find(&m,3)
var ms []map[string]interface{}
DB.Query().From("users").Get(&ms)

//Pagination
var users []User
DB.Table("users").Where("id", ">", 10).Where("id", "<", 28).Paginate(&users, 10, 1)

//Chunk/ChunkById
var total int
totalP := &total
DB.Table("users").OrderBy("id").Chunk(&[]User{}, 10, func(dest interface{}) error {
    us := dest.(*[]User)
    for _, user := range *us {
        assert.Equal(t, user.UserName, sql.NullString{
            String: fmt.Sprintf("user-%d", user.Age),
            Valid:  true,
        })
        *totalP++
    }
    return nil
})

var total int
totalP := &total
DB.Table("users").ChunkById(&[]User{}, 10, func(dest interface{}) error {
    us := dest.(*[]User)
    for _, user := range *us {
        assert.Equal(t, user.UserName, sql.NullString{
            String: fmt.Sprintf("user-%d", user.Age),
            Valid:  true,
        })
        *totalP++
    }
    return nil
})
//Query clause 
DB.Where("name","john@apple.com").OrWhere("email","john@apple.com").First(&user)

DB.Where("is_admin", 1).Where(map[string]interface{}{
    "name": "Joe", "location": "LA",
}, db.BOOLEAN_OR).Where(func(builder *db.Builder){
    builder.WhereYear("created_at", "<", 2010).WhereColumn("first_name", "last_name").OrWhereNull("activited_at")
}).ToSql()
// sql:"select `name`, `age`, `email` where `is_admin` = ? or (`name` = ? and `location` = ?) and (year(`created_at`) < ? and `first_name` = `last_name` or `activited_at` is null)"

// Insert/Update/Delete
DB.Table("users").Insert(map[string]interface{}{
    "name":       "go-eloquent",
    "age":        18,
    "created_at": now,
})
DB.Table("users").Where("id", id).Update(map[string]interface{}{
    "name":       "newname",
    "age":        18,
    "updated_at": now.Add(time.Hour * 1),
})
DB.Table("users").Where("id", id).Delete()

More Details,visit Docs

Credits

https://github.com/go-gorm/gorm
https://github.com/jmoiron/sqlx
https://github.com/qclaogui/database