Private/ConvertTo-QueryXML.ps1

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<#
 
    .COPYRIGHT
    Copyright (c) ECIT Solutions AS. All rights reserved. Licensed under the MIT license.
    See https://github.com/officecenter/OCH-Public/blob/master/LICENSE for license information.
 
#>


function ConvertTo-QueryXML {
     <#
      .SYNOPSIS
 
      .DESCRIPTION
 
      .INPUTS
 
      .OUTPUTS
 
      .EXAMPLE
 
      .NOTES
      NAME:
      .LINK
 
  #>

    [cmdletbinding()]
    param(    
        [Parameter(
            Mandatory = $true,
            ValueFromRemainingArguments = $true
        )]
        [string[]]
        $QueryText,
        
        [switch]
        $QuerystringOnly = $false

    )
    

    # List of allowed operator in QueryXML
    $Operator = @{
        '-and'   = 'and'
        '-or'    = 'or'
        '-begin' = 'begin'
        '-end'   = 'end'
    }

    # List of all allowed condition in QueryXML
    $ConditionOperator = @{
        '-eq'         = 'Equals'
        '-ne'         = 'NotEqual'
        '-gt'         = 'GreaterThan'
        '-lt'         = 'LessThan'
        '-ge'         = 'GreaterThanorEquals'
        '-le'         = 'LessThanOrEquals'
        '-beginswith' = 'BeginsWith'
        '-endswith'   = 'EndsWith'
        '-contains'   = 'Contains'
        '-isnotnull'  = 'IsNotNull'
        '-isnull'     = 'IsNull'
        '-isthisday'  = 'IsThisDay'
        '-like'       = 'Like'
        '-notlike'    = 'NotLike'
        '-soundslike' = 'SoundsLike'
    }

    $NoValueNeeded = @('-isnotnull', '-isnull')
  
    $UDF = $false

    # Create an XML document object. Only used to create XML elements.
    $xml = New-Object -TypeName XML
    
    # Create base element and add a single Entity definition to it.
    $queryxml = $xml.CreateElement('queryxml')
    $entityxml = $xml.CreateElement('entity')
    $null = $queryxml.AppendChild($entityxml)

    # Entity is the first element of the querytext
    $entityxml.InnerText = $QueryText[0]

    # Create an XML element for the query tag.
    # It will contain all condition.
    $Query = $xml.CreateElement('query')
    $null = $queryxml.AppendChild($Query)

    # Set generic pointer $Node to the query tag
    $Node = $Query

    # Create an index pointer that starts on the second element
    # of the querytext array
    for ($i = 1; $i -lt $QueryText.Count; $i++) {
        switch ($QueryText[$i]) {
            # Check for operator
            { $Operator.Keys -contains $_ } {
                # Element is an operator. Add a condition tag with
                # attribute 'operator' set to the value of element
                $Condition = $xml.CreateElement('condition')
                if ($_ -eq '-begin') {
                    # Add nested condition
                    $null = $Node.AppendChild($Condition)
                    $Node = $Condition
                    $Condition = $xml.CreateElement('condition')
                }
                if ('-or', '-and' -contains $_)
                { $Condition.SetAttribute('operator', $Operator[$_]) }
                   
                # Append condition to current $Node
                $null = $Node.AppendChild($Condition)

                # Set condition tag as current $Node. Next field tag
                # should be nested inside the condition tag.
                $Node = $Condition
                Break
            }
            # End a nested condition
            '-end' {
                $Node = $Node.ParentNode
                Break
            }
            # Next value is an UDF name
            '-udf' {
                $UDF = $true
                Break
            }
            # Check for a condition
            { $ConditionOperator.Keys -contains $_ } {
                # Element is a condition. Add an expression tag with
                # attribute 'op' set to the value of element
                $Expression = $xml.CreateElement('expression')
                $Expression.SetAttribute('op', $ConditionOperator[$_])

                # Append condition to current $Node
                $null = $Node.AppendChild($Expression)

                # Not all condition need a value.
                if ($NoValueNeeded -notcontains $_) {
                    # Increase pointer and add next element as
                    # Value to expression
                    $i++
                    $Expression.InnerText = $QueryText[$i]
                }

                # An expression closes a field tag. The next
                # element refers to the next level up.
                $Node = $Node.ParentNode

                # If the parentnode is a conditiontag we need
                # to go one more step up
                if ($Node.Name -eq 'condition')
                { $Node = $Node.ParentNode }
                Break
            }
            # Everything that aren't an operator or a condition is treated
            # as a field.
            default {
                # Create a field tag, fill it with element
                # and add it to current Node
                $field = $xml.CreateElement('field')
                $field.InnerText = $QueryText[$i]

                if ($UDF) {
                    $field.SetAttribute('udf', 'true')
                    $UDF = $false
                }
                $null = $Node.AppendChild($field)

                # The field tag is now the current Node
                $Node = $field
            }
        }
    }
    

    Return $($queryxml.OuterXml)
}