Skip to content

Commit

Permalink
Added trigger and changed schema table
Browse files Browse the repository at this point in the history
  • Loading branch information
grrlopes committed Aug 13, 2023
1 parent 168ef2c commit e79b179
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
23 changes: 17 additions & 6 deletions repositories/sqlite/schema.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package sqlite

const table string = `
CREATE TABLE IF NOT EXISTS command(
id INTEGER PRIMARY KEY AUTOINCREMENT,
cmd TEXT NOT NULL UNIQUE,
description TEXT NOT NULL
);
`
CREATE VIRTUAL TABLE IF NOT EXISTS commands_fts USING fts5(
id,
cmd,
desc,
updated_at,
created_at,
deleted_at,
content='commands',
content_rowid='id'
);
CREATE TRIGGER IF NOT EXISTS commands_insert AFTER INSERT ON commands
BEGIN
INSERT INTO commands_fts (rowid, cmd, desc, updated_at, created_at, deleted_at)
VALUES (new.id, new.cmd, new.desc, new.updated_at, new.created_at, new.deleted_at);
END;
`
19 changes: 5 additions & 14 deletions repositories/sqlite/sqliterepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ type SQLiteRepository struct {
database *gorm.DB
}

func NewSQLiteRepository() repositories.ISqliteRepository {
db, err := OpenDB()

if err != nil {
log.Fatal(err)
}

return &SQLiteRepository{
db: db,
}
}

func NewGormRepostory() repositories.ISqliteRepository {
db, err := GormOpenDB()
if err != nil {
Expand Down Expand Up @@ -97,7 +85,10 @@ func (sql *SQLiteRepository) Search(filter string, limit int, skip int) ([]entit
if filter == "*" {
result = sql.database.Limit(limit).Offset(skip).Where("cmd LIKE ?", "%"+""+"%").Find(&commands)
} else {
result = sql.database.Raw("SELECT cmd FROM commands WHERE cmd MATCH ? ORDER BY rank", filter).Find(&commands)
result = sql.database.Raw(
"SELECT cmd FROM commands_fts WHERE cmd MATCH ? ORDER BY rank LIMIT ? OFFSET ?",
filter, limit, skip,
).Find(&commands)
}
if result.Error != nil {
return commands, count, result.Error
Expand All @@ -118,7 +109,7 @@ func (sql *SQLiteRepository) SearchCount(filter string) (int, error) {
err = sql.database.Model(&command).Where("cmd LIKE ?", "%"+""+"%").Count(&count).Error
countResult = int(count)
} else {
err = sql.database.Raw("SELECT cmd FROM commands WHERE cmd MATCH ?", filter).Find(&command).Error
err = sql.database.Raw("SELECT cmd FROM commands_fts WHERE cmd MATCH ?", filter).Find(&command).Error
countResult = len(command)
}
return countResult, err
Expand Down
3 changes: 1 addition & 2 deletions ui/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

var (
repository repositories.ISqliteRepository = sqlite.NewSQLiteRepository()
repositoryGorm repositories.ISqliteRepository = sqlite.NewGormRepostory()
frepository repositories.IFileParsedRepository = fileparse.NewFparsedRepository()
usecasePager pager.InputBoundary = pager.NewPager(repositoryGorm)
Expand Down Expand Up @@ -173,7 +172,7 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
m.home.End = end
}

m.home.Viewport.Update(msg)
m.home.Viewport.Update(msg)
m.home.Viewport.SetContent(m.GetDataView())
return &m, tea.Batch(cmds...)
}
Expand Down

0 comments on commit e79b179

Please sign in to comment.