qustavo / dotsql

A Golang library for using SQL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

batch insert

vagra opened this issue · comments

INSERT INTO 'dirs' ('id', 'name', 'parent_id') VALUES
  ('1', 'data1', '0'),
  ('2', 'data2', '1'),
  ('3', 'data3', '2'),
  ('4', 'data4', '3');

dotsql can achieve this function?

for example:

-- name: batch-insert-dirs
INSERT INTO 'dirs' ('id', 'name', 'parent_id') VALUES ?;
var dirs []Dir = []Dir{}
...
dirs = append(dirs, dir)
...

_, err := dot.Exec(db, batch-insert-dirs, dirs)

I'm not sure, did you try?

I tried it, it doesn't work.

dotsql can achieve this function?

Nope

very good! using BEGIN and END for sqlite auto transaction. very fast!

func InsertFiles(disk_name string) {

    DBExec(db, "begin")

    for _, file := range files {
        DBExec(db, "add-file",
            file.id, file.parent_id, file.name, file.path, file.size, file.mod_time)
    }

    DBExec(db, "end")
}

dot.sql:

-- name: begin
BEGIN

-- name: end
END

-- name: add-file
INSERT INTO files (id, parent_id, name, path, size, mod_time) VALUES(?, ?, ?, ?, ?, ?);

Nicely done!