Go ã§ãªã¬ã¼ã·ã§ãã«ãã¼ã¿ãã¼ã¹ãå©ç¨ããã¢ããªã±ã¼ã·ã§ã³ãæ¸ãã¦ããã¨ããåçã« SQL ãçµã¿ç«ã¦ããå ´åã«ã¯ãããã¤ãã®æ¹æ³ãèãããã¾ã:
ã¯ã¨ãªãã«ãã使ããä¸ã®ä¸ã«ãã§ã«ããããåå¨ãã¾ããï¼ãã®ããã®ã©ã¤ãã©ãªãªã®ã§ï¼åçã«çæããã«ã¯ãã£ã¦ããã§ããããã®å ´åãããããã®ã©ã¤ãã©ãªã«åãããæ¸ãæ¹ãããªããã°ãªããªãã®ã§èªã¿æã«ãããç¨åº¦è² è·ãããç¹ãã¾ããGo ã¯è¨èªã¨ãã¦åé·ã«æ¸ããã¨ãããã¨ããææ³ãæã£ã¦ãããããDSL ç㪠API ã¨ã®ç¸æ§ãæªãã¨ããæ¬ ç¹ãããã¾ãï¼map ã®çµã¿ç«ã¦ãåé·ãæ¡ä»¶åå²ããå¼ãæ¸ããªããªã©ï¼ãã¾ããä¸è¬ã«ã¯ã¨ãªãã«ãããçæããã SQL ãã³ã¼ãããæ³åãã¥ãããªãåé¡ãããã¾ãã
æååé£çµã fmt.Sprintf
ã使ããçºè¡ããã SQL ã¯æ¯è¼çåããããããªãã¾ãããåçã«çµã¿ç«ã¦ã㨠SQL ãã¬ã¼ã¹ãã«ãã¨ãã¤ã³ããããå¤ãã½ã¼ã¹ã³ã¼ãä¸ã®é¢ããä½ç½®ã«ç»å ´ããããã«ãªãã¨ã対å¿ãåããã¥ãããªã£ã¦ãã¾ãã¾ãã
èªåã Go 㧠RDB ã使ã£ãã¢ããªã±ã¼ã·ã§ã³ãæ¸ãã¦ãã¦ãã®ç¹ã§ããªãå°ã£ã¦ãã¾ã£ãã®ã§ãã²ã¨ã¤ã®è§£æ±ºæ¡ã¨ãã¦ãæ
£ã親ããã fmt
ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ä¹ã£ãã£ãã¯ã¨ãªãã«ããæ¸ãã¾ããã
https://github.com/motemen/go-sqlf
使ãæ¹
sqlf.Printf(format string, values ...interface{}) sqlf.SQL
ãã»ã¼å¯ä¸ã® API ã§ããè¿ãå¤ã§ãã sqlf.SQL
ããçºè¡ãããï¼ãã¬ã¼ã¹ãã«ãã¤ãã®ï¼ã¯ã¨ãªã¨æç¸ããå¤ã®çµã表ãã¾ããããã«ãã®ãBuildSQL() (string, []interface{})
ã¡ã½ããã§å¾ããã query
㨠args
ã database/sql.DB.Query()
ãªã©ã® API ã«æ¸¡ããã¨ã§ãæãã¯ã¨ãªãå®è¡ã§ãã¾ãã
query, args := sqlf.Printf( "SELECT %s FROM %s WHERE col1 = %_ AND col2 IN (%_)", "id", // SELECT %s "table", // FROM %s "x", // col1 = %_ []interface{}{1, 2, 3}, // col2 IN (%_) ).BuildSQL() fmt.Println(query) // SELECT id FROM table WHERE col1 = ? AND col2 IN (?,?,?) fmt.Println(args) // [x 1 2 3]
è¦ãç®ã®ã¨ãã fmt
ã®ãã®ã¾ã¾ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ãããgo-sqlf ã§ã¯ç¹å¥ã« %_
ã¨ãããã¬ã¼ã¹ãã«ããå®ç¾©ããã¦ããã®ããã¤ã³ãã§ããä»ã¨éã£ã¦ãããã«å¯¾å¿ããå¤ã¯ãã®ã¾ã¾æååã«ã¯åãè¾¼ã¾ããã代ããã« SQL ã®ãã¬ã¼ã¹ãã«ãï¼?
ï¼ã¨ãªãã¾ããå¤èªèº«ã¯ BuildSQL
ããéã® args
ã®ã»ãã«åºç¾ãã¾ãï¼ä¸ã®ä¾ã® "x"
ï¼ã
ã¾ãç¹ã«å¤ã¨ã㦠[]interface{}
ãªã©ã¹ã©ã¤ã¹ãä¸ããããå ´åããã®åæ°ã«å¿ãããã¬ã¼ã¹ãã«ããæååã«åãè¾¼ã¾ãã¾ãï¼ä¸ã®ä¾ã® []interface{1,2,3}
ï¼ãWHERE ... IN (...)
ãªã¯ã¨ãªã®çæã«ä¾¿å©ã§ãã
BuildSQL()
ã®è¿ãå¤ã¯ãã®ã¾ã¾ database/sql
ã®å種 API ã«æ¸¡ããã¨ãæ³å®ãã¦ãã¦ãããã§æå³ããã¯ã¨ãªãå®å
¨ã«å®è¡ã§ãã¾ããã·ã§ã¼ãã«ããã¨ãã¦ã*sql.DB
ãå¼æ°ã«ã¨ã£ã¦å®è¡ãã Query
ã¨ãã£ãã¡ã½ããã使ããã¨ãã§ãã¾ãã
é¨åç㪠SQL ã®åãè¾¼ã¿
ä¸è¬ã«ã¯ã¨ãªãã«ãã使ãåæ©ã¨ãã¦ã¯ãä¾ãã° WHERE ç¯ã®ä¸é¨ã®å¼ãåçã«å¤åãããããã¨ãããã¾ããgo-sqlf ã§ã¯ãã®å ´åãsqlf.Printf
ã«ãã£ã¦çæããã sqlf.SQL
ãå¥ã® Printf
ã®å¼æ°ã«æ¸¡ããã¨ã§å®ç¾ãã¾ãã
wherePart := sqlf.Printf("col1 IN (%_)", []interface{}{"x", "y"}) query, args := sqlf.Printf( "SELECT id FROM table WHERE %_ AND col2 = %_", wherePart, "z", ).BuildSQL() fmt.Println(query) // SELECT id FROM table WHERE col1 IN (?,?) AND col2 = ? fmt.Println(args) // [x y z]
wherePart ã®ãã¬ã¼ã¹ãã«ãã¨ãã®å¼æ°ãæçµçãªçµæã«ãã¾ãå©ç¨ããã¦ãããã¨ãåããã¨æãã¾ãã
å®è£
fmt
ããã±ã¼ã¸ããã®ã¾ã¾å©ç¨ãã¦ãã¾ãï¼ãªã®ã§ %s
以å¤ã«ãä¾ãã° %d
ãªããããã®ã¾ã¾ä½¿ãã¾ãï¼ãfmt
ã® API ã¯å¼æ°ã fmt.Formatter
ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£
ãã¦ããã°ãã®å®è£
ãå°éãã¦æååå±éããã®ã§ãsqlf.Printf
ã«æ¸¡ãããå¼æ°ãé©å½ãªå®è£
ã§ãããã§ããã°ä¸è¨ã®ãããªæåãæå¤ã¨ç°¡åã«å®ç¾ã§ãã¾ãããå
æ¥ã® fmt
ã§ã¯ä½¿ããã¦ããªã %_
ãå©ç¨ã§ããããã§ãã
ã¨ã¯ããããã®ã¸ãã¯ã¡ããã¨å®è£ ãä»æ§ãææ¡ãã¦ããªãã®ã§ãä»å¾ãæå¹ãã©ããã¯åãããªããã¾ãããã©ã¼ããããæååã®å é ããç´åã«å¦çããããã¨ã«ä¾åãã¦ãã¾ãããã¾ããã®æåã¯å°æ¥çã«ãå¤åãªãããããªãããªâ¦â¦ã
以ä¸ãæãã¤ãã§æ¸ãããã¤ãªã®ã§ççµããããã¨æãã¾ããï¼MySQL ã®ãã¨ããèãããã¦ãªããï¼ããã£ãã便å©ãããªã®ã§ã©ãããå©ç¨ãã ããã