<p>[dat](github.com/mgutz/dat) is moving towards API stability. Unfortunately there are breaking changes (for the good) to be more consistent with database/sql and sqlx.</p>
<h1>Changes from legacy to v1</h1>
<ul>
<li><p>Original dat moved to legacy branch.</p></li>
<li><p>Move to gopkg.in for API stability.</p></li>
<li><p>Legacy <code>Connection</code> renamed to <code>DB</code> to be consistent with <code>database/sql</code></p></li>
<li><p><code>Connection</code> is now the interface for <code>DB</code> and <code>Tx</code>. Use <code>Connection</code> to
receive either a <code>DB</code> or <code>Tx</code>.</p></li>
<li><p>Support for nested transactions. <strong>Needs user testing and feedback</strong>.</p>
<p>In a nested transaction <em>only</em> the top-most commit commits to the
database if it has not been rollbacked. Any rollback in nested
function results in entire transaction being rollbacked and leaves the transaction
in <code>tx.IsRollbacked()</code> state.</p></li>
</ul>
<p>``` go</p>
<pre><code>func nested(conn runner.Connection) error {
tx, err := conn.Begin()
if err != nil {
return err
}
defer tx.AutoRollback()
_, err := tx.SQL('...').Exec()
if err != nil {
return err
}
return tx.Commit()
}
func fromDB() error {
return nested(DB)
}
func fromTx() error {
tx, err := DB.Begin()
if err != nil {
return err
}
defer tx.AutoRollback()
err := nested(tx)
if err ! = nil {
return logger.Error("Failed in nested", err)
}
// if Rollback was called, Commit returns an error
return tx.Commit()
}
</code></pre>
<p>```</p>
<ul>
<li> <code>SelectDoc.HasMany</code> and <code>SelectDoc.HasOne</code> renamed to <code>Many</code> and <code>One</code> for
retrieving hierarchical JSON documents. BTW, <code>SelectDoc</code> itself can be used
in <code>Many</code> and <code>One</code> to build N-deep hierarchies.</li>
</ul>
<p>```go</p>
<pre><code>DB.SelectDoc("id", "user_name", "avatar").
Many("recent_comments", `SELECT id, title FROM comments WHERE id = users.id LIMIT 10`).
Many("recent_posts", `SELECT id, title FROM posts WHERE author_id = users.id LIMIT 10`).
One("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()
</code></pre>
<p>```</p>
<ul>
<li><p>Session obsoleted. Go's db library does not support transaction-less
sessions. Use <code>Tx</code> if you need a session, otherwise use <code>DB</code> directly.</p></li>
<li><p>Fixes to dat.NullTime to properly work with JSON timestamps from HTTP
handlers and <code>timestamptz</code>.</p></li>
</ul>
<hr/>**评论:**<br/><br/>enobeer: <pre><p>Thanks for creating this. Any plans to support Join() in builder?</p></pre>mgutz: <pre><p>I don't plan to add a Join method. The first option is</p>
<pre><code>DB.Select('*').
From(`
users u INNER JOIN account acc on (acc.user_id = u.id)
`).
Where("acc.balance > $1", amount).
QueryStruct(&users)
</code></pre>
<p>There are also scopes</p>
<pre><code>brokeUsers := `
INNER JOIN account acc on (acc.user_id = u.user_id)
WHERE acc.balance < 1
`
richUsers := `
INNER JOIN account acc on (acc.user_id = u.user_id)
WHERE acc.balance > 1000000
`
err := DB.Select("*").From("users u").Scope(brokeUsers).QueryStruct(&users);
</code></pre></pre>enobeer: <pre><p>The first option looks perfect, thanks! I didn't realize From() accepts join statements.</p></pre>elithrar_: <pre><p>Great stuff—I like the change to <code>*runner.DB</code>.</p>
<p>Out of curiosity: have you ever tested the interpolation against an SQLi fuzzer/testing tool?</p></pre>mgutz: <pre><p>There is now. <a href="https://github.com/mgutz/dat/blob/v1/sqlx-runner/sqli_test.go" rel="nofollow">https://github.com/mgutz/dat/blob/v1/sqlx-runner/sqli_test.go</a></p></pre>elithrar_: <pre><p>Appreciated—just saw the commit come in via an IFTTT email (dat is on my "tell me when it's updated" list).</p>
<p>I've been a big fan of sqlx but dat's RTT performance + API (syntax) have been drawing me over. Current project is using Amazon RDS so avoiding 2+ 20ms round trips per query is a Big Deal (tm).</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传