go-sql-driver/mysql �ǥץ졼���ۥ���ִ��򥵥ݡ��Ȥ��ޤ���
����ε����Ǿ�������ޤ������� go-sql-driver/mysql �˥ɥ饤��¦�ǤΥץ졼���ۥ���ִ����������ץ�ꥯ�����Ȥ�Ф��Ƥ��ޤ�����
���줬�ޡ������줿�Τǡ��طʤΤ����餤��������ˡ��Ҳ𤷤Ƥ����ޤ���
�ط�
Go �� database/sql
�γ��פˤĤ��Ƥ�����ε����Dz��⤷�ޤ�����
���������������Ȥ��ꡢ DB.Prepare()
��Ȥ鷺��ľ�� DB.Exec()
�� DB.Query()
��Ȥä���硢
�ɥ饤��¦�ǤΥץ졼���ۥ���ִ����б����Ƥ��ʤ��ɥ饤�ФǤ� prepare, exec, close
��3��Υ饦��ɥȥ�åפ�ȯ�����뤳�Ȥˤʤꡢ�ѥե����ޥ󥹤������ʤ�ޤ���
����Ū�ˤ� DB.Prepare()
��Ȥ��Ф����ΤǤ���������ε����ǽ���������������ӥ�ƥ�������� Go 1.5 �ˤʤ�ޤ�ľ��ޤ��󤷡�
IN
�礬����SQLʸ�ʤɤǻ����� Prepare ������ñ¤·¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Í��ޤ���
�ޤ���O/R�ޥåѡ��䥯����ӥ������Ȥ���硢���Υ饤�֥�꤬ Prepared Statement �Υ���å�����б����Ƥ��ʤ��ȡ�
���ץꥱ�������ץ�����ޤ�����Ǥ��ʤ���ʬ�� DB.Prepare()
��Ȥ�ʤ��� DB.Query()
���Ȥ��륱�������ФƤ��ޤ���
���礦�� prepared statement �򥯥��������ȯ�Ԥ���ǥ��åȤ� ���Ĥ���
���� ����ˤʤä����Ȥ⤢�ꡢ go-sql-driver/mysql
�˥��ץ�������Ϳ���� RFC (Request For Comment)
���ä��ץ�ꥯ�����Ȥ��ޡ����˸�����ư���Ϥ�ޤ�����(�ޡ��������ޤǤ˹Ԥä����塼�˥󥰤ˤĤ��ƤϤޤ�����Ҳ𤷤褦�Ȼפ��ޤ���)
go-sql-driver/mysql �Υץ졼���ۥ���ִ��ˤĤ���
go-sql-drvier/mysql
�� go get -u github.com/go-sql-driver/mysql
�Ǽ������ޤ���
�ޤ����ε�ǽ�ϥޡ������줿�Ф���ʤΤǡ����Ǥ����Ѥ���Ƥ������Ϻ��٤��Υ��ޥ�ɤ�¹Ԥ��ƹ������Ƥ���������
sql.Open()
������������Ϥ� dsl �� interpolateParams=true
�Ȥ������ץ�����URL�Υ�����ѥ�᥿�η����Ϥ����Ȥˤ�ꡢ�ץ졼���ۥ���ִ���Í���ˤʤ�ޤ���
��������ʸ����򥨥������פ���ݤ˥Х���ñ�̤ǽ������Ƥ���Τǡ� collation
���ץ����� cp932
�� big5
�ʤɴ����ʥ��󥳡��ǥ��󥰤���ꤷ�����ϥ��顼�ˤ��Ƥ��ޤ���
(���ʤߤˡ� go-sql-driver/mysql �ǥ��ͥ������Υ��󥳡��ǥ��󥰤���ꤹ�� charset
���ץ�������侩�ʤΤǡ� collation
���ץ��������Ѥ��Ƥ�������)
collation�ϻ��ꤷ�ʤ����ǥե���Ȥ� utf8_general_ci
�Ǥ�����������Ǥ� utf8mb4_bin
�˻��ꤷ�Ƥ��ޤ���
package main import ( "database/sql" "log" _ "github.com/go-sql-driver/mysql" ) func main() { // localhost �� 3306 �֥ݡ��Ȥ�ư���Ƥ��� MySQL �ˡ��桼����̾=root, �ѥ����=password, �ǡ����١���=test ����³���롣 // interpolateParams=true ���ץ��������Ѥ��롣 db, err := sql.Open("mysql", "root:password@tcp(localhost:3306)/test?interpolateParams=true&collation=utf8mb4_bin") // �ץ졼���ۥ���ִ���Ԥ�ʤ����Ϥ����� //db, err := sql.Open("mysql", "root:password@tcp(localhost:3306)/test?collation=utf8mb4_bin") if err != nil { log.Fatal(err) } if _, err := db.Exec("SELECT SLEEP(?)", 42); err != nil { log.Fatal(err) } }
���Υץ�������¹Ԥ��Ƥ�����̤Υ��å���󤫤� SHOW FULL PROCESSLIST
��¹Ԥ���ȡ����ץ����θ��̤���ǧ�Ǥ��ޤ���
interpolateParams=true
���դ��ʤ����ϡ����Τ褦�� Command
�� Execute
�ˤʤꡢ Info
�Ǥϥץ졼���ۥ�������Τޤ�ɽ������ޤ���(�ȤƤ⥿���ߥ󥰤��ɤ���С� Execute
���ޥ�ɤ���ʤ��� Prepare
���ޥ�ɤ������뤫�⤷��ޤ���)
mysql> show full processlist; +------+------+-----------------+------+---------+------+------------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +------+------+-----------------+------+---------+------+------------+-----------------------+ | 7508 | root | localhost | NULL | Query | 0 | init | show full processlist | | 7511 | root | localhost:55283 | test | Execute | 2 | User sleep | SELECT SLEEP(?) | +------+------+-----------------+------+---------+------+------------+-----------------------+ 2 rows in set (0.00 sec)
interpolateParams=true
���դ������ϡ����Τ褦�� Command
�� Query
�ˤʤꡢ Info
�ǥץ졼���ۥ������ʬ���ͤ���������Ƥ��뤳�Ȥ��ǧ�Ǥ��ޤ���
mysql> show full processlist; +----+------+-----------------+------+---------+------+------------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+------------+-----------------------+ | 2 | root | localhost | NULL | Query | 0 | init | show full processlist | | 3 | root | localhost:49283 | test | Query | 1 | User sleep | SELECT SLEEP(42) | +----+------+-----------------+------+---------+------+------------+-----------------------+ 2 rows in set (0.00 sec)
ʸ����Υ��������פǤϡ� sql_mode
�� NO_BACKSLASH_ESCAPES
�����ꤷ�����ˤ��б����Ƥ��ޤ���
���η�̤� db.Exec("SELECT ?, SLEEP(?)", "Hel'lo\nWorld", 42)
�������Τ�ΤǤ���
mysql> SELECT @@GLOBAL.sql_mode; +-----------------------------------------------------------------+ | @@GLOBAL.sql_mode | +-----------------------------------------------------------------+ | NO_BACKSLASH_ESCAPES,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | +-----------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> show full processlist; +------+------+-----------------+------+---------+------+------------+-----------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +------+------+-----------------+------+---------+------+------------+-----------------------------------+ | 7515 | root | localhost | NULL | Query | 0 | init | show full processlist | | 7517 | root | localhost:55400 | test | Query | 2 | User sleep | SELECT 'Hel''lo World', SLEEP(42) | +------+------+-----------------+------+---------+------+------------+-----------------------------------+ 2 rows in set (0.00 sec)
@methane