Exemplo n.º 1
0
Arquivo: bind.go Projeto: qtabot/cqlc
func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, REALLY_BASIC)

	result := "FAILED"

	ctx := cqlc.NewContext()

	basic := ReallyBasic{
		Id:          "y",
		Int32Column: 2001,
	}

	err := ctx.Store(REALLY_BASIC.Bind(basic)).Exec(session)

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	fetched := fetchFirstReallyBasic(ctx, session, "y")

	if reflect.DeepEqual(fetched, basic) {
		result = "PASSED"
	} else {
		result = fmt.Sprintf("[%+v] [%+v]", fetched, basic)
	}

	os.Stdout.WriteString(result)

}
Exemplo n.º 2
0
Arquivo: batch.go Projeto: qtabot/cqlc
func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, BASIC_CLUSTERED)

	result := "FAILED"

	rows := 10

	ctx := cqlc.NewContext()
	batch := gocql.NewBatch(gocql.LoggedBatch)

	for i := 0; i < rows; i++ {
		err := ctx.Upsert(BASIC_CLUSTERED).
			SetInt64(BASIC_CLUSTERED.ID, int64(0)).
			SetInt32(BASIC_CLUSTERED.INT32_CLUSTER, int32(i)).
			SetInt32(BASIC_CLUSTERED.INT32_COLUMN, int32(i)).
			Batch(batch)

		if err != nil {
			log.Fatalf("Could not batch upsert: %v", err)
			os.Exit(1)
		}
	}

	err := session.ExecuteBatch(batch)

	if err != nil {
		log.Fatalf("Could not execute batch: %v", err)
		os.Exit(1)
	}

	iter, err := ctx.Select().From(BASIC_CLUSTERED).Where(BASIC_CLUSTERED.ID.Eq(0)).Fetch(session)

	basics, err := BindBasicClustered(iter)
	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	err = iter.Close()
	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	if len(basics) == rows {
		result = "PASSED"
	}

	os.Stdout.WriteString(result)
}
Exemplo n.º 3
0
func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, EVENTS)

	result := "FAILED"

	ctx := cqlc.NewContext()

	var sensorId int64 = 100

	ctx.Upsert(EVENTS).
		SetInt64(EVENTS.SENSOR, sensorId).
		SetTimeUUID(EVENTS.TIMESTAMP, gocql.TimeUUID()).
		SetFloat32(EVENTS.TEMPERATURE, 19.8).
		SetInt32(EVENTS.PRESSURE, 357).
		Exec(session)

	iter, err := ctx.Select().
		From(EVENTS).
		Where(
			EVENTS.SENSOR.Eq(sensorId),
			EVENTS.TIMESTAMP.Lt(gocql.TimeUUID())).
		Fetch(session)

	if err != nil {
		log.Fatalf("Could not execute query: %v", err)
		return
	}

	events, err := BindEvents(iter)
	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		return
	}

	err = iter.Close()
	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		return
	}

	if len(events) == 1 {
		result = "PASSED"
	}

	os.Stdout.WriteString(result)
}
Exemplo n.º 4
0
func main() {
	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, REALLY_BASIC)

	result := "FAILED"

	ctx := cqlc.NewContext()

	err := ctx.Upsert(REALLY_BASIC).
		SetString(REALLY_BASIC.ID, "x").
		SetInt32(REALLY_BASIC.INT32_COLUMN, 222).
		Exec(session)

	if err != nil {
		log.Fatalf("Could not execute upsert: %v", err)
		os.Exit(1)
	}

	err = ctx.Delete(REALLY_BASIC.INT32_COLUMN).From(REALLY_BASIC).Where(REALLY_BASIC.ID.Eq("x")).Exec(session)

	if err != nil {
		log.Fatalf("Could not execute delete: %v", err)
		os.Exit(1)
	}

	basic := fetchFirstBasic(ctx, session, "x")

	if basic.Int32Column != 0 {
		log.Fatalf("Got bogus basic: %v", basic)
		os.Exit(1)
	}

	err = ctx.Delete().From(REALLY_BASIC).Where(REALLY_BASIC.ID.Eq("x")).Exec(session)

	if err != nil {
		log.Fatalf("Could not execute delete: %v", err)
		os.Exit(1)
	}

	basic = fetchFirstBasic(ctx, session, "x")

	if basic == nil {
		result = "PASSED"
	}

	os.Stdout.WriteString(result)
}
Exemplo n.º 5
0
func main() {
	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, CLUSTER_BY_STRING_AND_INT)

	result := "FAILED"

	ctx := cqlc.NewContext()

	err := ctx.Upsert(CLUSTER_BY_STRING_AND_INT).
		SetString(CLUSTER_BY_STRING_AND_INT.ID, "a").
		SetString(CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER, "y").
		SetInt64(CLUSTER_BY_STRING_AND_INT.INT64_CLUSTER, 4).
		SetInt64(CLUSTER_BY_STRING_AND_INT.LAST_CLUSTER_ELEMENT, 40).
		SetInt32(CLUSTER_BY_STRING_AND_INT.INT32_COLUMN, 100).
		Exec(session)

	if err != nil {
		log.Fatalf("Could not execute upsert: %v", err)
		os.Exit(1)
	}

	id := CLUSTER_BY_STRING_AND_INT.ID.Eq("a")

	passed := expectComparisonResult(ctx, session, expect(1), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Gt("x"))
	passed = expectComparisonResult(ctx, session, expect(0), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Gt("y"))
	passed = expectComparisonResult(ctx, session, expect(0), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Ge("z"))
	passed = expectComparisonResult(ctx, session, expect(1), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Lt("z"))
	passed = expectComparisonResult(ctx, session, expect(1), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Le("y"))
	passed = expectComparisonResult(ctx, session, expect(0), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Lt("y"))
	passed = expectComparisonResult(ctx, session, expect(0), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Lt("y"))

	stringCluster := CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Eq("y")
	int64Cluster := CLUSTER_BY_STRING_AND_INT.INT64_CLUSTER.Eq(4)
	lastClusterElement := CLUSTER_BY_STRING_AND_INT.LAST_CLUSTER_ELEMENT.In(39, 40, 41)

	passed = expectComparisonResult(ctx, session, expect(1), id, stringCluster, int64Cluster, lastClusterElement)

	passed = expectComparisonResult(ctx, session, expect(1), CLUSTER_BY_STRING_AND_INT.ID.In("a"))

	if passed {
		result = "PASSED"
	}

	os.Stdout.WriteString(result)
}
Exemplo n.º 6
0
func readCounter(session *gocql.Session, key string) (bool, int64) {

	var counter int64

	ctx := cqlc.NewContext()
	found, err := ctx.Select(COUNTER.COUNTER_COLUMN).
		From(COUNTER).
		Where(COUNTER.ID.Eq(key)).
		Bind(COUNTER.COUNTER_COLUMN.To(&counter)).
		FetchOne(session)

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	return found, counter
}
Exemplo n.º 7
0
Arquivo: cas.go Projeto: qtabot/cqlc
func compareAndSwap(session *gocql.Session, id string, int32column int32) (bool, string, int32) {

	var casId string
	var casInt32column int32

	ctx := cqlc.NewContext()

	applied, err := ctx.Upsert(REALLY_BASIC).
		SetString(REALLY_BASIC.ID, id).
		SetInt32(REALLY_BASIC.INT32_COLUMN, int32column).
		IfExists(REALLY_BASIC.ID.To(&casId), REALLY_BASIC.INT32_COLUMN.To(&casInt32column)).
		Swap(session)

	if err != nil {
		log.Fatalf("Could not execute CAS statement: %v", err)
		os.Exit(1)
	}

	return applied, casId, casInt32column
}
Exemplo n.º 8
0
func main() {
	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, COUNTER)

	result := "FAILED"

	ctx := cqlc.NewContext()

	err := ctx.UpdateCounter(COUNTER).
		Increment(COUNTER.COUNTER_COLUMN, 13).
		Having(COUNTER.ID.Eq("x")).
		Exec(session)

	if err != nil {
		log.Fatalf("Could not execute counter increment: %v", err)
	}

	found, counter := readCounter(session, "x")
	if found && counter == 13 {
		result = "PASSED"
	}

	c := BasicCounter{
		Id:            "x",
		CounterColumn: 11,
	}

	err = ctx.Add(COUNTER.Bind(c)).Exec(session)

	if err != nil {
		log.Fatalf("Could not execute counter increment: %v", err)
	}

	found, counter = readCounter(session, "x")
	if !found && counter != 24 {
		result = fmt.Sprintf("Expected 24, but counter was %d", counter)
	}

	os.Stdout.WriteString(result)
}
Exemplo n.º 9
0
Arquivo: blob.go Projeto: qtabot/cqlc
func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, BASIC_BLOB)

	result := "FAILED"

	ctx := cqlc.NewContext()

	blob := BasicBlob{
		Id:         "baz",
		BlobColumn: []byte("foo"),
	}

	err := ctx.Store(BASIC_BLOB.Bind(blob)).Exec(session)

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	var b []byte

	found, err := ctx.Select().
		From(BASIC_BLOB).
		Where(BASIC_BLOB.ID.Eq("baz")).
		Bind(BASIC_BLOB.BLOB_COLUMN.To(&b)).
		FetchOne(session)

	if found && bytes.Equal(blob.BlobColumn, b) {
		result = "PASSED"
	} else {
		result = fmt.Sprintf("Blob was %s", string(b))
	}

	os.Stdout.WriteString(result)
}
Exemplo n.º 10
0
Arquivo: into.go Projeto: qtabot/cqlc
func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, BASIC)

	result := "FAILED"

	ctx := cqlc.NewContext()

	basic := Basic{
		Id:              "x",
		Int32Column:     999,
		Int64Column:     1 << 55,
		AsciiColumn:     "do-re-me",
		TimestampColumn: time.Date(1999, time.December, 31, 23, 59, 59, 59, time.UTC), // Keep it simple for reflect.DeepEqual
		BooleanColumn:   true,
		TextColumn:      "ipso",
		VarcharColumn:   "lorem",
		FloatColumn:     math.MaxFloat32,
		DoubleColumn:    math.MaxFloat64,
		DecimalColumn:   inf.NewDec(1, 9),
		TimeuuidColumn:  gocql.TimeUUID(),
		MapColumn:       map[string]string{"baz": "quux"},
		ArrayColumn:     []string{"baz", "quux"},
	}

	err := ctx.Store(BASIC.Bind(basic)).Exec(session)

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	var int32Column int32
	var decimalColumn *inf.Dec

	found, err := ctx.Select().
		From(BASIC).
		Where(BASIC.ID.Eq("x")).
		Bind(BASIC.INT32_COLUMN.To(&int32Column), BASIC.DECIMAL_COLUMN.To(&decimalColumn)).
		FetchOne(session)

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	if int32Column == 999 && reflect.DeepEqual(decimalColumn, basic.DecimalColumn) && found {

		found, err := ctx.Select().
			From(BASIC).
			Where(BASIC.ID.Eq("y")).
			Bind(BASIC.INT32_COLUMN.To(&int32Column), BASIC.DECIMAL_COLUMN.To(&decimalColumn)).
			FetchOne(session)

		if err != nil {
			log.Fatalf("Could not bind data: %v", err)
			os.Exit(1)
		}

		if !found {
			result = "PASSED"
		}

	} else {
		result = fmt.Sprintf("int32Column: %d, decimalColumn %v", int32Column, decimalColumn)
	}

	os.Stdout.WriteString(result)

}
Exemplo n.º 11
0
Arquivo: basic.go Projeto: qtabot/cqlc
func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, BASIC)

	result := "FAILED"

	ctx := cqlc.NewContext()

	basic := Basic{
		Id:              "x",
		Int32Column:     111,
		Int64Column:     1 << 32,
		AsciiColumn:     "ABC",
		TimestampColumn: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), // Keep it simple for reflect.DeepEqual
		BooleanColumn:   true,
		TextColumn:      "foo",
		VarcharColumn:   "bar",
		FloatColumn:     math.MaxFloat32,
		DoubleColumn:    math.MaxFloat64,
		DecimalColumn:   inf.NewDec(1, 3),
		TimeuuidColumn:  gocql.TimeUUID(),
		MapColumn:       map[string]string{"baz": "quux"},
		ArrayColumn:     []string{"baz", "quux"},
	}

	create(ctx, session, basic)

	iter, err := ctx.Select(
		BASIC.ID,
		BASIC.ASCII_COLUMN,
		BASIC.INT32_COLUMN,
		BASIC.INT64_COLUMN,
		BASIC.FLOAT_COLUMN,
		BASIC.DOUBLE_COLUMN,
		BASIC.DECIMAL_COLUMN,
		BASIC.TIMESTAMP_COLUMN,
		BASIC.TIMEUUID_COLUMN,
		BASIC.BOOLEAN_COLUMN,
		BASIC.TEXT_COLUMN,
		BASIC.VARCHAR_COLUMN,
		BASIC.MAP_COLUMN,
		BASIC.ARRAY_COLUMN).
		From(BASIC).
		Where(BASIC.ID.Eq("x")).
		Fetch(session)

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	result, _ = checkBasics(iter, basic)

	iter, err = ctx.Select(
		BASIC.ID,
		BASIC.ASCII_COLUMN,
		BASIC.INT32_COLUMN,
		BASIC.INT64_COLUMN,
		BASIC.FLOAT_COLUMN,
		BASIC.DOUBLE_COLUMN,
		BASIC.DECIMAL_COLUMN,
		BASIC.TIMESTAMP_COLUMN,
		BASIC.TIMEUUID_COLUMN,
		BASIC.BOOLEAN_COLUMN,
		BASIC.TEXT_COLUMN,
		BASIC.VARCHAR_COLUMN,
		BASIC.MAP_COLUMN,
		BASIC.ARRAY_COLUMN).
		From(BASIC).
		Fetch(session)

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	result, _ = checkBasics(iter, basic)

	// TODO write test case for a non-matching WHERE clause

	os.Stdout.WriteString(result)
}
Exemplo n.º 12
0
func main() {
	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, FIRST_TIMELINE)
	integration.Truncate(session, SECOND_TIMELINE)

	result := "FAILED"

	timestamp := gocql.TimeUUID()

	first := FirstTimeline{
		When: timestamp,
		Tag:  "foobar",
	}

	second := SecondTimeline{
		When:      timestamp,
		Latitude:  50.12,
		Longitude: 0.87,
	}

	ctx := cqlc.NewContext()
	err := ctx.Store(FIRST_TIMELINE.Bind(first)).Exec(session)
	err = ctx.Store(SECOND_TIMELINE.Bind(second)).Exec(session)

	if err != nil {
		log.Fatalf("Could not execute upsert: %v", err)
		os.Exit(1)
	}

	var tag string
	var latitude float32

	f1, err := fetchOne(ctx, session, FIRST_TIMELINE, timestamp, FIRST_TIMELINE.TAG.To(&tag))
	f2, err := fetchOne(ctx, session, SECOND_TIMELINE, timestamp, SECOND_TIMELINE.LATITUDE.To(&latitude))

	if err != nil {
		log.Fatalf("Could not execute select: %v", err)
		os.Exit(1)
	}

	if f1 && f2 && tag == "foobar" && math.Float32bits(latitude) == math.Float32bits(50.12) {

		// TODO Implement a FROM binding
		t := "bar"
		l := float32(72.34)

		err = upsert(ctx, session, FIRST_TIMELINE, timestamp, FIRST_TIMELINE.TAG.To(&t))
		err = upsert(ctx, session, SECOND_TIMELINE, timestamp, SECOND_TIMELINE.LATITUDE.To(&l))

		if err != nil {
			log.Fatalf("Could not execute upsert: %v", err)
			os.Exit(1)
		}

		f1, err = fetchOne(ctx, session, FIRST_TIMELINE, timestamp, FIRST_TIMELINE.TAG.To(&tag))
		f2, err = fetchOne(ctx, session, SECOND_TIMELINE, timestamp, SECOND_TIMELINE.LATITUDE.To(&latitude))

		if err != nil {
			log.Fatalf("Could not execute select: %v", err)
			os.Exit(1)
		}

		if f1 && f2 && tag == t && math.Float32bits(latitude) == math.Float32bits(l) {

			err = deleteByTimestamp(ctx, session, FIRST_TIMELINE, timestamp)
			err = deleteByTimestamp(ctx, session, SECOND_TIMELINE, timestamp)

			if err != nil {
				log.Fatalf("Could not execute delete: %v", err)
				os.Exit(1)
			}

			var tag string
			var latitude float32

			f1, err = fetchOne(ctx, session, FIRST_TIMELINE, timestamp, FIRST_TIMELINE.TAG.To(&tag))
			f2, err = fetchOne(ctx, session, SECOND_TIMELINE, timestamp, SECOND_TIMELINE.LATITUDE.To(&latitude))

			if err != nil {
				log.Fatalf("Could not execute select: %v", err)
				os.Exit(1)
			}

			if !f1 && !f2 && tag == "" && latitude == 0.0 {
				result = "PASSED"
			} else {
				result = fmt.Sprintf("After delete - Tag was: %s; Latitude was %f", tag, latitude)
			}
		} else {
			result = fmt.Sprintf("After upsert - Tag was: %s; Latitude was %f", tag, latitude)
		}

	} else {
		result = fmt.Sprintf("Before delete - Tag was: %s; Latitude was %f", tag, latitude)
	}

	os.Stdout.WriteString(result)
}