helpers.go 6.3 KB

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