Skip to content

Commit bb6e79b

Browse files
committed
Merge pull request #44 from arnehormann/master
test for thrown error when no charset could be set
2 parents 1be09eb + 3840c6c commit bb6e79b

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

connection.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ func (mc *mysqlConn) handleParams() (err error) {
5050
for _, charset := range charsets {
5151
// ignore errors here - a charset may not exist
5252
err = mc.exec("SET NAMES " + charset)
53-
if err == nil {
54-
var value []byte
55-
value, err = mc.getSystemVar("character_set_connection")
56-
if string(value) == charset {
57-
break
58-
}
53+
if err != nil {
54+
continue
5955
}
56+
var value []byte
57+
value, _ = mc.getSystemVar("character_set_connection")
58+
if string(value) == charset {
59+
err = nil
60+
break
61+
}
62+
err = errors.New("Could not set charset " + charset)
6063
}
6164
if err != nil {
6265
return

driver_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func mustQuery(t *testing.T, db *sql.DB, query string, args ...interface{}) (row
8484
func mustSetCharset(t *testing.T, charsetParam, expected string) {
8585
db, err := sql.Open("mysql", strings.Replace(dsn, charset, charsetParam, 1))
8686
if err != nil {
87-
t.Fatalf("Error connecting: %v", err)
87+
t.Fatalf("Error on Open: %v", err)
8888
}
8989

9090
rows := mustQuery(t, db, ("SELECT @@character_set_connection"))
@@ -98,7 +98,6 @@ func mustSetCharset(t *testing.T, charsetParam, expected string) {
9898
if got != expected {
9999
t.Fatalf("Expected connection charset %s but got %s", expected, got)
100100
}
101-
db.Close()
102101
}
103102

104103
func TestCharset(t *testing.T) {
@@ -111,6 +110,16 @@ func TestCharset(t *testing.T) {
111110
mustSetCharset(t, "charset=ascii", "ascii")
112111
}
113112

113+
func TestFailingCharset(t *testing.T) {
114+
db, err := sql.Open("mysql", strings.Replace(dsn, charset, "charset=none", 1))
115+
// run query to really establish connection...
116+
_, err = db.Exec("SELECT 1")
117+
if err == nil {
118+
db.Close()
119+
t.Fatalf("Connection must not succeed without a valid charset")
120+
}
121+
}
122+
114123
func TestFallbackCharset(t *testing.T) {
115124
if !getEnv() {
116125
t.Logf("MySQL-Server not running on %s. Skipping TestFallbackCharset", netAddr)

0 commit comments

Comments
 (0)