$ go get github.com/go-xorm/cmd/xorm
$ xorm help reverse
usage: xorm reverse [-m] driverName datasourceName tmplPath [generatedPath]
according database's tables and columns to generate codes for Go, C++ and etc.
-m Generated one go file for every table
driverName Database driver name, now supported four: mysql mymysql sqlite3 postgres
datasourceName Database connection uri, for detail infomation please visit driver's project page
tmplPath Template dir for generated. the default templates dir has provide 1 template
generatedPath This parameter is optional, if blank, the default value is model, then will
generated all codes in model dir
xorm reverse postgres "user=kyokomi host=localhost port=5432 dbname=roguegame sslmode=disable" templates/goxorm
model/tAccount.go
package model
type TAccount struct {
AccountId int64 `xorm:"not null BIGINT"`
Uuid string `xorm:"not null TEXT"`
Name string `xorm:"not null TEXT"`
Description string `xorm:"not null TEXT"`
}
$ xorm dump postgres "user=kyokomi host=localhost port=5432 dbname=roguegame sslmode=disable" > dump.sql
CREATE TABLE IF NOT EXISTS "t_account" ("account_id" BIGINT NOT NULL, "uuid" TEXT NOT NULL, "name" TEXT NOT NULL, "description" TEXT NOT NULL);
INSERT INTO "t_account" ("account_id", "uuid", "name", "description") VALUES (1, 'dddda', 'aaaa', 'hoge');
INSERT INTO "t_account" ("account_id", "uuid", "name", "description") VALUES (1, 'aaaa', 'dddd', 'hoge');
INSERT INTO "t_account" ("account_id", "uuid", "name", "description") VALUES (1, 'dddd', 'dddd', 'hoge');
$ xorm source postgres "user=kyokomi host=localhost port=5432 dbname=roguegame sslmode=disable" < dump.sql
panic: runtime error: invalid memory address or nil pointer dereference
roguegame=# drop table t_account;
DROP TABLE
roguegame=# \d
No relations found.
TABLEをdropしておく。
$ xorm source postgres "user=kyokomi host=localhost port=5432 dbname=roguegame sslmode=disable" < dump.sql
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x400d7ce]
goroutine 16 [running]:
runtime.panic(0x46fbf00, 0x4bbade4)
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/panic.c:279 +0xf5
main.runSource(0x4bb75c0, 0xc20800e020, 0x2, 0x2)
/Users/kyokomi/src/github.com/go-xorm/cmd/xorm/source.go:49 +0x34e
main.main()
/Users/kyokomi/src/github.com/go-xorm/cmd/xorm/xorm.go:63 +0x2be
goroutine 19 [finalizer wait]:
runtime.park(0x4029590, 0x4bed988, 0x4bbf749)
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:1369 +0x89
runtime.parkunlock(0x4bed988, 0x4bbf749)
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:1385 +0x3b
runfinq()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/mgc0.c:2644 +0xcf
runtime.goexit()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:1445
goroutine 21 [chan receive]:
database/sql.(*DB).connectionOpener(0xc208054080)
/usr/local/Cellar/go/1.3/libexec/src/pkg/database/sql/sql.go:583 +0x48
created by database/sql.Open
/usr/local/Cellar/go/1.3/libexec/src/pkg/database/sql/sql.go:442 +0x27c
goroutine 17 [syscall]:
runtime.goexit()
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/proc.c:1445
エラーってるがな。。。
roguegame=# select * from t_account
;
account_id | uuid | name | description
------------+------+------+-------------
1 | popo | popo | hoge
(1 row)
データは登録できてる。exitでコケてるっぽい?
コード上からCREATEするのキモいって人向けかな。
cmdツール使わないで、sqlそのまま普通に流したほうがよさそう。