* InsertRoute inserts an route to MySQL. */
(ctx context.Context, route *models.Route)
| 266 | InsertRoute inserts an route to MySQL. |
| 267 | */ |
| 268 | func (ds *MySQLDatastore) InsertRoute(ctx context.Context, route *models.Route) (*models.Route, error) { |
| 269 | hbyte, err := json.Marshal(route.Headers) |
| 270 | if err != nil { |
| 271 | return nil, err |
| 272 | } |
| 273 | |
| 274 | cbyte, err := json.Marshal(route.Config) |
| 275 | if err != nil { |
| 276 | return nil, err |
| 277 | } |
| 278 | |
| 279 | err = ds.Tx(func(tx *sql.Tx) error { |
| 280 | r := tx.QueryRow(`SELECT 1 FROM apps WHERE name=?`, route.AppName) |
| 281 | if err := r.Scan(new(int)); err != nil { |
| 282 | if err == sql.ErrNoRows { |
| 283 | return models.ErrAppsNotFound |
| 284 | } |
| 285 | } |
| 286 | same, err := tx.Query(`SELECT 1 FROM routes WHERE app_name=? AND path=?`, |
| 287 | route.AppName, route.Path) |
| 288 | if err != nil { |
| 289 | return err |
| 290 | } |
| 291 | defer same.Close() |
| 292 | if same.Next() { |
| 293 | return models.ErrRoutesAlreadyExists |
| 294 | } |
| 295 | |
| 296 | _, err = tx.Exec(` |
| 297 | INSERT INTO routes ( |
| 298 | app_name, |
| 299 | path, |
| 300 | image, |
| 301 | format, |
| 302 | maxc, |
| 303 | memory, |
| 304 | type, |
| 305 | timeout, |
| 306 | idle_timeout, |
| 307 | headers, |
| 308 | config |
| 309 | ) |
| 310 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`, |
| 311 | route.AppName, |
| 312 | route.Path, |
| 313 | route.Image, |
| 314 | route.Format, |
| 315 | route.MaxConcurrency, |
| 316 | route.Memory, |
| 317 | route.Type, |
| 318 | route.Timeout, |
| 319 | route.IdleTimeout, |
| 320 | string(hbyte), |
| 321 | string(cbyte), |
| 322 | ) |
| 323 | return err |
| 324 | }) |
| 325 |