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