(c *gin.Context)
| 162 | if _, ok := covered[r.TableName]; ok { |
| 163 | continue |
| 164 | } |
| 165 | out = append(out, autoTable{Schema: r.TableSchema, Name: r.TableName}) |
| 166 | } |
| 167 | sort.Slice(out, func(i, j int) bool { return out[i].key() < out[j].key() }) |
| 168 | return out, nil |
| 169 | } |
| 170 | |
| 171 | // resolveAutoTable validates an auto key against a FRESH discovery — the only |
| 172 | // table names ever interpolated into SQL are ones information_schema just |
| 173 | // returned, so a crafted key can't smuggle SQL. |
| 174 | func (h *modelsHandler) resolveAutoTable(key string) (autoTable, bool) { |
| 175 | if !strings.HasPrefix(key, autoKeyPrefix) { |
| 176 | return autoTable{}, false |
| 177 | } |
| 178 | tables, err := h.discoverTables() |
| 179 | if err != nil { |
| 180 | return autoTable{}, false |
| 181 | } |
| 182 | for _, t := range tables { |
| 183 | if t.key() == key { |
| 184 | return t, true |
| 185 | } |
| 186 | } |
| 187 | return autoTable{}, false |
| 188 | } |
| 189 | |
| 190 | // autoColumns returns the table's column names in ordinal order. |
| 191 | func (h *modelsHandler) autoColumns(t autoTable) ([]string, error) { |
| 192 | var cols []string |
| 193 | err := h.store.DB().Raw(` |
| 194 | SELECT column_name |
| 195 | FROM information_schema.columns |
| 196 | WHERE table_schema = ? AND table_name = ? |
| 197 | ORDER BY ordinal_position`, t.Schema, t.Name).Scan(&cols).Error |
| 198 | return cols, err |
| 199 | } |
| 200 | |
| 201 | // registerModelsAPI registers the read-only generic models explorer used by the |
| 202 | // admin dashboard's models browser. |
| 203 | func registerModelsAPI(r *gin.RouterGroup, store *configstore.ConfigStore) { |
| 204 | h := &modelsHandler{store: store} |
| 205 | r.GET("/models", h.listModels) |
| 206 | r.GET("/models/:model", h.getModel) |
| 207 | } |
| 208 | |
| 209 | // qualifiedTable returns the table name to read for a descriptor, schema- |
| 210 | // qualifying runtime tables with the CP runtime schema. |
| 211 | func (h *modelsHandler) qualifiedTable(d modelDescriptor) string { |
| 212 | if d.Runtime { |
| 213 | return h.store.RuntimeSchema() + "." + d.Table |
| 214 | } |
no test coverage detected