Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

4.1 KB
Raw Blame
package pg

import (
	"context"
	"database/sql"
	"fmt"
	"strings"

	"github.com/pkg/errors"

	"github.com/bytebase/omni/pg/ast"

	storepb "github.com/bytebase/bytebase/backend/generated-go/store"
	"github.com/bytebase/bytebase/backend/plugin/advisor"
	advisorcode "github.com/bytebase/bytebase/backend/plugin/advisor/code"
	pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
)

var (
	_ advisor.Advisor = (*InsertRowLimitAdvisor)(nil)
)

func init() {
	advisor.Register(storepb.Engine_POSTGRES, storepb.SQLReviewRule_STATEMENT_INSERT_ROW_LIMIT, &InsertRowLimitAdvisor{})
}

// InsertRowLimitAdvisor is the advisor checking for to limit INSERT rows.
type InsertRowLimitAdvisor struct {
}

// Check checks for the INSERT row limit.
func (*InsertRowLimitAdvisor) Check(ctx context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
	level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
	if err != nil {
		return nil, err
	}
	numberPayload := checkCtx.Rule.GetNumberPayload()
	if numberPayload == nil {
		return nil, errors.New("number_payload is required for this rule")
	}

	if int(numberPayload.Number) <= 0 {
		return nil, nil
	}

	rule := &insertRowLimitRule{
		OmniBaseRule: OmniBaseRule{
			Level: level,
			Title: checkCtx.Rule.Type.String(),
		},
		maxRow:     int(numberPayload.Number),
		driver:     checkCtx.Driver,
		ctx:        ctx,
		TenantMode: checkCtx.TenantMode,
	}

	adviceList := RunRules(checkCtx.ParsedStatements, []OmniRule{rule})
	return rule.explains.AppendSkippedAdvice(adviceList, rule.Title, advisorcode.InsertTooManyRows), nil
}

type insertRowLimitRule struct {
	OmniBaseRule

	maxRow     int
	driver     *sql.DB
	ctx        context.Context
	explains   advisor.ExplainBudget
	settings   sessionSettings
	TenantMode bool
}

func (*insertRowLimitRule) Name() string {
	return string(storepb.SQLReviewRule_STATEMENT_INSERT_ROW_LIMIT)
}

func (r *insertRowLimitRule) OnStatement(node ast.Node) {
	r.settings.add(node, r.TrimmedStmtText())
	node, text := pgparser.UnwrapExplainAnalyze(node, r.StmtText)
	switch n := node.(type) {
	case *ast.InsertStmt:
		r.checkInsert(n, text)
	default:
	}
}

func (r *insertRowLimitRule) checkInsert(ins *ast.InsertStmt, text string) {
	code := advisorcode.Ok
	rows := int64(0)
	statementText := strings.TrimRight(strings.TrimSpace(text), ";")

	// Count VALUES rows if this is INSERT ... VALUES.
	if sel, ok := ins.SelectStmt.(*ast.SelectStmt); ok && sel.ValuesLists != nil {
		rowCount := len(sel.ValuesLists.Items)
		if rowCount > r.maxRow {
			code = advisorcode.InsertTooManyRows
			rows = int64(rowCount)
		}
	} else if ins.SelectStmt != nil && r.driver != nil {
		// For INSERT ... SELECT, use EXPLAIN.
		if !r.explains.Spend(&storepb.Position{Line: r.ContentStartLine() + int32(r.BaseLine)}) {
			return
		}

		res, err := advisor.Query(r.ctx, advisor.QueryContext{
			TenantMode:    r.TenantMode,
			PreExecutions: r.settings.statements(),
		}, r.driver, storepb.Engine_POSTGRES, getExplainSQL(statementText))

		if err != nil {
			r.AddAdvice(&storepb.Advice{
				Status:  r.Level,
				Code:    advisorcode.InsertTooManyRows.Int32(),
				Title:   r.Title,
				Content: fmt.Sprintf("\"%s\" dry runs failed: %s", statementText, err.Error()),
				StartPosition: &storepb.Position{
					Line:   r.ContentStartLine(),
					Column: 0,
				},
			})
			return
		}

		rowCount, err := getInsertedRows(res)
		if err != nil {
			r.AddAdvice(&storepb.Advice{
				Status:  r.Level,
				Code:    advisorcode.Internal.Int32(),
				Title:   r.Title,
				Content: fmt.Sprintf("failed to get row count for \"%s\": %s", statementText, err.Error()),
				StartPosition: &storepb.Position{
					Line:   r.ContentStartLine(),
					Column: 0,
				},
			})
			return
		}

		if rowCount > int64(r.maxRow) {
			code = advisorcode.InsertTooManyRows
			rows = rowCount
		}
	}

	if code != advisorcode.Ok {
		r.AddAdvice(&storepb.Advice{
			Status:  r.Level,
			Code:    code.Int32(),
			Title:   r.Title,
			Content: fmt.Sprintf("The statement \"%s\" inserts %d rows. The count exceeds %d.", statementText, rows, r.maxRow),
			StartPosition: &storepb.Position{
				Line:   r.ContentStartLine(),
				Column: 0,
			},
		})
	}
}