47 lines
936 B
Go
47 lines
936 B
Go
package tags
|
|
|
|
import (
|
|
"database/sql"
|
|
_ "github.com/lib/pq"
|
|
"fmt"
|
|
"strings"
|
|
"strconv"
|
|
)
|
|
|
|
type Tag struct {
|
|
Name string
|
|
Parent int
|
|
Children []int
|
|
}
|
|
|
|
var Tags map[int]*Tag
|
|
|
|
func LoadTags(db *sql.DB) {
|
|
rows, err := db.Query("select tag_id, name, parent_id, array_agg(distinct(parent_of.tag_id)) from tags join tags as parent_of on tags.parent_id=parent_of.tag_id group by tags.tag_id")
|
|
|
|
|
|
for rows.Next() {
|
|
tag := &Tag{Children: nil}
|
|
|
|
var children sql.NullString
|
|
var tag_id int
|
|
err = rows.Scan(&tag_id, &tag.Name, &tag.Name, &tag.Parent, &children)
|
|
if err != nil {
|
|
fmt.Println("tags DB Error: ", err)
|
|
}
|
|
|
|
if children.Valid {
|
|
var childrenStr = children.String[1 : len(children.String)-1]
|
|
if childrenStr != "NULL" {
|
|
pids := strings.Split(childrenStr, ",")
|
|
for _, spid := range pids {
|
|
pid, _ := strconv.Atoi(spid)
|
|
|
|
tags.C
|
|
}
|
|
}
|
|
}
|
|
|
|
tags[tag_d] = tag
|
|
}
|
|
} |