func PopulateContactType(row mysql.Row, res mysql.Result, ch chan ContactType) { ctype := ContactType{ ID: row.Int(res.Map("contactTypeID")), Name: row.Str(res.Map("name")), } ch <- ctype }
func fillStruct(row mysql.Row, sample reflect.Value, rowIndex int) { rowSize := len(row) // 遍历输入的结构体, 将每个类型的值缓冲地址存到scanparam中 for i := 0; i < sample.NumField(); i++ { v := sample.Field(i) if i >= rowSize { log.Printf("out of row size, %d, rowsize: %d", i, rowSize) continue } //log.Println(i, rowIndex, row.Str(rowIndex)) switch v.Kind() { case reflect.Int32, reflect.Uint32, reflect.Int64: v.SetInt(row.ForceInt64(rowIndex)) case reflect.String: v.SetString(row.Str(rowIndex)) case reflect.Struct: fillStruct(row, v, rowIndex) default: log.Printf("dbfw.makeScanParam unsupport type: %s", v.Type()) } rowIndex++ } }
func (c Country) PopulateCountry(row mysql.Row, res mysql.Result, ch chan Country) { country := Country{ ID: row.Int(res.Map("countryID")), Name: row.Str(res.Map("name")), Abbr: row.Str(res.Map("abbr")), } ch <- country }
func (m MapIcon) PopulateIcon(row mysql.Row, res mysql.Result, ch chan MapIcon) { icon := MapIcon{ ID: row.Int(res.Map("ID")), DealerTierID: row.Int(res.Map("tier")), DealerTypeID: row.Int(res.Map("dealer_type")), MapIcon: row.Str(res.Map("mapicon")), MapIconShadow: row.Str(res.Map("mapiconshadow")), } ch <- icon }
func (d DealerType) PopulateType(row mysql.Row, res mysql.Result, ch chan DealerType) { dealertype := DealerType{ ID: row.Int(res.Map("dealer_type")), Name: row.Str(res.Map("type")), Online: row.Bool(res.Map("online")), Show: row.Bool(res.Map("show")), Label: row.Str(res.Map("label")), } ch <- dealertype }
func (l LandingPage) populateLandingPageImage(row mysql.Row, res mysql.Result, ch chan LandingPageImage) { image := LandingPageImage{ ID: row.Int(res.Map("id")), LandingPageID: row.Int(res.Map("landingPageID")), URL: row.Str(res.Map("url")), Sort: row.Int(res.Map("sort")), } ch <- image }
func (s State) PopulateState(row mysql.Row, res mysql.Result, ch chan State) { state := State{ ID: row.Int(res.Map("stateID")), CountryID: row.Int(res.Map("countryID")), Name: row.Str(res.Map("state")), Abbr: row.Str(res.Map("abbr")), } ch <- state }
func (l LandingPage) populateLandingPageData(row mysql.Row, res mysql.Result, ch chan LandingPageData) { data := LandingPageData{ ID: row.Int(res.Map("id")), LandingPageID: row.Int(res.Map("landingPageID")), Key: row.Str(res.Map("dataKey")), Value: row.Str(res.Map("dataValue")), } ch <- data }
func (r ContactReceiver) PopulateContactReceiver(row mysql.Row, res mysql.Result, ch chan ContactReceiver) { receiver := ContactReceiver{ ID: row.Int(res.Map("contactReceiverID")), FirstName: row.Str(res.Map("first_name")), LastName: row.Str(res.Map("last_name")), Email: row.Str(res.Map("email")), } receiver.GetTypes() ch <- receiver }
func PopulateRevision(row mysql.Row, res mysql.Result, ch chan ContentRevision) { var revision ContentRevision id := res.Map("revisionID") contentID := res.Map("contentID") contentText := res.Map("content_text") createdOn := res.Map("createdOn") active := res.Map("active") revision = ContentRevision{ ID: row.Int(id), ContentID: row.Int(contentID), ContentText: row.Str(contentText), CreatedOn: row.Time(createdOn, UTC), Active: row.Bool(active), } ch <- revision }
func (d DealerTier) PopulateTier(row mysql.Row, res mysql.Result, ch chan DealerTier) { tier := DealerTier{ ID: row.Int(res.Map("ID")), Name: row.Str(res.Map("tier")), Sort: row.Int(res.Map("sort")), } ch <- tier }
func (m MapicsCode) PopulateCode(row mysql.Row, res mysql.Result, ch chan MapicsCode) { code := MapicsCode{ ID: row.Int(res.Map("mCodeID")), Code: row.Str(res.Map("code")), Description: row.Str(res.Map("description")), } ch <- code }
func (f *FAQ) PopulateFAQ(row mysql.Row, res mysql.Result, ch chan FAQ) { faq := FAQ{ ID: row.Int(res.Map("faqID")), Question: row.Str(res.Map("question")), Answer: row.Str(res.Map("answer")), } ch <- faq }
func (c Customer) PopulateSimpleCustomer(row mysql.Row, res mysql.Result, ch chan Customer) { customer := Customer{ ID: row.Int(res.Map("cust_id")), Name: row.Str(res.Map("name")), CustomerID: row.Int(res.Map("customerID")), } ch <- customer }
func (c BlogCategory) PopulateCategory(row mysql.Row, res mysql.Result, ch chan BlogCategory) { category := BlogCategory{ ID: row.Int(res.Map("blogCategoryID")), Name: row.Str(res.Map("name")), Slug: row.Str(res.Map("slug")), Active: row.Bool(res.Map("active")), } ch <- category }
func (s SalesRep) PopulateSalesRep(row mysql.Row, res mysql.Result, ch chan SalesRep) { rep := SalesRep{ ID: row.Int(res.Map("salesRepID")), Name: row.Str(res.Map("name")), Code: row.Str(res.Map("code")), CustomerCount: row.Int(res.Map("customercount")), } ch <- rep }
func (c Customer) PopulateCustomer(row mysql.Row, res mysql.Result, ch chan Customer) { customer := Customer{ ID: row.Int(res.Map("cust_id")), Name: row.Str(res.Map("name")), Email: row.Str(res.Map("email")), Address: row.Str(res.Map("address")), Address2: row.Str(res.Map("address2")), City: row.Str(res.Map("city")), StateID: row.Int(res.Map("stateID")), PostalCode: row.Str(res.Map("postal_code")), Phone: row.Str(res.Map("phone")), Fax: row.Str(res.Map("fax")), ContactPerson: row.Str(res.Map("contact_person")), DealerTypeID: row.Int(res.Map("dealer_type")), Latitude: row.Str(res.Map("latitude")), Longitude: row.Str(res.Map("longitude")), Website: row.Str(res.Map("website")), CustomerID: row.Int(res.Map("customerID")), IsDummy: row.Bool(res.Map("isDummy")), ParentID: row.Int(res.Map("parentID")), SearchURL: row.Str(res.Map("searchURL")), ELocalURL: row.Str(res.Map("eLocalURL")), Logo: row.Str(res.Map("logo")), MapicsCodeID: row.Int(res.Map("mCodeID")), SalesRepID: row.Int(res.Map("salesRepID")), APIKey: row.Str(res.Map("APIKey")), Tier: row.Int(res.Map("tier")), ShowWebsite: row.Bool(res.Map("showWebsite")), LocationCount: row.Int(res.Map("locationCount")), UserCount: row.Int(res.Map("userCount")), PropertyCount: row.Int(res.Map("propertyCount")), } ch <- customer }
func (n NewsItem) PopulateNewsItem(row mysql.Row, res mysql.Result, ch chan NewsItem) { item := NewsItem{ ID: row.Int(res.Map("newsItemID")), Title: row.Str(res.Map("title")), Lead: row.Str(res.Map("lead")), Content: row.Str(res.Map("content")), PublishStart: row.Time(res.Map("publishStart"), UTC), PublishEnd: row.Time(res.Map("publishEnd"), UTC), Active: row.Bool(res.Map("active")), Slug: row.Str(res.Map("slug")), } ch <- item }
func PopulateContent(row mysql.Row, res mysql.Result, ch chan Content) { cid := res.Map("contentID") contentType := res.Map("content_type") pageTitle := res.Map("page_title") createdDate := res.Map("createdDate") lastModified := res.Map("lastModified") metaTitle := res.Map("meta_title") metaDesc := res.Map("meta_description") keywords := res.Map("keywords") isPrimary := res.Map("isPrimary") published := res.Map("published") active := res.Map("active") slug := res.Map("slug") requireAuth := res.Map("requireAuthentication") canonical := res.Map("canonical") var content Content id := row.Int(cid) revCh := make(chan ContentRevisions) go GetContentRevisions(id, revCh) revisions := <-revCh content = Content{ ID: id, ContentType: row.Str(contentType), PageTitle: row.Str(pageTitle), CreatedDate: row.Time(createdDate, UTC), LastModified: row.Time(lastModified, UTC), MetaTitle: row.Str(metaTitle), MetaDescription: row.Str(metaDesc), Keywords: row.Str(keywords), Primary: row.Bool(isPrimary), Published: row.Bool(published), Active: row.Bool(active), Slug: row.Str(slug), RequireAuth: row.Bool(requireAuth), Canonical: row.Str(canonical), Revisions: revisions, ActiveRevision: revisions.GetActiveRevision(), } ch <- content }
func (c CustomerUser) PopulateUser(row mysql.Row, res mysql.Result, ch chan CustomerUser) { user := CustomerUser{ ID: row.Str(res.Map("id")), CustID: row.Int(res.Map("cust_ID")), CustomerID: row.Int(res.Map("customerID")), Name: row.Str(res.Map("name")), Email: row.Str(res.Map("email")), DateAdded: row.Time(res.Map("date_added"), UTC), Active: row.Bool(res.Map("active")), LocationID: row.Int(res.Map("locationID")), IsSudo: row.Bool(res.Map("isSudo")), NotCustomer: row.Bool(res.Map("NotCustomer")), } ch <- user }
func (c Contact) PopulateContact(row mysql.Row, res mysql.Result, ch chan Contact) { contact := Contact{ ID: row.Int(res.Map("contactID")), FirstName: row.Str(res.Map("first_name")), LastName: row.Str(res.Map("last_name")), Email: row.Str(res.Map("email")), Phone: row.Str(res.Map("phone")), Subject: row.Str(res.Map("subject")), Message: row.Str(res.Map("message")), Created: row.Time(res.Map("createdDate"), UTC), Type: row.Str(res.Map("type")), Address1: row.Str(res.Map("address1")), Address2: row.Str(res.Map("address2")), City: row.Str(res.Map("city")), PostalCode: row.Str(res.Map("postalcode")), Country: row.Str(res.Map("country")), } ch <- contact }
func (p Post) PopulatePost(row mysql.Row, res mysql.Result, ch chan Post) { post := Post{ ID: row.Int(res.Map("blogPostID")), Title: row.Str(res.Map("post_title")), Slug: row.Str(res.Map("slug")), Content: row.Str(res.Map("post_text")), Published: row.Time(res.Map("publishedDate"), UTC), Created: row.Time(res.Map("createdDate"), UTC), LastModified: row.Time(res.Map("lastModified"), UTC), UserID: row.Int(res.Map("userID")), MetaTitle: row.Str(res.Map("meta_title")), MetaDesc: row.Str(res.Map("meta_description")), Keywords: row.Str(res.Map("keywords")), Active: row.Bool(res.Map("active")), } catchan := make(chan BlogCategories) comchan := make(chan Comments) authchan := make(chan User) go func(ch chan User) { author, _ := GetUserByID(post.UserID) ch <- author }(authchan) go func(ch chan Comments) { comments, _ := post.GetComments() ch <- comments }(comchan) go func(ch chan BlogCategories) { categories, _ := post.GetCategories() ch <- categories }(catchan) post.Author = <-authchan post.Categories = <-catchan post.Comments = <-comchan ch <- post }
// LoadRecord loads a mysql into a struct func LoadRecord(r interface{}, res mysql.Result, row mysql.Row) { s := structs.New(r) var err error for _, f := range s.Fields() { for index, field := range res.Fields() { if strings.ToLower(field.Name) == strings.ToLower(f.Name()) { unsigned := (field.Flags & _FLAG_UNSIGNED) != 0 switch field.Type { default: err = f.Set(row.Str(index)) case MYSQL_TYPE_BIT: err = f.Set(row.Bin(index)[0] == 1) case MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VARCHAR, MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_SET, MYSQL_TYPE_ENUM, MYSQL_TYPE_GEOMETRY: err = f.Set(row.Str(index)) case MYSQL_TYPE_TINY, MYSQL_TYPE_SHORT, MYSQL_TYPE_YEAR, MYSQL_TYPE_LONG, MYSQL_TYPE_INT24: if unsigned { err = f.Set(row.Uint(index)) if err != nil { err = f.Set(row.Uint64(index)) if err != nil { err = f.Set(row.Int(index)) } } } else { err = f.Set(row.Int(index)) } case MYSQL_TYPE_LONGLONG: if unsigned { err = f.Set(row.Uint64(index)) if err != nil { err = f.Set(int64(row.Uint64(index))) } } else { err = f.Set(int64(row.Uint64(index))) } case MYSQL_TYPE_FLOAT: err = f.Set(math.Float32frombits(uint32(row.Uint(index)))) case MYSQL_TYPE_DOUBLE: err = f.Set(math.Float64frombits(row.Uint64(index))) case MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDECIMAL: if row.Str(index) != "" { r, err := strconv.ParseFloat(row.Str(index), 64) if err == nil { err = f.Set(decimal.NewFromFloat(r)) if err != nil { fmt.Printf("error parsing Decimal field %s %s, %v \n", field.Name, row.Str(index), err) } } else { fmt.Printf("error parsing Decimal field %s %s, %v \n", field.Name, row.Str(index), err) } } case MYSQL_TYPE_DATE, MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIMESTAMP: tmp := row.Str(index) if tmp != "" { t, err := time.Parse("2006-01-02 15:04:05", row.Str(index)) // check out http://stackoverflow.com/questions/14106541/go-parsing-date-time-strings-which-are-not-standard-formats // and https://golang.org/src/time/format.go the string 2006-01-02 15:04:05 is not arbitrary and is not a date, it is the // actual format string. if err != nil { t, err = time.Parse("2006-01-02", row.Str(index)) if err != nil { fmt.Printf("error parsing date field %s %s, %v \n", field.Name, row.Str(index), err) } } err = f.Set(t) if err != nil { fmt.Printf("Error setting date %s %s => %v \n", field.Name, row.Str(index), t) } } case MYSQL_TYPE_TIME: err = f.Set(row.Duration(index)) } if err != nil { fmt.Printf("Error setting value on %s => %v\n", field.Name, err) } break } } } }
func (v Video) PopulateVideo(row mysql.Row, res mysql.Result, ch chan Video) { video := Video{ ID: row.Int(res.Map("videoID")), EmbedLink: row.Str(res.Map("embed_link")), DateAdded: row.Time(res.Map("dateAdded"), UTC), Sort: row.Int(res.Map("sort")), Title: row.Str(res.Map("title")), Description: row.Str(res.Map("description")), YouTubeID: row.Str(res.Map("youtubeID")), WatchPage: row.Str(res.Map("watchpage")), Screenshot: row.Str(res.Map("screenshot")), } ch <- video }
func (c CustomerLocation) PopulateLocation(row mysql.Row, res mysql.Result, ch chan CustomerLocation) { location := CustomerLocation{ ID: row.Int(res.Map("locationID")), CustomerID: row.Int(res.Map("cust_id")), Name: row.Str(res.Map("name")), Address: row.Str(res.Map("address")), City: row.Str(res.Map("city")), StateID: row.Int(res.Map("stateID")), PostalCode: row.Str(res.Map("postalCode")), Email: row.Str(res.Map("email")), Phone: row.Str(res.Map("phone")), Fax: row.Str(res.Map("fax")), Latitude: row.Float(res.Map("latitude")), Longitude: row.Float(res.Map("longitude")), ContactPerson: row.Str(res.Map("contact_person")), IsPrimary: row.Bool(res.Map("isprimary")), ShippingDefault: row.Bool(res.Map("ShippingDefault")), } ch <- location }
func (t Testimonial) PopulateTestimonial(row mysql.Row, res mysql.Result, ch chan Testimonial) { testimonial := Testimonial{ ID: row.Int(res.Map("testimonialID")), Rating: row.Float(res.Map("rating")), Title: row.Str(res.Map("title")), Content: row.Str(res.Map("testimonial")), DateAdded: row.Time(res.Map("dateAdded"), UTC), Approved: row.Bool(res.Map("approved")), Active: row.Bool(res.Map("active")), FirstName: row.Str(res.Map("first_name")), LastName: row.Str(res.Map("last_name")), Location: row.Str(res.Map("location")), } ch <- testimonial }
func (c CustomerUser) PopulateKey(row mysql.Row, res mysql.Result, ch chan APIKey) { keyType := APIKeyType{ ID: row.Str(res.Map("type_id")), Type: row.Str(res.Map("type")), DateAdded: row.Time(res.Map("typeDateAdded"), UTC), } key := APIKey{ ID: row.Str(res.Map("id")), UserID: row.Str(res.Map("user_id")), Key: row.Str(res.Map("api_key")), TypeID: keyType.ID, DateAdded: row.Time(res.Map("date_added"), UTC), KeyType: keyType, } ch <- key }
func PopulateMenu(row mysql.Row, res mysql.Result, ch chan Menu) { id := res.Map("menuID") name := res.Map("menu_name") isPrimary := res.Map("isPrimary") active := res.Map("active") displayName := res.Map("display_name") reqAuth := res.Map("requireAuthentication") showOnSitemap := res.Map("showOnSitemap") sort := res.Map("sort") var menu Menu menu = Menu{ ID: row.Int(id), Name: row.Str(name), Primary: row.Bool(isPrimary), Active: row.Bool(active), DisplayName: row.Str(displayName), RequireAuth: row.Bool(reqAuth), ShowOnSitemap: row.Bool(showOnSitemap), Sort: row.Int(sort), } ch <- menu }
func (c Comment) PopulateComment(row mysql.Row, res mysql.Result, ch chan Comment) { comment := Comment{ ID: row.Int(res.Map("commentID")), PostID: row.Int(res.Map("blogPostID")), Name: row.Str(res.Map("name")), Email: row.Str(res.Map("email")), Comment: row.Str(res.Map("comment_text")), Created: row.Time(res.Map("createdDate"), UTC), Approved: row.Bool(res.Map("approved")), Active: row.Bool(res.Map("active")), } ch <- comment }
func PopulateMenuItem(row mysql.Row, res mysql.Result, ch chan MenuItem) { var item MenuItem mcid := res.Map("menuContentID") menuID := res.Map("menuID") contentID := res.Map("contentID") menuSort := res.Map("menuSort") menuTitle := res.Map("menuTitle") menuLink := res.Map("menuLink") parentID := res.Map("parentID") linkTarget := res.Map("linkTarget") id := row.Int(mcid) if id > 0 { cid := row.Int(contentID) cch := make(chan Content) go PopulateContent(row, res, cch) item = MenuItem{ ID: id, MenuID: row.Int(menuID), ContentID: cid, Sort: row.Int(menuSort), Title: row.Str(menuTitle), Link: row.Str(menuLink), ParentID: row.Int(parentID), LinkTarget: row.Bool(linkTarget), Content: <-cch, } } ch <- item }