generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
productmysql.go
62 lines (54 loc) · 1.45 KB
/
productmysql.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
61
62
package sqltest
import (
"context"
"database/sql"
)
// MySQLProtocol is an interface for drivers that use the MySQL wire protocol.
type MySQLProtocol interface {
DataSourceForMySQL(
user, pass,
host, port,
database string,
) (DataSource, error)
}
// MySQLCompatibleProduct is a Product that is compatible with MySQL.
type MySQLCompatibleProduct struct {
ProductName string
DefaultPort string
}
// Name returns the human-readable name of the product.
func (p MySQLCompatibleProduct) Name() string {
return p.ProductName
}
// IsCompatibleWith return true if the product is compatible with d.
func (p MySQLCompatibleProduct) IsCompatibleWith(d Driver) bool {
_, ok := d.(MySQLProtocol)
return ok
}
// DefaultDataSource returns the default data source to use to connect to the
// product.
func (p MySQLCompatibleProduct) DefaultDataSource(d Driver) (DataSource, error) {
return d.(MySQLProtocol).DataSourceForMySQL(
"root", "rootpass",
"127.0.0.1", p.DefaultPort,
"mysql",
)
}
// CreateDatabase creates a new database with the given name.
func (MySQLCompatibleProduct) CreateDatabase(
ctx context.Context,
db *sql.DB,
name string,
) error {
_, err := db.ExecContext(ctx, "CREATE DATABASE `"+name+"`")
return err
}
// DropDatabase drops the database with the given name.
func (MySQLCompatibleProduct) DropDatabase(
ctx context.Context,
db *sql.DB,
name string,
) error {
_, err := db.ExecContext(ctx, "DROP DATABASE IF EXISTS `"+name+"`")
return err
}