helpers.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. case TypeUDT:
  49. return reflect.TypeOf(make(map[string]interface{}))
  50. default:
  51. return nil
  52. }
  53. }
  54. func dereference(i interface{}) interface{} {
  55. return reflect.Indirect(reflect.ValueOf(i)).Interface()
  56. }
  57. func getApacheCassandraType(class string) Type {
  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", "LexicalUUIDType":
  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. case "TupleType":
  96. return TypeTuple
  97. default:
  98. return TypeCustom
  99. }
  100. }
  101. func typeCanBeNull(typ TypeInfo) bool {
  102. switch typ.(type) {
  103. case CollectionType, UDTTypeInfo, TupleTypeInfo:
  104. return false
  105. }
  106. return true
  107. }
  108. func (r *RowData) rowMap(m map[string]interface{}) {
  109. for i, column := range r.Columns {
  110. val := dereference(r.Values[i])
  111. if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice {
  112. valCopy := reflect.MakeSlice(valVal.Type(), valVal.Len(), valVal.Cap())
  113. reflect.Copy(valCopy, valVal)
  114. m[column] = valCopy.Interface()
  115. } else {
  116. m[column] = val
  117. }
  118. }
  119. }
  120. func (iter *Iter) RowData() (RowData, error) {
  121. if iter.err != nil {
  122. return RowData{}, iter.err
  123. }
  124. columns := make([]string, 0)
  125. values := make([]interface{}, 0)
  126. for _, column := range iter.Columns() {
  127. val := column.TypeInfo.New()
  128. columns = append(columns, column.Name)
  129. values = append(values, val)
  130. }
  131. rowData := RowData{
  132. Columns: columns,
  133. Values: values,
  134. }
  135. return rowData, nil
  136. }
  137. // SliceMap is a helper function to make the API easier to use
  138. // returns the data from the query in the form of []map[string]interface{}
  139. func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {
  140. if iter.err != nil {
  141. return nil, iter.err
  142. }
  143. // Not checking for the error because we just did
  144. rowData, _ := iter.RowData()
  145. dataToReturn := make([]map[string]interface{}, 0)
  146. for iter.Scan(rowData.Values...) {
  147. m := make(map[string]interface{})
  148. rowData.rowMap(m)
  149. dataToReturn = append(dataToReturn, m)
  150. }
  151. if iter.err != nil {
  152. return nil, iter.err
  153. }
  154. return dataToReturn, nil
  155. }
  156. // MapScan takes a map[string]interface{} and populates it with a row
  157. // That is returned from cassandra.
  158. func (iter *Iter) MapScan(m map[string]interface{}) bool {
  159. if iter.err != nil {
  160. return false
  161. }
  162. // Not checking for the error because we just did
  163. rowData, _ := iter.RowData()
  164. for i, col := range rowData.Columns {
  165. if dest, ok := m[col]; ok {
  166. rowData.Values[i] = dest
  167. }
  168. }
  169. if iter.Scan(rowData.Values...) {
  170. rowData.rowMap(m)
  171. return true
  172. }
  173. return false
  174. }
  175. func copyBytes(p []byte) []byte {
  176. b := make([]byte, len(p))
  177. copy(b, p)
  178. return b
  179. }