helpers.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. "net"
  9. "reflect"
  10. "strings"
  11. "time"
  12. "gopkg.in/inf.v0"
  13. )
  14. type RowData struct {
  15. Columns []string
  16. Values []interface{}
  17. }
  18. func goType(t TypeInfo) reflect.Type {
  19. switch t.Type() {
  20. case TypeVarchar, TypeAscii, TypeInet, TypeText:
  21. return reflect.TypeOf(*new(string))
  22. case TypeBigInt, TypeCounter:
  23. return reflect.TypeOf(*new(int64))
  24. case TypeTimestamp:
  25. return reflect.TypeOf(*new(time.Time))
  26. case TypeBlob:
  27. return reflect.TypeOf(*new([]byte))
  28. case TypeBoolean:
  29. return reflect.TypeOf(*new(bool))
  30. case TypeFloat:
  31. return reflect.TypeOf(*new(float32))
  32. case TypeDouble:
  33. return reflect.TypeOf(*new(float64))
  34. case TypeInt:
  35. return reflect.TypeOf(*new(int))
  36. case TypeSmallInt:
  37. return reflect.TypeOf(*new(int16))
  38. case TypeTinyInt:
  39. return reflect.TypeOf(*new(int8))
  40. case TypeDecimal:
  41. return reflect.TypeOf(*new(*inf.Dec))
  42. case TypeUUID, TypeTimeUUID:
  43. return reflect.TypeOf(*new(UUID))
  44. case TypeList, TypeSet:
  45. return reflect.SliceOf(goType(t.(CollectionType).Elem))
  46. case TypeMap:
  47. return reflect.MapOf(goType(t.(CollectionType).Key), goType(t.(CollectionType).Elem))
  48. case TypeVarint:
  49. return reflect.TypeOf(*new(*big.Int))
  50. case TypeTuple:
  51. // what can we do here? all there is to do is to make a list of interface{}
  52. tuple := t.(TupleTypeInfo)
  53. return reflect.TypeOf(make([]interface{}, len(tuple.Elems)))
  54. case TypeUDT:
  55. return reflect.TypeOf(make(map[string]interface{}))
  56. case TypeDate:
  57. return reflect.TypeOf(*new(time.Time))
  58. case TypeDuration:
  59. return reflect.TypeOf(*new(Duration))
  60. default:
  61. return nil
  62. }
  63. }
  64. func dereference(i interface{}) interface{} {
  65. return reflect.Indirect(reflect.ValueOf(i)).Interface()
  66. }
  67. func getCassandraBaseType(name string) Type {
  68. switch name {
  69. case "ascii":
  70. return TypeAscii
  71. case "bigint":
  72. return TypeBigInt
  73. case "blob":
  74. return TypeBlob
  75. case "boolean":
  76. return TypeBoolean
  77. case "counter":
  78. return TypeCounter
  79. case "decimal":
  80. return TypeDecimal
  81. case "double":
  82. return TypeDouble
  83. case "float":
  84. return TypeFloat
  85. case "int":
  86. return TypeInt
  87. case "tinyint":
  88. return TypeTinyInt
  89. case "timestamp":
  90. return TypeTimestamp
  91. case "uuid":
  92. return TypeUUID
  93. case "varchar":
  94. return TypeVarchar
  95. case "text":
  96. return TypeText
  97. case "varint":
  98. return TypeVarint
  99. case "timeuuid":
  100. return TypeTimeUUID
  101. case "inet":
  102. return TypeInet
  103. case "MapType":
  104. return TypeMap
  105. case "ListType":
  106. return TypeList
  107. case "SetType":
  108. return TypeSet
  109. case "TupleType":
  110. return TypeTuple
  111. default:
  112. return TypeCustom
  113. }
  114. }
  115. func getCassandraType(name string) TypeInfo {
  116. if strings.HasPrefix(name, "frozen<") {
  117. return getCassandraType(strings.TrimPrefix(name[:len(name)-1], "frozen<"))
  118. } else if strings.HasPrefix(name, "set<") {
  119. return CollectionType{
  120. NativeType: NativeType{typ: TypeSet},
  121. Elem: getCassandraType(strings.TrimPrefix(name[:len(name)-1], "set<")),
  122. }
  123. } else if strings.HasPrefix(name, "list<") {
  124. return CollectionType{
  125. NativeType: NativeType{typ: TypeList},
  126. Elem: getCassandraType(strings.TrimPrefix(name[:len(name)-1], "list<")),
  127. }
  128. } else if strings.HasPrefix(name, "map<") {
  129. names := splitCompositeTypes(strings.TrimPrefix(name[:len(name)-1], "map<"))
  130. if len(names) != 2 {
  131. Logger.Printf("Error parsing map type, it has %d subelements, expecting 2\n", len(names))
  132. return NativeType{
  133. typ: TypeCustom,
  134. }
  135. }
  136. return CollectionType{
  137. NativeType: NativeType{typ: TypeMap},
  138. Key: getCassandraType(names[0]),
  139. Elem: getCassandraType(names[1]),
  140. }
  141. } else if strings.HasPrefix(name, "tuple<") {
  142. names := splitCompositeTypes(strings.TrimPrefix(name[:len(name)-1], "tuple<"))
  143. types := make([]TypeInfo, len(names))
  144. for i, name := range names {
  145. types[i] = getCassandraType(name)
  146. }
  147. return TupleTypeInfo{
  148. NativeType: NativeType{typ: TypeTuple},
  149. Elems: types,
  150. }
  151. } else {
  152. return NativeType{
  153. typ: getCassandraBaseType(name),
  154. }
  155. }
  156. }
  157. func splitCompositeTypes(name string) []string {
  158. if !strings.Contains(name, "<") {
  159. return strings.Split(name, ", ")
  160. }
  161. var parts []string
  162. lessCount := 0
  163. segment := ""
  164. for _, char := range name {
  165. if char == ',' && lessCount == 0 {
  166. if segment != "" {
  167. parts = append(parts, strings.TrimSpace(segment))
  168. }
  169. segment = ""
  170. continue
  171. }
  172. segment += string(char)
  173. if char == '<' {
  174. lessCount++
  175. } else if char == '>' {
  176. lessCount--
  177. }
  178. }
  179. if segment != "" {
  180. parts = append(parts, strings.TrimSpace(segment))
  181. }
  182. return parts
  183. }
  184. func apacheToCassandraType(t string) string {
  185. t = strings.Replace(t, apacheCassandraTypePrefix, "", -1)
  186. t = strings.Replace(t, "(", "<", -1)
  187. t = strings.Replace(t, ")", ">", -1)
  188. types := strings.FieldsFunc(t, func(r rune) bool {
  189. return r == '<' || r == '>' || r == ','
  190. })
  191. for _, typ := range types {
  192. t = strings.Replace(t, typ, getApacheCassandraType(typ).String(), -1)
  193. }
  194. // This is done so it exactly matches what Cassandra returns
  195. return strings.Replace(t, ",", ", ", -1)
  196. }
  197. func getApacheCassandraType(class string) Type {
  198. switch strings.TrimPrefix(class, apacheCassandraTypePrefix) {
  199. case "AsciiType":
  200. return TypeAscii
  201. case "LongType":
  202. return TypeBigInt
  203. case "BytesType":
  204. return TypeBlob
  205. case "BooleanType":
  206. return TypeBoolean
  207. case "CounterColumnType":
  208. return TypeCounter
  209. case "DecimalType":
  210. return TypeDecimal
  211. case "DoubleType":
  212. return TypeDouble
  213. case "FloatType":
  214. return TypeFloat
  215. case "Int32Type":
  216. return TypeInt
  217. case "ShortType":
  218. return TypeSmallInt
  219. case "ByteType":
  220. return TypeTinyInt
  221. case "DateType", "TimestampType":
  222. return TypeTimestamp
  223. case "UUIDType", "LexicalUUIDType":
  224. return TypeUUID
  225. case "UTF8Type":
  226. return TypeVarchar
  227. case "IntegerType":
  228. return TypeVarint
  229. case "TimeUUIDType":
  230. return TypeTimeUUID
  231. case "InetAddressType":
  232. return TypeInet
  233. case "MapType":
  234. return TypeMap
  235. case "ListType":
  236. return TypeList
  237. case "SetType":
  238. return TypeSet
  239. case "TupleType":
  240. return TypeTuple
  241. case "DurationType":
  242. return TypeDuration
  243. default:
  244. return TypeCustom
  245. }
  246. }
  247. func typeCanBeNull(typ TypeInfo) bool {
  248. switch typ.(type) {
  249. case CollectionType, UDTTypeInfo, TupleTypeInfo:
  250. return false
  251. }
  252. return true
  253. }
  254. func (r *RowData) rowMap(m map[string]interface{}) {
  255. for i, column := range r.Columns {
  256. val := dereference(r.Values[i])
  257. if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice {
  258. valCopy := reflect.MakeSlice(valVal.Type(), valVal.Len(), valVal.Cap())
  259. reflect.Copy(valCopy, valVal)
  260. m[column] = valCopy.Interface()
  261. } else {
  262. m[column] = val
  263. }
  264. }
  265. }
  266. // TupeColumnName will return the column name of a tuple value in a column named
  267. // c at index n. It should be used if a specific element within a tuple is needed
  268. // to be extracted from a map returned from SliceMap or MapScan.
  269. func TupleColumnName(c string, n int) string {
  270. return fmt.Sprintf("%s[%d]", c, n)
  271. }
  272. func (iter *Iter) RowData() (RowData, error) {
  273. if iter.err != nil {
  274. return RowData{}, iter.err
  275. }
  276. columns := make([]string, 0, len(iter.Columns()))
  277. values := make([]interface{}, 0, len(iter.Columns()))
  278. for _, column := range iter.Columns() {
  279. if c, ok := column.TypeInfo.(TupleTypeInfo); !ok {
  280. val := column.TypeInfo.New()
  281. columns = append(columns, column.Name)
  282. values = append(values, val)
  283. } else {
  284. for i, elem := range c.Elems {
  285. columns = append(columns, TupleColumnName(column.Name, i))
  286. values = append(values, elem.New())
  287. }
  288. }
  289. }
  290. rowData := RowData{
  291. Columns: columns,
  292. Values: values,
  293. }
  294. return rowData, nil
  295. }
  296. // TODO(zariel): is it worth exporting this?
  297. func (iter *Iter) rowMap() (map[string]interface{}, error) {
  298. if iter.err != nil {
  299. return nil, iter.err
  300. }
  301. rowData, _ := iter.RowData()
  302. iter.Scan(rowData.Values...)
  303. m := make(map[string]interface{}, len(rowData.Columns))
  304. rowData.rowMap(m)
  305. return m, nil
  306. }
  307. // SliceMap is a helper function to make the API easier to use
  308. // returns the data from the query in the form of []map[string]interface{}
  309. func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {
  310. if iter.err != nil {
  311. return nil, iter.err
  312. }
  313. // Not checking for the error because we just did
  314. rowData, _ := iter.RowData()
  315. dataToReturn := make([]map[string]interface{}, 0)
  316. for iter.Scan(rowData.Values...) {
  317. m := make(map[string]interface{}, len(rowData.Columns))
  318. rowData.rowMap(m)
  319. dataToReturn = append(dataToReturn, m)
  320. }
  321. if iter.err != nil {
  322. return nil, iter.err
  323. }
  324. return dataToReturn, nil
  325. }
  326. // MapScan takes a map[string]interface{} and populates it with a row
  327. // that is returned from cassandra.
  328. //
  329. // Each call to MapScan() must be called with a new map object.
  330. // During the call to MapScan() any pointers in the existing map
  331. // are replaced with non pointer types before the call returns
  332. //
  333. // iter := session.Query(`SELECT * FROM mytable`).Iter()
  334. // for {
  335. // // New map each iteration
  336. // row = make(map[string]interface{})
  337. // if !iter.MapScan(row) {
  338. // break
  339. // }
  340. // // Do things with row
  341. // if fullname, ok := row["fullname"]; ok {
  342. // fmt.Printf("Full Name: %s\n", fullname)
  343. // }
  344. // }
  345. //
  346. // You can also pass pointers in the map before each call
  347. //
  348. // var fullName FullName // Implements gocql.Unmarshaler and gocql.Marshaler interfaces
  349. // var address net.IP
  350. // var age int
  351. // iter := session.Query(`SELECT * FROM scan_map_table`).Iter()
  352. // for {
  353. // // New map each iteration
  354. // row := map[string]interface{}{
  355. // "fullname": &fullName,
  356. // "age": &age,
  357. // "address": &address,
  358. // }
  359. // if !iter.MapScan(row) {
  360. // break
  361. // }
  362. // fmt.Printf("First: %s Age: %d Address: %q\n", fullName.FirstName, age, address)
  363. // }
  364. func (iter *Iter) MapScan(m map[string]interface{}) bool {
  365. if iter.err != nil {
  366. return false
  367. }
  368. // Not checking for the error because we just did
  369. rowData, _ := iter.RowData()
  370. for i, col := range rowData.Columns {
  371. if dest, ok := m[col]; ok {
  372. rowData.Values[i] = dest
  373. }
  374. }
  375. if iter.Scan(rowData.Values...) {
  376. rowData.rowMap(m)
  377. return true
  378. }
  379. return false
  380. }
  381. func copyBytes(p []byte) []byte {
  382. b := make([]byte, len(p))
  383. copy(b, p)
  384. return b
  385. }
  386. var failDNS = false
  387. func LookupIP(host string) ([]net.IP, error) {
  388. if failDNS {
  389. return nil, &net.DNSError{}
  390. }
  391. return net.LookupIP(host)
  392. }