Esempio n. 1
0
func getRoutingPlan(statement Statement, router *router.Router) (plan *RoutingPlan) {
	plan = &RoutingPlan{}
	var where *Where
	switch stmt := statement.(type) {
	case *Insert:
		if _, ok := stmt.Rows.(SelectStatement); ok {
			panic(NewParserError("select in insert not allowed"))
		}

		plan.rule = router.GetRule(String(stmt.Table))

		if stmt.OnDup != nil {
			checkUpdateExprs(UpdateExprs(stmt.OnDup), plan.rule)
		}

		plan.criteria = plan.routingAnalyzeValues(stmt.Rows.(Values))
		plan.fullList = makeList(0, len(plan.rule.Nodes))
		return plan
	case *Replace:
		if _, ok := stmt.Rows.(SelectStatement); ok {
			panic(NewParserError("select in replace not allowed"))
		}

		plan.rule = router.GetRule(String(stmt.Table))
		plan.criteria = plan.routingAnalyzeValues(stmt.Rows.(Values))
		plan.fullList = makeList(0, len(plan.rule.Nodes))
		return plan

	case *Select:
		plan.rule = router.GetRule(String(stmt.From[0]))
		where = stmt.Where
	case *Update:
		plan.rule = router.GetRule(String(stmt.Table))

		checkUpdateExprs(stmt.Exprs, plan.rule)

		where = stmt.Where
	case *Delete:
		plan.rule = router.GetRule(String(stmt.Table))
		where = stmt.Where
	}

	if where != nil {
		plan.criteria = where.Expr
	} else {
		plan.rule = router.DefaultRule
	}
	plan.fullList = makeList(0, len(plan.rule.Nodes))

	return plan
}
Esempio n. 2
0
func newTestDBRule() *router.DBRules {
	var s = `
rules:
-
    db: mixer
    table: test1
    key: id
    type: hash
    nodes: node(1-10)

-
    db: mixer
    table: test2 
    key: id
    nodes: node1,node2,node3    
    type: range
    # range is -inf-10000 10000-20000 20000-+inf 
    range: -10000-20000-

-   db: mixer
    table: 
    key:
    nodes: node1
    type: default
`

	cfg, err := config.ParseConfigData([]byte(s))
	if err != nil {
		println(err.Error())
		panic(err)
	}

	var r *router.Router

	r, err = router.NewRouter(cfg)
	if err != nil {
		println(err.Error())
		panic(err)
	}

	return r.GetDBRules("mixer")
}