helpers.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "fmt"
  7. "math/big"
  8. "reflect"
  9. "strings"
  10. "time"
  11. )
  12. type RowData struct {
  13. Columns []string
  14. Values []interface{}
  15. }
  16. func goType(t TypeInfo) reflect.Type {
  17. switch t.Type() {
  18. case TypeVarchar, TypeAscii, TypeInet, TypeText:
  19. return reflect.TypeOf(*new(string))
  20. case TypeBigInt, TypeCounter:
  21. return reflect.TypeOf(*new(int64))
  22. case TypeTimestamp:
  23. return reflect.TypeOf(*new(time.Time))
  24. case TypeBlob:
  25. return reflect.TypeOf(*new([]byte))
  26. case TypeBoolean:
  27. return reflect.TypeOf(*new(bool))
  28. case TypeFloat:
  29. return reflect.TypeOf(*new(float32))
  30. case TypeDouble:
  31. return reflect.TypeOf(*new(float64))
  32. case TypeInt:
  33. return reflect.TypeOf(*new(int))
  34. case TypeSmallInt:
  35. return reflect.TypeOf(*new(int16))
  36. case TypeDecimal:
  37. return reflect.TypeOf(*new(*inf.Dec))
  38. case TypeUUID, TypeTimeUUID:
  39. return reflect.TypeOf(*new(UUID))
  40. case TypeList, TypeSet:
  41. return reflect.SliceOf(goType(t.(CollectionType).Elem))
  42. case TypeMap:
  43. return reflect.MapOf(goType(t.(CollectionType).Key), goType(t.(CollectionType).Elem))
  44. case TypeVarint:
  45. return reflect.TypeOf(*new(*big.Int))
  46. case TypeTuple:
  47. // what can we do here? all there is to do is to make a list of interface{}
  48. tuple := t.(TupleTypeInfo)
  49. return reflect.TypeOf(make([]interface{}, len(tuple.Elems)))
  50. case TypeUDT:
  51. return reflect.TypeOf(make(map[string]interface{}))
  52. default:
  53. return nil
  54. }
  55. }
  56. func dereference(i interface{}) interface{} {
  57. return reflect.Indirect(reflect.ValueOf(i)).Interface()
  58. }
  59. func getCassandraType(name string) Type {
  60. switch name {
  61. case "ascii":
  62. return TypeAscii
  63. case "bigint":
  64. return TypeBigInt
  65. case "blob":
  66. return TypeBlob
  67. case "boolean":
  68. return TypeBoolean
  69. case "counter":
  70. return TypeCounter
  71. case "decimal":
  72. return TypeDecimal
  73. case "double":
  74. return TypeDouble
  75. case "float":
  76. return TypeFloat
  77. case "int":
  78. return TypeInt
  79. case "timestamp":
  80. return TypeTimestamp
  81. case "uuid":
  82. return TypeUUID
  83. case "varchar", "text":
  84. return TypeVarchar
  85. case "varint":
  86. return TypeVarint
  87. case "timeuuid":
  88. return TypeTimeUUID
  89. case "inet":
  90. return TypeInet
  91. case "MapType":
  92. return TypeMap
  93. case "ListType":
  94. return TypeList
  95. case "SetType":
  96. return TypeSet
  97. case "TupleType":
  98. return TypeTuple
  99. default:
  100. if strings.HasPrefix(name, "set") {
  101. return TypeSet
  102. } else if strings.HasPrefix(name, "list") {
  103. return TypeList
  104. } else if strings.HasPrefix(name, "map") {
  105. return TypeMap
  106. } else if strings.HasPrefix(name, "tuple") {
  107. return TypeTuple
  108. }
  109. return TypeCustom
  110. }
  111. }
  112. func getApacheCassandraType(class string) Type {
  113. switch strings.TrimPrefix(class, apacheCassandraTypePrefix) {
  114. case "AsciiType":
  115. return TypeAscii
  116. case "LongType":
  117. return TypeBigInt
  118. case "BytesType":
  119. return TypeBlob
  120. case "BooleanType":
  121. return TypeBoolean
  122. case "CounterColumnType":
  123. return TypeCounter
  124. case "DecimalType":
  125. return TypeDecimal
  126. case "DoubleType":
  127. return TypeDouble
  128. case "FloatType":
  129. return TypeFloat
  130. case "Int32Type":
  131. return TypeInt
  132. case "ShortType":
  133. return TypeSmallInt
  134. case "ByteType":
  135. return TypeTinyInt
  136. case "DateType", "TimestampType":
  137. return TypeTimestamp
  138. case "UUIDType", "LexicalUUIDType":
  139. return TypeUUID
  140. case "UTF8Type":
  141. return TypeVarchar
  142. case "IntegerType":
  143. return TypeVarint
  144. case "TimeUUIDType":
  145. return TypeTimeUUID
  146. case "InetAddressType":
  147. return TypeInet
  148. case "MapType":
  149. return TypeMap
  150. case "ListType":
  151. return TypeList
  152. case "SetType":
  153. return TypeSet
  154. case "TupleType":
  155. return TypeTuple
  156. default:
  157. return TypeCustom
  158. }
  159. }
  160. func typeCanBeNull(typ TypeInfo) bool {
  161. switch typ.(type) {
  162. case CollectionType, UDTTypeInfo, TupleTypeInfo:
  163. return false
  164. }
  165. return true
  166. }
  167. func (r *RowData) rowMap(m map[string]interface{}) {
  168. for i, column := range r.Columns {
  169. val := dereference(r.Values[i])
  170. if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice {
  171. valCopy := reflect.MakeSlice(valVal.Type(), valVal.Len(), valVal.Cap())
  172. reflect.Copy(valCopy, valVal)
  173. m[column] = valCopy.Interface()
  174. } else {
  175. m[column] = val
  176. }
  177. }
  178. }
  179. // TupeColumnName will return the column name of a tuple value in a column named
  180. // c at index n. It should be used if a specific element within a tuple is needed
  181. // to be extracted from a map returned from SliceMap or MapScan.
  182. func TupleColumnName(c string, n int) string {
  183. return fmt.Sprintf("%s[%d]", c, n)
  184. }
  185. func (iter *Iter) RowData() (RowData, error) {
  186. if iter.err != nil {
  187. return RowData{}, iter.err
  188. }
  189. columns := make([]string, 0)
  190. values := make([]interface{}, 0)
  191. for _, column := range iter.Columns() {
  192. switch c := column.TypeInfo.(type) {
  193. case TupleTypeInfo:
  194. for i, elem := range c.Elems {
  195. columns = append(columns, TupleColumnName(column.Name, i))
  196. values = append(values, elem.New())
  197. }
  198. default:
  199. val := column.TypeInfo.New()
  200. columns = append(columns, column.Name)
  201. values = append(values, val)
  202. }
  203. }
  204. rowData := RowData{
  205. Columns: columns,
  206. Values: values,
  207. }
  208. return rowData, nil
  209. }
  210. // SliceMap is a helper function to make the API easier to use
  211. // returns the data from the query in the form of []map[string]interface{}
  212. func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {
  213. if iter.err != nil {
  214. return nil, iter.err
  215. }
  216. // Not checking for the error because we just did
  217. rowData, _ := iter.RowData()
  218. dataToReturn := make([]map[string]interface{}, 0)
  219. for iter.Scan(rowData.Values...) {
  220. m := make(map[string]interface{})
  221. rowData.rowMap(m)
  222. dataToReturn = append(dataToReturn, m)
  223. }
  224. if iter.err != nil {
  225. return nil, iter.err
  226. }
  227. return dataToReturn, nil
  228. }
  229. // MapScan takes a map[string]interface{} and populates it with a row
  230. // that is returned from cassandra.
  231. func (iter *Iter) MapScan(m map[string]interface{}) bool {
  232. if iter.err != nil {
  233. return false
  234. }
  235. // Not checking for the error because we just did
  236. rowData, _ := iter.RowData()
  237. for i, col := range rowData.Columns {
  238. if dest, ok := m[col]; ok {
  239. rowData.Values[i] = dest
  240. }
  241. }
  242. if iter.Scan(rowData.Values...) {
  243. rowData.rowMap(m)
  244. return true
  245. }
  246. return false
  247. }
  248. func copyBytes(p []byte) []byte {
  249. b := make([]byte, len(p))
  250. copy(b, p)
  251. return b
  252. }