helpers.go 4.1 KB

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