func()

in sources/spanner/infoschema.go [383:459]


func (isi InfoSchemaImpl) GetIndexes(conv *internal.Conv, table common.SchemaAndName, colNameIdMap map[string]string) ([]schema.Index, error) {
	q := `SELECT distinct c.INDEX_NAME,c.COLUMN_NAME,c.ORDINAL_POSITION,c.COLUMN_ORDERING,i.IS_UNIQUE
			FROM information_schema.index_columns AS c
			JOIN information_schema.indexes AS i
			ON c.INDEX_NAME=i.INDEX_NAME
			WHERE c.table_schema = '' AND i.INDEX_TYPE='INDEX' AND c.TABLE_NAME = @p1 ORDER BY c.INDEX_NAME, c.ORDINAL_POSITION;`
	if isi.SpDialect == constants.DIALECT_POSTGRESQL {
		q = `SELECT distinct c.INDEX_NAME,c.COLUMN_NAME,c.ORDINAL_POSITION,c.COLUMN_ORDERING,i.IS_UNIQUE
		FROM information_schema.index_columns AS c
		JOIN information_schema.indexes AS i
		ON c.INDEX_NAME=i.INDEX_NAME
		WHERE c.table_schema = 'public' AND i.INDEX_TYPE='INDEX' AND c.TABLE_NAME = $1 ORDER BY c.INDEX_NAME, c.ORDINAL_POSITION;`
	}
	stmt := spanner.Statement{
		SQL: q,
		Params: map[string]interface{}{
			"p1": table.Name,
		},
	}
	var iter spannerclient.RowIterator
	if isi.SpannerClient != nil {
		iter = isi.SpannerClient.Single().Query(isi.Ctx, stmt)
	} else {
		iter = isi.Client.Single().Query(isi.Ctx, stmt)
	}
	defer iter.Stop()
	var name, column, ordering string
	var isUnique bool
	var isPgUnique string
	var sequence int64
	indexMap := make(map[string]schema.Index)
	var indexNames []string
	var indexes []schema.Index
	for {
		row, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return nil, fmt.Errorf("couldn't read row while fetching interleaved tables: %w", err)
		}
		if isi.SpDialect == constants.DIALECT_POSTGRESQL {
			err = row.Columns(&name, &column, &sequence, &ordering, &isPgUnique)
			if err != nil {
				fmt.Println(err)
				conv.Unexpected(fmt.Sprintf("Can't scan: %v", err))
				continue
			}
		} else {
			err = row.Columns(&name, &column, &sequence, &ordering, &isUnique)
			if err != nil {
				fmt.Println(err)
				conv.Unexpected(fmt.Sprintf("Can't scan: %v", err))
				continue
			}
		}

		isUnique = isPgUnique == "YES"
		if _, found := indexMap[name]; !found {
			indexNames = append(indexNames, name)
			indexMap[name] = schema.Index{
				Id:     internal.GenerateIndexesId(),
				Name:   name,
				Unique: isUnique}
		}

		index := indexMap[name]
		index.Keys = append(index.Keys, schema.Key{
			ColId: colNameIdMap[column],
			Desc:  (ordering == "DESC")})
		indexMap[name] = index
	}
	for _, k := range indexNames {
		indexes = append(indexes, indexMap[k])
	}
	return indexes, nil
}