Пример #1
0
Файл: dao.go Проект: hyl87/dcat
// Find 查找单条数据
// colFieldMap 数据库表中列对应go中对象的字段
func (this *Dao) Find(colFieldMap map[string]interface{}, selectCol ...string) error {
	colNum := len(selectCol)
	if colNum == 0 || (colNum == 1 && selectCol[0] == "*") {
		selectCol = util.MapKeys(colFieldMap)
	}
	sort.Sort(sort.StringSlice(selectCol))
	this.selectCols = "`" + strings.Join(selectCol, "`,`") + "`"
	strSql := util.SelectSql(this)
	log.Println("Find sql:", strSql)
	err := this.ConnDb()
	if err != nil {
		return err
	}
	defer this.Close()
	row := this.QueryRow(strSql, this.whereVal...)
	scanInterface := make([]interface{}, 0, colNum)
	for _, column := range selectCol {
		scanInterface = append(scanInterface, colFieldMap[column])
	}
	err = row.Scan(scanInterface...)
	if err == sql.ErrNoRows {
		log.Println("Find", strSql, ":no result ret")
		return nil
	}
	return err
}
Пример #2
0
Файл: dao.go Проект: hyl87/dcat
// FindAll 查找多条数据
func (this *Dao) FindAll(selectCol ...string) (*sql.Rows, error) {
	sort.Sort(sort.StringSlice(selectCol))
	this.selectCols = "`" + strings.Join(selectCol, "`,`") + "`"
	strSql := util.SelectSql(this)
	fmt.Println("FindAll sql:", strSql)
	err := this.ConnDb()
	if err != nil {
		return nil, err
	}
	defer this.Close()
	return this.Query(strSql, this.whereVal...)
}