MCPcopy Index your code
hub / github.com/bytebase/bytebase / ListInstances

Method ListInstances

backend/store/instance.go:84–174  ·  view source on GitHub ↗

ListInstances lists all instance.

(ctx context.Context, find *FindInstanceMessage)

Source from the content-addressed store, hash-verified

82
83// ListInstances lists all instance.
84func (s *Store) ListInstances(ctx context.Context, find *FindInstanceMessage) ([]*InstanceMessage, error) {
85 where := qb.Q().Space("instance.workspace = ?", find.Workspace)
86 if filterQ := find.FilterQ; filterQ != nil {
87 where.And("?", filterQ)
88 }
89
90 if v := find.ResourceID; v != nil {
91 where.And("instance.resource_id = ?", *v)
92 }
93 if v := find.ResourceIDs; v != nil {
94 where.And("instance.resource_id = ANY(?)", *v)
95 }
96 if !find.ShowDeleted {
97 where.And("instance.deleted = ?", false)
98 }
99
100 q := qb.Q().Space(`
101 SELECT
102 instance.resource_id,
103 instance.workspace,
104 instance.environment,
105 instance.deleted,
106 instance.metadata
107 FROM instance
108 WHERE ?
109 `, where)
110
111 if len(find.OrderByKeys) > 0 {
112 orderBy := []string{}
113 for _, v := range find.OrderByKeys {
114 orderBy = append(orderBy, fmt.Sprintf("%s %s", v.Key, v.SortOrder.String()))
115 }
116 q.Space(fmt.Sprintf("ORDER BY %s", strings.Join(orderBy, ", ")))
117 } else {
118 q.Space("ORDER BY resource_id ASC")
119 }
120
121 if v := find.Limit; v != nil {
122 q.Space("LIMIT ?", *v)
123 }
124 if v := find.Offset; v != nil {
125 q.Space("OFFSET ?", *v)
126 }
127
128 query, queryArgs, err := q.ToSQL()
129 if err != nil {
130 return nil, errors.Wrapf(err, "failed to build sql")
131 }
132
133 var instanceMessages []*InstanceMessage
134 rows, err := s.GetDB().QueryContext(ctx, query, queryArgs...)
135 if err != nil {
136 return nil, err
137 }
138 defer rows.Close()
139 for rows.Next() {
140 var instanceMessage InstanceMessage
141 var environment sql.NullString

Callers 2

GetInstanceMethod · 0.95
HasSampleInstancesMethod · 0.95

Calls 14

GetDBMethod · 0.95
deobfuscateInstancesMethod · 0.95
QFunction · 0.92
getInstanceCacheKeyFunction · 0.85
SpaceMethod · 0.80
AndMethod · 0.80
JoinMethod · 0.80
ToSQLMethod · 0.80
QueryContextMethod · 0.80
ScanMethod · 0.80
UnmarshalMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected