1package dialectquery
2
3import "strings"
4
5// Querier is the interface that wraps the basic methods to create a dialect specific query.
6type Querier interface {
7 // CreateTable returns the SQL query string to create the db version table.
8 CreateTable(tableName string) string
9
10 // InsertVersion returns the SQL query string to insert a new version into the db version table.
11 InsertVersion(tableName string) string
12
13 // DeleteVersion returns the SQL query string to delete a version from the db version table.
14 DeleteVersion(tableName string) string
15
16 // GetMigrationByVersion returns the SQL query string to get a single migration by version.
17 //
18 // The query should return the timestamp and is_applied columns.
19 GetMigrationByVersion(tableName string) string
20
21 // ListMigrations returns the SQL query string to list all migrations in descending order by id.
22 //
23 // The query should return the version_id and is_applied columns.
24 ListMigrations(tableName string) string
25
26 // GetLatestVersion returns the SQL query string to get the last version_id from the db version
27 // table. Returns a nullable int64 value.
28 GetLatestVersion(tableName string) string
29}
30
31var _ Querier = (*QueryController)(nil)
32
33type QueryController struct{ Querier }
34
35// NewQueryController returns a new QueryController that wraps the given Querier.
36func NewQueryController(querier Querier) *QueryController {
37 return &QueryController{Querier: querier}
38}
39
40// Optional methods
41
42// TableExists returns the SQL query string to check if the version table exists. If the Querier
43// does not implement this method, it will return an empty string.
44//
45// Returns a boolean value.
46func (c *QueryController) TableExists(tableName string) string {
47 if t, ok := c.Querier.(interface{ TableExists(string) string }); ok {
48 return t.TableExists(tableName)
49 }
50 return ""
51}
52
53func parseTableIdentifier(name string) (schema, table string) {
54 schema, table, found := strings.Cut(name, ".")
55 if !found {
56 return "", name
57 }
58 return schema, table
59}