docset-sfdc/SFDashC/database.go

60 lines
1.4 KiB
Go
Raw Permalink Normal View History

2016-07-27 01:26:47 +00:00
package main
import (
"database/sql"
"github.com/coopernurse/gorp"
_ "github.com/mattn/go-sqlite3"
2017-07-15 21:24:10 +00:00
"os"
"path/filepath"
2016-07-27 01:26:47 +00:00
)
var dbmap *gorp.DbMap
2017-07-15 21:24:10 +00:00
var dbName = "docSet.dsidx"
2016-07-27 01:26:47 +00:00
2018-01-06 06:05:24 +00:00
// InitDb will initialize a new instance of a sqlite db for indexing
2017-07-15 21:24:10 +00:00
func InitDb(buildDir string) *gorp.DbMap {
dbPath := filepath.Join(buildDir, dbName)
err := os.MkdirAll(filepath.Dir(dbPath), 0755)
ExitIfError(err)
db, err := sql.Open("sqlite3", dbPath)
2016-07-27 01:26:47 +00:00
ExitIfError(err)
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
dbmap.AddTableWithName(SearchIndex{}, "searchIndex").SetKeys(true, "ID")
err = dbmap.CreateTablesIfNotExists()
ExitIfError(err)
err = dbmap.TruncateTables()
ExitIfError(err)
return dbmap
}
2018-01-06 06:05:24 +00:00
// SaveSearchIndex will index a particular entry into the sqlite3 database
func SaveSearchIndex(dbmap *gorp.DbMap, entry TOCEntry, entryType SupportedType, toc *AtlasTOC) {
if entry.LinkAttr.Href == "" || !entryType.IsValidType() {
2016-07-27 01:26:47 +00:00
return
}
relLink := entry.GetContentFilepath(toc, false)
2018-01-06 06:05:24 +00:00
name := entry.CleanTitle(entryType)
2016-07-27 01:26:47 +00:00
if entryType.ShowNamespace && len(entryHierarchy) > 0 {
// Show namespace for methods
name = entryHierarchy[len(entryHierarchy)-1] + "." + name
}
si := SearchIndex{
Name: name,
Type: entryType.TypeName,
Path: relLink,
}
2018-01-06 06:05:24 +00:00
err := dbmap.Insert(&si)
ExitIfError(err)
LogDebug("%s is indexed as a %s", entry.Text, entryType.TypeName)
2016-07-27 01:26:47 +00:00
}