helpers.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "fmt"
  7. "math/big"
  8. "reflect"
  9. "strings"
  10. "time"
  11. "gopkg.in/inf.v0"
  12. )
  13. type RowData struct {
  14. Columns []string
  15. Values []interface{}
  16. }
  17. func goType(t TypeInfo) reflect.Type {
  18. switch t.Type() {
  19. case TypeVarchar, TypeAscii, TypeInet, TypeText:
  20. return reflect.TypeOf(*new(string))
  21. case TypeBigInt, TypeCounter:
  22. return reflect.TypeOf(*new(int64))
  23. case TypeTimestamp:
  24. return reflect.TypeOf(*new(time.Time))
  25. case TypeBlob:
  26. return reflect.TypeOf(*new([]byte))
  27. case TypeBoolean:
  28. return reflect.TypeOf(*new(bool))
  29. case TypeFloat:
  30. return reflect.TypeOf(*new(float32))
  31. case TypeDouble:
  32. return reflect.TypeOf(*new(float64))
  33. case TypeInt:
  34. return reflect.TypeOf(*new(int))
  35. case TypeDecimal:
  36. return reflect.TypeOf(*new(*inf.Dec))
  37. case TypeUUID, TypeTimeUUID:
  38. return reflect.TypeOf(*new(UUID))
  39. case TypeList, TypeSet:
  40. return reflect.SliceOf(goType(t.(CollectionType).Elem))
  41. case TypeMap:
  42. return reflect.MapOf(goType(t.(CollectionType).Key), goType(t.(CollectionType).Elem))
  43. case TypeVarint:
  44. return reflect.TypeOf(*new(*big.Int))
  45. case TypeTuple:
  46. // what can we do here? all there is to do is to make a list of interface{}
  47. tuple := t.(TupleTypeInfo)
  48. return reflect.TypeOf(make([]interface{}, len(tuple.Elems)))
  49. case TypeUDT:
  50. return reflect.TypeOf(make(map[string]interface{}))
  51. default:
  52. return nil
  53. }
  54. }
  55. func dereference(i interface{}) interface{} {
  56. return reflect.Indirect(reflect.ValueOf(i)).Interface()
  57. }
  58. func getApacheCassandraType(class string) Type {
  59. switch strings.TrimPrefix(class, apacheCassandraTypePrefix) {
  60. case "AsciiType":
  61. return TypeAscii
  62. case "LongType":
  63. return TypeBigInt
  64. case "BytesType":
  65. return TypeBlob
  66. case "BooleanType":
  67. return TypeBoolean
  68. case "CounterColumnType":
  69. return TypeCounter
  70. case "DecimalType":
  71. return TypeDecimal
  72. case "DoubleType":
  73. return TypeDouble
  74. case "FloatType":
  75. return TypeFloat
  76. case "Int32Type":
  77. return TypeInt
  78. case "DateType", "TimestampType":
  79. return TypeTimestamp
  80. case "UUIDType", "LexicalUUIDType":
  81. return TypeUUID
  82. case "UTF8Type":
  83. return TypeVarchar
  84. case "IntegerType":
  85. return TypeVarint
  86. case "TimeUUIDType":
  87. return TypeTimeUUID
  88. case "InetAddressType":
  89. return TypeInet
  90. case "MapType":
  91. return TypeMap
  92. case "ListType":
  93. return TypeList
  94. case "SetType":
  95. return TypeSet
  96. case "TupleType":
  97. return TypeTuple
  98. default:
  99. return TypeCustom
  100. }
  101. }
  102. func typeCanBeNull(typ TypeInfo) bool {
  103. switch typ.(type) {
  104. case CollectionType, UDTTypeInfo, TupleTypeInfo:
  105. return false
  106. }
  107. return true
  108. }
  109. func (r *RowData) rowMap(m map[string]interface{}) {
  110. for i, column := range r.Columns {
  111. val := dereference(r.Values[i])
  112. if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice {
  113. valCopy := reflect.MakeSlice(valVal.Type(), valVal.Len(), valVal.Cap())
  114. reflect.Copy(valCopy, valVal)
  115. m[column] = valCopy.Interface()
  116. } else {
  117. m[column] = val
  118. }
  119. }
  120. }
  121. // TupeColumnName will return the column name of a tuple value in a column named
  122. // c at index n. It should be used if a specific element within a tuple is needed
  123. // to be extracted from a map returned from SliceMap or MapScan.
  124. func TupleColumnName(c string, n int) string {
  125. return fmt.Sprintf("%s[%d]", c, n)
  126. }
  127. func (iter *Iter) RowData() (RowData, error) {
  128. if iter.err != nil {
  129. return RowData{}, iter.err
  130. }
  131. columns := make([]string, 0)
  132. values := make([]interface{}, 0)
  133. for _, column := range iter.Columns() {
  134. switch c := column.TypeInfo.(type) {
  135. case TupleTypeInfo:
  136. for i, elem := range c.Elems {
  137. columns = append(columns, TupleColumnName(column.Name, i))
  138. values = append(values, elem.New())
  139. }
  140. default:
  141. val := column.TypeInfo.New()
  142. columns = append(columns, column.Name)
  143. values = append(values, val)
  144. }
  145. }
  146. rowData := RowData{
  147. Columns: columns,
  148. Values: values,
  149. }
  150. return rowData, nil
  151. }
  152. // SliceMap is a helper function to make the API easier to use
  153. // returns the data from the query in the form of []map[string]interface{}
  154. func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {
  155. if iter.err != nil {
  156. return nil, iter.err
  157. }
  158. // Not checking for the error because we just did
  159. rowData, _ := iter.RowData()
  160. dataToReturn := make([]map[string]interface{}, 0)
  161. for iter.Scan(rowData.Values...) {
  162. m := make(map[string]interface{})
  163. rowData.rowMap(m)
  164. dataToReturn = append(dataToReturn, m)
  165. }
  166. if iter.err != nil {
  167. return nil, iter.err
  168. }
  169. return dataToReturn, nil
  170. }
  171. // MapScan takes a map[string]interface{} and populates it with a row
  172. // That is returned from cassandra.
  173. func (iter *Iter) MapScan(m map[string]interface{}) bool {
  174. if iter.err != nil {
  175. return false
  176. }
  177. // Not checking for the error because we just did
  178. rowData, _ := iter.RowData()
  179. for i, col := range rowData.Columns {
  180. if dest, ok := m[col]; ok {
  181. rowData.Values[i] = dest
  182. }
  183. }
  184. if iter.Scan(rowData.Values...) {
  185. rowData.rowMap(m)
  186. return true
  187. }
  188. return false
  189. }
  190. func copyBytes(p []byte) []byte {
  191. b := make([]byte, len(p))
  192. copy(b, p)
  193. return b
  194. }