helpers.go 4.2 KB

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