You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.3 KiB
76 lines
1.3 KiB
8 years ago
|
package testfixtures
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
4 years ago
|
type sqlite struct {
|
||
8 years ago
|
baseHelper
|
||
|
}
|
||
|
|
||
4 years ago
|
func (*sqlite) paramType() int {
|
||
8 years ago
|
return paramTypeQuestion
|
||
|
}
|
||
|
|
||
4 years ago
|
func (*sqlite) databaseName(q queryable) (string, error) {
|
||
8 years ago
|
var seq int
|
||
6 years ago
|
var main, dbName string
|
||
|
err := q.QueryRow("PRAGMA database_list").Scan(&seq, &main, &dbName)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
8 years ago
|
dbName = filepath.Base(dbName)
|
||
6 years ago
|
return dbName, nil
|
||
8 years ago
|
}
|
||
|
|
||
4 years ago
|
func (*sqlite) tableNames(q queryable) ([]string, error) {
|
||
6 years ago
|
query := `
|
||
|
SELECT name
|
||
|
FROM sqlite_master
|
||
|
WHERE type = 'table';
|
||
|
`
|
||
|
rows, err := q.Query(query)
|
||
8 years ago
|
if err != nil {
|
||
6 years ago
|
return nil, err
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
|
var tables []string
|
||
|
for rows.Next() {
|
||
|
var table string
|
||
|
if err = rows.Scan(&table); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
tables = append(tables, table)
|
||
|
}
|
||
|
if err = rows.Err(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return tables, nil
|
||
|
}
|
||
|
|
||
4 years ago
|
func (*sqlite) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
|
||
6 years ago
|
defer func() {
|
||
|
if _, err2 := db.Exec("PRAGMA defer_foreign_keys = OFF"); err2 != nil && err == nil {
|
||
|
err = err2
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
if _, err = db.Exec("PRAGMA defer_foreign_keys = ON"); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
6 years ago
|
tx, err := db.Begin()
|
||
|
if err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
6 years ago
|
defer tx.Rollback()
|
||
8 years ago
|
|
||
|
if err = loadFn(tx); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return tx.Commit()
|
||
|
}
|