How to create SQL update query while using REST API PATCH Request?

blov · · 593 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am writing a rest api. I don&#39;t want to use PUT request for update. I will use PATCH. But how to create a dynamic SQL UPDATE query with golang. I use MySQL as a database.</p> <p>I found this <a href="Http://stackoverflow.com/questions/38206479/golang-rest-patch-and-building-an-update-query" rel="nofollow">Http://stackoverflow.com/questions/38206479/golang-rest-patch-and-building-an-update-query</a></p> <p>But it is working. Do you have any information about the subject?</p> <hr/>**评论:**<br/><br/>bkeroack: <pre><p>If your payloads are JSON you could accept <a href="http://jsonpatch.com/" rel="nofollow">jsonpatch</a> documents as the PATCH request body. </p></pre>victrolla: <pre><p>I had no idea Jsonpatch existed. Thanks!</p></pre>parnacsata: <pre><p>PUT or PATCH, it doesn&#39;t really matter. It just the method.</p> <p>You have to think up a logic to handle. Imagine a struct which holds your data model, for a userprofile or a single todo. You have some fields and an id for each row in your DB. </p> <p>You may require the whole fieldset for one item, then you dont have to do anything: Update all the fields where the id matches.</p> <p>Or you may want just the id of the item, and the field what you want to modify. Now there is two road you can take. Implement a modify for each field(you can write a smart func for that which does that) or get the data for that id, change that data which was modified and overwrite every field like in the first method I could think of.</p> <p>If you want to do it really neat, that would require some time.</p></pre>circuitously: <pre><p>I mean, technically there are differences between PUT and PATCH. With a PUT you&#39;re sending the whole object back, and a PATCH is for amending specific fields in a specific object. </p></pre>parnacsata: <pre><p>That is why I wrote: </p> <blockquote> <p>it doesn&#39;t <strong>really</strong> matter</p> </blockquote></pre>snakegums: <pre><p>Its not clear where you got stuck. Are you trying to solve this problem generically in Go? Go does not support generic programming directly. Common alternatives include code generation and runtime reflection.</p> <p>Not every problem lends itself well to generic programming either. It may be that the best solution is to write code specific to every resource you will handle.</p></pre>tmornini: <pre><blockquote> <p>I don&#39;t want to use PUT request for update. I will use PATCH.</p> </blockquote> <p>Why? You&#39;ll just be making the API development and usage more difficult...</p></pre>quiI: <pre><p>PUT and PATCH have clearly defined different semantics and use cases so its a little odd to just say that PATCH makes usage more difficult</p></pre>google_you: <pre><p>You end up string manipulation to build query, for array fields at least. Just use Mongodb</p></pre>

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

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