mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-12 17:27:50 +08:00
Ditch gorp dependency, use plain database/sql
This commit is contained in:
parent
5fc47d8099
commit
ab4c961cf2
52
store/dir.go
52
store/dir.go
|
@ -1,52 +1,54 @@
|
|||
package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/coopernurse/gorp"
|
||||
)
|
||||
|
||||
type Dir struct {
|
||||
Path string
|
||||
Score float64
|
||||
}
|
||||
|
||||
const (
|
||||
InitScore = 10
|
||||
ScoreIncrement = 10
|
||||
)
|
||||
|
||||
func init() {
|
||||
tableAdders = append(tableAdders, func(dm *gorp.DbMap) {
|
||||
t := dm.AddTable(Dir{}).SetKeys(false, "Path")
|
||||
t.ColMap("Path").SetUnique(true)
|
||||
})
|
||||
createTable["dir"] = `create table if not exists dir (path text unique primary key, score real default 0)`
|
||||
}
|
||||
|
||||
func (s *Store) AddDir(d string) error {
|
||||
tx, err := s.dm.Begin()
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Commit()
|
||||
|
||||
dir := Dir{}
|
||||
err = tx.SelectOne(&dir, "select * from Dir where Path=?", d)
|
||||
if err == sql.ErrNoRows {
|
||||
dir = Dir{Path: d, Score: InitScore}
|
||||
return tx.Insert(&dir)
|
||||
} else {
|
||||
dir.Score += ScoreIncrement
|
||||
_, err = tx.Update(&dir)
|
||||
// Insert when the path does not already exist
|
||||
_, err = tx.Exec("insert or ignore into dir (path) values(?)", d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Increment score
|
||||
_, err = tx.Exec("update dir set score = score + ? where path = ?", ScoreIncrement, d)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) FindDirs(p string) ([]Dir, error) {
|
||||
var dirs []Dir
|
||||
_, err := s.dm.Select(
|
||||
&dirs,
|
||||
"select * from Dir where Path glob ? order by score desc",
|
||||
rows, err := s.db.Query(
|
||||
"select path, score from dir where path glob ? order by score desc",
|
||||
"*"+EscapeGlob(p)+"*")
|
||||
return dirs, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
dir Dir
|
||||
dirs []Dir
|
||||
)
|
||||
|
||||
for rows.Next() {
|
||||
rows.Scan(&dir.Path, &dir.Score)
|
||||
dirs = append(dirs, dir)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dirs, nil
|
||||
}
|
||||
|
|
|
@ -2,17 +2,17 @@ package store
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/coopernurse/gorp"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
dm *gorp.DbMap
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
var tableAdders []func(*gorp.DbMap)
|
||||
var createTable = map[string]string{}
|
||||
|
||||
// DefaultDB returns the default database for storage.
|
||||
func DefaultDB() (*sql.DB, error) {
|
||||
|
@ -37,13 +37,14 @@ func NewStore() (*Store, error) {
|
|||
// NewStoreDB creates a new Store with a custom database. The database must be
|
||||
// a SQLite database.
|
||||
func NewStoreDB(db *sql.DB) (*Store, error) {
|
||||
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
|
||||
for _, ta := range tableAdders {
|
||||
ta(dbmap)
|
||||
st := &Store{db}
|
||||
|
||||
for t, q := range createTable {
|
||||
_, err := db.Exec(q)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create table %s: %v", t, q)
|
||||
}
|
||||
}
|
||||
err := dbmap.CreateTablesIfNotExists()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{dbmap}, nil
|
||||
|
||||
return st, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user