Choosing a database querying and mapping tool

agolangf · · 493 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, during the weekend I&#39;ve been Googling and experimenting with various libraries like sqlx, gorp and many others. But, I&#39;ve been unable to find one that satisfies my requirements while still not being too heavy or over-featured. It&#39;s also worth mentioning that I&#39;m new to Go, and that I&#39;m aware that this question is asked a lot.</p> <p>What I&#39;m looking for could be compared to the iBATIS/mybatis of Java/.NET, where you write your own queries and the mapping for the resulting objects/structs and nested object/structs collections. I prefer to write my own queries, since it&#39;s less trouble to find eventual errors.</p> <p>Now, the database schema I have has many to one and many to many relations. I want to end up with a struct that has a slice of the other struct. Example provided: <a href="https://pastebin.com/b86N3iaZ" rel="nofollow">https://pastebin.com/b86N3iaZ</a></p> <p>So far I&#39;ve been unable to come up with a good way to map the results from these queries without resorting to database-specific solutions like json_agg() from postgres. Ḯ&#39;ve settled on using postgres anyway, so here&#39;s what I came up with: <a href="http://sqlfiddle.com/#!17/1c91c/6" rel="nofollow">http://sqlfiddle.com/#!17/1c91c/6</a></p> <p>I can map the json to the nested slice of structs I suppose, but this has both an overhead in Go and the database, as the comments I put in the fiddle shows. I&#39;m aware that some of the Go ORM&#39;s support associations/relations, but I really, really can&#39;t stand the query building language they provide. I want to write SQL without making multiple queries to fill out my structs.</p> <p>If there&#39;s a solution I&#39;ve missed, then that&#39;d be great. If there isn&#39;t and I&#39;m forced to use an ORM, then which one is the best for my needs? Basic, simple.</p> <p>Thanks for reading.</p> <hr/>**评论:**<br/><br/>kaeshiwaza: <pre><p>Did you look at <a href="https://github.com/jmoiron/sqlx" rel="nofollow">https://github.com/jmoiron/sqlx</a> ?</p></pre>deproggen: <pre><p>Yes, and I&#39;d like to use it very much, if it can map many to many relations.</p></pre>kaeshiwaza: <pre><p>I don&#39;t know if it can map one to many, but many to many yes, with composition of the 3 tables.</p></pre>krak3n_: <pre><p>If you just wanna make SQL I like <a href="https://github.com/Masterminds/squirrel" rel="nofollow">https://github.com/Masterminds/squirrel</a></p></pre>deproggen: <pre><p>Squirrel &#34;&#34;&#34;&#34;&#34;helps&#34;&#34;&#34;&#34;&#34; me by adding a pointless layer on top of an already excellent and expressive language, SQL. At least, that&#39;s what I got from the documentation.</p></pre>tcrypt: <pre><p>Squirrel helps people when they need to build a query at run time so they don&#39;t have to manipulate strings and potentially cause errors. You are correct in that it is not a solution to the question you asked about.</p></pre>krak3n_: <pre><p>Another I have used is <a href="https://upper.io/db.v3/" rel="nofollow">https://upper.io/db.v3/</a> which I think will give you the relation mapping you are after <a href="https://tour.upper.io/sql-builder/02" rel="nofollow">https://tour.upper.io/sql-builder/02</a> - but does add layer on top of an already excellent and expressive language, SQL.</p></pre>[deleted]: <pre><p>Did you take a look at <a href="https://github.com/jinzhu/gorm" rel="nofollow">Gorm</a>? It supports relations, and also support custom SQL easily. </p> <p>Never used sqlx, but I think it should also cover your use case well.</p></pre>deproggen: <pre><p>I had a look at both. Gorm does support relations, although I&#39;m not sure if it supports mapping the relations from raw SQL. It&#39;s also one of the heavier libraries there is, which I&#39;d like to avoid if possible.</p> <p>I haven&#39;t been able to find a way for sqlx to handle mapping resultsets from many to many queries yet. Still trying.</p></pre>M1hkel: <pre><p>Maybe <a href="https://github.com/go-pg/pg/wiki/Writing-Queries" rel="nofollow">https://github.com/go-pg/pg/wiki/Writing-Queries</a></p></pre>deproggen: <pre><p>Does handle relations, but only using query building from the looks of it.</p></pre>ChristophBerger: <pre><p>Are you maybe looking for sth like <a href="https://github.com/volatiletech/sqlboiler" rel="nofollow">SQLboiler</a> that generates the code from the database schema rather than the other way round. It provides a <a href="https://github.com/volatiletech/sqlboiler#raw-query" rel="nofollow">&#34;raw query&#34; feature</a> for writing direct SQL, in addition to its own <a href="https://github.com/volatiletech/sqlboiler#query-building" rel="nofollow">query building mechanisms</a>.</p></pre>

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

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