Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support bulk insert #20

Merged
merged 5 commits into from
Nov 22, 2021
Merged

support bulk insert #20

merged 5 commits into from
Nov 22, 2021

Conversation

mackee
Copy link
Owner

@mackee mackee commented Nov 19, 2021

What's this?

Support the insert of multiple rows for MySQL and SQLite (3.7.11 or later).

See also: https://dev.mysql.com/doc/refman/8.0/en/insert.html , https://www.sqlite.org/lang_insert.html

Usage

You can call BulkInsert() after the New**SQL(). This returns a **BulkInsertSQL object. It can have multiple **InsertSQL object.

Example:

bq := NewFooSQL().BulkInsert()
for i := 0; i < 3; i++ {
    q := NewFooSQL().Insert().ValueBar(i)
    bq.Append(q)
}

// INSERT INTO `foo` (`bar`) VALUES (?),(?),(?); [0 1 2]
if _, err := bq.ExecContext(ctx, db); err != nil {
    return err
}

Also, **BulkInsert can call OnDuplicateKeyUpdate(). If you need append ON DULICATE KEY UPDATE ~ to the suffix, call this.

Example:

bq := NewFooSQL().BulkInsert()
bq.Append(...)

// INSERT INTO `foo` (`bar`) VALUES (?),(?) ON DUPLICATE KEY UPDATE `bar` = VALUES(`bar`);
_, err := bq.OnDuplicateKeyUpdate().
    SameValueOnUpdateFoo()

@mackee mackee merged commit f92f35c into master Nov 22, 2021
@mackee mackee deleted the feature/bulk-insert branch November 22, 2021 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant