helpers.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 getCassandraType(name string) Type {
  59. switch name {
  60. case "ascii":
  61. return TypeAscii
  62. case "bigint":
  63. return TypeBigInt
  64. case "blob":
  65. return TypeBlob
  66. case "boolean":
  67. return TypeBoolean
  68. case "counter":
  69. return TypeCounter
  70. case "decimal":
  71. return TypeDecimal
  72. case "double":
  73. return TypeDouble
  74. case "float":
  75. return TypeFloat
  76. case "int":
  77. return TypeInt
  78. case "timestamp":
  79. return TypeTimestamp
  80. case "uuid":
  81. return TypeUUID
  82. case "varchar", "text":
  83. return TypeVarchar
  84. case "varint":
  85. return TypeVarint
  86. case "timeuuid":
  87. return TypeTimeUUID
  88. case "inet":
  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. if strings.HasPrefix(name, "set") {
  100. return TypeSet
  101. } else if strings.HasPrefix(name, "list") {
  102. return TypeList
  103. } else if strings.HasPrefix(name, "map") {
  104. return TypeMap
  105. } else if strings.HasPrefix(name, "tuple") {
  106. return TypeTuple
  107. }
  108. return TypeCustom
  109. }
  110. }
  111. func getApacheCassandraType(class string) Type {
  112. switch strings.TrimPrefix(class, apacheCassandraTypePrefix) {
  113. case "AsciiType":
  114. return TypeAscii
  115. case "LongType":
  116. return TypeBigInt
  117. case "BytesType":
  118. return TypeBlob
  119. case "BooleanType":
  120. return TypeBoolean
  121. case "CounterColumnType":
  122. return TypeCounter
  123. case "DecimalType":
  124. return TypeDecimal
  125. case "DoubleType":
  126. return TypeDouble
  127. case "FloatType":
  128. return TypeFloat
  129. case "Int32Type":
  130. return TypeInt
  131. case "DateType", "TimestampType":
  132. return TypeTimestamp
  133. case "UUIDType", "LexicalUUIDType":
  134. return TypeUUID
  135. case "UTF8Type":
  136. return TypeVarchar
  137. case "IntegerType":
  138. return TypeVarint
  139. case "TimeUUIDType":
  140. return TypeTimeUUID
  141. case "InetAddressType":
  142. return TypeInet
  143. case "MapType":
  144. return TypeMap
  145. case "ListType":
  146. return TypeList
  147. case "SetType":
  148. return TypeSet
  149. case "TupleType":
  150. return TypeTuple
  151. default:
  152. return TypeCustom
  153. }
  154. }
  155. func typeCanBeNull(typ TypeInfo) bool {
  156. switch typ.(type) {
  157. case CollectionType, UDTTypeInfo, TupleTypeInfo:
  158. return false
  159. }
  160. return true
  161. }
  162. func (r *RowData) rowMap(m map[string]interface{}) {
  163. for i, column := range r.Columns {
  164. val := dereference(r.Values[i])
  165. if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice {
  166. valCopy := reflect.MakeSlice(valVal.Type(), valVal.Len(), valVal.Cap())
  167. reflect.Copy(valCopy, valVal)
  168. m[column] = valCopy.Interface()
  169. } else {
  170. m[column] = val
  171. }
  172. }
  173. }
  174. // TupeColumnName will return the column name of a tuple value in a column named
  175. // c at index n. It should be used if a specific element within a tuple is needed
  176. // to be extracted from a map returned from SliceMap or MapScan.
  177. func TupleColumnName(c string, n int) string {
  178. return fmt.Sprintf("%s[%d]", c, n)
  179. }
  180. func (iter *Iter) RowData() (RowData, error) {
  181. if iter.err != nil {
  182. return RowData{}, iter.err
  183. }
  184. columns := make([]string, 0)
  185. values := make([]interface{}, 0)
  186. for _, column := range iter.Columns() {
  187. switch c := column.TypeInfo.(type) {
  188. case TupleTypeInfo:
  189. for i, elem := range c.Elems {
  190. columns = append(columns, TupleColumnName(column.Name, i))
  191. values = append(values, elem.New())
  192. }
  193. default:
  194. val := column.TypeInfo.New()
  195. columns = append(columns, column.Name)
  196. values = append(values, val)
  197. }
  198. }
  199. rowData := RowData{
  200. Columns: columns,
  201. Values: values,
  202. }
  203. return rowData, nil
  204. }
  205. // SliceMap is a helper function to make the API easier to use
  206. // returns the data from the query in the form of []map[string]interface{}
  207. func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {
  208. if iter.err != nil {
  209. return nil, iter.err
  210. }
  211. // Not checking for the error because we just did
  212. rowData, _ := iter.RowData()
  213. dataToReturn := make([]map[string]interface{}, 0)
  214. for iter.Scan(rowData.Values...) {
  215. m := make(map[string]interface{})
  216. rowData.rowMap(m)
  217. dataToReturn = append(dataToReturn, m)
  218. }
  219. if iter.err != nil {
  220. return nil, iter.err
  221. }
  222. return dataToReturn, nil
  223. }
  224. // MapScan takes a map[string]interface{} and populates it with a row
  225. // That is returned from cassandra.
  226. func (iter *Iter) MapScan(m map[string]interface{}) bool {
  227. if iter.err != nil {
  228. return false
  229. }
  230. // Not checking for the error because we just did
  231. rowData, _ := iter.RowData()
  232. for i, col := range rowData.Columns {
  233. if dest, ok := m[col]; ok {
  234. rowData.Values[i] = dest
  235. }
  236. }
  237. if iter.Scan(rowData.Values...) {
  238. rowData.rowMap(m)
  239. return true
  240. }
  241. return false
  242. }
  243. func copyBytes(p []byte) []byte {
  244. b := make([]byte, len(p))
  245. copy(b, p)
  246. return b
  247. }