예제 #1
0
파일: trigger.go 프로젝트: postfix/DBGo
// Executes triggers according to the table operation.
func ExecuteTrigger(db *database.Database, t *table.Table, r *ra.Result, operation string, row1, row2 map[string]string) int {
	for column, _ := range row1 {
		raCopy := r.Copy()
		// Filter according to the column name and operation type.
		raCopy.MultipleSelect(ra.Condition{Alias: "COLUMN", Filter: filter.Eq{}, Parameter: column},
			ra.Condition{Alias: "OP", Filter: filter.Eq{}, Parameter: operation})
		// For each trigger.
		for i := 0; i < raCopy.NumberOfRows(); i++ {
			row, status := raCopy.Read(i)
			if status != st.OK {
				return status
			}
			/*
				Call the trigger function. Parameters given are:
				reference to database
				reference to table
				column name
				extra parameters as stored in trigger lookup table
				row1
				row2

				When insert, row1 is the new row, row2 is nil.
				When update, row1 is the new row, row2 is the old row.
				When delete, row1 is the deleted row, row2 is nil.
			*/
			status = TriggerFuncTable()[row["FUNC"]].Execute(db, t, column, strings.Split(strings.TrimSpace(row["PARAM"]), ";"), row1, row2)
			if status != st.OK {
				return status
			}
		}
	}
	return st.OK
}
예제 #2
0
// Deletes rows in a table of RA result according to some select conditions.
// The RA result is made a copy before using select conditions.
func findAndDelete(t *table.Table, query *ra.Result, conditions ...ra.Condition) int {
	_, status := query.Copy().MultipleSelect(conditions...)
	if status != st.OK {
		return status
	}
	for _, i := range query.Tables[t.Name].RowNumbers {
		status = t.Delete(i)
		if status != st.OK {
			return status
		}
	}
	return t.Flush()
}