dat.v1 - Postgres data access toolkit (BREAKING CHANGES)

polaris · · 673 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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(&#39;...&#39;).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(&#34;Failed in nested&#34;, 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(&#34;id&#34;, &#34;user_name&#34;, &#34;avatar&#34;). Many(&#34;recent_comments&#34;, `SELECT id, title FROM comments WHERE id = users.id LIMIT 10`). Many(&#34;recent_posts&#34;, `SELECT id, title FROM posts WHERE author_id = users.id LIMIT 10`). One(&#34;account&#34;, `SELECT balance FROM accounts WHERE user_id = users.id`). From(&#34;users&#34;). Where(&#34;id = $1&#34;, 4). QueryStruct(&amp;obj) // obj must be agreeable with json.Unmarshal() </code></pre> <p>```</p> <ul> <li><p>Session obsoleted. Go&#39;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&#39;t plan to add a Join method. The first option is</p> <pre><code>DB.Select(&#39;*&#39;). From(` users u INNER JOIN account acc on (acc.user_id = u.id) `). Where(&#34;acc.balance &gt; $1&#34;, amount). QueryStruct(&amp;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 &lt; 1 ` richUsers := ` INNER JOIN account acc on (acc.user_id = u.user_id) WHERE acc.balance &gt; 1000000 ` err := DB.Select(&#34;*&#34;).From(&#34;users u&#34;).Scope(brokeUsers).QueryStruct(&amp;users); </code></pre></pre>enobeer: <pre><p>The first option looks perfect, thanks! I didn&#39;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 &#34;tell me when it&#39;s updated&#34; list).</p> <p>I&#39;ve been a big fan of sqlx but dat&#39;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

673 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传