helpers.go 6.2 KB

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