@@ -27,6 +27,9 @@ const (
27
27
logicalOperatorNot = "not"
28
28
logicalOperatorAnd = "and"
29
29
logicalOperatorOr = "or"
30
+
31
+ doubleColonCastingOperator = "::" // NOTE: this is a PostgreSQL specific operator
32
+ singleColonRenameOperator = ":"
30
33
)
31
34
32
35
type CompiledQuery struct {
@@ -334,14 +337,61 @@ func (c *queryCompiler) CompileAsDelete(table string) (CompiledQuery, error) {
334
337
return rv , nil
335
338
}
336
339
340
+ func getSelectResultColumn (columnName string ) string {
341
+ // newName:name::text
342
+ // => cast(name as text) as newName
343
+
344
+ var (
345
+ columnType string
346
+ targetColumnName string
347
+ )
348
+
349
+ if strings .Contains (columnName , doubleColonCastingOperator ) {
350
+ ps := strings .SplitN (columnName , doubleColonCastingOperator , 2 )
351
+ if len (ps ) == 2 {
352
+ // is a valid casting call
353
+ columnName = ps [0 ]
354
+ columnType = ps [1 ]
355
+ }
356
+ // NOTE: if it's not a valid casting, since the columnType is still empty,
357
+ // no casting will be applied
358
+ }
359
+
360
+ if strings .Contains (columnName , singleColonRenameOperator ) {
361
+ ps := strings .SplitN (columnName , singleColonRenameOperator , 2 )
362
+ if len (ps ) == 2 {
363
+ // is a valid renaming call
364
+ targetColumnName = ps [0 ]
365
+ columnName = ps [1 ]
366
+ }
367
+ // NOTE: if it's not a valid renaming, since the targetColumnName is still empty,
368
+ // no renaming will be applied
369
+ }
370
+
371
+ if columnType == "" {
372
+ if targetColumnName == "" {
373
+ return columnName
374
+ }
375
+ return fmt .Sprintf ("%s as %s" , columnName , targetColumnName )
376
+ } else {
377
+ if targetColumnName == "" {
378
+ targetColumnName = columnName
379
+ }
380
+ return fmt .Sprintf ("cast(%s as %s) as %s" , columnName , columnType , targetColumnName )
381
+ }
382
+ }
383
+
337
384
func (c * queryCompiler ) getSelectResultColumns () []string {
338
385
v := c .getQueryParameter (queryParameterNameSelect )
339
386
if v == "" {
340
387
return []string {"*" }
341
388
}
342
389
343
390
vs := strings .Split (v , "," )
344
- // TOOD: support renaming, casting
391
+ // TOOD: support renaming
392
+ for idx := range vs {
393
+ vs [idx ] = getSelectResultColumn (vs [idx ])
394
+ }
345
395
346
396
return vs
347
397
}
0 commit comments