Skip to content

Instantly share code, notes, and snippets.

@jabaraster
Last active August 29, 2015 14:22
Show Gist options
  • Save jabaraster/987239234d478ea0fec2 to your computer and use it in GitHub Desktop.
Save jabaraster/987239234d478ea0fec2 to your computer and use it in GitHub Desktop.
genmaiのサンプル…のつもりが動かない
package main
import (
"os"
"fmt"
"time"
"math/rand"
_ "github.com/mattn/go-sqlite3"
_ "github.com/lib/pq"
"github.com/naoina/genmai"
)
type Product struct {
Id int64 `db:"pk"`
Code string `db:"unique"`
Name string
}
func main() {
var db *genmai.DB
var err error
db, err = genmai.New(&genmai.SQLite3Dialect{}, "./sample.db")
// db, err = genmai.New(&genmai.PostgresDialect{}, "host=192.168.50.13 dbname=app user=app password=app sslmode=disable")
if err != nil {
panic(err)
}
db.SetLogOutput(os.Stdout)
err = db.CreateTableIfNotExists(&Product{});
if err != nil {
panic(err)
}
rand.Seed(time.Now().UnixNano())
defer func() {
if err := recover(); err != nil {
db.Rollback()
panic(err)
} else {
db.Commit()
}
}()
if err = db.Begin(); err != nil {
panic(err)
}
insert(db)
insert(db)
}
func insert(db *genmai.DB) {
code := fmt.Sprintf("%d", rand.Int31n(99999))
name := fmt.Sprintf("%d", rand.Int31n(99999))
prod := Product{ Code: code, Name: name }
if _, err := db.Insert(&prod); err != nil {
panic(err)
}
}
[2015-06-04 23:07:13] [9.33ms] CREATE TABLE IF NOT EXISTS "product" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "code" text UNIQUE NOT NULL, "name" text NOT NULL);
[2015-06-04 23:07:13] [0.35ms] INSERT INTO "product" ("code", "name") VALUES (?, ?); ["39037", "57389"]
[2015-06-04 23:07:13] [0.09ms] SELECT last_insert_rowid();
fatal error: unexpected signal during runtime execution
[signal 0xb code=0x1 addr=0x18 pc=0x400e45f]
runtime stack:
runtime.gothrow(0x4415cd0, 0x2a)
/usr/local/go/src/runtime/panic.go:503 +0x8e
runtime.sigpanic()
/usr/local/go/src/runtime/sigpanic_unix.go:14 +0x5e
goroutine 1 [syscall, locked to thread]:
runtime.cgocall_errno(0x4001290, 0xc2080a35f0, 0x0)
/usr/local/go/src/runtime/cgocall.go:130 +0xf5 fp=0xc2080a35d0 sp=0xc2080a35a8
github.com/mattn/go-sqlite3._Cfunc_sqlite3_column_name(0x460b1d0, 0xc200000000, 0x0)
github.com/mattn/go-sqlite3/_obj/_cgo_gotypes.go:226 +0x44 fp=0xc2080a35f0 sp=0xc2080a35d0
github.com/mattn/go-sqlite3.(*SQLiteRows).Columns(0xc208054320, 0x0, 0x0, 0x0)
/Users/jabaraster/Documents/Develop/Go/myproject/src/github.com/mattn/go-sqlite3/sqlite3.go:475 +0xea fp=0xc2080a3658 sp=0xc2080a35f0
database/sql.(*Rows).Next(0xc20803c240, 0x42d7460)
/usr/local/go/src/database/sql/sql.go:1590 +0x65 fp=0xc2080a36e0 sp=0xc2080a3658
database/sql.(*Row).Scan(0xc20801e740, 0xc2080a3870, 0x1, 0x1, 0x0, 0x0)
/usr/local/go/src/database/sql/sql.go:1710 +0x26d fp=0xc2080a37e8 sp=0xc2080a36e0
github.com/naoina/genmai.(*DB).LastInsertId(0xc208050000, 0xc2080108c0, 0x0, 0x0)
/path_to_goroot/src/github.com/naoina/genmai/genmai.go:481 +0x1c6 fp=0xc2080a38b0 sp=0xc2080a37e8
github.com/naoina/genmai.(*DB).Insert(0xc208050000, 0x42d7820, 0xc20803abd0, 0x1, 0x0, 0x0)
/path_to_goroot/src/github.com/naoina/genmai/genmai.go:372 +0x10c5 fp=0xc2080a3dd0 sp=0xc2080a38b0
main.insert(0xc208050000)
/path_to_myproject/src/db.go:56 +0x2dd fp=0xc2080a3eb0 sp=0xc2080a3dd0
main.main()
/path_to_myproject/src/db.go:48 +0x334 fp=0xc2080a3f98 sp=0xc2080a3eb0
runtime.main()
/usr/local/go/src/runtime/proc.go:63 +0xf3 fp=0xc2080a3fe0 sp=0xc2080a3f98
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1 fp=0xc2080a3fe8 sp=0xc2080a3fe0
goroutine 5 [runnable]:
text/template/parse.lexText(0xc208056000, 0x44422a0)
/usr/local/go/src/text/template/parse/lex.go:228 +0x37b
text/template/parse.(*lexer).run(0xc208056000)
/usr/local/go/src/text/template/parse/lex.go:198 +0x5d
created by text/template/parse.lex
/usr/local/go/src/text/template/parse/lex.go:191 +0x1ac
goroutine 6 [runnable]:
database/sql.(*DB).connectionOpener(0xc20803e000)
/usr/local/go/src/database/sql/sql.go:588
created by database/sql.Open
/usr/local/go/src/database/sql/sql.go:452 +0x31c
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2
@jabaraster
Copy link
Author

56行目のInsertでエラーが起きる.
SQLiteをPostgreSQLにすると57行目でエラーが起きる.
いずれの場合もトランザクションを仕込まないとエラーは起きない.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment