A replacement for database sql

栏目: IT技术 · 发布时间: 5年前

内容简介:Imagine if you had the power to rewrite the notoriousWhat would it look like?It would look like the all-new

A replacement for database/sql

Imagine if you had the power to rewrite the notorious database/sql package so that it was friendlier, simpler and less verbose.

What would it look like?

It would look like the all-new dbq package . Version 2.2 was just released with so many bells and whistles that you’ll never directly use the database/sql package again.

A small subset of the features are showcased below. Also read to the end to see the new direction of the package.

Zero boilerplate code

You can execute simple queries in 8 lines instead of 30. And it comes with built-in unmarshaling.

A replacement for database sql

dbq vs database/sql

The contrast speaks for itself.

Flatten Query Args

You have a []string with a list of query arguments that you want to inject into a query without SQL-injection issues. You can feel the irritation building up.

func (db *DB) Query(query string, args ...interface{})

Unfortunately, args only accepts interface{} so you need to superfluously iterate over the entire []string to copy the values into a []interface{} . It makes your code look like Wuhan 2 months back. dbq has your back.

All slices are flattened automatically . No questions asked.

A replacement for database sql
No questions asked!

Mission-critical queries

What if you have a query that HAS TO SUCCEED . Failure is not an option®.

dbq has built-in retry capabilities so it will continue to attempt your query with exponentially increasing intervals.

Transaction management

What about transactions? Got you covered.

dbq.Tx(ctx, db, func(tx interface{}, ..., txCommit dbq.TxCommit) {   res, err := E(ctx, "exec statement")
   if err != nil {
      return // Automatic rollback
   }
   txCommit()})

You can have an arbitrarily complex database operation happening within a transaction without having to worry about rolling-back ON every SINGLE sad PATH. Just commit at the end.

Finally, THIS IS YOUR PACKAGE

The x subpackage houses custom query helper functions that are general xor MySQL/PostgreSQL specific.

You are welcome to submit PR’s with whatever custom functions you want. As long as you leave the core package pristine, and your code compiles and is well documented, then it’ll be welcomed with open arms.

As a warm-up, I’ve included a BulkUpdate function which allows you to update thousands of rows with 1 query without a transaction!

Stay safe boys and girls.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

电商产品经理宝典:电商后台系统产品逻辑全解析

电商产品经理宝典:电商后台系统产品逻辑全解析

刘志远 / 电子工业出版社 / 2017-10-1 / 49.00元

时至今日,对于产品经理的要求趋向业务型、平台型,甚至产生了细分领域专家。纯粹的前端产品经理(页面、交互)逐渐失去竞争力。而当后台产品经理的视野开始从功能延伸到模块,再延伸到子系统,最后关注整体系统时,就有了把控平台型产品的能力。 《电商产品经理宝典:电商后台系统产品逻辑全解析》围绕“电商后台产品”,从电商的整体产品架构入手,逐步剖析各支撑子系统。通过学习电商产品后台的架构和逻辑,可以让读者从......一起来看看 《电商产品经理宝典:电商后台系统产品逻辑全解析》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具