Latest version adds
- All errors print SQL and arguments
- single row Upsert and Insect (insert or select), both are single trip to DB
- MustRegisterFunction - used to synchronize stored procedures (UDF) defined as backtick literals in Go files with the database. Updates when checksum or version changes. Basically, keep stored procedures logic in your Go code not as separate migrations.
- More features to make working with JSON and embedded structs friendlier. Targeted at PostgreSQL 9.3+
Intuitive JSON Document or Go Hierarchical struct retrieval (SINGLE TRIP to database!)
con.SelectDoc("id", "user_name", "avatar").
HasMany("recent_comments", `SELECT id, title FROM comments WHERE id = users.id LIMIT 10`).
HasMany("recent_posts", `SELECT id, title FROM posts WHERE author_id = users.id LIMIT 10`).
HasOne("account", `SELECT balance FROM accounts WHERE user_id = users.id`).
From("users").
Where("id = $1", 4).
QueryStruct(&obj) // obj must be agreeable with json.Unmarshal()
results in
{
"id": 4,
"user_name": "mario",
"avatar": "https://imgur.com/a23x.jpg",
"recent_comments": [{"id": 1, "title": "..."}],
"recent_posts": [{"id": 1, "title": "..."}],
"account": {
"balance": 42.00
}
}
Simpler JSON retrieval for rapid application development
var json []byte
json, _ = con.SQL(`SELECT id, user_name, created_at FROM users WHERE user_name = $1 `,
"mario",
).QueryJSON()
// straight into map
var obj map[string]interface{}
con.SQL(`SELECT id, user_name, created_at FROM users WHERE user_name = $1 `,
"mario",
).QueryObject(&obj)
both result in
{
"id": 1,
"user_name": "mario",
"created_at": "2015-03-01T14:23"
}
**评论:**
RIC_FLAIR-WOOO:
mgutz:Wow, this is cool.
I've been using Postgresql's rows_to_json, and json_build_object functions to retrieve a JSON object and Unmarshal it to my Go structs. This looks like a nice wrapper that would really clean up some of my code.
Great work, can't wait to try it out.
cytospin:That's the magic behind the JSON methods. Let me know if the subqueries can be optimized.
mgutz:Just a note that this could be confused with dat.
pkieltyka:Darn it ... that project looks interesting though. I'm close to marking it 1.0 and will rename it then.
cytospin:I think you should keep your project the same .. yours is a Go library
I don't think you necessarily need to change it. I'm interested in both which is why I initially thought that yours had something to do with the other.
