-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.go
More file actions
283 lines (274 loc) · 6.71 KB
/
Copy pathfunctions.go
File metadata and controls
283 lines (274 loc) · 6.71 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package where
import (
"strings"
)
const (
// FunctionType constants define categories of SQL functions.
FunctionTypeScalar FunctionType = "scalar"
FunctionTypeAggregate FunctionType = "aggregate"
FunctionTypeWindow FunctionType = "window"
FunctionTypeDate FunctionType = "date"
FunctionTypeString FunctionType = "string"
FunctionTypeMath FunctionType = "math"
FunctionTypeConversion FunctionType = "conversion"
)
type (
// FunctionType categorizes SQL functions by their purpose.
FunctionType string
// FunctionDef defines metadata for a SQL function including argument validation.
FunctionDef struct {
Name string
Type FunctionType
MinArgs int
MaxArgs int // -1 means unlimited
Description string
}
)
// StandardFunctions contains metadata for commonly supported SQL functions.
// This is optional documentation - all functions are supported by default regardless of this list.
var StandardFunctions = map[string]FunctionDef{
"LOWER": {
Name: "LOWER",
Type: FunctionTypeString,
MinArgs: 1,
MaxArgs: 1,
Description: "Converts string to lowercase",
},
"UPPER": {
Name: "UPPER",
Type: FunctionTypeString,
MinArgs: 1,
MaxArgs: 1,
Description: "Converts string to uppercase",
},
"LENGTH": {
Name: "LENGTH",
Type: FunctionTypeString,
MinArgs: 1,
MaxArgs: 1,
Description: "Returns length of string",
},
"TRIM": {
Name: "TRIM",
Type: FunctionTypeString,
MinArgs: 1,
MaxArgs: 1,
Description: "Removes leading and trailing whitespace",
},
"LTRIM": {
Name: "LTRIM",
Type: FunctionTypeString,
MinArgs: 1,
MaxArgs: 1,
Description: "Removes leading whitespace",
},
"RTRIM": {
Name: "RTRIM",
Type: FunctionTypeString,
MinArgs: 1,
MaxArgs: 1,
Description: "Removes trailing whitespace",
},
"SUBSTRING": {
Name: "SUBSTRING",
Type: FunctionTypeString,
MinArgs: 2,
MaxArgs: 3,
Description: "Extracts substring from string",
},
"CONCAT": {
Name: "CONCAT",
Type: FunctionTypeString,
MinArgs: 2,
MaxArgs: -1,
Description: "Concatenates strings",
},
"COALESCE": {
Name: "COALESCE",
Type: FunctionTypeScalar,
MinArgs: 2,
MaxArgs: -1,
Description: "Returns first non-null value",
},
"GREATEST": {
Name: "GREATEST",
Type: FunctionTypeScalar,
MinArgs: 2,
MaxArgs: -1,
Description: "Returns greatest value",
},
"LEAST": {
Name: "LEAST",
Type: FunctionTypeScalar,
MinArgs: 2,
MaxArgs: -1,
Description: "Returns smallest value",
},
"NOW": {
Name: "NOW",
Type: FunctionTypeDate,
MinArgs: 0,
MaxArgs: 0,
Description: "Returns current timestamp",
},
"CURRENT_DATE": {
Name: "CURRENT_DATE",
Type: FunctionTypeDate,
MinArgs: 0,
MaxArgs: 0,
Description: "Returns current date",
},
"CURRENT_TIME": {
Name: "CURRENT_TIME",
Type: FunctionTypeDate,
MinArgs: 0,
MaxArgs: 0,
Description: "Returns current time",
},
"CURRENT_TIMESTAMP": {
Name: "CURRENT_TIMESTAMP",
Type: FunctionTypeDate,
MinArgs: 0,
MaxArgs: 0,
Description: "Returns current timestamp",
},
"DATE": {
Name: "DATE",
Type: FunctionTypeDate,
MinArgs: 1,
MaxArgs: 1,
Description: "Extracts date from timestamp",
},
"TIME": {
Name: "TIME",
Type: FunctionTypeDate,
MinArgs: 1,
MaxArgs: 1,
Description: "Extracts time from timestamp",
},
"YEAR": {
Name: "YEAR",
Type: FunctionTypeDate,
MinArgs: 1,
MaxArgs: 1,
Description: "Extracts year from date",
},
"MONTH": {
Name: "MONTH",
Type: FunctionTypeDate,
MinArgs: 1,
MaxArgs: 1,
Description: "Extracts month from date",
},
"DAY": {
Name: "DAY",
Type: FunctionTypeDate,
MinArgs: 1,
MaxArgs: 1,
Description: "Extracts day from date",
},
"HOUR": {
Name: "HOUR",
Type: FunctionTypeDate,
MinArgs: 1,
MaxArgs: 1,
Description: "Extracts hour from time",
},
"MINUTE": {
Name: "MINUTE",
Type: FunctionTypeDate,
MinArgs: 1,
MaxArgs: 1,
Description: "Extracts minute from time",
},
"SECOND": {
Name: "SECOND",
Type: FunctionTypeDate,
MinArgs: 1,
MaxArgs: 1,
Description: "Extracts second from time",
},
"EXTRACT": {
Name: "EXTRACT",
Type: FunctionTypeDate,
MinArgs: 2,
MaxArgs: 2,
Description: "Extracts date part from date/time",
},
"IF": {
Name: "IF",
Type: FunctionTypeScalar,
MinArgs: 3,
MaxArgs: 3,
Description: "Conditional expression",
},
"CAST": {
Name: "CAST",
Type: FunctionTypeConversion,
MinArgs: 2,
MaxArgs: 2,
Description: "Type conversion",
},
"ABS": {
Name: "ABS",
Type: FunctionTypeMath,
MinArgs: 1,
MaxArgs: 1,
Description: "Absolute value",
},
"ROUND": {
Name: "ROUND",
Type: FunctionTypeMath,
MinArgs: 1,
MaxArgs: 2,
Description: "Rounds a number",
},
"FLOOR": {
Name: "FLOOR",
Type: FunctionTypeMath,
MinArgs: 1,
MaxArgs: 1,
Description: "Rounds down to integer",
},
"CEIL": {
Name: "CEIL",
Type: FunctionTypeMath,
MinArgs: 1,
MaxArgs: 1,
Description: "Rounds up to integer",
},
"SQRT": {
Name: "SQRT",
Type: FunctionTypeMath,
MinArgs: 1,
MaxArgs: 1,
Description: "Square root",
},
"POWER": {
Name: "POWER",
Type: FunctionTypeMath,
MinArgs: 2,
MaxArgs: 2,
Description: "Raises to power",
},
}
// GetFunctionDef retrieves the function definition for the given function name.
// Function names are case-insensitive. Returns false if the function is not in StandardFunctions.
// Note: All functions are supported by default - this is only for documentation purposes.
func GetFunctionDef(name string) (FunctionDef, bool) {
def, ok := StandardFunctions[strings.ToUpper(name)]
return def, ok
}
// ValidateFunctionArgs validates that the given argument count is valid for the function.
// Returns true if the argument count is within the allowed range for functions in StandardFunctions.
// Note: This is optional validation - all functions are supported by default regardless of arity.
func ValidateFunctionArgs(name string, argCount int) bool {
def, ok := GetFunctionDef(name)
if !ok {
return false
}
if def.MaxArgs == -1 {
return argCount >= def.MinArgs
}
return argCount >= def.MinArgs && argCount <= def.MaxArgs
}