summary refs log tree commit diff stats
path: root/search/anagrams.go
blob: e04dfd7649c88c6639685bc114ae51b4affb1453 (plain) (blame)
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
package search

import "tildegit.org/andinus/grus/storage"

// Anagrams will search for unjumbled words in database, given sorted
// word along with all the anagrams.
func Anagrams(sorted string, db *storage.DB) (anagrams []string, err error) {
	db.Mu.RLock()
	defer db.Mu.RUnlock()

	stmt, err := db.Conn.Prepare("SELECT word FROM words WHERE sorted = ?")
	if err != nil {
		return
	}
	defer stmt.Close()

	rows, err := stmt.Query(sorted)
	if err != nil {
		return
	}
	defer rows.Close()
	for rows.Next() {
		var word string
		err = rows.Scan(&word)
		if err != nil {
			return
		}
		anagrams = append(anagrams, word)
	}
	err = rows.Err()
	if err != nil {
		return
	}
	return
}