sqlvalue.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package sqlite
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/spf13/cast"
  6. "github.com/wecisecode/util/msgpack"
  7. )
  8. func BlobMarshal(v interface{}) (s string) {
  9. bs, e := msgpack.Encode(v)
  10. if e != nil {
  11. t, e := cast.ToStringE(v)
  12. if e != nil {
  13. s = fmt.Sprintf("%#v", v)
  14. }
  15. s = t
  16. } else {
  17. s = string(bs)
  18. }
  19. return
  20. }
  21. func SliceTypeRecognize(tx []interface{}) (v interface{}, t string) {
  22. t = ""
  23. for _, a := range tx {
  24. switch a.(type) {
  25. case string:
  26. if t == "" {
  27. t = "string"
  28. }
  29. if t != "string" {
  30. t = ""
  31. break
  32. }
  33. case int:
  34. if t == "" {
  35. t = "int"
  36. }
  37. if t != "int" {
  38. t = ""
  39. break
  40. }
  41. }
  42. }
  43. switch t {
  44. case "string":
  45. tss := make([]string, len(tx))
  46. for i, a := range tx {
  47. tss[i] = cast.ToString(a)
  48. }
  49. v = tss
  50. case "int":
  51. tis := make([]int, len(tx))
  52. for i, a := range tx {
  53. tis[i] = cast.ToInt(a)
  54. }
  55. v = tis
  56. default:
  57. v = tx
  58. }
  59. return
  60. }
  61. func MapTypeRecognize(tx map[string]interface{}) (v interface{}, t string) {
  62. t = ""
  63. tvs := map[string]interface{}{}
  64. for k, a := range tx {
  65. switch ta := a.(type) {
  66. case []interface{}:
  67. av, at := SliceTypeRecognize(ta)
  68. if t == "" {
  69. t = "[]" + at
  70. }
  71. if t != "[]"+at {
  72. t = ""
  73. break
  74. }
  75. tvs[k] = av
  76. case []string:
  77. if t == "" {
  78. t = "[]string"
  79. }
  80. if t != "[]string" {
  81. t = ""
  82. break
  83. }
  84. tvs[k] = ta
  85. case []int:
  86. if t == "" {
  87. t = "[]int"
  88. }
  89. if t != "[]int" {
  90. t = ""
  91. break
  92. }
  93. tvs[k] = ta
  94. case string:
  95. if t == "" {
  96. t = "string"
  97. }
  98. if t != "string" {
  99. t = ""
  100. break
  101. }
  102. tvs[k] = ta
  103. case int:
  104. if t == "" {
  105. t = "int"
  106. }
  107. if t != "int" {
  108. t = ""
  109. break
  110. }
  111. tvs[k] = ta
  112. }
  113. }
  114. switch t {
  115. case "[]string":
  116. tss := make(map[string][]string, len(tx))
  117. for i, a := range tvs {
  118. tss[i] = cast.ToStringSlice(a)
  119. }
  120. v = tss
  121. case "[]int":
  122. tis := make(map[string][]int, len(tx))
  123. for i, a := range tvs {
  124. tis[i] = cast.ToIntSlice(a)
  125. }
  126. v = tis
  127. case "string":
  128. tss := make(map[string]string, len(tx))
  129. for i, a := range tvs {
  130. tss[i] = cast.ToString(a)
  131. }
  132. v = tss
  133. case "int":
  134. tis := make(map[string]int, len(tx))
  135. for i, a := range tvs {
  136. tis[i] = cast.ToInt(a)
  137. }
  138. v = tis
  139. default:
  140. v = tx
  141. }
  142. return
  143. }
  144. func MapsTypeRecognize(tx []map[string]interface{}) (v interface{}, t string) {
  145. t = ""
  146. tvs := []interface{}{}
  147. for _, x := range tx {
  148. av, at := MapTypeRecognize(x)
  149. if t == "" {
  150. t = at
  151. }
  152. if t != at {
  153. t = ""
  154. break
  155. }
  156. tvs = append(tvs, av)
  157. }
  158. switch t {
  159. case "[]string":
  160. tss := make([]map[string][]string, len(tx))
  161. for i, a := range tvs {
  162. tss[i] = a.(map[string][]string)
  163. }
  164. v = tss
  165. case "[]int":
  166. tis := make([]map[string][]int, len(tx))
  167. for i, a := range tvs {
  168. tis[i] = a.(map[string][]int)
  169. }
  170. v = tis
  171. case "string":
  172. tss := make([]map[string]string, len(tx))
  173. for i, a := range tvs {
  174. tss[i] = a.(map[string]string)
  175. }
  176. v = tss
  177. case "int":
  178. tis := make([]map[string]int, len(tx))
  179. for i, a := range tvs {
  180. tis[i] = a.(map[string]int)
  181. }
  182. v = tis
  183. default:
  184. v = tx
  185. }
  186. return
  187. }
  188. func BlobRecognize(x interface{}) (v interface{}, t string) {
  189. v = x
  190. switch tx := x.(type) {
  191. case []map[string]interface{}:
  192. v, t = MapsTypeRecognize(tx)
  193. case map[string]interface{}:
  194. v, t = MapTypeRecognize(tx)
  195. case []interface{}:
  196. v, t = SliceTypeRecognize(tx)
  197. case *interface{}:
  198. v, t = BlobRecognize(*tx)
  199. default:
  200. v = tx
  201. }
  202. return
  203. }
  204. func BlobUnmarshal(s string, x interface{}) (v interface{}) {
  205. e := msgpack.Decode([]byte(s), x)
  206. if e != nil {
  207. return s
  208. }
  209. v = x
  210. return
  211. }
  212. // 从 sqlite 读出的数据 到 go 语言中
  213. func SQLValueDecode(ftype_sampledata, value interface{}) (v interface{}) {
  214. v = value
  215. switch ftype := ftype_sampledata.(type) {
  216. case string:
  217. switch ftype {
  218. case "BOOL", "BOOLEAN":
  219. v = cast.ToBool(v)
  220. case "BLOB":
  221. s := cast.ToString(v)
  222. var x interface{}
  223. v = BlobUnmarshal(s, &x)
  224. v, _ = BlobRecognize(v)
  225. case "INT", "INTEGER":
  226. switch tv := v.(type) {
  227. case int8:
  228. v = int(tv)
  229. case int16:
  230. v = int(tv)
  231. case int32:
  232. v = int(tv)
  233. case int64:
  234. v = int(tv)
  235. case uint:
  236. v = int(tv)
  237. case uint8:
  238. v = int(tv)
  239. case uint16:
  240. v = int(tv)
  241. case uint32:
  242. v = int(tv)
  243. case uint64:
  244. v = int(tv)
  245. }
  246. case "DATE":
  247. // 兼容cassandra输出
  248. v = cast.ToTime(v)
  249. case "TIME", "DATETIME", "TIMESTAMP":
  250. v = NSTimeStampValue(v)
  251. }
  252. return
  253. }
  254. s := cast.ToString(v)
  255. v = BlobUnmarshal(s, ftype_sampledata)
  256. return
  257. }
  258. // 从 go 语言数据 到 sqlite 数据库中
  259. func SQLValueEncode(ftype string, v interface{}) interface{} {
  260. if v == nil {
  261. return nil
  262. }
  263. switch strings.ToLower(ftype) {
  264. case "int", "integer":
  265. return cast.ToInt(v)
  266. case "long", "bigint", "int64":
  267. return cast.ToInt64(v)
  268. case "int32":
  269. return cast.ToInt32(v)
  270. case "enum", "smallint", "int16":
  271. return cast.ToInt16(v)
  272. case "double", "float64":
  273. return cast.ToFloat64(v)
  274. case "float", "float32":
  275. return cast.ToFloat32(v)
  276. case "bool", "boolean":
  277. return cast.ToBool(v)
  278. case "string", "text", "varchar", "clob":
  279. return cast.ToString(v)
  280. case "date":
  281. return MSTimeStampValue(v).Format("2006-01-02")
  282. case "time", "datetime", "timestamp":
  283. return MSTimeStampValue(v).UnixNano()
  284. case "relation":
  285. sv := BlobMarshal(v)
  286. if sv == "{}" {
  287. // 兼容cassandra输入
  288. sv = "null"
  289. }
  290. return sv
  291. case "blob", "map", "list", "set", "bucket":
  292. fallthrough
  293. default:
  294. return BlobMarshal(v)
  295. }
  296. }