ascii.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 sqlchemy
  15. import (
  16. "yunion.io/x/pkg/gotypes"
  17. "yunion.io/x/pkg/util/reflectutils"
  18. )
  19. func (c *STableField) IsAscii() bool {
  20. return c.spec.IsAscii()
  21. }
  22. func (c *STableField) IsText() bool {
  23. return c.spec.IsText()
  24. }
  25. func (c *STableField) IsSearchable() bool {
  26. return c.spec.IsSearchable()
  27. }
  28. func (c *STableField) GetWidth() int {
  29. return c.spec.GetWidth()
  30. }
  31. func getTableField(f IQueryField) *STableField {
  32. if gotypes.IsNil(f) {
  33. return nil
  34. }
  35. switch tf := f.(type) {
  36. case *STableField:
  37. return tf
  38. case *sQueryField:
  39. return getTableField(tf.from.Field(f.Name()))
  40. case *SSubQueryField:
  41. return getTableField(tf.field)
  42. default:
  43. return nil
  44. }
  45. }
  46. func IsLongFieldText(f IQueryField) bool {
  47. tf := getTableField(f)
  48. if tf != nil {
  49. return tf.IsText() && tf.IsSearchable() && tf.GetWidth() == 0
  50. }
  51. return false
  52. }
  53. func isFieldRequireAscii(f IQueryField) bool {
  54. tf := getTableField(f)
  55. if tf != nil {
  56. return tf.IsAscii()
  57. }
  58. return false
  59. }
  60. func isVariableAscii(v interface{}) bool {
  61. if gotypes.IsNil(v) {
  62. return true
  63. }
  64. switch v.(type) {
  65. case IQueryField, *SQuery, *SSubQuery:
  66. return true
  67. default:
  68. vals := reflectutils.ExpandInterface(v)
  69. for _, val := range vals {
  70. if strVal, ok := val.(string); ok && len(strVal) > 0 {
  71. if !isPrintableAsciiString(strVal) {
  72. return false
  73. }
  74. } else if strVal, ok := val.(*string); ok && !gotypes.IsNil(strVal) && len(*strVal) > 0 {
  75. if !isPrintableAsciiString(*strVal) {
  76. return false
  77. }
  78. }
  79. }
  80. }
  81. return true
  82. }
  83. func isPrintableAscii(b byte) bool {
  84. if b >= 32 && b <= 126 {
  85. return true
  86. }
  87. return false
  88. }
  89. func isPrintableAsciiString(str string) bool {
  90. for _, b := range []byte(str) {
  91. if !isPrintableAscii(b) {
  92. return false
  93. }
  94. }
  95. return true
  96. }