@@ -3,8 +3,9 @@ package adapters
3
3
import (
4
4
"context"
5
5
"database/sql"
6
+ "fmt"
6
7
7
- // SQL driver for mysql
8
+ // load SQL driver for mysql
8
9
_ "github.com/go-sql-driver/mysql"
9
10
)
10
11
@@ -13,40 +14,62 @@ type mysqlAdapter struct {
13
14
}
14
15
15
16
func (adapter mysqlAdapter ) HasDatabase (ctx context.Context , database string ) (bool , error ) {
16
- return false , nil
17
+ var count int
18
+ query := "SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=?"
19
+ err := adapter .db .QueryRowContext (ctx , query , database ).Scan (& count )
20
+ return count == 1 , err
17
21
}
18
22
19
- func (adapter mysqlAdapter ) CreateDatabase (ctx context.Context , name string ) error {
20
- _ , err := adapter .db .Exec ("CREATE DATABASE IF NOT EXISTS $1;" , name )
23
+ func (adapter mysqlAdapter ) CreateDatabase (ctx context.Context , database string ) error {
24
+ query := fmt .Sprintf ("CREATE DATABASE %s;" , database )
25
+ _ , err := adapter .db .ExecContext (ctx , query )
21
26
return err
22
27
}
23
28
24
- func (adapter mysqlAdapter ) DeleteDatabase (ctx context.Context , name string ) error {
25
- _ , err := adapter .db .Exec ("DROP DATABASE IF EXISTS $1;" , name )
29
+ func (adapter mysqlAdapter ) DeleteDatabase (ctx context.Context , database string ) error {
30
+ query := fmt .Sprintf ("DROP DATABASE %s;" , database )
31
+ _ , err := adapter .db .ExecContext (ctx , query )
26
32
return err
27
33
}
28
34
29
- func (adapter mysqlAdapter ) HasDatabaseUserWithAccess (ctx context.Context , username string , database string ) (bool , error ) {
30
- // TODO implement
31
- return false , nil
35
+ func (adapter mysqlAdapter ) HasDatabaseUserWithAccess (ctx context.Context , database string , username string ) (bool , error ) {
36
+ var count int
37
+ query := "SELECT COUNT(*) FROM mysql.db WHERE Db=? AND USER=?"
38
+ err := adapter .db .QueryRowContext (ctx , query , database , username ).Scan (& count )
39
+ return count == 1 , err
32
40
}
33
41
34
- func (adapter mysqlAdapter ) CreateDatabaseUser (ctx context.Context , username string , password string , database string ) error {
35
- // TODO implement
36
- return nil
42
+ func (adapter mysqlAdapter ) CreateDatabaseUser (ctx context.Context , database string , username string , password string ) error {
43
+ // make password sql safe
44
+ quotedPassword := QuoteLiteral (password )
45
+ query := fmt .Sprintf ("CREATE USER '%s'@'%%' IDENTIFIED BY %s;" , username , quotedPassword )
46
+ _ , err := adapter .db .ExecContext (ctx , query )
47
+ if err != nil {
48
+ return err
49
+ }
50
+
51
+ query = fmt .Sprintf ("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%%';" , database , username )
52
+ _ , err = adapter .db .ExecContext (ctx , query )
53
+ if err != nil {
54
+ return err
55
+ }
56
+
57
+ _ , err = adapter .db .ExecContext (ctx , "FLUSH PRIVILEGES;" )
58
+ return err
37
59
}
38
60
39
61
func (adapter mysqlAdapter ) DeleteDatabaseUser (ctx context.Context , database string , username string ) error {
40
- // TODO implement
41
- return nil
62
+ query := fmt .Sprintf ("DROP USER %s;" , username )
63
+ _ , err := adapter .db .ExecContext (ctx , query )
64
+ return err
42
65
}
43
66
44
67
func (adapter mysqlAdapter ) Close (ctx context.Context ) error {
45
68
return adapter .db .Close ()
46
69
}
47
70
48
- func GetMysqlConnection (ctx context.Context , host string , adminUsername string , adminPassword string ) (* mysqlAdapter , error ) {
49
- db , err := sql .Open ("mysql" , adminUsername + ":" + adminPassword + "@" + host )
71
+ func GetMysqlConnection (ctx context.Context , dsn string ) (* mysqlAdapter , error ) {
72
+ db , err := sql .Open ("mysql" , dsn )
50
73
if err != nil {
51
74
return nil , err
52
75
}
0 commit comments