-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialect_sqlite3.go
53 lines (42 loc) · 1.82 KB
/
dialect_sqlite3.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
package rockhopper
import (
"database/sql"
"fmt"
)
// Sqlite3Dialect struct.
type Sqlite3Dialect struct{}
func (m Sqlite3Dialect) getTableNamesSQL() string {
return `SELECT name FROM sqlite_master WHERE type='table'`
}
func (m Sqlite3Dialect) createVersionTableSQL(tableName string) string {
return fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
id INTEGER PRIMARY KEY AUTOINCREMENT,
package TEXT NOT NULL DEFAULT 'main',
source_file VARCHAR(255) NOT NULL DEFAULT '',
version_id INTEGER NOT NULL,
is_applied INTEGER NOT NULL,
tstamp TIMESTAMP DEFAULT (datetime('now'))
);`, tableName)
}
func (m Sqlite3Dialect) insertVersionSQL(tableName string) string {
return fmt.Sprintf("INSERT INTO %s (package, source_file, version_id, is_applied) VALUES (?, ?, ?, ?)", tableName)
}
func (m Sqlite3Dialect) selectLastVersionSQL(tableName string) string {
return fmt.Sprintf("SELECT MAX(version_id) FROM %s WHERE package = ?", tableName)
}
func (m Sqlite3Dialect) queryVersionsSQL(tableName string) string {
return fmt.Sprintf("SELECT package, version_id, is_applied, tstamp FROM %s WHERE package = ? ORDER BY id DESC", tableName)
}
func (m Sqlite3Dialect) dbVersionQuery(db *sql.DB, tableName string) (*sql.Rows, error) {
rows, err := db.Query(fmt.Sprintf("SELECT package, version_id, is_applied from %s ORDER BY id DESC", tableName))
if err != nil {
return nil, err
}
return rows, err
}
func (m Sqlite3Dialect) migrationSQL(tableName string) string {
return fmt.Sprintf("SELECT tstamp, is_applied FROM %s WHERE package = ? AND version_id = ? ORDER BY tstamp DESC LIMIT 1", tableName)
}
func (m Sqlite3Dialect) deleteVersionSQL(tableName string) string {
return fmt.Sprintf("DELETE FROM %s WHERE package = ? AND version_id = ?", tableName)
}