Skip to content

Commit

Permalink
Changed 'respository sqlite func search' to handle fts match query
Browse files Browse the repository at this point in the history
  • Loading branch information
grrlopes committed Aug 13, 2023
1 parent 01deeb5 commit 168ef2c
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions repositories/sqlite/sqliterepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func NewGormRepostory() repositories.ISqliteRepository {
err = db.AutoMigrate(&entity.Commands{})
if err != nil {
log.Fatal("not able to migrate", err)
}
}

return &SQLiteRepository{
database: db,
}
}

func (sql *SQLiteRepository) Migrate() error {
_, err := sql.db.Exec(table)
err := sql.database.Exec(table).Error
return err
}

Expand Down Expand Up @@ -91,9 +91,14 @@ func (sql *SQLiteRepository) Search(filter string, limit int, skip int) ([]entit
var (
count int
commands []entity.Commands
result *gorm.DB
)

result := sql.database.Limit(limit).Offset(skip).Where("cmd LIKE ?", "%"+filter+"%").Find(&commands)
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)
}
if result.Error != nil {
return commands, count, result.Error
}
Expand All @@ -103,11 +108,18 @@ func (sql *SQLiteRepository) Search(filter string, limit int, skip int) ([]entit

func (sql *SQLiteRepository) SearchCount(filter string) (int, error) {
var (
count int64
command entity.Commands
count int64
countResult int
command []entity.Commands
err error
)

sql.database.Model(&command).Where("cmd LIKE ?", "%"+filter+"%").Count(&count)

return int(count), nil
if filter == "*" {
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
countResult = len(command)
}
return countResult, err
}

0 comments on commit 168ef2c

Please sign in to comment.