代码拉取完成,页面将自动刷新
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Package parseutil exposes shared functions used by the different parsers.
package parseutil
import (
"slices"
"gitee.com/damengde/atlas/sql/migrate"
"gitee.com/damengde/atlas/sql/schema"
)
// Rename describes rename of a resource.
type Rename struct {
From, To string
}
// RenameColumn patches DROP/ADD column commands to RENAME.
func RenameColumn(modify *schema.ModifyTable, r *Rename) {
changes := schema.Changes(modify.Changes)
switch i, j := changes.IndexDropColumn(r.From), changes.IndexAddColumn(r.To); {
case j == -1:
// Rename column.
case i != -1:
changes[max(i, j)] = &schema.RenameColumn{
From: changes[i].(*schema.DropColumn).C,
To: changes[j].(*schema.AddColumn).C,
}
changes.RemoveIndex(min(i, j))
// Rename column and add the previous name back.
default:
if i = changes.IndexModifyColumn(r.From); i == -1 {
// ADD COLUMN must come after the RENAME.
return
}
modify := changes[i].(*schema.ModifyColumn)
changes[min(i, j)] = &schema.RenameColumn{
From: modify.From,
To: changes[j].(*schema.AddColumn).C,
}
changes[max(i, j)] = &schema.AddColumn{
C: modify.To,
}
}
modify.Changes = changes
}
// RenameIndex patches DROP/ADD index commands to RENAME.
func RenameIndex(modify *schema.ModifyTable, r *Rename) {
changes := schema.Changes(modify.Changes)
i := changes.IndexDropIndex(r.From)
j := changes.IndexAddIndex(r.To)
if i != -1 && j != -1 {
changes[max(i, j)] = &schema.RenameIndex{
From: changes[i].(*schema.DropIndex).I,
To: changes[j].(*schema.AddIndex).I,
}
changes.RemoveIndex(min(i, j))
modify.Changes = changes
}
}
// RenameTable patches DROP/ADD table commands to RENAME.
func RenameTable(changes schema.Changes, r *Rename) schema.Changes {
i := changes.LastIndexDropTable(r.From)
j := changes.LastIndexAddTable(r.To)
if i != -1 && j != -1 {
changes[max(i, j)] = &schema.RenameTable{
From: changes[i].(*schema.DropTable).T,
To: changes[j].(*schema.AddTable).T,
}
changes.RemoveIndex(min(i, j))
}
return changes
}
// MatchStmtBefore reports if the file contains any statement that matches the predicate before the given position.
func MatchStmtBefore(stmts []*migrate.Stmt, pos int, p func(*migrate.Stmt) (bool, error)) (bool, error) {
i := slices.IndexFunc(stmts, func(s *migrate.Stmt) bool {
return s.Pos >= pos
})
if i != -1 {
stmts = stmts[:i]
}
for _, s := range stmts {
m, err := p(s)
if err != nil {
return false, err
}
if m {
return true, nil
}
}
return false, nil
}
// MatchStmtAfter reports if the file contains any statement that matches the predicate after the given position.
func MatchStmtAfter(stmts []*migrate.Stmt, pos int, p func(*migrate.Stmt) (bool, error)) (bool, error) {
i := slices.IndexFunc(stmts, func(s *migrate.Stmt) bool {
return s.Pos > pos
})
if i == -1 {
return false, nil
}
stmts = stmts[i:]
for _, s := range stmts {
m, err := p(s)
if err != nil {
return false, err
}
if m {
return true, nil
}
}
return false, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。