helpers.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "reflect"
  7. "speter.net/go/exp/math/dec/inf"
  8. "strings"
  9. "time"
  10. )
  11. type RowData struct {
  12. Columns []string
  13. Values []interface{}
  14. }
  15. // New creates a pointer to an empty version of whatever type
  16. // is referenced by the TypeInfo receiver
  17. func (t *TypeInfo) New() interface{} {
  18. return reflect.New(goType(t)).Interface()
  19. }
  20. func goType(t *TypeInfo) reflect.Type {
  21. switch t.Type {
  22. case TypeVarchar, TypeAscii:
  23. return reflect.TypeOf(*new(string))
  24. case TypeBigInt, TypeCounter:
  25. return reflect.TypeOf(*new(int64))
  26. case TypeTimestamp:
  27. return reflect.TypeOf(*new(time.Time))
  28. case TypeBlob:
  29. return reflect.TypeOf(*new([]byte))
  30. case TypeBoolean:
  31. return reflect.TypeOf(*new(bool))
  32. case TypeFloat:
  33. return reflect.TypeOf(*new(float32))
  34. case TypeDouble:
  35. return reflect.TypeOf(*new(float64))
  36. case TypeInt:
  37. return reflect.TypeOf(*new(int))
  38. case TypeDecimal:
  39. return reflect.TypeOf(*new(*inf.Dec))
  40. case TypeUUID, TypeTimeUUID:
  41. return reflect.TypeOf(*new(UUID))
  42. case TypeList, TypeSet:
  43. return reflect.SliceOf(goType(t.Elem))
  44. case TypeMap:
  45. return reflect.MapOf(goType(t.Key), goType(t.Elem))
  46. default:
  47. return nil
  48. }
  49. }
  50. func dereference(i interface{}) interface{} {
  51. return reflect.Indirect(reflect.ValueOf(i)).Interface()
  52. }
  53. func getApacheCassandraType(class string) Type {
  54. if strings.HasPrefix(class, apacheCassandraTypePrefix) {
  55. switch strings.TrimPrefix(class, apacheCassandraTypePrefix) {
  56. case "AsciiType":
  57. return TypeAscii
  58. case "LongType":
  59. return TypeBigInt
  60. case "BytesType":
  61. return TypeBlob
  62. case "BooleanType":
  63. return TypeBoolean
  64. case "CounterColumnType":
  65. return TypeCounter
  66. case "DecimalType":
  67. return TypeDecimal
  68. case "DoubleType":
  69. return TypeDouble
  70. case "FloatType":
  71. return TypeFloat
  72. case "Int32Type":
  73. return TypeInt
  74. case "DateType":
  75. return TypeTimestamp
  76. case "UUIDType":
  77. return TypeUUID
  78. case "UTF8Type":
  79. return TypeVarchar
  80. case "IntegerType":
  81. return TypeVarint
  82. case "TimeUUIDType":
  83. return TypeTimeUUID
  84. case "InetAddressType":
  85. return TypeInet
  86. case "MapType":
  87. return TypeMap
  88. case "ListType":
  89. return TypeInet
  90. case "SetType":
  91. return TypeInet
  92. }
  93. }
  94. return TypeCustom
  95. }
  96. func (r *RowData) rowMap(m map[string]interface{}) {
  97. for i, column := range r.Columns {
  98. val := dereference(r.Values[i])
  99. if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice {
  100. valCopy := reflect.MakeSlice(valVal.Type(), valVal.Len(), valVal.Cap())
  101. reflect.Copy(valCopy, valVal)
  102. m[column] = valCopy.Interface()
  103. } else {
  104. m[column] = val
  105. }
  106. }
  107. }
  108. func (iter *Iter) RowData() (RowData, error) {
  109. if iter.err != nil {
  110. return RowData{}, iter.err
  111. }
  112. columns := make([]string, 0)
  113. values := make([]interface{}, 0)
  114. for _, column := range iter.Columns() {
  115. val := column.TypeInfo.New()
  116. columns = append(columns, column.Name)
  117. values = append(values, val)
  118. }
  119. rowData := RowData{
  120. Columns: columns,
  121. Values: values,
  122. }
  123. return rowData, nil
  124. }
  125. // SliceMap is a helper function to make the API easier to use
  126. // returns the data from the query in the form of []map[string]interface{}
  127. func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {
  128. if iter.err != nil {
  129. return nil, iter.err
  130. }
  131. // Not checking for the error because we just did
  132. rowData, _ := iter.RowData()
  133. dataToReturn := make([]map[string]interface{}, 0)
  134. for iter.Scan(rowData.Values...) {
  135. m := make(map[string]interface{})
  136. rowData.rowMap(m)
  137. dataToReturn = append(dataToReturn, m)
  138. }
  139. if iter.err != nil {
  140. return nil, iter.err
  141. }
  142. return dataToReturn, nil
  143. }
  144. // MapScan takes a map[string]interface{} and populates it with a row
  145. // That is returned from cassandra.
  146. func (iter *Iter) MapScan(m map[string]interface{}) bool {
  147. if iter.err != nil {
  148. return false
  149. }
  150. // Not checking for the error because we just did
  151. rowData, _ := iter.RowData()
  152. if iter.Scan(rowData.Values...) {
  153. rowData.rowMap(m)
  154. return true
  155. }
  156. return false
  157. }