aboutsummaryrefslogtreecommitdiff
path: root/db/db.go
blob: 24c2a31e219d10174de8923941f207f4496cb93c (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package db

import (
	"context"
	"database/sql"
	"fmt"
	"os"

	"github.com/mattn/go-sqlite3"
)

var LibPath = "ext/libsqlite3ext"

type (
	DB struct {
		db     *sql.DB
		source string // for backup
	}

	Word struct {
		Word string
		Line string
	}
)

func Open(filename string) (*DB, error) {
	sql.Register("sqlite3_with_extensions", &sqlite3.SQLiteDriver{
		ConnectHook: func(conn *sqlite3.SQLiteConn) error {
			return conn.LoadExtension(LibPath, "sqlite3_spellfix_init")
		},
	})

	db, err := sql.Open("sqlite3_with_extensions", filename)
	if err != nil {
		return nil, err
	}

	return &DB{
		db:     db,
		source: filename,
	}, nil
}

func (d *DB) Migrate(ctx context.Context) error {
	_, err := d.db.ExecContext(
		ctx,
		`CREATE VIRTUAL TABLE IF NOT EXISTS words USING fts5 (word, line);
		CREATE VIRTUAL TABLE IF NOT EXISTS words_vocab USING fts5vocab('words', 'row');
		CREATE VIRTUAL TABLE IF NOT EXISTS spell USING spellfix1;`,
	)
	return err
}

func (d *DB) SelectDict(ctx context.Context, query string, limit int) ([]*Word, error) {
	rows, err := d.db.QueryContext(
		ctx,
		`SELECT
			word, line
		FROM words
		WHERE words MATCH ?
		ORDER BY rank
		LIMIT ?;`,
		query, limit,
	)
	if err != nil {
		return nil, err
	}

	words := make([]*Word, 0)
	for rows.Next() {
		w := Word{}
		err := rows.Scan(&w.Word, &w.Line)
		if err != nil {
			return nil, err
		}
		words = append(words, &w)
	}

	return words, err

}

func (d *DB) SelectSpell(ctx context.Context, query string) ([]string, error) {
	rows, err := d.db.QueryContext(
		ctx,
		`SELECT
			word
		FROM spell
		WHERE word MATCH ?;`,
		query,
	)
	if err != nil {
		return nil, err
	}

	words := make([]string, 0)
	for rows.Next() {
		w := ""
		err := rows.Scan(&w)
		if err != nil {
			return nil, err
		}
		words = append(words, w)
	}

	return words, nil

}

func (d *DB) InsertLine(ctx context.Context, word, line string) error {
	_, err := d.db.ExecContext(
		ctx,
		`INSERT INTO words (WORD, LINE) VALUES(?, ?);`,
		word, line,
	)
	if err != nil {
		return err
	}
	return err
}

func (d *DB) Consolidade(ctx context.Context) error {
	_, err := d.db.ExecContext(
		ctx,
		`INSERT INTO spell(word) SELECT term FROM words_vocab`,
	)
	if err != nil {
		return err
	}
	return err
}

func (d *DB) Backup(ctx context.Context, name string) error {
	destDb, err := sql.Open("sqlite3_with_extensions", name)
	if err != nil {
		return err
	}
	defer destDb.Close()

	return Copy(ctx, d.db, destDb)
}

func (d *DB) Restore(ctx context.Context, name string) error {
	if _, err := os.Stat(name); err != nil {
		return err
	}

	srcDb, err := sql.Open("sqlite3_with_extensions", name)
	if err != nil {
		return err
	}
	defer srcDb.Close()

	return Copy(ctx, srcDb, d.db)
}

func Copy(ctx context.Context, srcDb *sql.DB, destDb *sql.DB) error {
	destConn, err := destDb.Conn(ctx)
	if err != nil {
		return err
	}
	defer destConn.Close()

	srcConn, err := srcDb.Conn(ctx)
	if err != nil {
		return err
	}
	defer srcConn.Close()

	return destConn.Raw(func(destConn interface{}) error {
		return srcConn.Raw(func(srcConn interface{}) error {
			destSQLiteConn, ok := destConn.(*sqlite3.SQLiteConn)
			if !ok {
				return fmt.Errorf("can't convert destination connection to SQLiteConn")
			}

			srcSQLiteConn, ok := srcConn.(*sqlite3.SQLiteConn)
			if !ok {
				return fmt.Errorf("can't convert source connection to SQLiteConn")
			}

			b, err := destSQLiteConn.Backup("main", srcSQLiteConn, "main")
			if err != nil {
				return fmt.Errorf("error initializing SQLite backup: %w", err)
			}

			done, err := b.Step(-1)
			if !done {
				return fmt.Errorf("step of -1, but not done")
			}
			if err != nil {
				return fmt.Errorf("error in stepping backup: %w", err)
			}

			err = b.Finish()
			if err != nil {
				return fmt.Errorf("error finishing backup: %w", err)
			}

			return err
		})
	})

}