87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"strings"
|
|
"text/template"
|
|
)
|
|
|
|
//go:embed templates/*.sql.tmpl
|
|
var templateFS embed.FS
|
|
|
|
var (
|
|
MainQueryTmpl *template.Template
|
|
ShipBreakdownTmpl *template.Template
|
|
SystemBreakdownTmpl *template.Template
|
|
ModuleStatsTmpl *template.Template
|
|
ComprehensiveStatsTmpl *template.Template
|
|
)
|
|
|
|
func init() {
|
|
var err error
|
|
|
|
// Parse templates once at startup
|
|
MainQueryTmpl, err = template.ParseFS(templateFS, "templates/main_query.sql.tmpl")
|
|
if err != nil {
|
|
panic("failed to parse main_query template: " + err.Error())
|
|
}
|
|
|
|
ShipBreakdownTmpl, err = template.ParseFS(templateFS, "templates/ship_breakdown.sql.tmpl")
|
|
if err != nil {
|
|
panic("failed to parse ship_breakdown template: " + err.Error())
|
|
}
|
|
|
|
SystemBreakdownTmpl, err = template.ParseFS(templateFS, "templates/system_breakdown.sql.tmpl")
|
|
if err != nil {
|
|
panic("failed to parse system_breakdown template: " + err.Error())
|
|
}
|
|
|
|
ModuleStatsTmpl, err = template.ParseFS(templateFS, "templates/module_stats.sql.tmpl")
|
|
if err != nil {
|
|
panic("failed to parse module_stats template: " + err.Error())
|
|
}
|
|
|
|
ComprehensiveStatsTmpl, err = template.ParseFS(templateFS, "templates/comprehensive_stats.sql.tmpl")
|
|
if err != nil {
|
|
panic("failed to parse comprehensive_stats template: " + err.Error())
|
|
}
|
|
}
|
|
|
|
// ExecuteTemplate executes a pre-parsed template and builds args
|
|
func ExecuteTemplate(tmpl *template.Template, data interface{}) (string, []interface{}, error) {
|
|
var queryBuf strings.Builder
|
|
if err := tmpl.Execute(&queryBuf, data); err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
query := strings.TrimSpace(queryBuf.String())
|
|
args := []interface{}{}
|
|
|
|
if qp, ok := data.(QueryParams); ok {
|
|
if qp.Ship > 0 {
|
|
args = append(args, qp.Ship)
|
|
}
|
|
if len(qp.Systems) > 0 {
|
|
args = append(args, qp.Systems)
|
|
}
|
|
if len(qp.Modules) > 0 {
|
|
args = append(args, qp.Modules)
|
|
}
|
|
} else if msData, ok := data.(ModuleStatsData); ok {
|
|
args = append(args, msData.KillmailIDs)
|
|
} else if csData, ok := data.(ComprehensiveStatsData); ok {
|
|
if csData.Ship > 0 {
|
|
args = append(args, csData.Ship)
|
|
}
|
|
if len(csData.Systems) > 0 {
|
|
args = append(args, csData.Systems)
|
|
}
|
|
if len(csData.Modules) > 0 {
|
|
args = append(args, csData.Modules)
|
|
}
|
|
args = append(args, csData.KillmailLimit)
|
|
}
|
|
|
|
return query, args, nil
|
|
}
|