~welt/murse

murse/sql.go -rw-r--r-- 945 bytes
db85e43dwelt guiprep: adjust logging 4 months ago
                                                                                
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
package main

import (
	"database/sql"
	"log"

	_ "modernc.org/sqlite"
)

// Returns the latest revision number from a given SQLite connection
func latestRevisionFromSQL(db *sql.DB) (int, error) {
	rw, err := db.Query("SELECT revision FROM files WHERE revision = (SELECT MAX(revision) FROM files);")
	if err != nil {
		log.Fatal(err)
	}
	defer rw.Close()
	var rev int
	_ = rw.Next()
	err = rw.Scan(&rev)
	if err != nil {
		return 0, err
	}
	return rev, nil
}

// Returns a map of a file name and its hashes from a given revision.
func filesFromSQLite(db *sql.DB, rev int) (map[string]string, error) {
	rws, err := db.Query("SELECT path, checksum FROM files WHERE revision = ?;", rev)
	if err != nil {
		log.Fatal(err)
	}
	defer rws.Close()

	m := make(map[string]string)

	var path string
	var checksum string
	for rws.Next() {
		err := rws.Scan(&path, &checksum)
		if err != nil {
			log.Fatal(err)
		}
		m[path] = checksum
	}
	return m, nil
}