helpers.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. m[column] = dereference(r.Values[i])
  99. }
  100. }
  101. func (iter *Iter) RowData() (RowData, error) {
  102. if iter.err != nil {
  103. return RowData{}, iter.err
  104. }
  105. columns := make([]string, 0)
  106. values := make([]interface{}, 0)
  107. for _, column := range iter.Columns() {
  108. val := column.TypeInfo.New()
  109. columns = append(columns, column.Name)
  110. values = append(values, val)
  111. }
  112. rowData := RowData{
  113. Columns: columns,
  114. Values: values,
  115. }
  116. return rowData, nil
  117. }
  118. // SliceMap is a helper function to make the API easier to use
  119. // returns the data from the query in the form of []map[string]interface{}
  120. func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {
  121. if iter.err != nil {
  122. return nil, iter.err
  123. }
  124. // Not checking for the error because we just did
  125. rowData, _ := iter.RowData()
  126. dataToReturn := make([]map[string]interface{}, 0)
  127. for iter.Scan(rowData.Values...) {
  128. m := make(map[string]interface{})
  129. rowData.rowMap(m)
  130. dataToReturn = append(dataToReturn, m)
  131. }
  132. if iter.err != nil {
  133. return nil, iter.err
  134. }
  135. return dataToReturn, nil
  136. }
  137. // MapScan takes a map[string]interface{} and populates it with a row
  138. // That is returned from cassandra.
  139. func (iter *Iter) MapScan(m map[string]interface{}) bool {
  140. if iter.err != nil {
  141. return false
  142. }
  143. // Not checking for the error because we just did
  144. rowData, _ := iter.RowData()
  145. if iter.Scan(rowData.Values...) {
  146. rowData.rowMap(m)
  147. return true
  148. }
  149. return false
  150. }