Exemple #1
0
func TestQueryResult(t *testing.T) {
	want := "\xcb\x00\x00\x00\x04Fields\x009\x00\x00\x00\x030\x001\x00\x00\x00\x05Name\x00\x04\x00\x00\x00\x00name\x12Type\x00\x01\x00\x00\x00\x00\x00\x00\x00\x12Flags\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12RowsAffected\x00\x02\x00\x00\x00\x00\x00\x00\x00\x12InsertId\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04Rows\x00 \x00\x00\x00\x040\x00\x18\x00\x00\x00\x050\x00\x01\x00\x00\x00\x001\x051\x00\x02\x00\x00\x00\x00aa\x00\x00\x03Err\x002\x00\x00\x00\x12Code\x00\xe8\x03\x00\x00\x00\x00\x00\x00\x05Message\x00\x11\x00\x00\x00\x00failed due to err\x00\x00"
	custom := QueryResult{
		Fields:       []Field{{"name", 1, VT_ZEROVALUE_FLAG}},
		RowsAffected: 2,
		InsertId:     3,
		Rows: [][]sqltypes.Value{
			{{sqltypes.Numeric("1")}, {sqltypes.String("aa")}},
		},
		Err: &RPCError{1000, "failed due to err"},
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	var unmarshalled QueryResult
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if unmarshalled.Err == nil {
		t.Errorf("want %#v, got %#v", custom.Err, unmarshalled.Err)
	} else {
		if *custom.Err != *unmarshalled.Err {
			t.Errorf("want %#v, got %#v", custom.Err, unmarshalled.Err)
		}
	}
	if custom.RowsAffected != unmarshalled.RowsAffected {
		t.Errorf("want %v, got %#v", custom.RowsAffected, unmarshalled.RowsAffected)
	}
	if custom.InsertId != unmarshalled.InsertId {
		t.Errorf("want %v, got %#v", custom.InsertId, unmarshalled.InsertId)
	}
	if custom.Fields[0].Name != unmarshalled.Fields[0].Name {
		t.Errorf("want %v, got %#v", custom.Fields[0].Name, unmarshalled.Fields[0].Name)
	}
	if custom.Fields[0].Type != unmarshalled.Fields[0].Type {
		t.Errorf("want %v, got %#v", custom.Fields[0].Type, unmarshalled.Fields[0].Type)
	}
	if !bytes.Equal(custom.Rows[0][0].Raw(), unmarshalled.Rows[0][0].Raw()) {
		t.Errorf("want %s, got %s", custom.Rows[0][0].Raw(), unmarshalled.Rows[0][0].Raw())
	}
	if !bytes.Equal(custom.Rows[0][1].Raw(), unmarshalled.Rows[0][1].Raw()) {
		t.Errorf("want %s, got %s", custom.Rows[0][0].Raw(), unmarshalled.Rows[0][0].Raw())
	}

	extra, err := bson.Marshal(&extraQueryResult{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(extra, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
}
Exemple #2
0
func TestQueryResult(t *testing.T) {
	want := "\x85\x00\x00\x00\x04Fields\x00*\x00\x00\x00\x030\x00\"\x00\x00\x00\x05Name\x00\x04\x00\x00\x00\x00name\x12Type\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12RowsAffected\x00\x02\x00\x00\x00\x00\x00\x00\x00\x12InsertId\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04Rows\x00 \x00\x00\x00\x040\x00\x18\x00\x00\x00\x050\x00\x01\x00\x00\x00\x001\x051\x00\x02\x00\x00\x00\x00aa\x00\x00\x00"
	custom := QueryResult{
		Fields:       []Field{{"name", 1}},
		RowsAffected: 2,
		InsertId:     3,
		Rows: [][]sqltypes.Value{
			{{sqltypes.Numeric("1")}, {sqltypes.String("aa")}},
		},
	}
	encoded, err := bson.Marshal(&custom)
	if err != nil {
		t.Error(err)
	}
	got := string(encoded)
	if want != got {
		t.Errorf("want\n%#v, got\n%#v", want, got)
	}

	var unmarshalled QueryResult
	err = bson.Unmarshal(encoded, &unmarshalled)
	if err != nil {
		t.Error(err)
	}
	if custom.RowsAffected != unmarshalled.RowsAffected {
		t.Errorf("want %v, got %#v", custom.RowsAffected, unmarshalled.RowsAffected)
	}
	if custom.InsertId != unmarshalled.InsertId {
		t.Errorf("want %v, got %#v", custom.InsertId, unmarshalled.InsertId)
	}
	if custom.Fields[0].Name != unmarshalled.Fields[0].Name {
		t.Errorf("want %v, got %#v", custom.Fields[0].Name, unmarshalled.Fields[0].Name)
	}
	if custom.Fields[0].Type != unmarshalled.Fields[0].Type {
		t.Errorf("want %v, got %#v", custom.Fields[0].Type, unmarshalled.Fields[0].Type)
	}
	if !bytes.Equal(custom.Rows[0][0].Raw(), unmarshalled.Rows[0][0].Raw()) {
		t.Errorf("want %s, got %s", custom.Rows[0][0].Raw(), unmarshalled.Rows[0][0].Raw())
	}
	if !bytes.Equal(custom.Rows[0][1].Raw(), unmarshalled.Rows[0][1].Raw()) {
		t.Errorf("want %s, got %s", custom.Rows[0][0].Raw(), unmarshalled.Rows[0][0].Raw())
	}

	unexpected, err := bson.Marshal(&badQueryResult{})
	if err != nil {
		t.Error(err)
	}
	err = bson.Unmarshal(unexpected, &unmarshalled)
	want = "Unrecognized tag Extra"
	if err == nil || want != err.Error() {
		t.Errorf("want %v, got %v", want, err)
	}
}
Exemple #3
0
func TestCharaterSet(t *testing.T) {
	qr, err := framework.NewDefaultClient().Execute("select * from vtocc_test where intval=1", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "intval",
				Type:  3,
				Flags: 0,
			}, {
				Name:  "floatval",
				Type:  4,
				Flags: 0,
			}, {
				Name:  "charval",
				Type:  253,
				Flags: 0,
			}, {
				Name:  "binval",
				Type:  253,
				Flags: mysql.FlagBinary,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.Fractional("1.12345")},
				sqltypes.Value{Inner: sqltypes.String("\xc2\xa2")},
				sqltypes.Value{Inner: sqltypes.String("\x00\xff")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
}
Exemple #4
0
func (sbc *sandboxConn) Rollback(transactionId int64) error {
	sbc.ExecCount.Add(1)
	sbc.RollbackCount.Add(1)
	if sbc.mustDelay != 0 {
		time.Sleep(sbc.mustDelay)
	}
	return sbc.getError()
}

// Close does not change ExecCount
func (sbc *sandboxConn) Close() {
	sbc.CloseCount.Add(1)
}

func (sbc *sandboxConn) EndPoint() topo.EndPoint {
	return sbc.endPoint
}

var singleRowResult = &mproto.QueryResult{
	Fields: []mproto.Field{
		{"id", 3},
		{"value", 253}},
	RowsAffected: 1,
	InsertId:     0,
	Rows: [][]sqltypes.Value{{
		{sqltypes.Numeric("1")},
		{sqltypes.String("foo")},
	}},
}
Exemple #5
0
func TestInts(t *testing.T) {
	client := framework.NewDefaultClient()
	defer client.Execute("delete from vtocc_ints", nil)

	_, err := client.Execute(
		"insert into vtocc_ints values(:tiny, :tinyu, :small, "+
			":smallu, :medium, :mediumu, :normal, :normalu, :big, :bigu, :year)",
		map[string]interface{}{
			"tiny":    int32(-128),
			"tinyu":   uint32(255),
			"small":   int32(-32768),
			"smallu":  uint32(65535),
			"medium":  int32(-8388608),
			"mediumu": uint32(16777215),
			"normal":  int64(-2147483648),
			"normalu": uint64(4294967295),
			"big":     int64(-9223372036854775808),
			"bigu":    uint64(18446744073709551615),
			"year":    2012,
		},
	)
	if err != nil {
		t.Error(err)
		return
	}
	qr, err := client.Execute("select * from vtocc_ints where tiny = -128", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "tiny",
				Type:  mysql.TypeTiny,
				Flags: 0,
			}, {
				Name:  "tinyu",
				Type:  mysql.TypeTiny,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "small",
				Type:  mysql.TypeShort,
				Flags: 0,
			}, {
				Name:  "smallu",
				Type:  mysql.TypeShort,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "medium",
				Type:  mysql.TypeInt24,
				Flags: 0,
			}, {
				Name:  "mediumu",
				Type:  mysql.TypeInt24,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "normal",
				Type:  mysql.TypeLong,
				Flags: 0,
			}, {
				Name:  "normalu",
				Type:  mysql.TypeLong,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "big",
				Type:  mysql.TypeLonglong,
				Flags: 0,
			}, {
				Name:  "bigu",
				Type:  mysql.TypeLonglong,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "y",
				Type:  mysql.TypeYear,
				Flags: mysql.FlagUnsigned,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("-128")},
				sqltypes.Value{Inner: sqltypes.Numeric("255")},
				sqltypes.Value{Inner: sqltypes.Numeric("-32768")},
				sqltypes.Value{Inner: sqltypes.Numeric("65535")},
				sqltypes.Value{Inner: sqltypes.Numeric("-8388608")},
				sqltypes.Value{Inner: sqltypes.Numeric("16777215")},
				sqltypes.Value{Inner: sqltypes.Numeric("-2147483648")},
				sqltypes.Value{Inner: sqltypes.Numeric("4294967295")},
				sqltypes.Value{Inner: sqltypes.Numeric("-9223372036854775808")},
				sqltypes.Value{Inner: sqltypes.Numeric("18446744073709551615")},
				sqltypes.Value{Inner: sqltypes.Numeric("2012")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
}
Exemple #6
0
func TestMiscTypes(t *testing.T) {
	client := framework.NewDefaultClient()
	defer client.Execute("delete from vtocc_misc", nil)

	_, err := client.Execute(
		"insert into vtocc_misc values(:id, :b, :d, :dt, :t)",
		map[string]interface{}{
			"id": 1,
			"b":  "\x01",
			"d":  "2012-01-01",
			"dt": "2012-01-01 15:45:45",
			"t":  "15:45:45",
		},
	)
	if err != nil {
		t.Error(err)
		return
	}
	qr, err := client.Execute("select * from vtocc_misc where id = 1", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "id",
				Type:  mysql.TypeLong,
				Flags: 0,
			}, {
				Name:  "b",
				Type:  mysql.TypeBit,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "d",
				Type:  mysql.TypeDate,
				Flags: mysql.FlagBinary,
			}, {
				Name:  "dt",
				Type:  mysql.TypeDatetime,
				Flags: mysql.FlagBinary,
			}, {
				Name:  "t",
				Type:  mysql.TypeTime,
				Flags: mysql.FlagBinary,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.String("\x01")},
				sqltypes.Value{Inner: sqltypes.String("2012-01-01")},
				sqltypes.Value{Inner: sqltypes.String("2012-01-01 15:45:45")},
				sqltypes.Value{Inner: sqltypes.String("15:45:45")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
}
Exemple #7
0
func TestFractionals(t *testing.T) {
	client := framework.NewDefaultClient()
	defer client.Execute("delete from vtocc_fracts", nil)

	_, err := client.Execute(
		"insert into vtocc_fracts values(:id, :deci, :num, :f, :d)",
		map[string]interface{}{
			"id":   1,
			"deci": "1.99",
			"num":  "2.99",
			"f":    3.99,
			"d":    4.99,
		},
	)
	if err != nil {
		t.Error(err)
		return
	}
	qr, err := client.Execute("select * from vtocc_fracts where id = 1", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "id",
				Type:  mysql.TypeLong,
				Flags: 0,
			}, {
				Name:  "deci",
				Type:  mysql.TypeNewDecimal,
				Flags: 0,
			}, {
				Name:  "num",
				Type:  mysql.TypeNewDecimal,
				Flags: 0,
			}, {
				Name:  "f",
				Type:  mysql.TypeFloat,
				Flags: 0,
			}, {
				Name:  "d",
				Type:  mysql.TypeDouble,
				Flags: 0,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.Fractional("1.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("2.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("3.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("4.99")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
}
Exemple #8
0
func TestBatchTransaction(t *testing.T) {
	client := framework.NewClient()
	queries := []proto.BoundQuery{{
		Sql: "insert into vitess_test values(4, null, null, null)",
	}, {
		Sql: "select * from vitess_test where intval = 4",
	}, {
		Sql: "delete from vitess_test where intval = 4",
	}}

	wantRows := [][]sqltypes.Value{
		[]sqltypes.Value{
			sqltypes.Value{Inner: sqltypes.Numeric("4")},
			sqltypes.Value{},
			sqltypes.Value{},
			sqltypes.Value{},
		},
	}

	// Not in transaction, AsTransaction false
	qrl, err := client.ExecuteBatch(queries, false)
	if err != nil {
		t.Error(err)
		return
	}
	if !reflect.DeepEqual(qrl.List[1].Rows, wantRows) {
		t.Errorf("Rows: \n%#v, want \n%#v", qrl.List[1].Rows, wantRows)
	}

	// Not in transaction, AsTransaction true
	qrl, err = client.ExecuteBatch(queries, true)
	if err != nil {
		t.Error(err)
		return
	}
	if !reflect.DeepEqual(qrl.List[1].Rows, wantRows) {
		t.Errorf("Rows: \n%#v, want \n%#v", qrl.List[1].Rows, wantRows)
	}

	// In transaction, AsTransaction false
	func() {
		err = client.Begin()
		if err != nil {
			t.Error(err)
			return
		}
		defer client.Commit()
		qrl, err = client.ExecuteBatch(queries, false)
		if err != nil {
			t.Error(err)
			return
		}
		if !reflect.DeepEqual(qrl.List[1].Rows, wantRows) {
			t.Errorf("Rows: \n%#v, want \n%#v", qrl.List[1].Rows, wantRows)
		}
	}()

	// In transaction, AsTransaction true
	func() {
		err = client.Begin()
		if err != nil {
			t.Error(err)
			return
		}
		defer client.Rollback()
		qrl, err = client.ExecuteBatch(queries, true)
		want := "error: cannot start a new transaction in the scope of an existing one"
		if err == nil || err.Error() != want {
			t.Errorf("Error: %v, want %s", err, want)
		}
	}()
}
Exemple #9
0
func TestBindInSelect(t *testing.T) {
	client := framework.NewClient()

	// Int bind var.
	qr, err := client.Execute(
		"select :bv from dual",
		map[string]interface{}{"bv": 1},
	)
	if err != nil {
		t.Error(err)
		return
	}
	want := &mproto.QueryResult{
		Fields: []mproto.Field{{
			Name:  "1",
			Type:  mysql.TypeLonglong,
			Flags: mysql.FlagBinary,
		}},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
			},
		},
	}
	if !reflect.DeepEqual(qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", qr, want)
	}

	// String bind var.
	qr, err = client.Execute(
		"select :bv from dual",
		map[string]interface{}{"bv": "abcd"},
	)
	if err != nil {
		t.Error(err)
		return
	}
	want = &mproto.QueryResult{
		Fields: []mproto.Field{{
			Name:  "abcd",
			Type:  mysql.TypeVarString,
			Flags: 0,
		}},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.String("abcd")},
			},
		},
	}
	if !reflect.DeepEqual(qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", qr, want)
	}

	// Binary bind var.
	qr, err = client.Execute(
		"select :bv from dual",
		map[string]interface{}{"bv": "\x00\xff"},
	)
	if err != nil {
		t.Error(err)
		return
	}
	want = &mproto.QueryResult{
		Fields: []mproto.Field{{
			Name:  "",
			Type:  mysql.TypeVarString,
			Flags: 0,
		}},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.String("\x00\xff")},
			},
		},
	}
	if !reflect.DeepEqual(qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", qr, want)
	}
}
Exemple #10
0
func TestBatchRead(t *testing.T) {
	client := framework.NewClient()
	queries := []proto.BoundQuery{{
		Sql:           "select * from vitess_a where id = :a",
		BindVariables: map[string]interface{}{"a": 2},
	}, {
		Sql:           "select * from vitess_b where id = :b",
		BindVariables: map[string]interface{}{"b": 2},
	}}
	qr1 := mproto.QueryResult{
		Fields: []mproto.Field{{
			Name:  "eid",
			Type:  mysql.TypeLonglong,
			Flags: 0,
		}, {
			Name:  "id",
			Type:  mysql.TypeLong,
			Flags: 0,
		}, {
			Name:  "name",
			Type:  mysql.TypeVarString,
			Flags: 0,
		}, {
			Name:  "foo",
			Type:  mysql.TypeVarString,
			Flags: mysql.FlagBinary,
		}},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.Numeric("2")},
				sqltypes.Value{Inner: sqltypes.String("bcde")},
				sqltypes.Value{Inner: sqltypes.String("fghi")},
			},
		},
	}
	qr2 := mproto.QueryResult{
		Fields: []mproto.Field{{
			Name:  "eid",
			Type:  mysql.TypeLonglong,
			Flags: 0,
		}, {
			Name:  "id",
			Type:  mysql.TypeLong,
			Flags: 0,
		}},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.Numeric("2")},
			},
		},
	}
	want := &proto.QueryResultList{
		List: []mproto.QueryResult{qr1, qr2},
	}

	qrl, err := client.ExecuteBatch(queries, false)
	if err != nil {
		t.Error(err)
		return
	}
	if !reflect.DeepEqual(qrl, want) {
		t.Errorf("ExecueBatch: \n%#v, want \n%#v", qrl, want)
	}
}
Exemple #11
0
func TestCompareRows(t *testing.T) {
	table := []struct {
		fields      []mproto.Field
		left, right []sqltypes.Value
		want        int
	}{
		{
			fields: []mproto.Field{{"a", mproto.VT_LONG, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Numeric("123")}},
			right:  []sqltypes.Value{{sqltypes.Numeric("14")}},
			want:   1,
		},
		{
			fields: []mproto.Field{
				{"a", mproto.VT_LONG, mproto.VT_ZEROVALUE_FLAG},
				{"b", mproto.VT_LONG, mproto.VT_ZEROVALUE_FLAG},
			},
			left: []sqltypes.Value{
				{sqltypes.Numeric("555")},
				{sqltypes.Numeric("12")},
			},
			right: []sqltypes.Value{
				{sqltypes.Numeric("555")},
				{sqltypes.Numeric("144")},
			},
			want: -1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_LONG, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Numeric("144")}},
			right:  []sqltypes.Value{{sqltypes.Numeric("144")}},
			want:   0,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_LONGLONG, mproto.VT_UNSIGNED_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Numeric("9223372036854775809")}},
			right:  []sqltypes.Value{{sqltypes.Numeric("9223372036854775810")}},
			want:   -1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_LONGLONG, mproto.VT_UNSIGNED_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Numeric("9223372036854775819")}},
			right:  []sqltypes.Value{{sqltypes.Numeric("9223372036854775810")}},
			want:   1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_DOUBLE, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Fractional("3.14")}},
			right:  []sqltypes.Value{{sqltypes.Fractional("3.2")}},
			want:   -1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_DOUBLE, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Fractional("123.4")}},
			right:  []sqltypes.Value{{sqltypes.Fractional("123.2")}},
			want:   1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_STRING, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.String("abc")}},
			right:  []sqltypes.Value{{sqltypes.String("abb")}},
			want:   1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_STRING, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.String("abc")}},
			right:  []sqltypes.Value{{sqltypes.String("abd")}},
			want:   -1,
		},
	}
	for _, tc := range table {
		got, err := CompareRows(tc.fields, len(tc.fields), tc.left, tc.right)
		if err != nil {
			t.Errorf("CompareRows error: %v", err)
			continue
		}
		if got != tc.want {
			t.Errorf("CompareRows(%v, %v, %v) = %v, want %v", tc.fields, tc.left, tc.right, got, tc.want)
		}
	}
}
Exemple #12
0
func TestDeleteEqual(t *testing.T) {
	router, sbc, _, sbclookup := createRouterEnv()

	sbc.setResults([]*mproto.QueryResult{&mproto.QueryResult{
		Fields: []mproto.Field{
			{"id", 3, mproto.VT_ZEROVALUE_FLAG},
			{"name", 253, mproto.VT_ZEROVALUE_FLAG},
		},
		RowsAffected: 1,
		InsertId:     0,
		Rows: [][]sqltypes.Value{{
			{sqltypes.Numeric("1")},
			{sqltypes.String("myname")},
		}},
	}})
	_, err := routerExec(router, "delete from user where id = 1", nil)
	if err != nil {
		t.Error(err)
	}
	wantQueries := []tproto.BoundQuery{{
		Sql:           "select id, name from user where id = 1 for update",
		BindVariables: map[string]interface{}{},
	}, {
		Sql: "delete from user where id = 1 /* _routing keyspace_id:166b40b44aba4bd6 */",
		BindVariables: map[string]interface{}{
			"keyspace_id": "\x16k@\xb4J\xbaK\xd6",
		},
	}}
	if !reflect.DeepEqual(sbc.Queries, wantQueries) {
		t.Errorf("sbc.Queries: %+v, want %+v\n", sbc.Queries, wantQueries)
	}

	wantQueries = []tproto.BoundQuery{{
		Sql: "delete from user_idx where id in ::id",
		BindVariables: map[string]interface{}{
			"id": []interface{}{int64(1)},
		},
	}, {
		Sql: "delete from name_user_map where name in ::name and user_id = :user_id",
		BindVariables: map[string]interface{}{
			"user_id": int64(1),
			"name":    []interface{}{"myname"},
		},
	}}
	if !reflect.DeepEqual(sbclookup.Queries, wantQueries) {
		t.Errorf("sbclookup.Queries: %+v, want %+v\n", sbclookup.Queries, wantQueries)
	}

	sbc.Queries = nil
	sbclookup.Queries = nil
	sbc.setResults([]*mproto.QueryResult{&mproto.QueryResult{}})
	_, err = routerExec(router, "delete from user where id = 1", nil)
	if err != nil {
		t.Error(err)
	}
	wantQueries = []tproto.BoundQuery{{
		Sql:           "select id, name from user where id = 1 for update",
		BindVariables: map[string]interface{}{},
	}, {
		Sql: "delete from user where id = 1 /* _routing keyspace_id:166b40b44aba4bd6 */",
		BindVariables: map[string]interface{}{
			"keyspace_id": "\x16k@\xb4J\xbaK\xd6",
		},
	}}
	if !reflect.DeepEqual(sbc.Queries, wantQueries) {
		t.Errorf("sbc.Queries: %+v, want %+v\n", sbc.Queries, wantQueries)
	}
	if sbclookup.Queries != nil {
		t.Errorf("sbclookup.Queries: %+v, want nil\n", sbclookup.Queries)
	}

	sbc.Queries = nil
	sbclookup.Queries = nil
	sbclookup.setResults([]*mproto.QueryResult{&mproto.QueryResult{}})
	_, err = routerExec(router, "delete from music where id = 1", nil)
	if err != nil {
		t.Error(err)
	}
	wantQueries = []tproto.BoundQuery{{
		Sql: "select user_id from music_user_map where music_id = :music_id",
		BindVariables: map[string]interface{}{
			"music_id": int64(1),
		},
	}}
	if !reflect.DeepEqual(sbclookup.Queries, wantQueries) {
		t.Errorf("sbclookup.Queries: %+v, want %+v\n", sbclookup.Queries, wantQueries)
	}
	if sbc.Queries != nil {
		t.Errorf("sbc.Queries: %+v, want nil\n", sbc.Queries)
	}
}
Exemple #13
0
func TestInts(t *testing.T) {
	client := framework.NewClient()
	defer client.Execute("delete from vitess_ints", nil)

	_, err := client.Execute(
		"insert into vitess_ints values(:tiny, :tinyu, :small, "+
			":smallu, :medium, :mediumu, :normal, :normalu, :big, :bigu, :year)",
		map[string]interface{}{
			"tiny":    int32(-128),
			"tinyu":   uint32(255),
			"small":   int32(-32768),
			"smallu":  uint32(65535),
			"medium":  int32(-8388608),
			"mediumu": uint32(16777215),
			"normal":  int64(-2147483648),
			"normalu": uint64(4294967295),
			"big":     int64(-9223372036854775808),
			"bigu":    uint64(18446744073709551615),
			"year":    2012,
		},
	)
	if err != nil {
		t.Error(err)
		return
	}
	qr, err := client.Execute("select * from vitess_ints where tiny = -128", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "tiny",
				Type:  mysql.TypeTiny,
				Flags: 0,
			}, {
				Name:  "tinyu",
				Type:  mysql.TypeTiny,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "small",
				Type:  mysql.TypeShort,
				Flags: 0,
			}, {
				Name:  "smallu",
				Type:  mysql.TypeShort,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "medium",
				Type:  mysql.TypeInt24,
				Flags: 0,
			}, {
				Name:  "mediumu",
				Type:  mysql.TypeInt24,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "normal",
				Type:  mysql.TypeLong,
				Flags: 0,
			}, {
				Name:  "normalu",
				Type:  mysql.TypeLong,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "big",
				Type:  mysql.TypeLonglong,
				Flags: 0,
			}, {
				Name:  "bigu",
				Type:  mysql.TypeLonglong,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "y",
				Type:  mysql.TypeYear,
				Flags: mysql.FlagUnsigned,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("-128")},
				sqltypes.Value{Inner: sqltypes.Numeric("255")},
				sqltypes.Value{Inner: sqltypes.Numeric("-32768")},
				sqltypes.Value{Inner: sqltypes.Numeric("65535")},
				sqltypes.Value{Inner: sqltypes.Numeric("-8388608")},
				sqltypes.Value{Inner: sqltypes.Numeric("16777215")},
				sqltypes.Value{Inner: sqltypes.Numeric("-2147483648")},
				sqltypes.Value{Inner: sqltypes.Numeric("4294967295")},
				sqltypes.Value{Inner: sqltypes.Numeric("-9223372036854775808")},
				sqltypes.Value{Inner: sqltypes.Numeric("18446744073709551615")},
				sqltypes.Value{Inner: sqltypes.Numeric("2012")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
	wantTypes := []query.Type{
		sqltypes.Int8,
		sqltypes.Uint8,
		sqltypes.Int16,
		sqltypes.Uint16,
		sqltypes.Int24,
		sqltypes.Uint24,
		sqltypes.Int32,
		sqltypes.Uint32,
		sqltypes.Int64,
		sqltypes.Uint64,
		sqltypes.Year,
	}
	for i, field := range qr.Fields {
		got, err := sqltypes.MySQLToType(field.Type, field.Flags)
		if err != nil {
			t.Errorf("col: %d, err: %v", i, err)
			continue
		}
		if got != wantTypes[i] {
			t.Errorf("Unexpected type: col: %d, %d, want %d", i, got, wantTypes[i])
		}
	}
}
Exemple #14
0
func TestMiscTypes(t *testing.T) {
	client := framework.NewClient()
	defer client.Execute("delete from vitess_misc", nil)

	_, err := client.Execute(
		"insert into vitess_misc values(:id, :b, :d, :dt, :t)",
		map[string]interface{}{
			"id": 1,
			"b":  "\x01",
			"d":  "2012-01-01",
			"dt": "2012-01-01 15:45:45",
			"t":  "15:45:45",
		},
	)
	if err != nil {
		t.Error(err)
		return
	}
	qr, err := client.Execute("select * from vitess_misc where id = 1", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "id",
				Type:  mysql.TypeLong,
				Flags: 0,
			}, {
				Name:  "b",
				Type:  mysql.TypeBit,
				Flags: mysql.FlagUnsigned,
			}, {
				Name:  "d",
				Type:  mysql.TypeDate,
				Flags: mysql.FlagBinary,
			}, {
				Name:  "dt",
				Type:  mysql.TypeDatetime,
				Flags: mysql.FlagBinary,
			}, {
				Name:  "t",
				Type:  mysql.TypeTime,
				Flags: mysql.FlagBinary,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.String("\x01")},
				sqltypes.Value{Inner: sqltypes.String("2012-01-01")},
				sqltypes.Value{Inner: sqltypes.String("2012-01-01 15:45:45")},
				sqltypes.Value{Inner: sqltypes.String("15:45:45")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
	wantTypes := []query.Type{
		sqltypes.Int32,
		sqltypes.Bit,
		sqltypes.Date,
		sqltypes.Datetime,
		sqltypes.Time,
	}
	for i, field := range qr.Fields {
		got, err := sqltypes.MySQLToType(field.Type, field.Flags)
		if err != nil {
			t.Errorf("col: %d, err: %v", i, err)
			continue
		}
		if got != wantTypes[i] {
			t.Errorf("Unexpected type: col: %d, %d, want %d", i, got, wantTypes[i])
		}
	}
}
Exemple #15
0
func TestFractionals(t *testing.T) {
	client := framework.NewClient()
	defer client.Execute("delete from vitess_fracts", nil)

	_, err := client.Execute(
		"insert into vitess_fracts values(:id, :deci, :num, :f, :d)",
		map[string]interface{}{
			"id":   1,
			"deci": "1.99",
			"num":  "2.99",
			"f":    3.99,
			"d":    4.99,
		},
	)
	if err != nil {
		t.Error(err)
		return
	}
	qr, err := client.Execute("select * from vitess_fracts where id = 1", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "id",
				Type:  mysql.TypeLong,
				Flags: 0,
			}, {
				Name:  "deci",
				Type:  mysql.TypeNewDecimal,
				Flags: 0,
			}, {
				Name:  "num",
				Type:  mysql.TypeNewDecimal,
				Flags: 0,
			}, {
				Name:  "f",
				Type:  mysql.TypeFloat,
				Flags: 0,
			}, {
				Name:  "d",
				Type:  mysql.TypeDouble,
				Flags: 0,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.Fractional("1.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("2.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("3.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("4.99")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
	wantTypes := []query.Type{
		sqltypes.Int32,
		sqltypes.Decimal,
		sqltypes.Decimal,
		sqltypes.Float32,
		sqltypes.Float64,
	}
	for i, field := range qr.Fields {
		got, err := sqltypes.MySQLToType(field.Type, field.Flags)
		if err != nil {
			t.Errorf("col: %d, err: %v", i, err)
			continue
		}
		if got != wantTypes[i] {
			t.Errorf("Unexpected type: col: %d, %d, want %d", i, got, wantTypes[i])
		}
	}
}