/
driver.go
76 lines (66 loc) · 1.6 KB
/
driver.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package dbmate
import (
"database/sql"
"fmt"
"io"
"net/url"
"github.com/amacneil/dbmate/v2/pkg/dbutil"
)
// Driver provides top level database functions
type Driver interface {
Open() (*sql.DB, error)
DatabaseExists() (bool, error)
CreateDatabase() error
DropDatabase() error
DumpSchema(*sql.DB) ([]byte, error)
MigrationsTableExists(*sql.DB) (bool, error)
CreateMigrationsTable(*sql.DB) error
SelectMigrations(*sql.DB, int) (map[string]bool, error)
InsertMigration(dbutil.Transaction, string) error
DeleteMigration(dbutil.Transaction, string) error
Ping() error
QueryError(string, error) error
}
// DriverConfig holds configuration passed to driver constructors
type DriverConfig struct {
DatabaseURL *url.URL
Log io.Writer
MigrationsTableName string
}
// DriverFunc represents a driver constructor
type DriverFunc func(DriverConfig) Driver
type QueryError struct {
Err error
Query string
Position int
}
func (e *QueryError) Error() string {
if e.Position > 0 {
line := 1
column := 1
offset := 0
for _, ch := range e.Query {
offset++
if offset >= e.Position {
break
}
// don't count CR as a column in CR/LF sequences
if ch == '\r' {
continue
}
if ch == '\n' {
line++
column = 1
continue
}
column++
}
return fmt.Sprintf("line: %d, column: %d, position: %d: %s", line, column, e.Position, e.Err.Error())
}
return e.Err.Error()
}
var drivers = map[string]DriverFunc{}
// RegisterDriver registers a driver constructor for a given URL scheme
func RegisterDriver(f DriverFunc, scheme string) {
drivers[scheme] = f
}