Exemplo n.º 1
0
// Exec implements the stmt.Statement Exec interface.
func (s *DeleteStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
	// TODO: unify single from and multi from together.
	if s.MultiTable {
		return s.execMultiTable(ctx)
	}
	t, err := getTable(ctx, s.TableIdent)
	if err != nil {
		return nil, err
	}

	ok, err := s.tryDeleteUsingIndex(ctx, t)
	if err != nil {
		return nil, err
	}
	if ok {
		// Delete using index OK.
		log.Info("try delete with index OK")
		return nil, nil
	}

	fields := field.ColsToResultFields(t.Cols(), t.TableName().L)

	var cnt uint64
	err = t.IterRecords(ctx, t.FirstKey(), t.Cols(), func(h int64, data []interface{}, cols []*column.Col) (bool, error) {
		if s.Limit != nil && cnt >= s.Limit.Count {
			return false, nil
		}
		ok, err = s.hitWhere(ctx, fields, data)
		if err != nil {
			return false, errors.Trace(err)
		}
		if !ok {
			return true, nil
		}
		err = s.removeRow(ctx, t, h, data)
		if err != nil {
			return false, err
		}
		cnt++
		return true, nil
	})
	if err != nil {
		return nil, err
	}
	return nil, nil
}
Exemplo n.º 2
0
// GetFields implements plan.Plan GetFields interface.
func (r *indexPlan) GetFields() []*field.ResultField {
	return field.ColsToResultFields(r.src.Cols(), r.src.TableName().O)
}
Exemplo n.º 3
0
func (*testResultFieldSuite) TestMain(c *C) {
	col := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeLong),
			Name:      model.NewCIStr("c1"),
		},
	}
	col.Flag |= mysql.UnsignedFlag

	r := &field.ResultField{
		Col:          col,
		Name:         "c1",
		OrgTableName: "t1",
	}

	c.Assert(r.String(), Equals, "c1")
	r.TableName = "a"
	c.Assert(r.String(), Equals, "a.c1")
	r.DBName = "test"
	c.Assert(r.String(), Equals, "test.a.c1")

	cr := r.Clone()
	c.Assert(r.String(), Equals, cr.String())

	col1 := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeLong),
			Name:      model.NewCIStr("c2"),
		},
	}
	col1.Flag |= mysql.UnsignedFlag

	r1 := &field.ResultField{
		Col:          col1,
		Name:         "c2",
		TableName:    "a",
		OrgTableName: "t1",
		DBName:       "test",
	}

	rs := []*field.ResultField{r, r1}
	ns := field.RFQNames(rs)
	c.Assert(ns, HasLen, 2)
	c.Assert(ns[0], Equals, "\"c1\"")
	c.Assert(ns[1], Equals, "\"c2\"")

	col2 := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeVarchar),
			Name:      model.NewCIStr("c3"),
		},
	}
	col2.Flag |= mysql.UnsignedFlag
	col3 := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeBlob),
			Name:      model.NewCIStr("c4"),
		},
	}
	col3.Flag |= mysql.UnsignedFlag

	cols := []*column.Col{&col, &col1, &col2, &col3}
	rs = field.ColsToResultFields(cols, "t")
	c.Assert(rs, HasLen, 4)
	c.Assert(rs[2].Tp, Equals, mysql.TypeVarString)
	c.Assert(rs[3].Tp, Equals, mysql.TypeBlob)

	// For CheckAmbiguousField
	err := field.CheckAmbiguousField("c1", rs, field.OrgFieldNameFlag)
	c.Assert(err, IsNil)

	col4 := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeVarchar),
			Name:      model.NewCIStr("c2"),
		},
	}
	r2 := &field.ResultField{
		Col:          col4,
		Name:         "c22",
		TableName:    "b",
		OrgTableName: "t2",
		DBName:       "test",
	}
	rs = []*field.ResultField{r, r1, r2}
	// r1 and r2 are ambiguous: same column name but different table names
	err = field.CheckAmbiguousField("c2", rs, field.OrgFieldNameFlag)
	c.Assert(err, NotNil)
	// r1 and r2 with different alias name
	err = field.CheckAmbiguousField("c2", rs, field.FieldNameFlag)
	c.Assert(err, IsNil)

	// For CloneFieldByName
	_, err = field.CloneFieldByName("cx", rs, field.OrgFieldNameFlag)
	c.Assert(err, NotNil)
	_, err = field.CloneFieldByName("c2", rs, field.OrgFieldNameFlag)
	c.Assert(err, IsNil)

	// For check all fields name
	names := []string{"cx"}
	err = field.CheckAllFieldNames(names, rs, field.OrgFieldNameFlag)
	c.Assert(err, NotNil)
	names = []string{"c1"}
	err = field.CheckAllFieldNames(names, rs, field.OrgFieldNameFlag)
	c.Assert(err, IsNil)

	// For ContainAllFieldNames
	names = []string{"cx", "c2"}
	b := field.ContainAllFieldNames(names, rs, field.OrgFieldNameFlag)
	c.Assert(b, IsFalse)
	names = []string{"c2", "c1"}
	b = field.ContainAllFieldNames(names, rs, field.OrgFieldNameFlag)
	c.Assert(b, IsTrue)

	// For GetFieldIndex
	f1 := &field.Field{
		Expr:   &expression.Ident{CIStr: model.NewCIStr("c1")},
		AsName: "a",
	}
	f2 := &field.Field{
		Expr:   &expression.Ident{CIStr: model.NewCIStr("c2")},
		AsName: "a",
	}
	fs := []*field.Field{f1, f2}
	idxs := field.GetFieldIndex("c1", fs, field.OrgFieldNameFlag)
	c.Assert(idxs, HasLen, 1)

	idxs = field.GetFieldIndex("a", fs, field.FieldNameFlag)
	c.Assert(idxs, HasLen, 2)

}
Exemplo n.º 4
0
func (*testResultFieldSuite) TestMain(c *C) {
	col := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeLong),
			Name:      model.NewCIStr("c1"),
		},
	}
	col.Flag |= mysql.UnsignedFlag

	r := &field.ResultField{
		Col:          col,
		Name:         "c1",
		OrgTableName: "t1",
	}

	c.Assert(r.String(), Equals, "c1")
	r.TableName = "a"
	c.Assert(r.String(), Equals, "a.c1")
	r.DBName = "test"
	c.Assert(r.String(), Equals, "test.a.c1")

	cr := r.Clone()
	c.Assert(r.String(), Equals, cr.String())

	col1 := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeLong),
			Name:      model.NewCIStr("c2"),
		},
	}
	col1.Flag |= mysql.UnsignedFlag

	r1 := &field.ResultField{
		Col:          col1,
		Name:         "c2",
		TableName:    "a",
		OrgTableName: "t1",
		DBName:       "test",
	}

	rs := []*field.ResultField{r, r1}
	ns := field.RFQNames(rs)
	c.Assert(ns, HasLen, 2)
	c.Assert(ns[0], Equals, "\"c1\"")
	c.Assert(ns[1], Equals, "\"c2\"")

	col2 := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeVarchar),
			Name:      model.NewCIStr("c3"),
		},
	}
	col2.Flag |= mysql.UnsignedFlag
	col3 := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeBlob),
			Name:      model.NewCIStr("c4"),
		},
	}
	col3.Flag |= mysql.UnsignedFlag

	cols := []*column.Col{&col, &col1, &col2, &col3}
	rs = field.ColsToResultFields(cols, "t")
	c.Assert(rs, HasLen, 4)
	c.Assert(rs[2].Tp, Equals, mysql.TypeVarString)
	c.Assert(rs[3].Tp, Equals, mysql.TypeBlob)

	col4 := column.Col{
		ColumnInfo: model.ColumnInfo{
			FieldType: *types.NewFieldType(mysql.TypeVarchar),
			Name:      model.NewCIStr("c2"),
		},
	}
	r2 := &field.ResultField{
		Col:          col4,
		Name:         "c22",
		TableName:    "b",
		OrgTableName: "t2",
		DBName:       "test",
	}
	rs = []*field.ResultField{r, r1, r2}

	// For CloneFieldByName
	_, err := field.CloneFieldByName("cx", rs)
	c.Assert(err, NotNil)
	_, err = field.CloneFieldByName("c2", rs)
	c.Assert(err, IsNil)

	// For ContainAllFieldNames
	names := []string{"cx", "c2"}
	b := field.ContainAllFieldNames(names, rs)
	c.Assert(b, IsFalse)
	names = []string{"c2", "c1"}
	b = field.ContainAllFieldNames(names, rs)
	c.Assert(b, IsTrue)
}