Example #1
0
// Execute applies a dml sql select expression to the specified context's
//
//     writeContext in the case of sql query is similar to a recordset for selects,
//       or for delete, insert, update it is like the storage layer
//
func (m *SqlVm) ExecuteSelect(writeContext datasource.ContextWriter, readContext datasource.ContextReader) (err error) {
	//defer errRecover(&err)
	s := &State{
		ExprVm:        m,
		ContextReader: readContext,
	}
	s.rv = reflect.ValueOf(s)

	// Check and see if we are where Guarded
	if m.sel.Where != nil {
		//u.Debugf("Has a Where:  %v", m.Request.Where.Root.StringAST())
		whereValue, ok := s.Walk(m.sel.Where)
		if !ok {
			return SqlEvalError
		}
		switch whereVal := whereValue.(type) {
		case value.BoolValue:
			if whereVal == value.BoolValueFalse {
				u.Debugf("Filtering out")
				return nil
			}
		}
		//u.Debugf("Matched where: %v", whereValue)
	}
	for _, col := range m.sel.Columns {
		if col.Guard != nil {
			// TODO:  evaluate if guard
		}
		if col.Star {
			for k, v := range readContext.Row() {
				writeContext.Put(&expr.Column{As: k}, nil, v)
			}
		} else {
			//u.Debugf("tree.Root: as?%v %#v", col.As, col.Tree.Root)
			v, ok := s.Walk(col.Tree.Root)
			if ok {
				writeContext.Put(col, readContext, v)
			}
		}

	}

	//writeContext.Put()
	return
}
Example #2
0
func (m *SqlVm) ExecuteDelete(writeContext datasource.ContextWriter, readContext datasource.ContextReader) (err error) {
	//defer errRecover(&err)
	scanner, ok := readContext.(datasource.RowScanner)
	if !ok {
		return fmt.Errorf("Must implement RowScanner: %T", writeContext)
	}
	s := &State{
		ExprVm:        m,
		ContextReader: readContext,
	}
	s.rv = reflect.ValueOf(s)

	// Check and see if we are where Guarded
	if m.del.Where != nil {
		u.Debugf("Has a Where:  %v", m.del.Where.StringAST())

		for row := scanner.Next(); ; row = scanner.Next() {
			if row == nil {
				break
			}
			whereValue, ok := s.Walk(m.del.Where)
			u.Infof("where: %v %v", ok, whereValue)
			if !ok {
				continue
			}
			switch whereVal := whereValue.(type) {
			case value.BoolValue:
				if whereVal == value.BoolValueTrue {
					if err := writeContext.Delete(row); err != nil {
						u.Errorf("error %v", err)
					}
				}
			}
		}

	}
	// //u.Debugf("tree.Root: as?%v %#v", col.As, col.Tree.Root)
	// v, ok := s.Walk(col.Tree.Root)
	// if ok {
	// 	writeContext.Put(col, readContext, v)
	// }

	return
}