helpers.go 10 KB

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