1package dialectquery
2
3import "fmt"
4
5type Sqlite3 struct{}
6
7var _ Querier = (*Sqlite3)(nil)
8
9func (s *Sqlite3) CreateTable(tableName string) string {
10 q := `CREATE TABLE %s (
11 id INTEGER PRIMARY KEY AUTOINCREMENT,
12 version_id INTEGER NOT NULL,
13 is_applied INTEGER NOT NULL,
14 tstamp TIMESTAMP DEFAULT (datetime('now'))
15 )`
16 return fmt.Sprintf(q, tableName)
17}
18
19func (s *Sqlite3) InsertVersion(tableName string) string {
20 q := `INSERT INTO %s (version_id, is_applied) VALUES (?, ?)`
21 return fmt.Sprintf(q, tableName)
22}
23
24func (s *Sqlite3) DeleteVersion(tableName string) string {
25 q := `DELETE FROM %s WHERE version_id=?`
26 return fmt.Sprintf(q, tableName)
27}
28
29func (s *Sqlite3) GetMigrationByVersion(tableName string) string {
30 q := `SELECT tstamp, is_applied FROM %s WHERE version_id=? ORDER BY tstamp DESC LIMIT 1`
31 return fmt.Sprintf(q, tableName)
32}
33
34func (s *Sqlite3) ListMigrations(tableName string) string {
35 q := `SELECT version_id, is_applied from %s ORDER BY id DESC`
36 return fmt.Sprintf(q, tableName)
37}
38
39func (s *Sqlite3) GetLatestVersion(tableName string) string {
40 q := `SELECT MAX(version_id) FROM %s`
41 return fmt.Sprintf(q, tableName)
42}