github.com/RevenueMonster/sqlike@v1.0.6/sql/expr/json.go (about) 1 package expr 2 3 import ( 4 "encoding/json" 5 6 "github.com/RevenueMonster/sqlike/sqlike/primitive" 7 ) 8 9 // JSON_QUOTE : 10 func JSON_QUOTE(doc interface{}) (f primitive.JSONFunc) { 11 f.Type = primitive.JSON_QUOTE 12 switch vi := doc.(type) { 13 case string: 14 f.Args = append(f.Args, wrapRaw(vi)) 15 default: 16 f.Args = append(f.Args, vi) 17 } 18 return 19 } 20 21 // JSON_UNQUOTE : 22 func JSON_UNQUOTE(doc interface{}) (f primitive.JSONFunc) { 23 f.Type = primitive.JSON_UNQUOTE 24 switch vi := doc.(type) { 25 case string: 26 f.Args = append(f.Args, wrapRaw(vi)) 27 default: 28 f.Args = append(f.Args, vi) 29 } 30 return 31 } 32 33 // JSON_EXTRACT : 34 func JSON_EXTRACT(doc interface{}, path string, otherPaths ...string) (f primitive.JSONFunc) { 35 f.Type = primitive.JSON_EXTRACT 36 f.Args = append(f.Args, doc) 37 for _, p := range append([]string{path}, otherPaths...) { 38 f.Args = append(f.Args, wrapRaw(p)) 39 } 40 return 41 } 42 43 // JSON_KEYS : 44 func JSON_KEYS(doc interface{}, paths ...string) (f primitive.JSONFunc) { 45 f.Type = primitive.JSON_KEYS 46 f.Args = append(f.Args, doc) 47 for _, p := range paths { 48 f.Args = append(f.Args, primitive.Value{ 49 Raw: p, 50 }) 51 } 52 return 53 } 54 55 // JSON_SET : 56 func JSON_SET(doc interface{}, path string, value interface{}, pathValues ...interface{}) (f primitive.JSONFunc) { 57 length := len(pathValues) 58 if length > 0 && length%2 != 0 { 59 panic("invalid argument len for JSON_SET(json_doc, path, val[, path, val] ...)") 60 } 61 f.Type = primitive.JSON_SET 62 f.Args = append(f.Args, doc, wrapRaw(path), value) 63 f.Args = append(f.Args, pathValues...) 64 return 65 } 66 67 // JSON_INSERT : 68 func JSON_INSERT(doc interface{}, path string, value interface{}, pathValues ...interface{}) (f primitive.JSONFunc) { 69 length := len(pathValues) 70 if length > 0 && length%2 != 0 { 71 panic("invalid argument len for JSON_INSERT(json_doc, path, val[, path, val] ...)") 72 } 73 f.Type = primitive.JSON_INSERT 74 f.Args = append(f.Args, doc, wrapRaw(path), value) 75 f.Args = append(f.Args, pathValues...) 76 return 77 } 78 79 // JSON_REMOVE : 80 func JSON_REMOVE(doc interface{}, path string, paths ...string) (f primitive.JSONFunc) { 81 f.Type = primitive.JSON_REMOVE 82 f.Args = append(f.Args, doc, wrapRaw(path)) 83 for _, p := range paths { 84 f.Args = append(f.Args, wrapRaw(p)) 85 } 86 return 87 } 88 89 // JSON_REPLACE : 90 func JSON_REPLACE(doc interface{}, path string, value interface{}, pathValues ...interface{}) (f primitive.JSONFunc) { 91 length := len(pathValues) 92 if length > 0 && length%2 != 0 { 93 panic("invalid argument len for JSON_REPLACE(json_doc, path, val[, path, val] ...)") 94 } 95 f.Type = primitive.JSON_REPLACE 96 f.Args = append(f.Args, doc, wrapRaw(path), value) 97 f.Args = append(f.Args, pathValues...) 98 return 99 } 100 101 // JSON_VALID : 102 func JSON_VALID(val interface{}) (f primitive.JSONFunc) { 103 f.Type = primitive.JSON_VALID 104 f.Args = append(f.Args, val) 105 return 106 } 107 108 // JSON_CONTAINS : 109 func JSON_CONTAINS(target, candidate interface{}, paths ...string) (f primitive.JSONFunc) { 110 f.Type = primitive.JSON_CONTAINS 111 for _, arg := range []interface{}{target, candidate} { 112 switch vi := arg.(type) { 113 case string, json.RawMessage: 114 f.Args = append(f.Args, primitive.Value{ 115 Raw: vi, 116 }) 117 case primitive.Column: 118 f.Args = append(f.Args, vi) 119 default: 120 f.Args = append(f.Args, vi) 121 } 122 } 123 if len(paths) > 0 { 124 for _, p := range paths { 125 f.Args = append(f.Args, p) 126 } 127 } 128 return 129 } 130 131 // JSON_TYPE : 132 func JSON_TYPE(val interface{}) (f primitive.JSONFunc) { 133 f.Type = primitive.JSON_TYPE 134 f.Args = append(f.Args, val) 135 return 136 } 137 138 // MemberOf : mysql 8.0.17 139 func MemberOf(val interface{}, arr interface{}) (f primitive.JSONFunc) { 140 f.Prefix = val 141 f.Type = primitive.MEMBER_OF 142 f.Args = append(f.Args, wrapColumn(arr)) 143 return 144 } 145 146 // JSONColumn : 147 func JSONColumn(column string, nested ...string) (c primitive.JSONColumn) { 148 c.Column = column 149 c.Nested = nested 150 c.UnquoteResult = false 151 return 152 } 153 154 func wrapJSONColumn(it interface{}) interface{} { 155 switch vi := it.(type) { 156 case primitive.Column: 157 return primitive.CastAs{ 158 Value: vi, 159 DataType: primitive.JSON, 160 } 161 case primitive.JSONFunc: 162 return vi 163 default: 164 return primitive.Value{Raw: vi} 165 } 166 }