Example #1
0
func (ar *ActiveRecord) Delete(output interface{}) (int64, error) {
	defer ar.Init()
	ar.ScanPK(output)

	st := utils.Struct{output}
	results := st.StructToSnakeKeyMap()
	if ar.TableName == "" {
		ar.TableName = ar.getTableName(output)
	}

	id := results[strings.ToLower(ar.PrimaryKey)]
	condition := fmt.Sprintf("%v%v%v='%v'", ar.QuoteIdentifier, strings.ToLower(ar.PrimaryKey), ar.QuoteIdentifier, id)
	statement := fmt.Sprintf("DELETE FROM %v%v%v WHERE %v",
		ar.QuoteIdentifier,
		ar.TableName,
		ar.QuoteIdentifier,
		condition)
	if OnDebug {
		fmt.Println(statement)
		fmt.Println(ar)
	}

	res, err := ar.Exec(statement)
	if err != nil {
		return -1, err
	}
	Affectid, err := res.RowsAffected()

	if err != nil {
		return -1, err
	}

	return Affectid, nil
}
Example #2
0
func (ar *ActiveRecord) FindAll(rowsSlicePtr interface{}) error {
	ar.ScanPK(rowsSlicePtr)
	sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
	if sliceValue.Kind() != reflect.Slice {
		return errors.New("needs a pointer to a slice")
	}

	sliceElementType := sliceValue.Type().Elem()
	st := utils.Struct{reflect.New(sliceElementType).Interface()}
	var keys []string
	results := st.StructToSnakeKeyMap()
	if ar.TableName == "" {
		ar.TableName = ar.getTableName(rowsSlicePtr)
	}

	for key, _ := range results {
		keys = append(keys, key)
	}

	ar.ColumnStr = strings.Join(keys, ", ")
	resultsSlice, err := ar.FindMap()
	if err != nil {
		return err
	}

	for _, results := range resultsSlice {
		newValue := reflect.New(sliceElementType)
		utils.M(results).MapToStruct(newValue.Interface())
		sliceValue.Set(reflect.Append(sliceValue, reflect.Indirect(reflect.ValueOf(newValue.Interface()))))
	}

	return nil
}
Example #3
0
func (ar *ActiveRecord) Find(output interface{}) error {
	st := utils.Struct{output}
	ar.ScanPK(output)
	var keys []string
	results := st.StructToSnakeKeyMap()
	if ar.TableName == "" {
		ar.TableName = ar.getTableName(output)
	}

	for key, _ := range results {
		keys = append(keys, key)
	}

	ar.ColumnStr = strings.Join(keys, ", ")
	ar.Limit(1)
	resultsSlice, err := ar.FindMap()
	if err != nil {
		return err
	}

	if len(resultsSlice) == 0 {
		return nil
	} else if len(resultsSlice) == 1 {
		results := resultsSlice[0]
		utils.M(results).MapToStruct(output)
	} else {
		return errors.New("More Then One Records")
	}

	return nil
}
Example #4
0
//if the struct has PrimaryKey == 0 insert else update
func (ar *ActiveRecord) Save(output interface{}) (int64, error) {
	ar.ScanPK(output)
	st := utils.Struct{output}
	results := st.StructToSnakeKeyMap()
	if ar.TableName == "" {
		ar.TableName = ar.getTableName(output)
	}

	id := results[strings.ToLower(ar.PrimaryKey)]
	var opRes int64
	delete(results, strings.ToLower(ar.PrimaryKey))
	if reflect.ValueOf(id).Int() == 0 {
		structPtr := reflect.ValueOf(output)
		structVal := structPtr.Elem()
		structField := structVal.FieldByName(ar.PrimaryKey)
		id, err := ar.Insert(results)
		if err != nil {
			return 0, err
		}

		opRes = id
		structField.Set(reflect.ValueOf(id))

		return opRes, nil
	} else {
		var condition string
		if ar.ParamIdentifier == "pg" {
			condition = fmt.Sprintf("%v%v%v=$%v", ar.QuoteIdentifier, strings.ToLower(ar.PrimaryKey), ar.QuoteIdentifier, ar.ParamIteration)
		} else {
			condition = fmt.Sprintf("%v%v%v=?", ar.QuoteIdentifier, ar.PrimaryKey, ar.QuoteIdentifier)
		}

		ar.Where(condition, id)
		opRes, err := ar.Update(results)
		if err != nil {
			return opRes, err
		}
	}

	return opRes, nil
}