示例#1
0
func (r *Repository) FindOneBy(db *mysql551.Mysql, mInfo *model551.ModelInformation, param map[string]interface{}) interface{} {

	sql := mInfo.SqlInformation.Select
	var values []interface{}
	for key, value := range param {
		sql = string551.Join(sql, " AND `"+key+"` = ?")
		values = append(values, value)
	}

	rows := db.Query(sql, values...)
	defer rows.Close()

	var model interface{} = nil

	for rows.Next() {
		if model != nil {
			panic(errors.New("Extraction result there is more than one."))
		}
		model = mInfo.NewFuncPointer()
		modelScan, ok := model.(model551.ScanInterface)
		if !ok {
			panic(errors.New("Not found: 'T.Scan(rows *sql.rows) error' method"))
		}

		err := modelScan.Scan(rows)
		if err != nil {
			panic(err)
		}

	}

	return model

}
示例#2
0
func (r *Repository) Find(db *mysql551.Mysql, mInfo *model551.ModelInformation, id int64) interface{} {

	sql := mInfo.SqlInformation.Select + " AND `" + mInfo.TableInformation.PrimaryKey + "` = ?"
	param := []interface{}{id}

	rows := db.Query(sql, param...)
	defer rows.Close()
	if !rows.Next() {
		return nil
	}

	model := mInfo.NewFuncPointer()
	modelScan, ok := model.(model551.ScanInterface)
	if !ok {
		panic(errors.New("Not found: 'T.Scan(rows *sql.rows) error' method"))
	}

	err := modelScan.Scan(rows)
	if err != nil {
		panic(err)
	}

	return model

}