gocql.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. "errors"
  7. "fmt"
  8. "strings"
  9. )
  10. type Config struct {
  11. Nodes []string
  12. CQLVersion string
  13. Keyspace string
  14. Consistency Consistency
  15. DefaultPort int
  16. }
  17. func (c *Config) normalize() {
  18. if c.CQLVersion == "" {
  19. c.CQLVersion = "3.0.0"
  20. }
  21. if c.DefaultPort == 0 {
  22. c.DefaultPort = 9042
  23. }
  24. for i := 0; i < len(c.Nodes); i++ {
  25. c.Nodes[i] = strings.TrimSpace(c.Nodes[i])
  26. if strings.IndexByte(c.Nodes[i], ':') < 0 {
  27. c.Nodes[i] = fmt.Sprintf("%s:%d", c.Nodes[i], c.DefaultPort)
  28. }
  29. }
  30. }
  31. type Session struct {
  32. cfg *Config
  33. pool []*connection
  34. }
  35. func NewSession(cfg Config) *Session {
  36. cfg.normalize()
  37. pool := make([]*connection, 0, len(cfg.Nodes))
  38. for _, address := range cfg.Nodes {
  39. con, err := connect(address, &cfg)
  40. if err == nil {
  41. pool = append(pool, con)
  42. }
  43. }
  44. return &Session{cfg: &cfg, pool: pool}
  45. }
  46. func (s *Session) Query(stmt string, args ...interface{}) *Query {
  47. return &Query{
  48. stmt: stmt,
  49. args: args,
  50. cons: s.cfg.Consistency,
  51. ctx: s,
  52. }
  53. }
  54. func (s *Session) executeQuery(query *Query) (buffer, error) {
  55. // TODO(tux21b): do something clever here
  56. return s.pool[0].executeQuery(query)
  57. }
  58. func (s *Session) Close() {
  59. return
  60. }
  61. type Consistency uint16
  62. const (
  63. ConAny Consistency = 0x0000
  64. ConOne Consistency = 0x0001
  65. ConTwo Consistency = 0x0002
  66. ConThree Consistency = 0x0003
  67. ConQuorum Consistency = 0x0004
  68. ConAll Consistency = 0x0005
  69. ConLocalQuorum Consistency = 0x0006
  70. ConEachQuorum Consistency = 0x0007
  71. ConSerial Consistency = 0x0008
  72. ConLocalSerial Consistency = 0x0009
  73. )
  74. var ErrNotFound = errors.New("not found")
  75. type Query struct {
  76. stmt string
  77. args []interface{}
  78. cons Consistency
  79. ctx interface {
  80. executeQuery(query *Query) (buffer, error)
  81. }
  82. }
  83. var ErrQueryUnbound = errors.New("can not execute unbound query")
  84. func NewQuery(stmt string) *Query {
  85. return &Query{stmt: stmt, cons: ConQuorum}
  86. }
  87. func (q *Query) Exec() error {
  88. frame, err := q.request()
  89. if err != nil {
  90. return err
  91. }
  92. if frame[3] == opResult {
  93. frame.skipHeader()
  94. kind := frame.readInt()
  95. if kind == 3 {
  96. keyspace := frame.readString()
  97. fmt.Println("set keyspace:", keyspace)
  98. } else {
  99. }
  100. }
  101. return nil
  102. }
  103. func (q *Query) request() (buffer, error) {
  104. return q.ctx.executeQuery(q)
  105. }
  106. func (q *Query) Consistency(cons Consistency) *Query {
  107. q.cons = cons
  108. return q
  109. }
  110. func (q *Query) Scan(values ...interface{}) error {
  111. found := false
  112. iter := q.Iter()
  113. if iter.Scan(values...) {
  114. found = true
  115. }
  116. if err := iter.Close(); err != nil {
  117. return err
  118. } else if !found {
  119. return ErrNotFound
  120. }
  121. return nil
  122. }
  123. func (q *Query) Iter() *Iter {
  124. iter := new(Iter)
  125. frame, err := q.request()
  126. if err != nil {
  127. iter.err = err
  128. return iter
  129. }
  130. frame.skipHeader()
  131. kind := frame.readInt()
  132. if kind == resultKindRows {
  133. iter.setFrame(frame)
  134. }
  135. return iter
  136. }
  137. type Iter struct {
  138. err error
  139. pos int
  140. numRows int
  141. info []columnInfo
  142. flags int
  143. frame buffer
  144. }
  145. func (iter *Iter) setFrame(frame buffer) {
  146. info := frame.readMetaData()
  147. iter.flags = 0
  148. iter.info = info
  149. iter.numRows = frame.readInt()
  150. iter.pos = 0
  151. iter.err = nil
  152. iter.frame = frame
  153. }
  154. func (iter *Iter) Scan(values ...interface{}) bool {
  155. if iter.err != nil || iter.pos >= iter.numRows {
  156. return false
  157. }
  158. iter.pos++
  159. if len(values) != len(iter.info) {
  160. iter.err = errors.New("count mismatch")
  161. return false
  162. }
  163. for i := 0; i < len(values); i++ {
  164. data := iter.frame.readBytes()
  165. if err := Unmarshal(iter.info[i].TypeInfo, data, values[i]); err != nil {
  166. iter.err = err
  167. return false
  168. }
  169. }
  170. return true
  171. }
  172. func (iter *Iter) Close() error {
  173. return iter.err
  174. }
  175. type columnInfo struct {
  176. Keyspace string
  177. Table string
  178. Name string
  179. TypeInfo *TypeInfo
  180. }
  181. type Error struct {
  182. Code int
  183. Message string
  184. }
  185. func (e Error) Error() string {
  186. return e.Message
  187. }