functions.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mysql
  15. import (
  16. "fmt"
  17. "yunion.io/x/sqlchemy"
  18. )
  19. // GROUP_CONCAT2 represents the SQL function GROUP_CONCAT
  20. func (mysql *SMySQLBackend) GROUP_CONCAT2(name string, sep string, field sqlchemy.IQueryField) sqlchemy.IQueryField {
  21. return sqlchemy.NewFunctionField(name, true, fmt.Sprintf("GROUP_CONCAT(%%s SEPARATOR '%s')", sep), field)
  22. }
  23. // cast field to string
  24. func (mysql *SMySQLBackend) CASTString(field sqlchemy.IQueryField, fieldname string) sqlchemy.IQueryField {
  25. return sqlchemy.NewFunctionField(fieldname, false, `CAST(%s AS CHAR)`, field)
  26. }
  27. // cast field to integer
  28. func (mysql *SMySQLBackend) CASTInt(field sqlchemy.IQueryField, fieldname string) sqlchemy.IQueryField {
  29. return sqlchemy.NewFunctionField(fieldname, false, `CAST(%s AS SIGNED)`, field)
  30. }
  31. // cast field to float
  32. func (mysql *SMySQLBackend) CASTFloat(field sqlchemy.IQueryField, fieldname string) sqlchemy.IQueryField {
  33. return sqlchemy.NewFunctionField(fieldname, false, `CAST(%s AS DECIMAL)`, field)
  34. }