helpers.go 10 KB

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