Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/btfak/sqlext
extend gorm and gocql
https://github.com/btfak/sqlext
gocql gorm
Last synced: about 1 month ago
JSON representation
extend gorm and gocql
- Host: GitHub
- URL: https://github.com/btfak/sqlext
- Owner: btfak
- License: apache-2.0
- Created: 2017-11-01T11:20:17.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-01T11:46:48.000Z (about 7 years ago)
- Last Synced: 2024-06-20T02:06:26.522Z (7 months ago)
- Topics: gocql, gorm
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 15
- Watchers: 5
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqlext
sqlext use to extend [gorm](https://github.com/jinzhu/gorm) and [gocql](https://github.com/gocql/gocql)
- gorm not support batch insert,`BatchInsert` is a universal batch insert API.
- gocql not support bind querying data to struct,`MapToStruct` fill struct fields with map value## example
- BatchInsert
```go
type GroupMember struct {
ID int64
GroupID int64
Type int8
Extra []byte
JoinTime time.Time
}var mydb *gorm.DB
var members = []GroupMember{}
sqlext.BatchInsert(mydb.DB(),members)//generated SQL: INSERT INTO group_member (id, group_id, type, extra, join_time) VALUES (?,?,?,?,?), (?,?,?,?,?) ...
```- MapToStruct
```go
// MapToStruct
func GetUsersByIDs(userIDs []int64) (users []User, err error) {
iter := Cassandra.Query("SELECT * FROM user WHERE id IN ?", userIDs).Iter()
for {
row := make(map[string]interface{})
if !iter.MapScan(row) {
break
}
user := User{}
err = sqlext.MapToStruct(row, &user)
if err != nil {
return nil, err
}
users = append(users, user)
}
return users, nil
}
```