@@ -1610,68 +1610,33 @@ func TestCollation(t *testing.T) {
16101610		t .Skipf ("MySQL server not running on %s" , netAddr )
16111611	}
16121612
1613- 	defaultCollation  :=  "utf8mb4_general_ci" 
1613+ 	// MariaDB may override collation specified by handshake with `character_set_collations` variable. 
1614+ 	// https://mariadb.com/kb/en/setting-character-sets-and-collations/#changing-default-collation 
1615+ 	// https://mariadb.com/kb/en/server-system-variables/#character_set_collations 
1616+ 	// utf8mb4_general_ci, utf8mb3_general_ci will be overridden by default MariaDB. 
1617+ 	// Collations other than charasets default are not overridden. So other utf8mb4_* and utf8mb3_* 
1618+ 	// collations are safe. 
16141619	testCollations  :=  []string {
1615- 		"" ,               // do not set 
1616- 		defaultCollation , // driver default 
16171620		"latin1_general_ci" ,
16181621		"binary" ,
16191622		"utf8mb4_unicode_ci" ,
16201623		"cp1257_bin" ,
16211624	}
16221625
16231626	for  _ , collation  :=  range  testCollations  {
1624- 		var  expected , tdsn  string 
1625- 		if  collation  !=  ""  {
1626- 			tdsn  =  dsn  +  "&collation="  +  collation 
1627- 			expected  =  collation 
1628- 		} else  {
1629- 			tdsn  =  dsn 
1630- 			expected  =  defaultCollation 
1631- 		}
1632- 
1633- 		runTests (t , tdsn , func (dbt  * DBTest ) {
1634- 			// see https://mariadb.com/kb/en/setting-character-sets-and-collations/#changing-default-collation 
1635- 			// when character_set_collations is set for the charset, it overrides the default collation 
1636- 			// so we need to check if the default collation is overridden 
1637- 			forceExpected  :=  expected 
1638- 			var  defaultCollations  string 
1639- 			err  :=  dbt .db .QueryRow ("SELECT @@character_set_collations" ).Scan (& defaultCollations )
1640- 			if  err  ==  nil  {
1641- 				// Query succeeded, need to check if we should override expected collation 
1642- 				collationMap  :=  make (map [string ]string )
1643- 				pairs  :=  strings .Split (defaultCollations , "," )
1644- 				for  _ , pair  :=  range  pairs  {
1645- 					parts  :=  strings .Split (pair , "=" )
1646- 					if  len (parts ) ==  2  {
1647- 						collationMap [parts [0 ]] =  parts [1 ]
1648- 					}
1649- 				}
1627+ 		t .Run (collation , func (t  * testing.T ) {
1628+ 			tdsn  :=  dsn  +  "&collation="  +  collation 
1629+ 			expected  :=  collation 
16501630
1651- 				// Get charset prefix from expected collation 
1652- 				parts  :=  strings .Split (expected , "_" )
1653- 				if  len (parts ) >  0  {
1654- 					charset  :=  parts [0 ]
1655- 					if  newCollation , ok  :=  collationMap [charset ]; ok  {
1656- 						forceExpected  =  newCollation 
1657- 					}
1631+ 			runTests (t , tdsn , func (dbt  * DBTest ) {
1632+ 				var  got  string 
1633+ 				if  err  :=  dbt .db .QueryRow ("SELECT @@collation_connection" ).Scan (& got ); err  !=  nil  {
1634+ 					dbt .Fatal (err )
16581635				}
1659- 			}
1660- 
1661- 			var  got  string 
1662- 			if  err  :=  dbt .db .QueryRow ("SELECT @@collation_connection" ).Scan (& got ); err  !=  nil  {
1663- 				dbt .Fatal (err )
1664- 			}
1665- 
1666- 			if  got  !=  expected  {
1667- 				if  forceExpected  !=  expected  {
1668- 					if  got  !=  forceExpected  {
1669- 						dbt .Fatalf ("expected forced connection collation %s but got %s" , forceExpected , got )
1670- 					}
1671- 				} else  {
1636+ 				if  got  !=  expected  {
16721637					dbt .Fatalf ("expected connection collation %s but got %s" , expected , got )
16731638				}
1674- 			}
1639+ 			}) 
16751640		})
16761641	}
16771642}
0 commit comments