GithubHelp home page GithubHelp logo

sqlxbatch-go's Introduction

SQLx Batch Executor

Small package to do batch updates and inserts using sqlx.
Create a batcher, add values and then let it batch insert or update.

The package will take into account the fact that there's a limit to how many sql placeholders you're allowed to have in 1 prepared statement.

Dependancies

Dependancies are managed with glide, enjoy ;-)

Usage

Execer

The batcher expects an object which it can use to execute queries with, the interface for it matches that of sqlx but could also take other things that implent it:

type Execer interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
}

Insert

In the query leave a %s where the placeholders go.

b, err := NewBatchInserter(dbTx,
    "INSERT INTO mytable "+
        "(id, name, other) "+
        "VALUES %s", 3) // it's import to specify how many fields there will be here; 3
assert.NoError(err)

b.AddN(nil, "roobs", "dev")
b.AddN(nil, "bb", "boss")

err = b.BatchExec()
assert.NoError(err)

will execute:

INSERT INTO mytable (id, name, other) VALUES (NULL, "roobs", "dev"), (NULL, "bb", "boss");

Update

In the query leave a %s where the placeholders go.

b, err := NewBatchUpdater(dbTx,
    "UPDATE mytable SET other = 'nub'"+
        "WHERE name IN(%s)", 1) // it's import to specify how many fields there will be here; however update only supports 1
assert.NoError(err)

b.AddN("roobs")
b.AddN("bb")

err = b.BatchExec()
assert.NoError(err)

will execute:

UPDATE mytable SET other = "nub" WHERE name IN ("roobs", "bb");

Base Args

When you have query arguments that don't need to be repeated you can add those with AddBaseArg.

b, err := NewBatchUpdater(dbTx,
    "UPDATE mytable SET other = 'nub' "+
        "WHERE id > ? AND name IN(%s) AND other = ?", 1)
assert.NoError(err)

b.AddN("roobs")
b.AddN("bb")

b.AddBaseArg(0, BASE_ARG_BEFORE)
b.AddBaseArg("dev", BASE_ARG_AFTER)

err = b.BatchExec()
assert.NoError(err)

will execute:

UPDATE mytable SET other = "nub" WHERE id > 0 AND name IN ("roobs", "bb") AND other = "dev";

Any Query

If whatever you want doesn't really work with NewBatchInserter or NewBatchUpdater;
you can use NewBatchExecer and specify the values part of the query that is repeated.

b, err := NewBatchExecer(dbTx,
    "INSERT INTO mytable "+
        "(id, name, other) "+
        "VALUES %s",
    2, "(NULL, ?, ?)")
assert.NoError(err)

b.AddN("roobs", "dev")
b.AddN("bb", "boss")

err = b.BatchExec()
assert.NoError(err)

will execute:

INSERT INTO mytable (id, name, other) VALUES (NULL, "roobs", "dev"), (NULL, "bb", "boss");

Testing

To run the tests you need to create a test DB for the tests to use, so that tests can run in parallel you need N test DBs equal to your amount of cores.
You can easily create these with ```./tools/create-dbs.sh __test_btcwalletd_sqlxbatch `cat /proc/cpuinfo | grep processor | wc -l````

Running tests simply works with go test.

sqlxbatch-go's People

Contributors

rubensayshi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.