helpers.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "math/big"
  7. "reflect"
  8. "strings"
  9. "time"
  10. "gopkg.in/inf.v0"
  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:
  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 TypeDecimal:
  35. return reflect.TypeOf(*new(*inf.Dec))
  36. case TypeUUID, TypeTimeUUID:
  37. return reflect.TypeOf(*new(UUID))
  38. case TypeList, TypeSet:
  39. return reflect.SliceOf(goType(t.(CollectionType).Elem))
  40. case TypeMap:
  41. return reflect.MapOf(goType(t.(CollectionType).Key), goType(t.(CollectionType).Elem))
  42. case TypeVarint:
  43. return reflect.TypeOf(*new(*big.Int))
  44. case TypeTuple:
  45. // what can we do here? all there is to do is to make a list of interface{}
  46. tuple := t.(TupleTypeInfo)
  47. return reflect.TypeOf(make([]interface{}, len(tuple.Elems)))
  48. case TypeUDT:
  49. return reflect.TypeOf(make(map[string]interface{}))
  50. default:
  51. return nil
  52. }
  53. }
  54. func dereference(i interface{}) interface{} {
  55. return reflect.Indirect(reflect.ValueOf(i)).Interface()
  56. }
  57. func getApacheCassandraType(class string) Type {
  58. switch strings.TrimPrefix(class, apacheCassandraTypePrefix) {
  59. case "AsciiType":
  60. return TypeAscii
  61. case "LongType":
  62. return TypeBigInt
  63. case "BytesType":
  64. return TypeBlob
  65. case "BooleanType":
  66. return TypeBoolean
  67. case "CounterColumnType":
  68. return TypeCounter
  69. case "DecimalType":
  70. return TypeDecimal
  71. case "DoubleType":
  72. return TypeDouble
  73. case "FloatType":
  74. return TypeFloat
  75. case "Int32Type":
  76. return TypeInt
  77. case "DateType", "TimestampType":
  78. return TypeTimestamp
  79. case "UUIDType", "LexicalUUIDType":
  80. return TypeUUID
  81. case "UTF8Type":
  82. return TypeVarchar
  83. case "IntegerType":
  84. return TypeVarint
  85. case "TimeUUIDType":
  86. return TypeTimeUUID
  87. case "InetAddressType":
  88. return TypeInet
  89. case "MapType":
  90. return TypeMap
  91. case "ListType":
  92. return TypeList
  93. case "SetType":
  94. return TypeSet
  95. case "TupleType":
  96. return TypeTuple
  97. default:
  98. return TypeCustom
  99. }
  100. }
  101. func (r *RowData) rowMap(m map[string]interface{}) {
  102. for i, column := range r.Columns {
  103. val := dereference(r.Values[i])
  104. if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice {
  105. valCopy := reflect.MakeSlice(valVal.Type(), valVal.Len(), valVal.Cap())
  106. reflect.Copy(valCopy, valVal)
  107. m[column] = valCopy.Interface()
  108. } else {
  109. m[column] = val
  110. }
  111. }
  112. }
  113. func (iter *Iter) RowData() (RowData, error) {
  114. if iter.err != nil {
  115. return RowData{}, iter.err
  116. }
  117. columns := make([]string, 0)
  118. values := make([]interface{}, 0)
  119. for _, column := range iter.Columns() {
  120. val := column.TypeInfo.New()
  121. columns = append(columns, column.Name)
  122. values = append(values, val)
  123. }
  124. rowData := RowData{
  125. Columns: columns,
  126. Values: values,
  127. }
  128. return rowData, nil
  129. }
  130. // SliceMap is a helper function to make the API easier to use
  131. // returns the data from the query in the form of []map[string]interface{}
  132. func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {
  133. if iter.err != nil {
  134. return nil, iter.err
  135. }
  136. // Not checking for the error because we just did
  137. rowData, _ := iter.RowData()
  138. dataToReturn := make([]map[string]interface{}, 0)
  139. for iter.Scan(rowData.Values...) {
  140. m := make(map[string]interface{})
  141. rowData.rowMap(m)
  142. dataToReturn = append(dataToReturn, m)
  143. }
  144. if iter.err != nil {
  145. return nil, iter.err
  146. }
  147. return dataToReturn, nil
  148. }
  149. // MapScan takes a map[string]interface{} and populates it with a row
  150. // That is returned from cassandra.
  151. func (iter *Iter) MapScan(m map[string]interface{}) bool {
  152. if iter.err != nil {
  153. return false
  154. }
  155. // Not checking for the error because we just did
  156. rowData, _ := iter.RowData()
  157. for i, col := range rowData.Columns {
  158. if dest, ok := m[col]; ok {
  159. rowData.Values[i] = dest
  160. }
  161. }
  162. if iter.Scan(rowData.Values...) {
  163. rowData.rowMap(m)
  164. return true
  165. }
  166. return false
  167. }