Skip to content

Commit adb9e85

Browse files
committed
Added tags
1 parent 825aac4 commit adb9e85

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

pkg/indexer/schema.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ type File struct {
3131
}
3232

3333
type Doc struct {
34-
Name string `sqlite:"name,primary,foreign,join:name"` // Index name, primary key
35-
Path string `sqlite:"path,primary,foreign,join:path"` // Relative path, primary key
36-
Title string `sqlite:"title,notnull"` // Title of the document, text
37-
Description string `sqlite:"description"` // Description of the document, text
38-
Shortform string `sqlite:"shortform"` // Shortform of the document, html
34+
Name string `sqlite:"name,primary,foreign,join:name"` // Index name, primary key
35+
Path string `sqlite:"path,primary,foreign,join:path"` // Relative path, primary key
36+
Title string `sqlite:"title,notnull"` // Title of the document, text
37+
Description string `sqlite:"description"` // Description of the document, text
38+
Shortform string `sqlite:"shortform"` // Shortform of the document, html
39+
Tags []string `sqlite:"-"` // Tags added via DocTag table
40+
}
41+
42+
type DocTag struct {
43+
Name string `sqlite:"name,primary,foreign"` // Index name, primary key
44+
Path string `sqlite:"path,primary,foreign"` // Relative path, primary key
45+
Tag string `sqlite:"tag,notnull"` // Document tag
3946
}
4047

4148
// View is used as the content source for the search virtual table
@@ -66,6 +73,7 @@ const (
6673
fileTableName = "file"
6774
searchTableName = "search"
6875
docTableName = "doc"
76+
tagTableName = "tag"
6977
viewTableName = "view"
7078
searchTriggerInsertName = "search_insert"
7179
searchTriggerDeleteName = "search_delete"
@@ -92,6 +100,7 @@ var (
92100
var (
93101
fileTable = sqobj.MustRegisterClass(N(fileTableName), File{})
94102
docTable = sqobj.MustRegisterClass(N(docTableName), Doc{}).ForeignKey(fileTable)
103+
tagTable = sqobj.MustRegisterClass(N(tagTableName), DocTag{}).ForeignKey(docTable)
95104
viewTable = sqobj.MustRegisterView(N(viewTableName), View{}, true, fileTable, docTable)
96105
searchTable = sqobj.MustRegisterVirtual(N(searchTableName), "fts5", Search{}, "content="+Quote(viewTableName))
97106
)
@@ -113,6 +122,9 @@ func CreateSchema(ctx context.Context, conn SQConnection, schema string, tokeniz
113122
if err := docTable.Create(txn, schema); err != nil {
114123
return err
115124
}
125+
if err := tagTable.Create(txn, schema); err != nil {
126+
return err
127+
}
116128
if err := viewTable.Create(txn, schema); err != nil {
117129
return err
118130
}
@@ -196,11 +208,22 @@ func GetFile(schema string, rowid int64) (SQStatement, []interface{}, []reflect.
196208
}
197209

198210
func UpsertDoc(txn SQTransaction, doc *Doc) (int64, error) {
199-
if n, err := docTable.UpsertKeys(txn, doc); err != nil {
211+
n, err := docTable.UpsertKeys(txn, doc)
212+
if err != nil {
200213
return 0, err
201-
} else {
202-
return n[0], nil
203214
}
215+
// Add any tags
216+
for _, tag := range doc.Tags {
217+
if _, err := tagTable.UpsertKeys(txn, &DocTag{
218+
Name: doc.Name,
219+
Path: doc.Path,
220+
Tag: tag,
221+
}); err != nil {
222+
return 0, err
223+
}
224+
}
225+
// Return rowid for the document
226+
return n[0], nil
204227
}
205228

206229
func Query(schema string, snippet bool) SQSelect {

pkg/indexer/store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ func (s *Store) insert(ctx context.Context, txn SQTransaction, name, path string
292292
Title: doc.Title(),
293293
Description: doc.Description(),
294294
Shortform: string(doc.Shortform()), // TODO: html2text
295+
Tags: doc.Tags(),
295296
}); err != nil {
296297
return err
297298
} else {

pkg/lang/join.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ func (j *join) String() string {
4848
// PUBLIC METHODS
4949

5050
func (j *join) Join(expr ...SQExpr) SQJoin {
51-
return &join{j.l, j.r, "JOIN", expr, nil}
51+
return &join{j.l, j.r, "JOIN", expr, j.cols}
5252
}
5353

5454
func (j *join) LeftJoin(expr ...SQExpr) SQJoin {
55-
return &join{j.l, j.r, "LEFT JOIN", expr, nil}
55+
return &join{j.l, j.r, "LEFT JOIN", expr, j.cols}
5656
}
5757

5858
func (j *join) LeftInnerJoin(expr ...SQExpr) SQJoin {
59-
return &join{j.l, j.r, "LEFT INNER JOIN", expr, nil}
59+
return &join{j.l, j.r, "LEFT INNER JOIN", expr, j.cols}
6060
}
6161

6262
func (j *join) Using(cols ...string) SQJoin {

pkg/sqobj/sqreflect.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func NewReflect(proto interface{}) (*SQReflect, error) {
9696
// Set columns
9797
var result error
9898
for _, field := range fields {
99+
if field == nil {
100+
// Ignored fields
101+
continue
102+
}
99103
// Check for duplicate column name
100104
if _, exists := r.colmap[field.Name]; exists {
101105
result = multierror.Append(result, ErrDuplicateEntry.With(field.Name))
@@ -112,6 +116,10 @@ func NewReflect(proto interface{}) (*SQReflect, error) {
112116

113117
// Set indexes
114118
for _, field := range fields {
119+
if field == nil {
120+
// Ignored fields
121+
continue
122+
}
115123
for _, tag := range field.Tags {
116124
name, unique := parseTagIndexValue(tag)
117125
if name != "" {
@@ -129,6 +137,10 @@ func NewReflect(proto interface{}) (*SQReflect, error) {
129137
// Set joins. The join names are aliases so when joining two tables, the aliases
130138
// are used to match up the columns
131139
for _, field := range fields {
140+
if field == nil {
141+
// Ignored fields
142+
continue
143+
}
132144
for _, tag := range field.Tags {
133145
name := parseTagJoinValue(tag)
134146
if name == "" {

pkg/sqobj/sqview.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (this *View) join(l, r *Class, leftjoin bool) SQJoin {
155155
// or if the column names are the same,
156156
// this [LEFT} JOIN other USING (alias,alias)
157157
join := J(l.SQSource, r.SQSource)
158-
expr := make([]SQExpr, len(aliases))
158+
expr := make([]SQExpr, 0, len(aliases))
159159
using := make([]string, 0, len(aliases))
160160
for _, alias := range aliases {
161161
lcol := l.joinmap[alias]
@@ -174,6 +174,8 @@ func (this *View) join(l, r *Class, leftjoin bool) SQJoin {
174174
} else {
175175
join = join.Join(expr...)
176176
}
177+
178+
// Return success
177179
return join
178180
}
179181

0 commit comments

Comments
 (0)