SQLite FTS example (#20)
* SQLite FTS example * refactor: remove via cfg chaining --------- Co-authored-by: Joao Goncalves <joao.goncalves01@gmail.com>
This commit is contained in:
9
internal/examples/shakespeare/README.md
Normal file
9
internal/examples/shakespeare/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
## Shake.db
|
||||
|
||||
This DB was constructed following this excellent talk: https://www.youtube.com/watch?v=RqubKSF3wig
|
||||
|
||||
## Running
|
||||
|
||||
It's important to pass the fts5 build tag, or you'll get `no such module: fts5`.
|
||||
|
||||
Run with: `go run -tags fts5 main.go`
|
||||
109
internal/examples/shakespeare/main.go
Normal file
109
internal/examples/shakespeare/main.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/go-via/via"
|
||||
"github.com/go-via/via/h"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type DataSource interface {
|
||||
Open()
|
||||
Query(str string) (*sql.Rows, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type ShakeDB struct {
|
||||
db *sql.DB
|
||||
findByTextStmt *sql.Stmt
|
||||
}
|
||||
|
||||
func (shakeDB *ShakeDB) Prepare() {
|
||||
db, err := sql.Open("sqlite3", "shake.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
stmt, err := db.Prepare(`select play,player,plays.text
|
||||
from playsearch inner join plays on playsearch.playsrowid=plays.rowid where playsearch.text match ?
|
||||
order by plays.play, plays.player limit 200;`)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
shakeDB.db = db
|
||||
shakeDB.findByTextStmt = stmt
|
||||
}
|
||||
|
||||
func (shakeDB *ShakeDB) Query(str string) (*sql.Rows, error) {
|
||||
return shakeDB.findByTextStmt.Query(str)
|
||||
}
|
||||
|
||||
func (shakeDB *ShakeDB) Close() {
|
||||
if shakeDB.db != nil {
|
||||
shakeDB.db.Close()
|
||||
shakeDB.db = nil
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
v := via.New()
|
||||
|
||||
v.Config(via.Options{
|
||||
DevMode: true,
|
||||
DocumentTitle: "Search",
|
||||
LogLvl: via.LogLevelWarn,
|
||||
})
|
||||
|
||||
v.AppendToHead(
|
||||
h.Link(h.Rel("stylesheet"), h.Href("https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css")),
|
||||
h.StyleEl(h.Raw(".no-wrap { white-space: nowrap; }")),
|
||||
)
|
||||
shakeDB := &ShakeDB{}
|
||||
shakeDB.Prepare()
|
||||
defer shakeDB.Close()
|
||||
|
||||
v.Page("/", func(c *via.Context) {
|
||||
query := c.Signal("whether tis")
|
||||
var rowsTable H
|
||||
runQuery := func() {
|
||||
qry := query.String()
|
||||
start := time.Now()
|
||||
rows, error := shakeDB.Query(qry)
|
||||
fmt.Println("query ", qry, "took", time.Since(start))
|
||||
if error != nil {
|
||||
rowsTable = h.Div(h.Text("Error: " + error.Error()))
|
||||
} else {
|
||||
table, err := RenderTable(rows, []string{"no-wrap", "no-wrap", ""})
|
||||
if err != nil {
|
||||
rowsTable = h.Div(h.Text("Error: " + err.Error()))
|
||||
} else {
|
||||
rowsTable = table
|
||||
}
|
||||
}
|
||||
}
|
||||
runQueryAction := c.Action(func() {
|
||||
runQuery()
|
||||
c.Sync()
|
||||
})
|
||||
runQuery()
|
||||
c.View(func() h.H {
|
||||
return h.Div(
|
||||
h.H2(h.Text("Search")), h.FieldSet(
|
||||
h.Attr("role", "group"),
|
||||
h.Input(
|
||||
h.Type("text"),
|
||||
query.Bind(),
|
||||
h.Attr("autofocus"),
|
||||
runQueryAction.OnKeyDown("Enter"),
|
||||
),
|
||||
h.Button(h.Text("Search"), runQueryAction.OnClick())),
|
||||
rowsTable,
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
v.Start()
|
||||
}
|
||||
77
internal/examples/shakespeare/rowsToHTML.go
Normal file
77
internal/examples/shakespeare/rowsToHTML.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-via/via/h"
|
||||
)
|
||||
|
||||
type H = h.H
|
||||
|
||||
func valueToString(v any) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
if b, ok := v.([]byte); ok {
|
||||
return string(b)
|
||||
}
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
|
||||
// RenderTable takes sql.Rows and an array of CSS class names for each column.
|
||||
// Returns a complete HTML table as a gomponent.
|
||||
func RenderTable(rows *sql.Rows, columnClasses []string) (H, error) {
|
||||
cols, err := rows.Columns()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
headerCells := make([]h.H, len(cols))
|
||||
for i, col := range cols {
|
||||
headerCells[i] = h.Th(h.Attr("scope", "col"), h.Text(col))
|
||||
}
|
||||
thead := h.THead(h.Tr(headerCells...))
|
||||
|
||||
var bodyRows []h.H
|
||||
for rows.Next() {
|
||||
values := make([]any, len(cols))
|
||||
scanArgs := make([]any, len(cols))
|
||||
for i := range values {
|
||||
scanArgs[i] = &values[i]
|
||||
}
|
||||
|
||||
if err := rows.Scan(scanArgs...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cells := make([]h.H, len(values))
|
||||
if len(values) > 0 {
|
||||
var thAttrs []h.H
|
||||
thAttrs = append(thAttrs, h.Attr("scope", "row"))
|
||||
if len(columnClasses) > 0 && columnClasses[0] != "" {
|
||||
thAttrs = append(thAttrs, h.Class(columnClasses[0]))
|
||||
}
|
||||
thAttrs = append(thAttrs, h.Text(valueToString(values[0])))
|
||||
cells[0] = h.Th(thAttrs...)
|
||||
|
||||
for i := 1; i < len(values); i++ {
|
||||
var tdAttrs []h.H
|
||||
if i < len(columnClasses) && columnClasses[i] != "" {
|
||||
tdAttrs = append(tdAttrs, h.Class(columnClasses[i]))
|
||||
}
|
||||
tdAttrs = append(tdAttrs, h.Text(valueToString(values[i])))
|
||||
cells[i] = h.Td(tdAttrs...)
|
||||
}
|
||||
}
|
||||
|
||||
bodyRows = append(bodyRows, h.Tr(cells...))
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tbody := h.TBody(bodyRows...)
|
||||
return h.Table(thead, tbody), nil
|
||||
}
|
||||
197
internal/examples/shakespeare/rowsToHTML_test.go
Normal file
197
internal/examples/shakespeare/rowsToHTML_test.go
Normal file
@@ -0,0 +1,197 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRenderTable(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
assert.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
columns := []string{"play", "player", "text"}
|
||||
mock.ExpectQuery("SELECT").
|
||||
WillReturnRows(
|
||||
sqlmock.NewRows(columns).
|
||||
AddRow("Hamlet", "Hamlet", "To be or not to be").
|
||||
AddRow("Macbeth", "Macbeth", "Out, out brief candle!").
|
||||
AddRow("Romeo and Juliet", "Juliet", "O Romeo, Romeo!"),
|
||||
)
|
||||
|
||||
rows, err := db.Query("SELECT play, player, text FROM plays")
|
||||
assert.NoError(t, err)
|
||||
defer rows.Close()
|
||||
|
||||
table, err := RenderTable(rows, []string{"no-wrap", "no-wrap", ""})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, table)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = table.Render(&buf)
|
||||
assert.NoError(t, err)
|
||||
|
||||
html := buf.String()
|
||||
|
||||
assert.Contains(t, html, "<table>")
|
||||
assert.Contains(t, html, "</table>")
|
||||
assert.Contains(t, html, "<thead>")
|
||||
assert.Contains(t, html, "</thead>")
|
||||
assert.Contains(t, html, "<tbody>")
|
||||
assert.Contains(t, html, "</tbody>")
|
||||
|
||||
assert.Contains(t, html, `<th scope="col">play</th>`)
|
||||
assert.Contains(t, html, `<th scope="col">player</th>`)
|
||||
assert.Contains(t, html, `<th scope="col">text</th>`)
|
||||
|
||||
assert.Contains(t, html, `<th scope="row" class="no-wrap">Hamlet</th>`)
|
||||
assert.Contains(t, html, `<th scope="row" class="no-wrap">Macbeth</th>`)
|
||||
assert.Contains(t, html, `<th scope="row" class="no-wrap">Romeo and Juliet</th>`)
|
||||
|
||||
assert.Contains(t, html, `<td class="no-wrap">Hamlet</td>`)
|
||||
assert.Contains(t, html, `<td class="no-wrap">Macbeth</td>`)
|
||||
assert.Contains(t, html, `<td class="no-wrap">Juliet</td>`)
|
||||
|
||||
assert.Contains(t, html, "<td>To be or not to be</td>")
|
||||
assert.Contains(t, html, "<td>Out, out brief candle!</td>")
|
||||
assert.Contains(t, html, "<td>O Romeo, Romeo!</td>")
|
||||
}
|
||||
|
||||
func TestRenderTableWithNilValues(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
assert.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
columns := []string{"id", "name", "description"}
|
||||
mock.ExpectQuery("SELECT").
|
||||
WillReturnRows(
|
||||
sqlmock.NewRows(columns).
|
||||
AddRow(1, "Item 1", nil).
|
||||
AddRow(2, nil, "Description 2"),
|
||||
)
|
||||
|
||||
rows, err := db.Query("SELECT id, name, description FROM items")
|
||||
assert.NoError(t, err)
|
||||
defer rows.Close()
|
||||
|
||||
table, err := RenderTable(rows, []string{"no-wrap", "no-wrap", ""})
|
||||
assert.NoError(t, err)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = table.Render(&buf)
|
||||
assert.NoError(t, err)
|
||||
|
||||
html := buf.String()
|
||||
|
||||
assert.Contains(t, html, `<th scope="row" class="no-wrap">1</th>`)
|
||||
assert.Contains(t, html, `<td class="no-wrap">Item 1</td>`)
|
||||
assert.Contains(t, html, "<td></td>")
|
||||
|
||||
assert.Contains(t, html, `<th scope="row" class="no-wrap">2</th>`)
|
||||
}
|
||||
|
||||
func TestRenderTableWithByteValues(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
assert.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
columns := []string{"id", "data"}
|
||||
mock.ExpectQuery("SELECT").
|
||||
WillReturnRows(
|
||||
sqlmock.NewRows(columns).
|
||||
AddRow(1, []byte("binary data")),
|
||||
)
|
||||
|
||||
rows, err := db.Query("SELECT id, data FROM blobs")
|
||||
assert.NoError(t, err)
|
||||
defer rows.Close()
|
||||
|
||||
table, err := RenderTable(rows, []string{"no-wrap", "no-wrap"})
|
||||
assert.NoError(t, err)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = table.Render(&buf)
|
||||
assert.NoError(t, err)
|
||||
|
||||
html := buf.String()
|
||||
assert.Contains(t, html, `<td class="no-wrap">binary data</td>`)
|
||||
}
|
||||
|
||||
func TestRenderTableWithSpecialCharacters(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
assert.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
columns := []string{"id", "content"}
|
||||
mock.ExpectQuery("SELECT").
|
||||
WillReturnRows(
|
||||
sqlmock.NewRows(columns).
|
||||
AddRow(1, "<script>alert('XSS')</script>").
|
||||
AddRow(2, "A & B"),
|
||||
)
|
||||
|
||||
rows, err := db.Query("SELECT id, content FROM texts")
|
||||
assert.NoError(t, err)
|
||||
defer rows.Close()
|
||||
|
||||
table, err := RenderTable(rows, []string{"", ""})
|
||||
assert.NoError(t, err)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = table.Render(&buf)
|
||||
assert.NoError(t, err)
|
||||
|
||||
html := buf.String()
|
||||
|
||||
assert.Contains(t, html, "<script>")
|
||||
assert.Contains(t, html, "&")
|
||||
|
||||
assert.NotContains(t, html, "<script>alert")
|
||||
}
|
||||
|
||||
func TestRenderTableEmptyRows(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
assert.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
columns := []string{"id", "name"}
|
||||
mock.ExpectQuery("SELECT").
|
||||
WillReturnRows(sqlmock.NewRows(columns))
|
||||
|
||||
rows, err := db.Query("SELECT id, name FROM items")
|
||||
assert.NoError(t, err)
|
||||
defer rows.Close()
|
||||
|
||||
table, err := RenderTable(rows, []string{"", ""})
|
||||
assert.NoError(t, err)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = table.Render(&buf)
|
||||
assert.NoError(t, err)
|
||||
|
||||
html := buf.String()
|
||||
|
||||
assert.Contains(t, html, "<thead>")
|
||||
assert.Contains(t, html, `<th scope="col">id</th>`)
|
||||
assert.Contains(t, html, `<th scope="col">name</th>`)
|
||||
|
||||
tbodyStart := strings.Index(html, "<tbody>")
|
||||
tbodyEnd := strings.Index(html, "</tbody>")
|
||||
assert.True(t, tbodyStart < tbodyEnd)
|
||||
|
||||
tbody := html[tbodyStart+7 : tbodyEnd]
|
||||
assert.NotContains(t, tbody, "<tr>")
|
||||
}
|
||||
|
||||
func TestValueToString(t *testing.T) {
|
||||
assert.Equal(t, "", valueToString(nil))
|
||||
assert.Equal(t, "hello", valueToString([]byte("hello")))
|
||||
assert.Equal(t, "42", valueToString(42))
|
||||
assert.Equal(t, "3.14", valueToString(3.14))
|
||||
assert.Equal(t, "true", valueToString(true))
|
||||
assert.Equal(t, "test string", valueToString("test string"))
|
||||
}
|
||||
BIN
internal/examples/shakespeare/shake.db
Normal file
BIN
internal/examples/shakespeare/shake.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user