コード例 #1
0
ファイル: sitecache.go プロジェクト: mrmorphic/goss
func (c *SiteCache) ReadRow(r *sql.Rows) error {
	cols, e := r.Columns()
	colCount := len(cols)

	var field []interface{}
	for i := 0; i < colCount; i++ {
		switch {
		case cols[i][:2] == "b:":
			field = append(field, new(sql.NullBool))
		case cols[i][:2] == "f:":
			field = append(field, new(sql.NullFloat64))
		case cols[i][:2] == "i:":
			field = append(field, new(sql.NullInt64))
		case cols[i][:2] == "s:":
			field = append(field, new(sql.NullString))
		case cols[i][:2] == "t:":
			field = append(field, new(time.Time))
		default:
			field = append(field, new(sql.NullString))
		}
	}

	e = r.Scan(field...)

	if e != nil {
		fmt.Printf("got an error though: %s\n", e)
		return e
	}

	m := &siteCacheEntry{}

	for i, c := range cols {
		v := flatten(field[i])
		data.Set(m, c, v)
	}

	c.raw = append(c.raw, m)

	return nil
}
コード例 #2
0
ファイル: dataquerysql.go プロジェクト: mrmorphic/goss
func DataObjectFromRow(r *sql.Rows) (interface{}, error) {
	// Get columns
	cols, e := r.Columns()
	colCount := len(cols)
	classNameCol := -1

	var field []interface{}
	for i := 0; i < colCount; i++ {
		if cols[i] == "ClassName" {
			classNameCol = i
		}
		switch {
		case cols[i][:2] == "b:":
			field = append(field, new(sql.NullBool))
		case cols[i][:2] == "f:":
			field = append(field, new(sql.NullFloat64))
		case cols[i][:2] == "i:":
			field = append(field, new(sql.NullInt64))
		case cols[i][:2] == "s:":
			field = append(field, new(sql.NullString))
		case cols[i][:2] == "t:":
			field = append(field, new(time.Time))
		default:
			field = append(field, new(sql.NullString))
		}
	}

	//fmt.Printf("cols are %s\n", cols)
	//fmt.Printf("there are %d columns\n", colCount)
	//fmt.Println("about to scan values")
	// get associated value
	e = r.Scan(field...)

	//fmt.Println("scanned fields")
	if e != nil {
		fmt.Printf("got an error though: %s\n", e)
		return nil, e
	}

	className := ""
	if classNameCol >= 0 {
		className = flatten(field[classNameCol]).(string)
	}

	m := GetModelInstance(className)
	// m := NewDataObjectMap()
	mdo, ok := m.(DataObject)

	for i, c := range cols {
		v := flatten(field[i])
		if ok {

			mdo.Set(c, v)
		} else {
			// doesn't implement DataObject, so may be a plain struct, so use data package.
			data.Set(m, c, v)
		}
	}

	return m, nil
}