//get the execute database for update sql func (c *ClientConn) getUpdateExecDB(sql string, tokens []string, tokensLen int) (*ExecuteDB, error) { var ruleDB string executeDB := new(ExecuteDB) executeDB.sql = sql schema := c.proxy.schema router := schema.rule rules := router.Rules if len(rules) != 0 { for i := 0; i < tokensLen; i++ { if strings.ToLower(tokens[i]) == mysql.TK_STR_SET { DBName, tableName := sqlparser.GetDBTable(tokens[i-1]) //if the token[i+1] like this:kingshard.test_shard_hash if DBName != "" { ruleDB = DBName } else { ruleDB = c.db } if router.GetRule(ruleDB, tableName) != router.DefaultRule { return nil, nil } else { break } } } } err := c.setExecuteNode(tokens, tokensLen, executeDB) if err != nil { return nil, err } return executeDB, nil }
//get the execute database for select sql func (c *ClientConn) getSelectExecDB(sql string, tokens []string, tokensLen int) (*ExecuteDB, error) { var ruleDB string executeDB := new(ExecuteDB) executeDB.sql = sql executeDB.IsSlave = true schema := c.proxy.schema router := schema.rule rules := router.Rules if len(rules) != 0 { for i := 1; i < tokensLen; i++ { if strings.ToLower(tokens[i]) == mysql.TK_STR_FROM { if i+1 < tokensLen { DBName, tableName := sqlparser.GetDBTable(tokens[i+1]) //if the token[i+1] like this:kingshard.test_shard_hash if DBName != "" { ruleDB = DBName } else { ruleDB = c.db } if router.GetRule(ruleDB, tableName) != router.DefaultRule { return nil, nil } else { //if the table is not shard table,send the sql //to default db break } } } if strings.ToLower(tokens[i]) == mysql.TK_STR_LAST_INSERT_ID { return nil, nil } } } //if send to master if 2 < tokensLen { if strings.ToLower(tokens[1]) == mysql.TK_STR_MASTER_HINT { executeDB.IsSlave = false } } err := c.setExecuteNode(tokens, tokensLen, executeDB) if err != nil { return nil, err } return executeDB, nil }