My generic db bulk loader, please critique

blov · · 524 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<pre><code>func BulkInsert(db *sql.DB, tableName string, unsavedRows []interface{}) error { var query bytes.Buffer query.WriteString(&#34;UPSERT INTO &#34; + tableName + &#34; (&#34;) t := reflect.TypeOf(unsavedRows[0]) // assume whole collection is same type valueArgs := make([]interface{}, 0) for i := 0; i &lt; t.NumField(); i++ { query.WriteString(t.Field(i).Tag.Get(&#34;column&#34;)) if i != t.NumField() -1 { query.WriteString(&#34;,&#34;) } } query.WriteString(&#34;) VALUES &#34;) fieldNum := 1 for i, post := range unsavedRows { s := reflect.ValueOf(&amp;post).Elem() query.WriteString(&#34;(&#34;) for j := 0; j &lt; t.NumField(); j++ { valueArgs = append(valueArgs, s.Elem().Field(j).Interface()) query.WriteString(&#34;$&#34; + strconv.Itoa(fieldNum)) if j != t.NumField() - 1 { query.WriteString(&#34;,&#34;) } fieldNum += 1 } query.WriteString(&#34;)&#34;) if i != len(unsavedRows) - 1 { query.WriteString(&#34;,&#34;) } } _, err := db.Exec(query.String(), valueArgs...) return err } </code></pre> <p>Assuming we have a struct with a tag like:</p> <pre><code>type Cat struct { Name string `column:&#34;name&#34;` } </code></pre> <hr/>**评论:**<br/><br/>LogicX: <pre><p>if working with postgresql, this is not the most efficient way to load data.</p> <p>Also Ikd review <a href="https://github.com/lukasmartinelli/pgfutter" rel="nofollow">pgfutter</a> which also happens to be coded in go.</p></pre>Tr1glav: <pre><p>What if I need to perform some data transformation prior to import? Would it be more efficient to do it, write it out to json/csv and then use this tool or simply call the function above? </p></pre>

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

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