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
|
package storage
import (
"fmt"
)
type MySQLAdapter struct{}
func (m MySQLAdapter) ConvertGenericPlaceholders(query string, argsCount int) string {
// MySQL uses ? as its query argument placeholder, which we're using
// as our generic placeholder so no transformation is necessary.
return query
}
func (m MySQLAdapter) TableExists(
executor sqlExecutor,
tableName string,
) (bool, error) {
// Note: MySQL doesn't have schemas. So the "table_schema"
// should be the currently selected database.
databaseName, err := m.DatabaseName(executor)
if err != nil {
return false, fmt.Errorf("unable to retrieve database name: %w", err)
}
var exists bool
err = executor.QueryRow(`
SELECT EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = ?
AND TABLE_NAME = ?
);
`, databaseName, tableName).Scan(&exists)
if err != nil {
return false, fmt.Errorf(
"unable to check if %s exists: %w",
tableName,
err,
)
}
return exists, nil
}
func (m MySQLAdapter) DatabaseName(executor sqlExecutor) (string, error) {
var name string
err := executor.QueryRow("SELECT DATABASE();").Scan(&name)
if err != nil {
return "", fmt.Errorf("unable to retrieve database name: %w", err)
}
// MySQL allows connecting without a database selected.
if name == "" {
return "", fmt.Errorf("no database is currently selected")
}
return name, nil
}
|