// Route routes schema/table to target schema/table
// don't support to route schema/table to multiple schema/table
Route routes schema/table to target schema/table
don't support to route schema/table to multiple schema/table
// Route routes schema / table to target schema / table // don ' t support to route schema / table to multiple schema / table
func (r *Table) Route(schema, table string) (string, string, error) {
	rules := r.Match(schema, table)
	var (
		schemaRules = make([]*TableRule, 0, len(rules))
		tableRules  = make([]*TableRule, 0, len(rules))
	)
	for i := range rules {
		rule, ok := rules[i].(*TableRule)
		if !ok {
			return "", "", errors.NotValidf("table route rule %+v", rules[i])
		}
		if len(rule.TablePattern) == 0 {
			schemaRules = append(schemaRules, rule)
		} else {
			tableRules = append(tableRules, rule)
		}
	}
	var (
		targetSchema string
		targetTable  string
	)
	if len(table) == 0 || len(tableRules) == 0 {
		if len(schemaRules) > 1 {
			return "", "", errors.NotSupportedf("route %s/%s to rule set(%d)", schema, table, len(schemaRules))
		}
		if len(schemaRules) == 1 {
			targetSchema, targetTable = schemaRules[0].TargetSchema, schemaRules[0].TargetTable
		}
	} else {
		if len(tableRules) > 1 {
			return "", "", errors.NotSupportedf("route %s/%s to rule set(%d)", schema, table, len(tableRules))
		}
		targetSchema, targetTable = tableRules[0].TargetSchema, tableRules[0].TargetTable
	}
	if len(targetSchema) == 0 {
		targetSchema = schema
	}
	if len(targetTable) == 0 {
		targetTable = table
	}
	return targetSchema, targetTable, nil
}
func ( r * Table ) Route ( schema , table string ) ( string , string , error ) { rules := r . Match ( schema , table ) var ( schemaRules = make ( [ ] * TableRule , 0 , len ( rules ) ) tableRules = make ( [ ] * TableRule , 0 , len ( rules ) ) ) for i := range rules { rule , ok := rules [ i ] . ( * TableRule ) if ! ok { return " " , " " , errors . NotValidf ( " " , rules [ i ] ) } if len ( rule . TablePattern ) == 0 { schemaRules = append ( schemaRules , rule ) } else { tableRules = append ( tableRules , rule ) } } var ( targetSchema string targetTable string ) if len ( table ) == 0 || len ( tableRules ) == 0 { if len ( schemaRules ) > 1 { return " " , " " , errors . NotSupportedf ( " " , schema , table , len ( schemaRules ) ) } if len ( schemaRules ) == 1 { targetSchema , targetTable = schemaRules [ 0 ] . TargetSchema , schemaRules [ 0 ] . TargetTable } } else { if len ( tableRules ) > 1 { return " " , " " , errors . NotSupportedf ( " " , schema , table , len ( tableRules ) ) } targetSchema , targetTable = tableRules [ 0 ] . TargetSchema , tableRules [ 0 ] . TargetTable } if len ( targetSchema ) == 0 { targetSchema = schema } if len ( targetTable ) == 0 { targetTable = table } return targetSchema , targetTable , nil }
Route routes schema/table to target schema/table
don't support to route schema/table to multiple schema/table
Route routes schema / table to target schema / table don ' t support to route schema / table to multiple schema / table
// classify rules into schema level rules and table level // table level rules have highest priority