helpers.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // New creates a pointer to an empty version of whatever type
  17. // is referenced by the TypeInfo receiver
  18. func (t *TypeInfo) New() interface{} {
  19. return reflect.New(goType(t)).Interface()
  20. }
  21. func goType(t *TypeInfo) reflect.Type {
  22. switch t.Type {
  23. case TypeVarchar, TypeAscii, TypeInet:
  24. return reflect.TypeOf(*new(string))
  25. case TypeBigInt, TypeCounter:
  26. return reflect.TypeOf(*new(int64))
  27. case TypeTimestamp:
  28. return reflect.TypeOf(*new(time.Time))
  29. case TypeBlob:
  30. return reflect.TypeOf(*new([]byte))
  31. case TypeBoolean:
  32. return reflect.TypeOf(*new(bool))
  33. case TypeFloat:
  34. return reflect.TypeOf(*new(float32))
  35. case TypeDouble:
  36. return reflect.TypeOf(*new(float64))
  37. case TypeInt:
  38. return reflect.TypeOf(*new(int))
  39. case TypeDecimal:
  40. return reflect.TypeOf(*new(*inf.Dec))
  41. case TypeUUID, TypeTimeUUID:
  42. return reflect.TypeOf(*new(UUID))
  43. case TypeList, TypeSet:
  44. return reflect.SliceOf(goType(t.Elem))
  45. case TypeMap:
  46. return reflect.MapOf(goType(t.Key), goType(t.Elem))
  47. case TypeVarint:
  48. return reflect.TypeOf(*new(*big.Int))
  49. default:
  50. return nil
  51. }
  52. }
  53. func dereference(i interface{}) interface{} {
  54. return reflect.Indirect(reflect.ValueOf(i)).Interface()
  55. }
  56. func getApacheCassandraType(class string) Type {
  57. if strings.HasPrefix(class, apacheCassandraTypePrefix) {
  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":
  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. }
  96. }
  97. return TypeCustom
  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. }