-
Notifications
You must be signed in to change notification settings - Fork 141
/
mysql_test.go
60 lines (52 loc) · 1.64 KB
/
mysql_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package odbc
import (
"database/sql"
"flag"
"fmt"
"testing"
"time"
)
var (
mysrv = flag.String("mysrv", "server", "mysql server name")
mydb = flag.String("mydb", "dbname", "mysql database name")
myuser = flag.String("myuser", "", "mysql user name")
mypass = flag.String("mypass", "", "mysql password")
)
func mysqlConnect() (db *sql.DB, stmtCount int, err error) {
// from https://dev.mysql.com/doc/connector-odbc/en/connector-odbc-configuration-connection-parameters.html
conn := fmt.Sprintf("driver=mysql;server=%s;database=%s;user=%s;password=%s;",
*mysrv, *mydb, *myuser, *mypass)
db, err = sql.Open("odbc", conn)
if err != nil {
return nil, 0, err
}
stats := db.Driver().(*Driver).Stats
return db, stats.StmtCount, nil
}
func TestMYSQLTime(t *testing.T) {
db, sc, err := mysqlConnect()
if err != nil {
t.Fatal(err)
}
defer closeDB(t, db, sc, sc)
db.Exec("drop table temp")
exec(t, db, "create table temp(id int not null auto_increment primary key, time time)")
now := time.Now()
// SQL_TIME_STRUCT only supports hours, minutes and seconds
now = time.Date(1, time.January, 1, now.Hour(), now.Minute(), now.Second(), 0, time.Local)
_, err = db.Exec("insert into temp (time) values(?)", now)
if err != nil {
t.Fatal(err)
}
var ret time.Time
if err := db.QueryRow("select time from temp where id = ?", 1).Scan(&ret); err != nil {
t.Fatal(err)
}
if ret != now {
t.Fatalf("unexpected return value: want=%v, is=%v", now, ret)
}
exec(t, db, "drop table temp")
}