Skip to content

Commit

Permalink
Merge branch 'release/v0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
dancannon committed Apr 13, 2014
2 parents 9037954 + 665bc47 commit 148a64d
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 131 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Changelog

## V1.11 - 27 November 2013
## v0.2 (RethinkDB v1.12) - 13 April

* Changed `Connect` to use `ConnectOpts` instead of `map[string]interface{}`
* Migrated to new `Group`/`Ungroup` functions, these replace `GroupedMapReduce` and `GroupBy`
* Added new aggregators
* Removed base parameter for `Reduce`
* Added `Object` function
* Added `Upcase`, `Downcase` and `Split` string functions
* Added `GROUPED_DATA` pseudotype
* Fixed query printing

## v0.1 (RethinkDB v1.11) - 27 November 2013

* Added noreply writes
* Added the new terms `index_status`, `index_wait` and `sync`
Expand Down
49 changes: 21 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
# GoRethink - RethinkDB Driver for Go

[![wercker status](https://app.wercker.com/status/e315e764041af8e80f0c68280d4b4de2/m "wercker status")](https://app.wercker.com/project/bykey/e315e764041af8e80f0c68280d4b4de2)
GoRethink - RethinkDB Driver for Go [![wercker status](https://app.wercker.com/status/e315e764041af8e80f0c68280d4b4de2/s/master "wercker status")](https://app.wercker.com/project/bykey/e315e764041af8e80f0c68280d4b4de2) [![GoDoc](https://godoc.org/github.com/dancannon/gorethink?status.png)](https://godoc.org/github.com/dancannon/gorethink)
=====================

[Go](http://golang.org/) driver for [RethinkDB](http://www.rethinkdb.com/) made by [Daniel Cannon](http://github.com/dancannon) and based off of Christopher Hesse's [RethinkGo](https://github.com/christopherhesse/rethinkgo) driver.

Current supported RethinkDB version: 1.11 | Documentation: [GoDoc](http://godoc.org/github.com/dancannon/gorethink)
Current version: v0.2 (RethinkDB v1.12)

**Version 0.2 introduced some BC breaking changes, for more information check the [change log](CHANGELOG.md)**

## Installation

```sh
go get -u github.com/dancannon/gorethink
```

If you do not have the [goprotobuf](https://code.google.com/p/goprotobuf/) runtime installed, it is required:

```sh
brew install mercurial # if you do not have mercurial installed
go get code.google.com/p/goprotobuf/{proto,protoc-gen-go}
```

## Connection

### Basic Connection
Expand All @@ -32,15 +26,14 @@ import (

var session *r.Session

session, err := r.Connect(map[string]interface{}{
"address": "localhost:28015",
"database": "test",
"authkey": "14daak1cad13dj",
})
session, err := r.Connect(r.ConnectOpts{
Address: "localhost:28015",
Database: "test",
})

if err != nil {
log.Fatalln(err.Error())
}
if err != nil {
log.Fatalln(err.Error())
}

```
See the [documentation](http://godoc.org/github.com/dancannon/gorethink#Connect) for a list of supported arguments to Connect().
Expand All @@ -56,16 +49,16 @@ import (

var session *r.Session

session, err := r.Connect(map[string]interface{}{
"address": "localhost:28015",
"database": "test",
"maxIdle": 10,
"idleTimeout": time.Second * 10,
})
session, err := r.Connect(r.ConnectOpts{
Address: "localhost:28015",
Database: "test",
MaxIdle: 10,
IdleTimeout: time.Second * 10,
})

if err != nil {
log.Fatalln(err.Error())
}
if err != nil {
log.Fatalln(err.Error())
}
```

A pre-configured [Pool](http://godoc.org/github.com/dancannon/gorethink#Pool) instance can also be passed to Connect().
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Go driver for RethinkDB
//
// Current supported RethinkDB version: 1.11
// Current version: v0.2 (RethinkDB v1.12)
// For more in depth information on how to use RethinkDB check out the API docs
// at http://rethinkdb.com/api
package gorethink
40 changes: 40 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
package gorethink

import (
"bytes"
"fmt"

p "github.com/dancannon/gorethink/ql2"
)

func printCarrots(t RqlTerm, frames []*p.Frame) string {
var frame *p.Frame
if len(frames) > 1 {
frame, frames = frames[0], frames[1:]
} else if len(frames) == 1 {
frame, frames = frames[0], []*p.Frame{}
}

for i, arg := range t.args {
if frame.GetPos() == int64(i) {
t.args[i] = RqlTerm{
termType: p.Term_DATUM,
data: printCarrots(arg, frames),
}
}
}

for k, arg := range t.optArgs {
if frame.GetOpt() == k {
t.optArgs[k] = RqlTerm{
termType: p.Term_DATUM,
data: printCarrots(arg, frames),
}
}
}

b := &bytes.Buffer{}
for _, c := range t.String() {
if c != '^' {
b.WriteString(" ")
} else {
b.WriteString("^")
}
}

return b.String()
}

// Connection/Response errors
// ----------------------------------------------------------------------------
type rqlResponseError struct {
Expand Down
8 changes: 4 additions & 4 deletions example_query_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func ExampleRqlTerm_Get() {
Gender string `gorethink:"gender"`
}

sess, err := r.Connect(map[string]interface{}{
"address": url,
sess, err := r.Connect(r.ConnectOpts{
Address: url,
})

// Setup table
Expand Down Expand Up @@ -54,8 +54,8 @@ func ExampleRqlTerm_GetAll_compound() {
Gender string `gorethink:"gender"`
}

sess, err := r.Connect(map[string]interface{}{
"address": url,
sess, err := r.Connect(r.ConnectOpts{
Address: url,
})

// Setup table
Expand Down
15 changes: 8 additions & 7 deletions example_query_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package gorethink_test

import (
"fmt"
r "github.com/dancannon/gorethink"
"log"

r "github.com/dancannon/gorethink"
)

func ExampleRqlTerm_TableCreate() {
sess, err := r.Connect(map[string]interface{}{
"address": url,
sess, err := r.Connect(r.ConnectOpts{
Address: url,
})

// Setup database
Expand All @@ -26,8 +27,8 @@ func ExampleRqlTerm_TableCreate() {
}

func ExampleRqlTerm_IndexCreate() {
sess, err := r.Connect(map[string]interface{}{
"address": url,
sess, err := r.Connect(r.ConnectOpts{
Address: url,
})

// Setup database
Expand All @@ -46,8 +47,8 @@ func ExampleRqlTerm_IndexCreate() {
}

func ExampleRqlTerm_IndexCreate_compound() {
sess, err := r.Connect(map[string]interface{}{
"address": url,
sess, err := r.Connect(r.ConnectOpts{
Address: url,
})

// Setup database
Expand Down
7 changes: 4 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package gorethink_test

import (
"fmt"
r "github.com/dancannon/gorethink"
"log"
"os"

r "github.com/dancannon/gorethink"
)

var session *r.Session
Expand All @@ -19,8 +20,8 @@ func init() {
}

func Example() {
session, err := r.Connect(map[string]interface{}{
"address": url,
session, err := r.Connect(r.ConnectOpts{
Address: url,
})
if err != nil {
log.Fatalln(err.Error())
Expand Down
11 changes: 6 additions & 5 deletions gorethink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package gorethink
import (
"encoding/json"
"flag"
test "launchpad.net/gocheck"
"os"
"testing"
"time"

test "launchpad.net/gocheck"
)

var sess *Session
Expand Down Expand Up @@ -37,10 +38,10 @@ var _ = test.Suite(&RethinkSuite{})

func (s *RethinkSuite) SetUpSuite(c *test.C) {
var err error
sess, err = Connect(map[string]interface{}{
"address": url,
"maxIdle": 3,
"maxActive": 3,
sess, err = Connect(ConnectOpts{
Address: url,
MaxIdle: 3,
MaxActive: 3,
})
c.Assert(err, test.IsNil)
}
Expand Down
50 changes: 45 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package gorethink

import (
"code.google.com/p/goprotobuf/proto"
"fmt"
p "github.com/dancannon/gorethink/ql2"
"strconv"
"strings"

"code.google.com/p/goprotobuf/proto"
p "github.com/dancannon/gorethink/ql2"
)

type OptArgs interface {
Expand All @@ -15,6 +16,7 @@ type termsList []RqlTerm
type termsObj map[string]RqlTerm
type RqlTerm struct {
name string
rootTerm bool
termType p.Term_TermType
data interface{}
args []RqlTerm
Expand Down Expand Up @@ -55,7 +57,45 @@ func (t RqlTerm) build() *p.Term {
}
}

// compose returns a string representation of the query tree
func (t RqlTerm) compose(args []string, optArgs map[string]string) string {
switch t.termType {
case p.Term_MAKE_ARRAY:
return fmt.Sprintf("[%s]", strings.Join(argsToStringSlice(t.args), ", "))
case p.Term_MAKE_OBJ:
return fmt.Sprintf("{%s}", strings.Join(optArgsToStringSlice(t.optArgs), ", "))
case p.Term_FUNC:
// Get string representation of each argument
args := []string{}
for _, v := range t.args[0].args {
args = append(args, fmt.Sprintf("var_%d", v.data))
}

return fmt.Sprintf("func(%s r.RqlTerm) r.RqlTerm { return %s }",
strings.Join(args, ", "),
t.args[1].String(),
)
case p.Term_VAR:
return fmt.Sprintf("var_%s", t.args[0])
case p.Term_IMPLICIT_VAR:
return "r.Row"
case p.Term_DATUM:
switch v := t.data.(type) {
case string:
return strconv.Quote(v)
default:
return fmt.Sprintf("%v", v)
}

default:
if t.rootTerm {
return fmt.Sprintf("r.%s(%s)", t.name, strings.Join(allArgsToStringSlice(t.args, t.optArgs), ", "))
} else {
return fmt.Sprintf("%s.%s(%s)", t.args[0].String(), t.name, strings.Join(allArgsToStringSlice(t.args[1:], t.optArgs), ", "))
}
}
}

// String returns a string representation of the query tree
func (t RqlTerm) String() string {
switch t.termType {
case p.Term_MAKE_ARRAY:
Expand Down Expand Up @@ -86,10 +126,10 @@ func (t RqlTerm) String() string {
}

default:
if t.name != "" {
if t.rootTerm {
return fmt.Sprintf("r.%s(%s)", t.name, strings.Join(allArgsToStringSlice(t.args, t.optArgs), ", "))
} else {
return fmt.Sprintf("(%s)", strings.Join(allArgsToStringSlice(t.args, t.optArgs), ", "))
return fmt.Sprintf("%s.%s(%s)", t.args[0].String(), t.name, strings.Join(allArgsToStringSlice(t.args[1:], t.optArgs), ", "))
}
}
}
Expand Down
Loading

0 comments on commit 148a64d

Please sign in to comment.