dat - Enhanced PostgreSQL builder

xuanbao · · 1011 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://github.com/mgutz/dat">https://github.com/mgutz/dat</a></p> <p>Latest version adds</p> <ul> <li>All errors print SQL and arguments</li> <li>single row Upsert and Insect (insert or select), both are single trip to DB</li> <li>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.</li> <li>More features to make working with JSON and embedded structs friendlier. Targeted at PostgreSQL 9.3+</li> </ul> <p>Intuitive JSON Document or Go Hierarchical struct retrieval (SINGLE TRIP to database!)</p> <pre><code>con.SelectDoc(&#34;id&#34;, &#34;user_name&#34;, &#34;avatar&#34;). HasMany(&#34;recent_comments&#34;, `SELECT id, title FROM comments WHERE id = users.id LIMIT 10`). HasMany(&#34;recent_posts&#34;, `SELECT id, title FROM posts WHERE author_id = users.id LIMIT 10`). HasOne(&#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>results in</p> <pre><code>{ &#34;id&#34;: 4, &#34;user_name&#34;: &#34;mario&#34;, &#34;avatar&#34;: &#34;https://imgur.com/a23x.jpg&#34;, &#34;recent_comments&#34;: [{&#34;id&#34;: 1, &#34;title&#34;: &#34;...&#34;}], &#34;recent_posts&#34;: [{&#34;id&#34;: 1, &#34;title&#34;: &#34;...&#34;}], &#34;account&#34;: { &#34;balance&#34;: 42.00 } } </code></pre> <p>Simpler JSON retrieval for rapid application development</p> <pre><code>var json []byte json, _ = con.SQL(`SELECT id, user_name, created_at FROM users WHERE user_name = $1 `, &#34;mario&#34;, ).QueryJSON() // straight into map var obj map[string]interface{} con.SQL(`SELECT id, user_name, created_at FROM users WHERE user_name = $1 `, &#34;mario&#34;, ).QueryObject(&amp;obj) </code></pre> <p>both result in</p> <pre><code>{ &#34;id&#34;: 1, &#34;user_name&#34;: &#34;mario&#34;, &#34;created_at&#34;: &#34;2015-03-01T14:23&#34; } </code></pre> <hr/>**评论:**<br/><br/>RIC_FLAIR-WOOO: <pre><p>Wow, this is cool. </p> <p>I&#39;ve been using Postgresql&#39;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.</p> <p>Great work, can&#39;t wait to try it out.</p></pre>mgutz: <pre><p>That&#39;s the magic behind the JSON methods. Let me know if the subqueries can be optimized.</p></pre>cytospin: <pre><p>Just a note that this could be confused with <a href="http://dat-data.com" rel="nofollow">dat</a>.</p></pre>mgutz: <pre><p>Darn it ... that project looks interesting though. I&#39;m close to marking it 1.0 and will rename it then.</p></pre>pkieltyka: <pre><p>I think you should keep your project the same .. yours is a Go library</p></pre>cytospin: <pre><p>I don&#39;t think you necessarily need to change it. I&#39;m interested in both which is why I initially thought that yours had something to do with the other.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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