Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for Tuples #152

Open
mmatczuk opened this issue Apr 22, 2020 · 0 comments
Open

Improve support for Tuples #152

mmatczuk opened this issue Apr 22, 2020 · 0 comments

Comments

@mmatczuk
Copy link
Contributor

We should be able to scan and bind arrays and slices.

At the moment

func datatypesTuplesSimple(t *testing.T, session gocqlx.Session) {
	err := session.ExecStmt(`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}`)
	if err != nil {
		t.Fatal("create keyspace:", err)
	}

	err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS examples.tuples(k int PRIMARY KEY, c tuple<int,int>)`)
	if err != nil {
		t.Fatal("create table:", err)
	}

	coordinates1 := [2]int{12, 34}
	coordinates2 := [2]int{56, 78}

	insert := session.Query(qb.Insert("examples.tuples").Columns("k").TupleColumn("c", 2).ToCql())
	insert.BindMap(qb.M{
		"k":   1,
		"c_0": coordinates1[0],
		"c_1": coordinates1[1],
	})
	if err := insert.Exec(); err != nil {
		t.Fatal("Exec() failed:", err)
	}
	insert.BindMap(qb.M{
		"k":   2,
		"c_0": coordinates2[0],
		"c_1": coordinates2[1],
	})
	if err := insert.Exec(); err != nil {
		t.Fatal("Exec() failed:", err)
	}

	row := &struct {
		K  int
		C [2]int
		C0 int `db:"c[0]"`
		C1 int `db:"c[1]"`
	}{}

	q := session.Query(qb.Select("examples.tuples").Columns("k", "c").ToCql())
	if err := q.Get(row); err != nil {
		t.Fatal(err)
	}
	t.Log(row)
}

At the moment it fails with gocql: not enough columns to scan into: have 2 want 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant