sqllaã¨ããSQLãã«ãã¼ãæ¸ãã¾ãããç¹å¾´ã¯ããç¨åº¦åå®å ¨ã§ãããã¨ããªãã¬ã¯ã·ã§ã³ã使ç¨ãã¦ããªããã¨ãæ¢åã®ã¯ã¨ãªãã«ãããããç¨åº¦é«éã§ãããã¨ã§ãã
使ãæ¹
ã¤ã³ã¹ãã¼ã«ã¯ããç°¡åã§ãgoãå ¥ã£ã¦ããç°å¢ã§ä»¥ä¸ã®ã³ãã³ããæã¡ã¾ãã
$ go get github.com/mackee/go-sqlla/cmd/sqlla
ã¾ã以ä¸ã®æ§ãªãã¼ãã«æ§é ã表ããstructãç¨æãã¾ãã
ããã¦ãsqllaã«å¿
è¦ãªã¿ã°ãä»å ãã¦ããã¾ãã
ã¾ãå¾è¿°ã®go generate
ã§å¿
è¦ãªã³ã¡ã³ãã足ãã¾ãããã
user.go
//go:generate sqlla //+table: user type User struct { ID uint64 `db:"user"` Name string `db:"name"` }
ããã¦ãã®ãã¡ã¤ã«ãç½®ããã¦ãããã£ã¬ã¯ããªå ã§ä»¥ä¸ã®ã³ãã³ããå®è¡ãã¾ãã
$ go generate
ããã¨ãuser_auto.go
ã¨ãããã¡ã¤ã«ãçæããã¾ãã
ãã®ãã¡ã¤ã«ã«SQLãçµã¿ç«ã¦ãã¡ã½ãããçãã¦ãã¾ãã以ä¸ã®ããã«ä½¿ãã¾ãã
q := NewUserSQL().Select().ID(uint64(1)) // SELECT id, name FROM user WHERE id = 1 query, args, err := q.ToSql() row := db.QueryRow(query, args...) var id uint64 var name string err := row.Scan(&id, &name)
ID(uint64(1))
ã®ã¨ããã¯uint64
ã®å以å¤ã®ãã®ãå
¥ããã¨ã³ã³ãã¤ã«æã«ã³ã±ãããã«ãªã£ã¦ããã®ã§å®å
¨ã§ãããè£å®ãå¹ããã大éã®ãã¼ãã«ãè¤éãªã«ã©ã ãããã¢ããªã±ã¼ã·ã§ã³ãæ¸ãã¨ãã«ä¾¿å©ã§ãã
ã¾ãLIMITå¥ãOrderByå¥ã使ããã¨ãåºæ¥ã¾ãã
åºæ¬çãªCRUDã«ã¤ãã¦ã¯ãµãã¼ããã¦ãã¾ãã
ä»å¾ãããããã¨
READMEã«THIS IS A ALPHA QUALITY RELEASE. API MAY CHANGE WITHOUT NOTICE.
ã¨æ¸ããã¦ããããã«ã¾ã experimentalãªæãã§ã¡ãã£ã¨å¸¸ç¨ã«ã¯èããªãæãã§ãããããããæ©ãã§ããã®ã§ããã°ãæ¸ãã¦ã¿ã¾ããã
ä»å¾ãããããã¨ã¨ãã¦ã¯ã
- æååé£çµã«
+
ã使ã£ã¦ããã®ãããããã³ã¬ãããªãé度çã«è¶³ãå¼ã£å¼µã£ã¦ããã¨æãããã - INæ¼ç®åã®ãµãã¼ãã以ä¸ã®æ§ãªãã®ãèãã¦ãã
NewUserSQL().Select().IDIn(1,2,3,4)
ã¡ãªã¿ã«æ¼ç®åå¨ãã¯ã¡ãã£ã¨ã¾ã ä¸æ ¼å¥½
NewUserSQL().Select().ID(uint64(100), sqlla.OpGreater)
=
ã®ã¨ãã¯çç¥ã§ããããã«å¯å¤é·å¼æ°ã§ãã£ã¦ããã®ã§ãæ¼ç®åãå¾ç½®ã«ãªã£ã¦ãããsqllaã®æ¼ç®åãè¦ããªãã¦ã¯ãªããªãã®ã§ã©ããããããªã¨ãªã£ã¦ããã
ããæ¸ãã¦ã¦æã£ãããã©ã¾ã intç³»ã¨stringãããµãã¼ããã¦ããªãã£ãã®ã§floatç³»ã¨time.Timeã¨ãããµãã¼ãããããã¨[]byteã§blobãããããªãã
JOINå¥ã®ãµãã¼ã
NewUserSQL().Select().Name("hogehoge").Join(func (r *UserJoiner) { rj := NewUserItemJoiner() r.RelationId(rj.UserId) r.AddColumn(rj.ItemId, rj.Amount) }) // SELECT user.id, user.name, user_item.item_id, user_item.amount FROM user JOIN user_item ON user.id = user_item.id WHERE user.name = "hogehoge";
åºæ¥ããã©ããã¯ãã£ã¦ã¿ãªãã¨ããããªãã
ã¨ã¾ããããªæãã§ãªã¬ãªã¬DSLçãªãã®ãè¦ããªãã¨ãããªãã®ã§ãã·ã³ãã«ãªãã¤ã§ããããã£ã¦ã®ã¯ãid:motemenããã®sqlfãè¯ãã¨æãã¾ãã
ã¾ããä»äºã§ã¯squirrelã使ã£ã¦ãããsqllaã®ã¡ã½ãããã§ã¤ã³ã¨ããsquirrelãåèã«ãã¦ãã¾ãã
ããããã®åæ©ã¯squirrelããªãã¬ã¯ã·ã§ã³ã使ã£ã¦ãããgoã®åããã£ã³ã»ã«ãããã®ã§åã使ãããã«ãããã£ãã¨ããã®ãããã¾ãã
ãã®ä»
go-genddlãªããã®ãæ¸ãã¦ããã¾ãããä»äºã§ããã®æ¹é çã使ã£ã¦ããã¾ãã
ä¸ã§æ¸ãããããªã¿ã°ä»ãstructãCREATE TABLE ~
ã¨ãã£ãDDLã«å¤æãã¦ãããåã§ããæè¿sqlite3ããµãã¼ããã¦ãããsqllaã®ãã¹ãã§ã使ã£ã¦ããã¾ãã
github.com
æ¹é ç¹ã¯è¤åã¤ã³ããã¯ã¹ãè¤åã¦ãã¼ã¯ãã¼ãè¤åPKãè²¼ãããåºæ¥ãã¨ããã§ãããã¡ãã£ã¨å¤§éæãããã®ã§ãã£ã¨ã¡ããã¨ãããã®ã«ãããã¨ããã
ã¨æã£ã¦ãããdrone.ioã§ä¼¼ããããªã®ãåºãã£ã½ãã§ãã
ã¿ããªä¼¼ããããªãã¨èãã¦ãããã§ããããã
ã¾ããä»äºã§ã¯migrationã«GitDDL
ã使ã£ã¦ããã¾ããã¡ãªPerl
metacpan.org
migrationã¹ã¯ãªããçãªãã®ãæ¸ããã«æ¸ãã®ã§é常ã«ä¾¿å©ãªã®ã§ããããããããèµ·åãé ãã®ã§Goã§æ¸ããªãããªã¨æã£ã¦ããããã©ãSQLãã¼ãµã¼ã ã£ããDiffãåºãã¨ãããé常ã«è¤éã¨ãããåã®è ã§ã¯ã¨ã¦ãæ¸ããªãã£ãã®ã§ãããæè¿id:soh335ãããschemalexã¨ããã®ãæ¸ãã¦ããã
ã¾ã 試ãã¦ã¯ããªãã
sqllaã«ããç¨åº¦æ©è½ãå ¥ã£ãã次ã¯ORM(structã§ã¯ï¼)ã ã¨æãã¾ãã®ã§ãæå¾ ãã ããã