func (self *TableInfo) fetchIndexes(conn *DBConnection) bool { indexes, err := conn.ExecuteFetch([]byte(fmt.Sprintf("show index from %s", self.Name)), 10000) if err != nil { relog.Warning("%s", err.Error()) return false } var currentIndex *schema.Index currentName := "" for _, row := range indexes.Rows { indexName := row[2].(string) if currentName != indexName { currentIndex = self.AddIndex(indexName) currentName = indexName } currentIndex.AddColumn(row[4].(string)) } if len(self.Indexes) == 0 { return true } pkIndex := self.Indexes[0] if pkIndex.Name != "PRIMARY" { return true } self.PKColumns = make([]int, len(pkIndex.Columns)) for i, pkCol := range pkIndex.Columns { self.PKColumns[i] = self.FindColumn(pkCol) } return true }
func getPKValuesFromColumns(columns *Node, pkIndex *schema.Index) (columnNumbers []int) { columnNumbers = make([]int, len(pkIndex.Columns)) for i, _ := range columnNumbers { columnNumbers[i] = -1 } for i, column := range columns.Sub { index := pkIndex.FindColumn(string(column.Value)) if index == -1 { continue } columnNumbers[index] = i } return columnNumbers }
func (self *Node) execAnalyzeUpdateExpressions(pkIndex *schema.Index) (pkValues []interface{}, ok bool) { for i := 0; i < self.Len(); i++ { columnName := string(self.At(i).At(0).Value) if index := pkIndex.FindColumn(columnName); index != -1 { value := self.At(i).At(1).execAnalyzeValue() if value == nil { relog.Warning("expression is too complex %v", self.At(i).At(0)) return nil, false } if pkValues == nil { pkValues = make([]interface{}, len(pkIndex.Columns)) } pkValues[index] = string(value.Value) } } return pkValues, true }
func getInsertPKValues(columns *Node, rowList *Node, pkIndex *schema.Index) (pkValues []interface{}) { for i := 0; i < rowList.Len(); i++ { if columns.Len() != rowList.At(i).At(0).Len() { // NODE_LIST->'('->NODE_LIST panic(NewParserError("number of columns does not match number of values")) } } pkValues = make([]interface{}, len(pkIndex.Columns)) for i := 0; i < columns.Len(); i++ { index := pkIndex.FindColumn(string(columns.At(i).Value)) if index == -1 { continue } if rowList.Len() == 1 { // simple node := rowList.At(0).At(0).At(i) // NODE_LIST->'('->NODE_LIST->Value value := node.execAnalyzeValue() if value == nil { relog.Warning("insert is too complex %v", node) return nil } pkValues[index] = string(value.Value) } else { // composite values := make([]interface{}, rowList.Len()) for j := 0; j < rowList.Len(); j++ { node := rowList.At(j).At(0).At(i) // NODE_LIST->'('->NODE_LIST->Value value := node.execAnalyzeValue() if value == nil { relog.Warning("insert is too complex %v", node) return nil } values[j] = string(value.Value) } pkValues[index] = values } } return pkValues }