Skip to content

Commit

Permalink
Merge remote-tracking branch 'gocql/master' into mmt/update
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed Feb 15, 2021
2 parents 3af2863 + 4364a4b commit fa86c11
Show file tree
Hide file tree
Showing 29 changed files with 1,464 additions and 121 deletions.
3 changes: 3 additions & 0 deletions .github/issue_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Please answer these questions before submitting your issue. Thanks!
### What version of Gocql are you using?


### What version of Go are you using?


### What did you do?


Expand Down
3 changes: 3 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,6 @@ Ivan Boyarkin <ivan.boyarkin@kiwi.com>; <mr.vanboy@gmail.com>
Yura Sokolov <y.sokolov@joom.com>; <funny.falcon@gmail.com>
Jorge Bay <jorgebg@apache.org>
Dmitriy Kozlov <hummerd@mail.ru>
Alexey Romanovsky <alexus1024+gocql@gmail.com>
Jaume Marhuenda Beltran <jaumemarhuenda@gmail.com>
Piotr Dulikowski <piodul@scylladb.com>
68 changes: 1 addition & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,73 +226,7 @@ statement.
Example
-------

```go
/* Before you execute the program, Launch `cqlsh` and execute:
create keyspace example with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
create table example.tweet(timeline text, id UUID, text text, PRIMARY KEY(id));
create index on example.tweet(timeline);
*/
package main

import (
"fmt"
"log"

"github.com/scylladb/gocql"
)

func main() {
// connect to the cluster
cluster := gocql.NewCluster("192.168.1.1", "192.168.1.2", "192.168.1.3")
cluster.Keyspace = "example"
cluster.Consistency = gocql.Quorum
session, _ := cluster.CreateSession()
defer session.Close()

// insert a tweet
if err := session.Query(`INSERT INTO tweet (timeline, id, text) VALUES (?, ?, ?)`,
"me", gocql.TimeUUID(), "hello world").Exec(); err != nil {
log.Fatal(err)
}

var id gocql.UUID
var text string

/* Search for a specific set of records whose 'timeline' column matches
* the value 'me'. The secondary index that we created earlier will be
* used for optimizing the search */
if err := session.Query(`SELECT id, text FROM tweet WHERE timeline = ? LIMIT 1`,
"me").Consistency(gocql.One).Scan(&id, &text); err != nil {
log.Fatal(err)
}
fmt.Println("Tweet:", id, text)

// list all tweets
iter := session.Query(`SELECT id, text FROM tweet WHERE timeline = ?`, "me").Iter()
for iter.Scan(&id, &text) {
fmt.Println("Tweet:", id, text)
}
if err := iter.Close(); err != nil {
log.Fatal(err)
}
}
```


Authentication
-------

```go
cluster := gocql.NewCluster("192.168.1.1", "192.168.1.2", "192.168.1.3")
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: "user",
Password: "password"
}
cluster.Keyspace = "example"
cluster.Consistency = gocql.Quorum
session, _ := cluster.CreateSession()
defer session.Close()
```
See [package documentation](https://pkg.go.dev/github.com/scylladb/gocql#pkg-examples).

Data Binding
------------
Expand Down
23 changes: 23 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,27 @@ func createMaterializedViews(t *testing.T, session *Session) {
PRIMARY KEY (userid));`).Exec(); err != nil {
t.Fatalf("failed to create materialized view with err: %v", err)
}
if err := session.Query(`CREATE TABLE IF NOT EXISTS gocql_test.view_table2 (
userid text,
year int,
month int,
PRIMARY KEY (userid));`).Exec(); err != nil {
t.Fatalf("failed to create materialized view with err: %v", err)
}
if err := session.Query(`CREATE MATERIALIZED VIEW IF NOT EXISTS gocql_test.view_view AS
SELECT year, month, userid
FROM gocql_test.view_table
WHERE year IS NOT NULL AND month IS NOT NULL AND userid IS NOT NULL
PRIMARY KEY (userid, year);`).Exec(); err != nil {
t.Fatalf("failed to create materialized view with err: %v", err)
}
if err := session.Query(`CREATE MATERIALIZED VIEW IF NOT EXISTS gocql_test.view_view2 AS
SELECT year, month, userid
FROM gocql_test.view_table2
WHERE year IS NOT NULL AND month IS NOT NULL AND userid IS NOT NULL
PRIMARY KEY (userid, year);`).Exec(); err != nil {
t.Fatalf("failed to create materialized view with err: %v", err)
}
}

func createFunctions(t *testing.T, session *Session) {
Expand Down Expand Up @@ -235,6 +249,15 @@ func createAggregate(t *testing.T, session *Session) {
`).Exec(); err != nil {
t.Fatalf("failed to create aggregate with err: %v", err)
}
if err := session.Query(`
CREATE OR REPLACE AGGREGATE gocql_test.average2(int)
SFUNC avgState
STYPE tuple<int,bigint>
FINALFUNC avgFinal
INITCOND (0,0);
`).Exec(); err != nil {
t.Fatalf("failed to create aggregate with err: %v", err)
}
}

func staticAddressTranslator(newAddr net.IP, newPort int) AddressTranslator {
Expand Down
16 changes: 14 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,26 @@ func (s *Session) dialWithoutObserver(ctx context.Context, host *HostInfo, cfg *
dialer = d
}

conn, err := dialer.DialContext(ctx, "tcp", host.HostnameAndPort())
addr := host.HostnameAndPort()
conn, err := dialer.DialContext(ctx, "tcp", addr)
if err != nil {
return nil, err
}
if cfg.tlsConfig != nil {
// the TLS config is safe to be reused by connections but it must not
// be modified after being used.
tconn := tls.Client(conn, cfg.tlsConfig)
tlsConfig := cfg.tlsConfig
if !tlsConfig.InsecureSkipVerify && tlsConfig.ServerName == "" {
colonPos := strings.LastIndex(addr, ":")
if colonPos == -1 {
colonPos = len(addr)
}
hostname := addr[:colonPos]
// clone config to avoid modifying the shared one.
tlsConfig = tlsConfig.Clone()
tlsConfig.ServerName = hostname
}
tconn := tls.Client(conn, tlsConfig)
if err := tconn.Handshake(); err != nil {
conn.Close()
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func hostInfo(addr string, defaultPort int) ([]*HostInfo, error) {
if err != nil {
return nil, err
} else if len(ips) == 0 {
return nil, fmt.Errorf("No IP's returned from DNS lookup for %q", addr)
return nil, fmt.Errorf("no IP's returned from DNS lookup for %q", addr)
}

// Filter to v4 addresses if any present
Expand Down Expand Up @@ -177,7 +177,7 @@ func (c *controlConn) shuffleDial(endpoints []*HostInfo) (*Conn, error) {
return conn, nil
}

Logger.Printf("gocql: unable to dial control conn %v: %v\n", host.ConnectAddress(), err)
Logger.Printf("gocql: unable to dial control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
}

return nil, err
Expand Down
Loading

0 comments on commit fa86c11

Please sign in to comment.