Need help with Relational Databases and Golang

agolangf · · 617 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello all, I have alot of experience with relational databases in other languages (php, c#, java, etc) but whenever i try to use the sql package in go it feels superrrr clunky.... how do you guys deal with nulls? (i like using deletedAt nullable date time columns) I hate having to use things like sql.NullString and I especially hate having to use mysql.NullTime because it kills my portability. Right now Im leaning twords using <a href="https://github.com/boltdb/bolt">https://github.com/boltdb/bolt</a> but I like relational DB&#39;s....</p> <p>It just feels clunky...</p> <p>edit: preliminary tests show that you can use string/date pointers rather then sql.NullString, I need to explore this further....</p> <p>thanks <a href="/u/TheMerovius">/u/TheMerovius</a></p> <hr/>**评论:**<br/><br/>twek: <pre><p>Im going to give this a try i think...</p> <p><a href="http://jmoiron.github.io/sqlx/" rel="nofollow">http://jmoiron.github.io/sqlx/</a></p></pre>BoTuLoX: <pre><p>It adds some nifty functionality. To deal with nullable time I just make it a pointer.</p></pre>twek: <pre><p>yeah thats where im getting too... I really dislike those nullable structs pointers seem nicer</p></pre>Growlizing: <pre><p>I also found it extremely clunky. Something like gorp did help me though, which I&#39;ve now started using for rdbms.</p></pre>twek: <pre><p>glad im not the only one! I looked at gorp but gorm seemed more supported and it wasnt very unclunky either</p></pre>Growlizing: <pre><p>Yeah, well, I&#39;m too scarred from java and ruby world I think to ever use a full ORM again :(</p></pre>twek: <pre><p>I know that feeling.</p> <p>In java i roll my own DAO&#39;s using springs NamedJDBCTemplate</p> <p>its the only spring i can stomach</p></pre>TheMerovius: <pre><p>a) If your data can&#39;t be NULL, use <code>string</code>. If your data <em>can</em> be NULL, you&#39;ll have to use <code>sql.NullString</code> or something like it anyway. I don&#39;t see, how that affects your &#34;portability&#34;? (I think… maybe? You can also use a pointer. Not sure)</p> <p>b) If you don&#39;t want to put <code>sql.NullString</code> like things into your struct, you can scan into an anonymous struct and then manually copy the fields over with whatever logic you like.</p> <p>But I find it difficult seeing your problem. I don&#39;t see how PHP specifically makes this easier?</p></pre>twek: <pre><p>the portability problem comes from the fact that the standard sql package does not contain a sql.NullDate so you have to use mysql.NullDate which locks you to the mysql package (not sure if theres an alternative in sqlite etc packages) I havnt tried a date pointer... Ill have to try that. </p> <p>I thought about the anonymous struct thing and its probably my best bet (because yes i dont like sql.NullString in my struct if i can help it)</p> <p>Im not having any PROBLEMS (i.e the code works)</p> <p>but the development of said code feels clunky is all... and when that happens I like to think theres a better way.</p></pre>TheMerovius: <pre><blockquote> <p>the portability problem comes from the fact that the standard sql package does not contain a sql.NullDate so you have to use mysql.NullDate which locks you to the mysql package (not sure if theres an alternative in sqlite etc packages) I havnt tried a date pointer... Ill have to try that.</p> </blockquote> <p>You can use a mysql.NullDate with any other sql driver too. Or just copy-paste the type into your own code and use that (then you don&#39;t need to import the mysql-driver.</p> <p>Also, in my experience, every SQL database has a different feature-set (in particular, dates are AFAIK one of the things that differ wildly) so you very probably are not portable between databases anyway. :)</p> <blockquote> <p>and when that happens I like to think theres a better way.</p> </blockquote> <p>I like sqlx whenever I need SQL stuff :) It gives a good tradeoff between simplicity and power, IMHO.</p> <p>[edit] To elaborate on portability: For example, how you give placeholder in prepared statements varies, sometimes you need positional arguments, sometimes you need &#39;?&#39; (I think mysql does that) and sometimes both work.</p></pre>twek: <pre><p>I have not tried sqlx yet but just the fact that I can use pointers in my structs rather then sql.Null**** is already a great win, the alternative is to write a nice search feature for boltdb. Which would give me an excuse to brush up on cool stuff like breadth first search and graphs....</p></pre>twek: <pre><p>And php/java etc make this easier by allowing nullable primitives </p></pre>TheMerovius: <pre><p><code>sql.NullString</code> is a &#34;nullable primitive&#34;. It gives you <em>precisely</em> the same semantics, just slightly different syntax.</p></pre>twek: <pre><p>Well its a struct not a primitive</p></pre>TheMerovius: <pre><p>As I said, it gives you precisely the same semantics. You just have to write <code>if !nullString.Valid()</code> instead of <code>if (!defined null_string)</code>, or however you check for undefinednes in PHP and use <code>nullString.Value()</code>, instead of <code>null_string</code>. You are splitting hairs.</p></pre>WellAdjustedOutlaw: <pre><p>A word of warning. If you think a key/value store is going to help you when you need an rdbms, you&#39;re in for a world of hurt. Also, if you think using and learning a new db is going to be a cakewalk vs the probably years of experience you have with something like MySQL, you&#39;re going to have a very bumpy ride. Just my $0.02</p> <p>I found the Go approach to databases a bit wonky until I understood Go more. Not that I&#39;m good now, I just seem to understand other Golang developers&#39; intentions more readily now. /shrug</p></pre>twek: <pre><p>Luckily I also have years of experience with writing various btree key value stores, for my application to work I would just have to write an efficient search for boltdb I was thinking a dictionary with a graph like component... iono Ill probably just use mysql and pointers</p></pre>ptman: <pre><p>Not all Go ORMs can handle pointers to time.Time: <a href="https://github.com/astaxie/beego/issues/1019" rel="nofollow">https://github.com/astaxie/beego/issues/1019</a></p></pre>

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

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