A Golang library for treating SQL file.
sqlfile can execute multiple queries defined in .sql file with database/sql
go get github.com/tanimutomo/sqlfile
SQL) Prepare sql file.
Don't forget add ;
at last of each query.
-- example.sql
INSERT INTO users ( -- users table
id, name, email, created_at, updated_at
) VALUES (
1, 'user1', 'user1@example.com', now(), now()
);
INSERT INTO articles ( -- articles table
id, user_id, title, content, created_at, updated_at
) VALUES (
1, 1, 'title1', "-- About -- \n I'm sqlfile.", now(), now() -- post1
), (
2, 1, 'title2', '- About - \n I''m sqlfile.', now(), now() -- post2
);
Go) Load and Execute sql file.
import (
"database/sql"
"github.com/tanimutomo/sqlfile"
)
// Get a database handler
db, err := sql.Open("DBMS", "CONNECTION")
// Initialize SqlFile
s := sqlfile.New()
// Load input file and store queries written in the file
err := s.File("example.sql")
// Load input files and store queries written in the files
err := s.Files("example.sql", "example2.sql")
// Load files in the input directory and store queries written in the files
err := s.Directory("./examples")
// Execute the stored queries
// transaction is used to execute queries in Exec()
res, err := s.Exec(db)