From 96ee2a76aa998927bdc6634e048355f08bdef8b8 Mon Sep 17 00:00:00 2001 From: Annika Hannig Date: Thu, 13 Jan 2022 16:16:11 +0100 Subject: [PATCH] added find by prefix --- pkg/store/backends/postgres/routes_backend.go | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/pkg/store/backends/postgres/routes_backend.go b/pkg/store/backends/postgres/routes_backend.go index 79f9d44..96428cd 100644 --- a/pkg/store/backends/postgres/routes_backend.go +++ b/pkg/store/backends/postgres/routes_backend.go @@ -143,14 +143,35 @@ func (b *RoutesBackend) FindByNeighbors( } listQry := strings.Join(vars, ",") qry := ` - SELECT route - FROM routes + SELECT route FROM routes WHERE neighbor_id IN (` + listQry + `)` rows, err := b.pool.Query(ctx, qry, neighborIDs...) if err != nil { return nil, err } + return fetchRoutes(rows) +} + +// FindByPrefix will return the prefixes matching a pattern +func (b *RoutesBackend) FindByPrefix( + ctx context.Context, + prefix string, +) (api.LookupRoutes, error) { + // We are searching route.Network + qry := ` + SELECT route FROM routes + WHERE network ILIKE $1 + ` + rows, err := b.pool.Query(ctx, qry, prefix+"%") + if err != nil { + return nil, err + } + return fetchRoutes(rows) +} + +// Private fetchRoutes will load the queried result set +func fetchRoutes(rows pgx.Rows) (api.LookupRoutes, error) { cmd := rows.CommandTag() results := make(api.LookupRoutes, 0, cmd.RowsAffected()) for rows.Next() { @@ -162,12 +183,3 @@ func (b *RoutesBackend) FindByNeighbors( } return results, nil } - -// FindByPrefix will return the prefixes matching a pattern -func (b *RoutesBackend) FindByPrefix( - ctx context.Context, - prefix string, -) (api.LookupRoutes, error) { - // We are searching route.Network - return nil, nil -}