summaryrefslogtreecommitdiff
path: root/internal/mapr/whereclause.go
blob: 617aa7933a7ed73333ed7b6d8269eaa7cdf1f38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package mapr

import (
	"strconv"

	"github.com/mimecast/dtail/internal/io/dlog"
)

// WhereClause interprets the where clause of the mapreduce query.
func (q *Query) WhereClause(fields map[string]string) bool {
	for _, wc := range q.Where {
		if wc.Operation > FloatOperation {
			if !whereClauseFloatValues(fields, wc) {
				return false
			}
			continue
		}
		if !whereClauseStringValues(fields, wc) {
			return false
		}
	}
	return true
}

func whereClauseFloatValues(fields map[string]string, wc whereCondition) bool {
	var lValue, rValue float64
	var ok bool

	if lValue, ok = whereClauseFloatValue(fields, wc.lString, wc.lFloat, wc.lType); !ok {
		return false
	}
	if rValue, ok = whereClauseFloatValue(fields, wc.rString, wc.rFloat, wc.rType); !ok {
		return false
	}
	if ok = wc.floatClause(lValue, rValue); !ok {
		return false
	}

	return true
}

func whereClauseFloatValue(fields map[string]string, str string, float float64,
	t fieldType) (float64, bool) {

	switch t {
	case Float:
		return float, true
	case Field:
		value, ok := fields[str]
		if !ok {
			return 0, false
		}
		f, err := strconv.ParseFloat(value, 64)
		if err != nil {
			return 0, false
		}
		return f, true
	default:
		dlog.Common.Error("Unexpected argument in 'where' clause", str, float, t)
		return 0, false
	}
}

func whereClauseStringValues(fields map[string]string, wc whereCondition) bool {
	var lValue, rValue string
	var ok bool

	if lValue, ok = whereClauseStringValue(fields, wc.lString, wc.lType); !ok {
		return false
	}
	if rValue, ok = whereClauseStringValue(fields, wc.rString, wc.rType); !ok {
		return false
	}
	if ok = wc.stringClause(lValue, rValue); !ok {
		return false
	}

	return true
}

func whereClauseStringValue(fields map[string]string, str string,
	t fieldType) (string, bool) {

	switch t {
	case Field:
		value, ok := fields[str]
		if !ok {
			return str, false
		}
		return value, true
	case String:
		return str, true
	default:
		dlog.Common.Error("Unexpected argument in 'where' clause", str, t)
		return str, false
	}
}