<p>Hi,
during the weekend I've been Googling and experimenting with various libraries like sqlx, gorp and many others. But, I've been unable to find one that satisfies my requirements while still not being too heavy or over-featured. It's also worth mentioning that I'm new to Go, and that I'm aware that this question is asked a lot.</p>
<p>What I'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'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'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. Ḯ've settled on using postgres anyway, so here'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'm aware that some of the Go ORM's support associations/relations, but I really, really can'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's a solution I've missed, then that'd be great. If there isn't and I'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'd like to use it very much, if it can map many to many relations.</p></pre>kaeshiwaza: <pre><p>I don'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 """""helps""""" me by adding a pointless layer on top of an already excellent and expressive language, SQL. At least, that'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'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'm not sure if it supports mapping the relations from raw SQL. It's also one of the heavier libraries there is, which I'd like to avoid if possible.</p>
<p>I haven'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">"raw query" 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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传