func (db Database) GetUrlAlias(nid int32) string { sql := `select pid, src as Source, dst as Alias, language from %surl_alias where src = ?` s2 := fmt.Sprintf(sql, db.Prefix) source := fmt.Sprintf("node/%d", nid) list, err := db.DbMap.Select(UrlAlias{}, s2, source) util.CheckErrFatal(err, s2) if len(list) > 1 { util.Fatal("Expected only one alias for %s but got %d.\n%+v\n", source, len(list), list) } if len(list) == 1 { return list[0].(*UrlAlias).Alias } return source }
func (db Database) CCKDataForNode(node *JoinedNodeDataBody, fields []*CCKFieldType) (map[CCKField]interface{}, error) { results := make(map[CCKField]interface{}) var err error selectFields := make([]CCKField, 0, 100) _sql := "SELECT " first := true for _, cft := range fields { fmt.Printf("looping through dbcolumns for %s\n", cft.Name) for k, v := range cft.DBColumns { fmt.Printf("%#v = %#v\n", k, v) key := k.(string) dbColumn := v.(map[interface{}]interface{}) typ, ok := dbColumn["type"] if ok { cckField := CCKField{cft.Name, key, typ.(string)} if !first { _sql = _sql + ", " } _sql = _sql + cft.Name + "_" + key selectFields = append(selectFields, cckField) first = false } } } if first { return results, nil } _sql += " FROM %scontent_type_%s WHERE vid = %d and nid = %d" query := fmt.Sprintf(_sql, db.Prefix, node.Type, node.Vid, node.Nid) rows, err := db.Db.Query(query) util.CheckErrFatal(err, "Selecting CCK field values for node.", node.Nid) defer rows.Close() columnNames, err := rows.Columns() if err != nil { util.Fatal("Error getting column names: %v", err) // or whatever error handling is appropriate } if len(columnNames) != len(selectFields) { util.Fatal("column names length and select fields length do not match!") } columns := make([]interface{}, len(columnNames)) columnPointers := make([]interface{}, len(columnNames)) for i := 0; i < len(columnNames); i++ { columnPointers[i] = &columns[i] } if rows.Next() { if err := rows.Scan(columnPointers...); err != nil { util.Fatal("Error reading a row: %v", err) } for i, colName := range selectFields { switch colName.Type { case "text", "varchar": switch cv := columns[i].(type) { case []byte: results[colName] = string(cv) default: if cv != nil { results[colName] = cv.(string) } } default: results[colName] = columns[i] } } } return results, nil }
func main() { flag.Parse() if *version { fmt.Fprintf(os.Stderr, "Version 0.1\n") os.Exit(0) } if *dbName == "" { flag.Usage() os.Exit(1) } if *user == "" { *user = *dbName } if *pass == "" { fmt.Printf("Password: "******"content") { fmt.Fprintln(os.Stderr, "There is no content directory here. Did you mean to try somewhere else?") os.Exit(2) } // username:password@protocol(address)/dbname?param=value db := model.Connect(*driver, *user+":"+*pass+"@tcp("+*host+")/"+*dbName, *prefix, *verbose) cckFieldTypes, err := db.CCKFields() if err != nil && *emvideoField != "" { util.Fatal("Unable to retrieve CCK Field metadata: %s", err.Error()) } allBookPagesAsMap := make(map[int32]*model.BookPage) //db.AllBookPagesAsMap() // fmt.Println("\nnode types:") // spew.Dump(db.AllNodeTypes()) // fmt.Println("\nbooks:") // spew.Dump(db.AllBooksAsMap()) // fmt.Println("\nbook pages:") // spew.Dump(allBookPagesAsMap) // fmt.Println("\nmenus:") // spew.Dump(db.AllMenus()) processVocabs(db) // for _, node := range model.AllNodes(db, *prefix) { // fmt.Printf("%v\n", node) // } offset := 0 nodes := db.JoinedNodeFields(offset, 10) for len(nodes) > 0 { for _, node := range nodes { alias := db.GetUrlAlias(node.Nid) terms := db.JoinedTaxonomyTerms(node.Nid) menus := db.JoinedMenusForPath(fmt.Sprintf("node/%d", node.Nid)) emvideos := make([]model.Emvideo, 0, 10) if *emvideoField != "" { cckData, err := db.CCKDataForNode(node, cckFieldTypes[node.Type]) if err != nil { util.Fatal("Unable to get CCK field data for node: %s", err.Error()) } for _, cckFieldType := range cckFieldTypes[node.Type] { if cckFieldType.Name == *emvideoField { video, err := model.EmvideoForNodeField(cckFieldType, cckData) if err == nil { emvideos = append(emvideos, *video) } } } } // hasMenuOrBook := false fmt.Printf("node/%d %s %s\n", node.Nid, alias, node.Title) if bookPage, exists := allBookPagesAsMap[node.Nid]; exists { // spew.Printf(" book %v\n", bookPage) if len(menus) == 0 { menus = db.MenusForMlid(bookPage.Mlid) } // hasMenuOrBook = true } if len(menus) > 0 { // spew.Printf(" menu %v\n", menus) // hasMenuOrBook = true } // if !hasMenuOrBook { // fmt.Printf(" --\n") // } processNode(node, alias, terms, menus, emvideos) } offset += len(nodes) nodes = db.JoinedNodeFields(offset, 10) } fmt.Printf("Total %d nodes.\n", offset) }