Skip to content

Commit 2543f67

Browse files
committed
Updated go-sqlite Next function
1 parent c33cb23 commit 2543f67

19 files changed

+115
-129
lines changed

pkg/indexer/indexer.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ func (i *Indexer) IsIndexing() bool {
173173
// Walk will initiate a walk of the index, and block until context is
174174
// cancelled or walk is started
175175
func (i *Indexer) Walk(ctx context.Context, fn WalkFunc) error {
176-
if fn == nil {
177-
return ErrBadParameter.With("WalkFunc")
178-
}
179176
select {
180177
case <-ctx.Done():
181178
return ctx.Err()

pkg/indexer/indexer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const (
1414
)
1515

1616
func Test_Indexer_000(t *testing.T) {
17-
indexer, err := NewIndexer("test", TEST_PATH_1)
17+
indexer, err := NewIndexer("test", TEST_PATH_1, nil)
1818
if err != nil {
1919
t.Fatal(err)
2020
} else {
@@ -28,7 +28,7 @@ func Test_Indexer_001(t *testing.T) {
2828
defer cancel()
2929

3030
// Create indexer
31-
indexer, err := NewIndexer("test", TEST_PATH_1)
31+
indexer, err := NewIndexer("test", TEST_PATH_1, nil)
3232
if err != nil {
3333
t.Fatal(err)
3434
}
@@ -54,7 +54,7 @@ func Test_Indexer_001(t *testing.T) {
5454
indexer.Exclude("/bin")
5555
indexer.Exclude("/Projects")
5656
indexer.Exclude("/Documents")
57-
if err := indexer.Walk(ctx); err != nil {
57+
if err := indexer.Walk(ctx, nil); err != nil {
5858
t.Error(err)
5959
}
6060

pkg/indexer/schema.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// Namespace imports
99
. "github.com/mutablelogic/go-sqlite"
1010
. "github.com/mutablelogic/go-sqlite/pkg/lang"
11-
"github.com/mutablelogic/go-sqlite/pkg/quote"
11+
. "github.com/mutablelogic/go-sqlite/pkg/quote"
1212
)
1313

1414
///////////////////////////////////////////////////////////////////////////////
@@ -81,7 +81,7 @@ func CreateSchema(ctx context.Context, conn SQConnection, schema string, tokeniz
8181
"parent",
8282
"filename",
8383
"content="+filesTableName,
84-
"tokenize="+quote.Quote(tokenizer),
84+
"tokenize="+Quote(tokenizer),
8585
).IfNotExists()); err != nil {
8686
return err
8787
}
@@ -117,8 +117,8 @@ func ListIndexWithCount(ctx context.Context, conn SQConnection, schema string) (
117117
return err
118118
}
119119
for {
120-
row, err := r.Next()
121-
if err != nil {
120+
row := r.Next()
121+
if row == nil {
122122
break
123123
}
124124
if len(row) == 2 {
@@ -155,15 +155,24 @@ func Delete(schema string, evt *QueueEvent) (SQStatement, []interface{}) {
155155
[]interface{}{evt.Name, evt.Path}
156156
}
157157

158-
func Query(schema string, indexes []string) SQSelect {
159-
return S(N(searchTableName).WithSchema(schema)).
160-
To(
161-
N("rowid"),
162-
N("rank"),
163-
N("name"),
164-
N("parent"),
165-
N("filename"),
166-
).
167-
Where(Q(searchTableName, " MATCH ", P)).
168-
Order(N("rank"))
158+
func Query(schema string) SQSelect {
159+
// Set the query join
160+
queryJoin := J(
161+
N(searchTableName).WithSchema(schema),
162+
N(filesTableName).WithSchema(schema),
163+
).LeftJoin(Q(N(searchTableName), ".rowid=", N(filesTableName), ".rowid"))
164+
165+
// Return the select
166+
return S(queryJoin).To(
167+
N("rowid").WithSchema(searchTableName),
168+
N("rank").WithSchema(searchTableName),
169+
N("name").WithSchema(filesTableName),
170+
N("path").WithSchema(filesTableName),
171+
N("parent").WithSchema(filesTableName),
172+
N("filename").WithSchema(filesTableName),
173+
N("isdir").WithSchema(filesTableName),
174+
N("ext").WithSchema(filesTableName),
175+
N("modtime").WithSchema(filesTableName),
176+
N("size").WithSchema(filesTableName),
177+
).Where(Q(searchTableName, " MATCH ", P)).Order(N("rank"))
169178
}

pkg/lang/select.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,42 @@ import (
44
"fmt"
55
"strings"
66

7-
sqlite "github.com/mutablelogic/go-sqlite"
7+
// Namespace imports
8+
. "github.com/mutablelogic/go-sqlite"
89
)
910

1011
///////////////////////////////////////////////////////////////////////////////
1112
// TYPES
1213

1314
type sel struct {
14-
source []sqlite.SQSource
15+
source []SQExpr
1516
distinct bool
1617
limit, offset uint
1718
where []interface{}
18-
to []sqlite.SQSource
19-
order []sqlite.SQSource
19+
to []SQSource
20+
order []SQSource
2021
}
2122

2223
///////////////////////////////////////////////////////////////////////////////
2324
// LIFECYCLE
2425

2526
// S defines a select statement
26-
func S(sources ...sqlite.SQSource) sqlite.SQSelect {
27+
func S(sources ...SQExpr) SQSelect {
2728
return &sel{sources, false, 0, 0, nil, nil, nil}
2829
}
2930

3031
///////////////////////////////////////////////////////////////////////////////
3132
// PROPERTIES
3233

33-
func (this *sel) WithDistinct() sqlite.SQSelect {
34+
func (this *sel) WithDistinct() SQSelect {
3435
return &sel{this.source, true, this.limit, this.offset, this.where, this.to, this.order}
3536
}
3637

37-
func (this *sel) WithLimitOffset(limit, offset uint) sqlite.SQSelect {
38+
func (this *sel) WithLimitOffset(limit, offset uint) SQSelect {
3839
return &sel{this.source, this.distinct, limit, offset, this.where, this.to, this.order}
3940
}
4041

41-
func (this *sel) Where(v ...interface{}) sqlite.SQSelect {
42+
func (this *sel) Where(v ...interface{}) SQSelect {
4243
if len(v) == 0 {
4344
// Reset where clause
4445
return &sel{this.source, this.distinct, this.limit, this.offset, nil, this.to, this.order}
@@ -47,7 +48,7 @@ func (this *sel) Where(v ...interface{}) sqlite.SQSelect {
4748
return &sel{this.source, this.distinct, this.limit, this.offset, append(this.where, v...), this.to, this.order}
4849
}
4950

50-
func (this *sel) To(v ...sqlite.SQSource) sqlite.SQSelect {
51+
func (this *sel) To(v ...SQSource) SQSelect {
5152
if len(v) == 0 {
5253
// Reset to clause
5354
return &sel{this.source, this.distinct, this.limit, this.offset, this.where, nil, this.order}
@@ -56,7 +57,7 @@ func (this *sel) To(v ...sqlite.SQSource) sqlite.SQSelect {
5657
return &sel{this.source, this.distinct, this.limit, this.offset, this.where, append(this.to, v...), this.order}
5758
}
5859

59-
func (this *sel) Order(v ...sqlite.SQSource) sqlite.SQSelect {
60+
func (this *sel) Order(v ...SQSource) SQSelect {
6061
if len(v) == 0 {
6162
// Reset order clause
6263
return &sel{this.source, this.distinct, this.limit, this.offset, this.where, this.to, nil}

pkg/lang/select_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,19 @@ func Test_Select_001(t *testing.T) {
5959
}
6060
}
6161
}
62+
63+
func Test_Select_002(t *testing.T) {
64+
tests := []struct {
65+
In SQStatement
66+
Query string
67+
}{
68+
{S(J(N("a"), N("b"))), `SELECT * FROM a CROSS JOIN b`},
69+
{S(J(N("a"), N("b")).Join(Q("a=b"))), `SELECT * FROM a JOIN b ON a=b`},
70+
}
71+
72+
for i, test := range tests {
73+
if v := test.In.Query(); v != test.Query {
74+
t.Errorf("Test %d, Unexpected return from Query(): %q, wanted %q", i, v, test.Query)
75+
}
76+
}
77+
}

pkg/sqlite3/cache_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package sqlite3_test
22

33
import (
44
"context"
5-
"errors"
6-
"io"
75
"math/rand"
86
"sync"
97
"testing"
@@ -43,11 +41,9 @@ func Test_Cache_001(t *testing.T) {
4341
}
4442
defer r.Close()
4543
for {
46-
row, err := r.Next()
47-
if errors.Is(err, io.EOF) {
44+
row := r.Next()
45+
if row == nil {
4846
break
49-
} else if err != nil {
50-
t.Error("Next Error: ", err)
5147
} else if len(row) != 1 {
5248
t.Error("Unexpected row length: ", row, " expected [", n, "]", r.Columns())
5349
} else if int64(n) != row[0] {

pkg/sqlite3/conn.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,7 @@ func (txn *Txn) Query(st SQStatement, v ...interface{}) (SQResults, error) {
214214
}
215215

216216
// Get a results object
217-
r, err := txn.Conn.ConnCache.Prepare(txn.Conn.ConnEx, st.Query())
218-
if err != nil {
219-
return nil, err
220-
}
221-
222-
// Execute first query
223-
if err := r.NextQuery(v...); err != nil {
224-
return nil, err
225-
} else {
226-
return r, nil
227-
}
217+
return txn.Conn.ConnCache.Prepare(txn.Conn.ConnEx, st.Query())
228218
}
229219

230220
// Flags returns the Open Flags

pkg/sqlite3/results.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func (r *Results) NextQuery(v ...interface{}) error {
7474
}
7575

7676
// Return a row from the results, or return io.EOF if all results have been consumed
77-
func (r *Results) Next(t ...reflect.Type) ([]interface{}, error) {
77+
func (r *Results) Next(t ...reflect.Type) []interface{} {
7878
if r.results == nil {
79-
return nil, io.EOF
79+
return nil
8080
} else {
8181
return r.results.Next(t...)
8282
}

pkg/sqobj/sqiterator.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package sqobj
22

33
import (
4-
"errors"
5-
"io"
64
"reflect"
75

86
// Modules
@@ -49,13 +47,10 @@ func (i *Iterator) Next() interface{} {
4947
if i.rs == nil {
5048
return nil
5149
}
52-
v, err := i.rs.Next(i.t...)
53-
if err != nil {
50+
v := i.rs.Next(i.t...)
51+
if v == nil {
5452
i.rs = nil
5553
i.rowid = 0
56-
if !errors.Is(err, io.EOF) {
57-
panic(err)
58-
}
5954
return nil
6055
}
6156

plugin/indexer/handlers.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package main
22

33
import (
44
"context"
5-
"io"
65
"net/http"
6+
"reflect"
77
"regexp"
88
"strings"
9+
"time"
910

1011
// Packages
1112
router "github.com/mutablelogic/go-server/pkg/httprouter"
@@ -55,8 +56,13 @@ type ResultResponse struct {
5556
}
5657

5758
type FileResponse struct {
58-
Parent string `json:"parent"`
59-
Filename string `json:"filename"`
59+
Path string `json:"path"`
60+
Parent string `json:"parent"`
61+
Filename string `json:"filename"`
62+
IsDir bool `json:"isdir,omitempty"`
63+
Ext string `json:"ext"`
64+
ModTime time.Time `json:"modtime"`
65+
Size int64 `json:"size"`
6066
}
6167

6268
///////////////////////////////////////////////////////////////////////////////
@@ -182,11 +188,9 @@ func (p *plugin) ServeQuery(w http.ResponseWriter, req *http.Request) {
182188
}
183189
n := int64(0)
184190
for {
185-
rows, err := r.Next()
186-
if err == io.EOF {
191+
rows := r.Next(nil, nil, nil, nil, nil, nil, nil, nil, reflect.TypeOf(time.Time{}))
192+
if rows == nil {
187193
return nil
188-
} else if err != nil {
189-
return err
190194
} else {
191195
n = n + 1
192196
}
@@ -196,8 +200,13 @@ func (p *plugin) ServeQuery(w http.ResponseWriter, req *http.Request) {
196200
Rank: rows[1].(float64),
197201
Index: rows[2].(string),
198202
File: FileResponse{
199-
Parent: rows[3].(string),
200-
Filename: rows[4].(string),
203+
Path: rows[3].(string),
204+
Parent: rows[4].(string),
205+
Filename: rows[5].(string),
206+
IsDir: int64ToBool(rows[6].(int64)),
207+
Ext: rows[7].(string),
208+
ModTime: rows[8].(time.Time),
209+
Size: rows[9].(int64),
201210
},
202211
})
203212
}

plugin/indexer/util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ func uintMin(a, b uint) uint {
66
}
77
return b
88
}
9+
10+
func int64ToBool(i int64) bool {
11+
if i == 0 {
12+
return false
13+
}
14+
return true
15+
}

plugin/sqlite3/handlers.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,9 @@ func results(r SQResults) (SqlResultResponse, error) {
392392

393393
// Iterate through the rows, break when maximum number of results is reached
394394
for {
395-
row, err := r.Next()
396-
if errors.Is(err, io.EOF) {
395+
row := r.Next()
396+
if row == nil {
397397
break
398-
} else if err != nil {
399-
return result, err
400398
} else {
401399
result.Results = append(result.Results, interfaceSliceCopy(row))
402400
}

sqlite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ type SQResults interface {
107107
// if types are provided, then returned row is cast to
108108
// appopriate types. The returned row needs to be copied
109109
// if not transient
110-
Next(...reflect.Type) ([]interface{}, error)
110+
Next(...reflect.Type) []interface{}
111111

112112
// Close results and discard when done
113113
Close() error

sys/sqlite3/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,14 @@ func main() {
232232
## Results
233233

234234
Results are returned from the `Exec` method after a statement is executed. If there are no results,
235-
then a call to `func (*Results) Next() ([]interface{},error)` will return `nil` in place of an
235+
then a call to `func (*Results) Next() []interface{}` will return `nil` in place of an
236236
array of values. You should repeatedly call the `Next` method until this occurs. For example,
237237

238238
```go
239239
func ReadResults(r *Results) error {
240240
for {
241-
row, err := r.Next()
242-
if err != nil {
243-
return err
244-
} else if row == nil {
241+
row := r.Next()
242+
if row == nil {
245243
return nil
246244
}
247245
// Handle row
@@ -261,10 +259,8 @@ example,
261259
func ReadResults(r *Results) error {
262260
cast := []reflect.Type{ reflect.TypeOf(bool), reflect.TypeOf(uint) }
263261
for {
264-
row, err := r.Next(cast...)
265-
if err != nil {
266-
return err
267-
} else if row == nil {
262+
row := r.Next(cast...)
263+
if row == nil {
268264
return nil
269265
}
270266
// Handle row which has bool as first element and uint as second element

0 commit comments

Comments
 (0)