Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: query builder (#17813)
p0ny68d9e2a5d647
diff --git a/backend/common/qb/qb.go b/backend/common/qb/qb.goindex 3299846275..2035fea70e 100644--- a/backend/common/qb/qb.go+++ b/backend/common/qb/qb.go@@ -45,25 +45,45 @@ func makePart(text string, params ...any) QueryPart {var newParams []anyvar errs []error+ // Use unique delimiters to avoid conflicts with nested query ? characters+ const placeholder = "\x00QB_PLACEHOLDER\x00"+ const escapeMarker = "\x00QB_ESCAPE\x00"++ // First, temporarily replace ?? (escaped ?) with a marker to preserve it+ text = strings.ReplaceAll(text, "??", escapeMarker)++ // Then, mark all remaining single ? placeholders with a unique marker+ // This prevents nested queries' ? characters from interfering with subsequent replacements+ text = strings.ReplaceAll(text, "?", placeholder)+for _, param := range params {// Check if the parameter is a *Queryif nestedQuery, ok := param.(*Query); ok {- // Expand the nested query inline - use toRawSql which keeps ? placeholders+ // Expand the nested query inline - use toRawSQL which keeps ? placeholdersnestedSQL, nestedParams, err := nestedQuery.toRawSQL()if err != nil {errs = append(errs, errors.Wrap(err, "failed to expand nested query"))continue}- // Replace the first ? with the nested SQL- text = strings.Replace(text, "?", nestedSQL, 1)+ // Replace the first placeholder with the nested SQL+ text = strings.Replace(text, placeholder, nestedSQL, 1)// Add the nested params to our param listnewParams = append(newParams, nestedParams...)} else {- // Regular parameter+ // Regular parameter - replace placeholder with ?+ text = strings.Replace(text, placeholder, "?", 1)newParams = append(newParams, param)}}+ // Check for leftover placeholders (more ? than parameters provided)+ if strings.Contains(text, placeholder) {+ errs = append(errs, errors.New("mismatched parameters: more ? placeholders than parameters provided"))+ }++ // Restore ?? escape sequences+ text = strings.ReplaceAll(text, escapeMarker, "??")+return QueryPart{Text: text,Params: newParams,@@ -109,6 +129,11 @@ func (q *Query) Or(text string, params ...any) *Query {return q.Join(" OR ", text, params...)}+// Comma adds a query part with a comma separator. Convenience wrapper for Join(", ", ...).+func (q *Query) Comma(text string, params ...any) *Query {+ return q.Join(", ", text, params...)+}+// Where adds a WHERE clause.func (q *Query) Where(text string, params ...any) *Query {return q.Join(" WHERE ", text, params...)@@ -124,6 +149,10 @@ func (q *Query) Len() int {// ToSQL generates PostgreSQL-compatible SQL with $1, $2, ... placeholders.// Returns the SQL string, parameters slice, and any error.+//+// Placeholder rules:+// - Single ? → parameter placeholder (converted to $1, $2, etc.)+// - Double ?? → literal ? in SQL (for PostgreSQL JSONB operators like ?, ?|, ?&)func (q *Query) ToSQL() (string, []any, error) {if q == nil {return "", nil, errors.New("cannot generate SQL from nil Query")@@ -145,19 +174,30 @@ func (q *Query) ToSQL() (string, []any, error) {sql := sqlBuilder.String()- // Second pass: replace ? placeholders with $1, $2, etc.- sqlParts := strings.Split(sql, "?")- if len(sqlParts)-1 != len(params) {+ // Second pass: replace ? placeholders with $1, $2, etc., handling ?? escape sequence+ // Use a unique delimiter that won't appear in SQL+ const delimiter = "\x00"+ // Replace ?? with delimiter temporarily+ sql = strings.ReplaceAll(sql, "??", delimiter)+ // Split on remaining ? (these are all placeholders)+ parts := strings.Split(sql, "?")++ // Verify parameter count matches+ if len(parts)-1 != len(params) {return "", nil, errors.New("mismatched parameters: ? placeholders count does not match params count")}+ // Build final SQL with $1, $2, etc.var builder strings.Builderfor i := range params {- builder.WriteString(sqlParts[i])+ builder.WriteString(parts[i])builder.WriteString("$")builder.WriteString(strconv.Itoa(i + 1))}- builder.WriteString(sqlParts[len(sqlParts)-1])+ builder.WriteString(parts[len(parts)-1])++ // Replace delimiter back to ?+ finalSQL := strings.ReplaceAll(builder.String(), delimiter, "?")- return builder.String(), params, nil+ return finalSQL, params, nil}diff --git a/backend/common/qb/qb_test.go b/backend/common/qb/qb_test.goindex 7efbf6ae3f..0df0284d71 100644--- a/backend/common/qb/qb_test.go+++ b/backend/common/qb/qb_test.go@@ -307,3 +307,487 @@ func TestQuery_ErrorHandling(t *testing.T) {require.Error(t, err)require.Contains(t, err.Error(), "nil Query")}++func TestQuery_JSONBExistsOperator(t *testing.T) {+ // Test the ? operator (key existence check)+ q := Q().Space("SELECT * FROM tasks").+ Where("payload ?? 'schema_version'")+ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM tasks WHERE payload ? 'schema_version'", sql)+ require.Empty(t, args)+}++func TestQuery_JSONBExistsAnyOperator(t *testing.T) {+ // Test the ?| operator (any key exists)+ q := Q().Space("DELETE FROM issue_comment").+ Where("(payload->'taskUpdate')??|'{toEarliestAllowedTime, fromEarliestAllowedTime}'")+ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "DELETE FROM issue_comment WHERE (payload->'taskUpdate')?|'{toEarliestAllowedTime, fromEarliestAllowedTime}'", sql)+ require.Empty(t, args)+}++func TestQuery_JSONBExistsAllOperator(t *testing.T) {+ // Test the ?& operator (all keys exist)+ q := Q().Space("SELECT * FROM issues").+ Where("payload->'labels' ??& ?::TEXT[]", []string{"bug", "critical"})+ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM issues WHERE payload->'labels' ?& $1::TEXT[]", sql)+ require.Equal(t, []any{[]string{"bug", "critical"}}, args)+}++func TestQuery_JSONBMixedOperators(t *testing.T) {+ // Test mixing JSONB operators with regular parameters+ q := Q().Space("SELECT * FROM tasks").+ Where("instance = ?", "prod").+ And("payload ?? 'type'").+ And("payload->>'status' = ?", "completed")+ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM tasks WHERE instance = $1 AND payload ? 'type' AND payload->>'status' = $2", sql)+ require.Equal(t, []any{"prod", "completed"}, args)+}++func TestQuery_JSONBComplexQuery(t *testing.T) {+ // Realistic example from the codebase+ q := Q().Space("SELECT * FROM revision").+ Where("TRUE").+ And("instance = ?", "prod").+ And("db_name = ?", "main").+ And("payload->>'type' = ?", "MIGRATE").+ And("payload ?? 'schema_version'").+ And("deleted_at IS NULL").+ Space("ORDER BY version DESC").+ Space("LIMIT ?", 10)+ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM revision WHERE TRUE AND instance = $1 AND db_name = $2 AND payload->>'type' = $3 AND payload ? 'schema_version' AND deleted_at IS NULL ORDER BY version DESC LIMIT $4", sql)+ require.Equal(t, []any{"prod", "main", "MIGRATE", 10}, args)+}++func TestQuery_EscapedQuestionMarks(t *testing.T) {+ // Test multiple ?? in a row - need to escape each ?+ q := Q().Space("SELECT '??????' as question_marks")+ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT '???' as question_marks", sql)+ require.Empty(t, args)+}++func TestQuery_JSONBWithComposition(t *testing.T) {+ // Test JSONB operators with query composition+ labelFilter := Q().Space("payload->'labels' ??& ?::TEXT[]", []string{"important"})+ typeFilter := Q().Space("payload ?? 'type'")++ q := Q().Space("SELECT * FROM issues").+ Where("?", labelFilter).+ And("?", typeFilter).+ And("status = ?", "open")++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM issues WHERE payload->'labels' ?& $1::TEXT[] AND payload ? 'type' AND status = $2", sql)+ require.Equal(t, []any{[]string{"important"}, "open"}, args)+}++func TestQuery_Comma(t *testing.T) {+ // Build a SET clause with comma separators+ set := Q()+ set.Comma("name = ?", "Alice")+ set.Comma("email = ?", "[email protected]")+ set.Comma("active = ?", true)++ sql, args, err := set.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "name = $1, email = $2, active = $3", sql)+ require.Equal(t, []any{"Alice", "[email protected]", true}, args)+}++func TestQuery_CommaInUpdate(t *testing.T) {+ // Realistic UPDATE with conditional SET fields+ name := "Alice"+ active := true++ set := Q()+ if name != "" {+ set.Comma("name = ?", name)+ }+ set.Comma("active = ?", active)++ q := Q().Space("UPDATE users SET ?", set).+ Space("WHERE id = ?", 123)++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "UPDATE users SET name = $1, active = $2 WHERE id = $3", sql)+ require.Equal(t, []any{"Alice", true, 123}, args)+}++func TestQuery_CommaWithNullHandling(t *testing.T) {+ // UPDATE with NULL assignment+ projectID := "proj-1"+ environmentID := "" // Empty string means NULL++ set := Q()+ set.Comma("project = ?", projectID)+ if environmentID == "" {+ set.Comma("environment = NULL")+ } else {+ set.Comma("environment = ?", environmentID)+ }++ q := Q().Space("UPDATE db SET ?", set).+ Space("WHERE instance = ? AND name = ?", "inst-1", "db-1")++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "UPDATE db SET project = $1, environment = NULL WHERE instance = $2 AND name = $3", sql)+ require.Equal(t, []any{"proj-1", "inst-1", "db-1"}, args)+}++func TestQuery_Len(t *testing.T) {+ // Test Len() method+ q := Q()+ require.Equal(t, 0, q.Len())++ q.Space("SELECT * FROM users")+ require.Equal(t, 1, q.Len())++ q.Where("id = ?", 123)+ require.Equal(t, 2, q.Len())++ q.And("active = ?", true)+ require.Equal(t, 3, q.Len())++ // Nil query+ var nilQ *Query+ require.Equal(t, 0, nilQ.Len())+}++func TestQuery_DeeplyNestedComposition(t *testing.T) {+ // Test 3+ levels of nested queries+ innermost := Q().Space("SELECT category_id FROM products WHERE price > ?", 100)+ middle := Q().Space("SELECT user_id FROM orders WHERE product_id IN (?)", innermost)+ outer := Q().Space("SELECT * FROM users WHERE id IN (?)", middle)++ sql, args, err := outer.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM users WHERE id IN (SELECT user_id FROM orders WHERE product_id IN (SELECT category_id FROM products WHERE price > $1))", sql)+ require.Equal(t, []any{100}, args)+}++func TestQuery_DeeplyNestedWithMultipleParams(t *testing.T) {+ // Test deeply nested with multiple parameters at each level+ level3 := Q().Space("SELECT id FROM items WHERE stock > ? AND category = ?", 10, "electronics")+ level2 := Q().Space("SELECT order_id FROM order_items WHERE item_id IN (?) AND quantity > ?", level3, 2)+ level1 := Q().Space("SELECT * FROM orders WHERE id IN (?) AND status = ?", level2, "completed")++ sql, args, err := level1.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM orders WHERE id IN (SELECT order_id FROM order_items WHERE item_id IN (SELECT id FROM items WHERE stock > $1 AND category = $2) AND quantity > $3) AND status = $4", sql)+ require.Equal(t, []any{10, "electronics", 2, "completed"}, args)+}++func TestQuery_Reuse(t *testing.T) {+ // Test reusing the same query object multiple times+ baseWhere := Q().Space("deleted_at IS NULL")++ // First use+ q1 := Q().Space("SELECT * FROM users WHERE ?", baseWhere).And("active = ?", true)+ sql1, args1, err := q1.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM users WHERE deleted_at IS NULL AND active = $1", sql1)+ require.Equal(t, []any{true}, args1)++ // Second use - baseWhere should be reusable+ q2 := Q().Space("SELECT * FROM posts WHERE ?", baseWhere).And("published = ?", true)+ sql2, args2, err := q2.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM posts WHERE deleted_at IS NULL AND published = $1", sql2)+ require.Equal(t, []any{true}, args2)++ // baseWhere should still work independently+ sql3, args3, err := baseWhere.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "deleted_at IS NULL", sql3)+ require.Empty(t, args3)+}++func TestQuery_MixedEscapedAndPlaceholders(t *testing.T) {+ // Test mixing ?? (escaped) with ? (placeholders)+ q := Q().Space("SELECT * FROM data").+ Where("metadata ?? 'key'").+ And("value = ?", "test").+ And("tags ??| ?::TEXT[]", []string{"important"})++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM data WHERE metadata ? 'key' AND value = $1 AND tags ?| $2::TEXT[]", sql)+ require.Equal(t, []any{"test", []string{"important"}}, args)+}++func TestQuery_MultipleConsecutiveEscapes(t *testing.T) {+ // Test multiple ?? in various positions+ q := Q().Space("SELECT").+ Join(", ", "data ?? 'a'").+ Join(", ", "data ?? 'b'").+ Join(", ", "data ?? 'c'").+ Space("FROM table")++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT, data ? 'a', data ? 'b', data ? 'c' FROM table", sql)+ require.Empty(t, args)+}++func TestQuery_ComplexMixedOperators(t *testing.T) {+ // Complex query with mixed ??, ?, and nested queries+ subquery := Q().Space("SELECT id FROM items WHERE tags ??& ?::TEXT[]", []string{"urgent"})++ q := Q().Space("SELECT * FROM tasks").+ Where("metadata ?? 'version'").+ And("status = ?", "active").+ And("metadata ??| '{priority, deadline}'").+ And("item_id IN (?)", subquery).+ And("assignee = ?", "alice")++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM tasks WHERE metadata ? 'version' AND status = $1 AND metadata ?| '{priority, deadline}' AND item_id IN (SELECT id FROM items WHERE tags ?& $2::TEXT[]) AND assignee = $3", sql)+ require.Equal(t, []any{"active", []string{"urgent"}, "alice"}, args)+}++func TestQuery_ParameterOrdering(t *testing.T) {+ // Ensure parameters maintain correct order through chaining+ // Use separate Space/And calls since nested queries expand into first ? only+ part1 := Q().Space("a = ?", 1)+ part2 := Q().Space("b = ?", 2)+ part3 := Q().Space("c = ?", 3)++ q := Q().Space("SELECT * FROM test").+ Where("(?)", part1).+ And("(?)", part2).+ And("(?)", part3).+ And("d = ?", 4)++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM test WHERE (a = $1) AND (b = $2) AND (c = $3) AND d = $4", sql)+ require.Equal(t, []any{1, 2, 3, 4}, args)+}++func TestQuery_EmptyNestedQuery(t *testing.T) {+ // Test empty nested query+ empty := Q()+ q := Q().Space("SELECT * FROM users WHERE ?", empty)++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "SELECT * FROM users WHERE ", sql)+ require.Empty(t, args)+}++func TestQuery_MultipleNilQueries(t *testing.T) {+ // Test multiple nil nested queries trigger errors+ var nilQ1 *Query+ var nilQ2 *Query++ q := Q().Space("SELECT * FROM users WHERE id IN (?) OR status IN (?)", nilQ1, nilQ2)++ _, _, err := q.ToSQL()+ require.Error(t, err)+ require.Contains(t, err.Error(), "nil Query")+}++func TestQuery_ComplexRealWorldExample(t *testing.T) {+ // Realistic complex query combining multiple patterns+ instanceID := "prod-db"+ filterDeleted := true+ filterStatus := "active"+ hasTypeFilter := true+ types := []string{"MIGRATE", "DATA"}++ // Build SELECT with JSONB operators+ q := Q().Space("SELECT id, payload->>'name' as name, payload->>'status' as status").+ Space("FROM tasks").+ Where("instance = ?", instanceID)++ if filterDeleted {+ q.And("deleted_at IS NULL")+ }++ if filterStatus != "" {+ q.And("payload->>'status' = ?", filterStatus)+ }++ if hasTypeFilter {+ q.And("payload ?? 'type'").+ And("payload->>'type' = ANY(?)", types)+ }++ // Add subquery for counting related items+ countSubquery := Q().Space("SELECT COUNT(*) FROM task_runs WHERE task_id = tasks.id AND status = ?", "completed")+ q.And("(?) > ?", countSubquery, 0)++ q.Space("ORDER BY created_at DESC").+ Space("LIMIT ?", 50)++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Contains(t, sql, "SELECT id, payload->>'name' as name")+ require.Contains(t, sql, "WHERE instance = $1")+ require.Contains(t, sql, "AND deleted_at IS NULL")+ require.Contains(t, sql, "AND payload->>'status' = $2")+ require.Contains(t, sql, "AND payload ? 'type'")+ require.Contains(t, sql, "AND payload->>'type' = ANY($3)")+ require.Contains(t, sql, "AND (SELECT COUNT(*) FROM task_runs WHERE task_id = tasks.id AND status = $4) > $5")+ require.Contains(t, sql, "ORDER BY created_at DESC")+ require.Contains(t, sql, "LIMIT $6")+ require.Equal(t, []any{instanceID, filterStatus, types, "completed", 0, 50}, args)+}++func TestQuery_UpdateWithComplexSet(t *testing.T) {+ // Complex UPDATE with conditional SET fields and JSONB+ id := 123+ name := "Updated Name"+ updateMetadata := true+ metadataKey := "version"+ metadataValue := "2.0"++ set := Q()+ set.Comma("name = ?", name)+ set.Comma("updated_at = NOW()")++ if updateMetadata {+ set.Comma("metadata = jsonb_set(metadata, ?, ?)", "{"+metadataKey+"}", metadataValue)+ }++ where := Q().Space("id = ?", id).And("deleted_at IS NULL")++ q := Q().Space("UPDATE tasks SET ?", set).+ Space("WHERE ?", where).+ Space("RETURNING *")++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "UPDATE tasks SET name = $1, updated_at = NOW(), metadata = jsonb_set(metadata, $2, $3) WHERE id = $4 AND deleted_at IS NULL RETURNING *", sql)+ require.Equal(t, []any{name, "{" + metadataKey + "}", metadataValue, id}, args)+}++func TestQuery_InsertWithReturning(t *testing.T) {+ // INSERT with RETURNING clause+ q := Q().Space("INSERT INTO users (name, email, metadata)").+ Space("VALUES (?, ?, ?::JSONB)", "Alice", "[email protected]", `{"role": "admin"}`).+ Space("RETURNING id, created_at")++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "INSERT INTO users (name, email, metadata) VALUES ($1, $2, $3::JSONB) RETURNING id, created_at", sql)+ require.Equal(t, []any{"Alice", "[email protected]", `{"role": "admin"}`}, args)+}++func TestQuery_CTE(t *testing.T) {+ // Common Table Expression (CTE) - must be built in one query to maintain proper structure+ q := Q().Space("WITH recent_orders AS (").+ Space("SELECT user_id, COUNT(*) as order_count").+ Space("FROM orders").+ Space("WHERE created_at > ?", "2024-01-01").+ Space("GROUP BY user_id").+ Space(")").+ Space("SELECT u.*, ro.order_count").+ Space("FROM users u").+ Space("JOIN recent_orders ro ON u.id = ro.user_id").+ Where("ro.order_count > ?", 5)++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Contains(t, sql, "WITH recent_orders AS")+ require.Contains(t, sql, "WHERE created_at > $1")+ require.Contains(t, sql, "WHERE ro.order_count > $2")+ require.Equal(t, []any{"2024-01-01", 5}, args)+}++func TestQuery_RegressionTest_BatchUpdateDatabase(t *testing.T) {+ // Multiple database conditions+ databases := []struct {+ InstanceID string+ DatabaseName string+ }{+ {"inst-1", "db-1"},+ {"inst-2", "db-2"},+ }++ set := Q().Comma("project = ?", "proj-123")++ where := Q()+ for _, db := range databases {+ where.Or("(db.instance = ? AND db.name = ?)", db.InstanceID, db.DatabaseName)+ }++ q := Q().Space("UPDATE db SET ? WHERE ?", set, where)++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "UPDATE db SET project = $1 WHERE (db.instance = $2 AND db.name = $3) OR (db.instance = $4 AND db.name = $5)", sql)+ require.Equal(t, []any{"proj-123", "inst-1", "db-1", "inst-2", "db-2"}, args)+}++func TestQuery_ConditionalOrWhereClause(t *testing.T) {+ // Test building WHERE clause with only Or() calls - pattern from actual codebase+ // This tests that the first Or() doesn't add " OR " prefix+ where := Q()++ // First condition+ environmentID := "prod"+ where.Or("environment = ?", environmentID)++ // Multiple database conditions+ databases := []struct {+ InstanceID string+ DatabaseName string+ }{+ {"inst-1", "db-1"},+ {"inst-2", "db-2"},+ }++ for _, db := range databases {+ where.Or("(db.instance = ? AND db.name = ?)", db.InstanceID, db.DatabaseName)+ }++ // Build final UPDATE query+ set := Q().Comma("project = ?", "proj-123")+ q := Q().Space("UPDATE db SET ?", set).Space("WHERE ?", where)++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ require.Equal(t, "UPDATE db SET project = $1 WHERE environment = $2 OR (db.instance = $3 AND db.name = $4) OR (db.instance = $5 AND db.name = $6)", sql)+ require.Equal(t, []any{"proj-123", "prod", "inst-1", "db-1", "inst-2", "db-2"}, args)+}++func TestQuery_ConditionalOrOnlyDatabases(t *testing.T) {+ // Test when only database conditions exist (no environment filter)+ where := Q()++ databases := []struct {+ InstanceID string+ DatabaseName string+ }{+ {"inst-1", "db-1"},+ {"inst-2", "db-2"},+ }++ for _, db := range databases {+ where.Or("(db.instance = ? AND db.name = ?)", db.InstanceID, db.DatabaseName)+ }++ set := Q().Comma("project = ?", "proj-123")+ q := Q().Space("UPDATE db SET ?", set).Space("WHERE ?", where)++ sql, args, err := q.ToSQL()+ require.NoError(t, err)+ // First Or() should NOT have " OR " prefix+ require.Equal(t, "UPDATE db SET project = $1 WHERE (db.instance = $2 AND db.name = $3) OR (db.instance = $4 AND db.name = $5)", sql)+ require.Equal(t, []any{"proj-123", "inst-1", "db-1", "inst-2", "db-2"}, args)+}diff --git a/backend/store/audit_log.go b/backend/store/audit_log.goindex 6f8af5e4da..cf1afb5262 100644--- a/backend/store/audit_log.go+++ b/backend/store/audit_log.go@@ -11,6 +11,7 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -38,57 +39,63 @@ type AuditLogFind struct {}func (s *Store) CreateAuditLog(ctx context.Context, payload *storepb.AuditLog) error {- query := `- INSERT INTO audit_log (payload) VALUES ($1)- `-p, err := protojson.Marshal(payload)if err != nil {return errors.Wrapf(err, "failed to marshal payload")}- if _, err := s.GetDB().ExecContext(ctx, query, p); err != nil {+ q := qb.Q().Space("INSERT INTO audit_log (payload) VALUES (?)", p)+ sql, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ if _, err := s.GetDB().ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to create audit log")}return nil}func (s *Store) SearchAuditLogs(ctx context.Context, find *AuditLogFind) ([]*AuditLog, error) {- where, args := []string{"TRUE"}, []any{}- if v := find.Filter; v != nil {- where = append(where, v.Where)- args = append(args, v.Args...)- }- if v := find.Project; v != nil {- where = append(where, fmt.Sprintf("payload->>'parent' = $%d", len(args)+1))- args = append(args, *v)- }-- query := fmt.Sprintf(`+ q := qb.Q().Space(`SELECTid,created_at,payloadFROM audit_log- WHERE %s`, strings.Join(where, " AND "))+ WHERE TRUE+ `)++ if v := find.Filter; v != nil {+ // Convert $1, $2, etc. to ? for qb+ q.And(ConvertDollarPlaceholders(v.Where), v.Args...)+ }+ if v := find.Project; v != nil {+ q.And("payload->>'parent' = ?", *v)+ }if len(find.OrderByKeys) > 0 {orderBy := []string{}for _, v := range find.OrderByKeys {orderBy = append(orderBy, fmt.Sprintf("%s %s", v.Key, v.SortOrder.String()))}- query += fmt.Sprintf(" ORDER BY %s", strings.Join(orderBy, ", "))+ q.Space(fmt.Sprintf("ORDER BY %s", strings.Join(orderBy, ", ")))} else {- query += " ORDER BY id DESC"+ q.Space("ORDER BY id DESC")}if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}- rows, err := s.GetDB().QueryContext(ctx, query, args...)+ rows, err := s.GetDB().QueryContext(ctx, sql, args...)if err != nil {return nil, errors.Wrapf(err, "failed to query context")}diff --git a/backend/store/changelist.go b/backend/store/changelist.goindex 2ac31865be..252f79238b 100644--- a/backend/store/changelist.go+++ b/backend/store/changelist.go@@ -3,14 +3,13 @@ package storeimport ("context""database/sql"- "fmt"- "strings""time""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -56,13 +55,27 @@ func (s *Store) GetChangelist(ctx context.Context, find *FindChangelistMessage)// ListChangelists returns a list of changelists.func (s *Store) ListChangelists(ctx context.Context, find *FindChangelistMessage) ([]*ChangelistMessage, error) {- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().Space(`+ SELECT+ creator_id,+ updated_at,+ project,+ name,+ payload+ FROM changelist+ WHERE TRUE+ `)if v := find.ProjectID; v != nil {- where, args = append(where, fmt.Sprintf("changelist.project = $%d", len(args)+1)), append(args, *v)+ q.And("changelist.project = ?", *v)}if v := find.ResourceID; v != nil {- where, args = append(where, fmt.Sprintf("changelist.name = $%d", len(args)+1)), append(args, *v)+ q.And("changelist.name = ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})@@ -71,17 +84,7 @@ func (s *Store) ListChangelists(ctx context.Context, find *FindChangelistMessage}defer tx.Rollback()- rows, err := tx.QueryContext(ctx, fmt.Sprintf(`- SELECT- creator_id,- updated_at,- project,- name,- payload- FROM changelist- WHERE %s`, strings.Join(where, " AND ")),- args...,- )+ rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -128,30 +131,28 @@ func (s *Store) CreateChangelist(ctx context.Context, create *ChangelistMessage)return nil, err}- query := `+ q := qb.Q().Space(`INSERT INTO changelist (creator_id,project,name,payload)- VALUES ($1, $2, $3, $4)- RETURNING updated_at;- `+ VALUES (?, ?, ?, ?)+ RETURNING updated_at+ `, create.CreatorID, create.ProjectID, create.ResourceID, payload)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, err}defer tx.Rollback()- if err := tx.QueryRowContext(ctx, query,- create.CreatorID,- create.ProjectID,- create.ResourceID,- payload,- ).Scan(- &create.UpdatedAt,- ); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&create.UpdatedAt); err != nil {return nil, err}if err := tx.Commit(); err != nil {@@ -163,25 +164,27 @@ func (s *Store) CreateChangelist(ctx context.Context, create *ChangelistMessage)// UpdateChangelist updates a changelist.func (s *Store) UpdateChangelist(ctx context.Context, update *UpdateChangelistMessage) error {- tx, err := s.GetDB().BeginTx(ctx, nil)- if err != nil {- return errors.Wrapf(err, "failed to begin transaction")- }-- set, args := []string{"updated_at = $1"}, []any{time.Now()}+ q := qb.Q().Space("UPDATE changelist SET updated_at = ?", time.Now())if v := update.Payload; v != nil {payload, err := protojson.Marshal(update.Payload)if err != nil {return err}- set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, payload)+ q.Comma("payload = ?", payload)}- args = append(args, update.ProjectID, update.ResourceID)+ q.Space("WHERE project = ? AND name = ?", update.ProjectID, update.ResourceID)- if _, err := tx.ExecContext(ctx, fmt.Sprintf(`- UPDATE changelist- SET `+strings.Join(set, ", ")+`- WHERE project = $%d AND name = $%d`, len(set)+1, len(set)+2), args...); err != nil {+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return errors.Wrapf(err, "failed to begin transaction")+ }++ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}@@ -190,16 +193,23 @@ func (s *Store) UpdateChangelist(ctx context.Context, update *UpdateChangelistMe// DeleteChangelist deletes a changelist.func (s *Store) DeleteChangelist(ctx context.Context, projectID, resourceID string) error {+ q := qb.Q().Space(`+ DELETE FROM changelist+ WHERE changelist.project = ? AND changelist.name = ?+ `, projectID, resourceID)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return err}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `- DELETE FROM changelist- WHERE changelist.project = $1 AND changelist.name = $2;`,- projectID, resourceID); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}diff --git a/backend/store/changelog.go b/backend/store/changelog.goindex 4d3abce147..bf0e46eafa 100644--- a/backend/store/changelog.go+++ b/backend/store/changelog.go@@ -15,6 +15,7 @@ import (exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -71,7 +72,18 @@ type UpdateChangelogMessage struct {}func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (int64, error) {- query := `+ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return 0, errors.Wrapf(err, "failed to begin tx")+ }+ defer tx.Rollback()++ p, err := protojson.Marshal(create.Payload)+ if err != nil {+ return 0, errors.Wrapf(err, "failed to marshal")+ }++ q := qb.Q().Space(`INSERT INTO changelog (instance,db_name,@@ -80,29 +92,23 @@ func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (sync_history_id,payload) VALUES (- $1,- $2,- $3,- $4,- $5,- $6+ ?,+ ?,+ ?,+ ?,+ ?,+ ?)RETURNING id- `-- tx, err := s.GetDB().BeginTx(ctx, nil)- if err != nil {- return 0, errors.Wrapf(err, "failed to begin tx")- }- defer tx.Rollback()+ `, create.InstanceID, create.DatabaseName, create.Status, create.PrevSyncHistoryUID, create.SyncHistoryUID, p)- p, err := protojson.Marshal(create.Payload)+ query, args, err := q.ToSQL()if err != nil {- return 0, errors.Wrapf(err, "failed to marshal")+ return 0, errors.Wrapf(err, "failed to build sql")}var id int64- if err := tx.QueryRowContext(ctx, query, create.InstanceID, create.DatabaseName, create.Status, create.PrevSyncHistoryUID, create.SyncHistoryUID, p).Scan(&id); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&id); err != nil {return 0, errors.Wrapf(err, "failed to insert")}@@ -114,30 +120,25 @@ func (s *Store) CreateChangelog(ctx context.Context, create *ChangelogMessage) (}func (s *Store) UpdateChangelog(ctx context.Context, update *UpdateChangelogMessage) error {- args := []any{update.UID}- var set []string-+ set := qb.Q()if v := update.SyncHistoryUID; v != nil {- set = append(set, fmt.Sprintf("sync_history_id = $%d", len(args)+1))- args = append(args, *v)+ set.Comma("sync_history_id = ?", *v)}if v := update.RevisionUID; v != nil {- set = append(set, fmt.Sprintf(`payload = payload || '{"revision": "%d"}'`, *v))+ set.Comma("payload = payload || jsonb_build_object('revision', ?::BIGINT)", *v)}if v := update.Status; v != nil {- set = append(set, fmt.Sprintf("status = $%d", len(args)+1))- args = append(args, *v)+ set.Comma("status = ?", *v)}- if len(set) == 0 {+ if set.Len() == 0 {return errors.Errorf("update nothing")}- query := fmt.Sprintf(`- UPDATE changelog- SET %s- WHERE id = $1- `, strings.Join(set, " , "))+ query, args, err := qb.Q().Space("UPDATE changelog SET ? WHERE id = ?", set, update.UID).ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -157,36 +158,6 @@ func (s *Store) UpdateChangelog(ctx context.Context, update *UpdateChangelogMess}func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage) ([]*ChangelogMessage, error) {- where, args := []string{"TRUE"}, []any{}- if v := find.UID; v != nil {- where, args = append(where, fmt.Sprintf("changelog.id = $%d", len(args)+1)), append(args, *v)- }- if v := find.InstanceID; v != nil {- where, args = append(where, fmt.Sprintf("changelog.instance = $%d", len(args)+1)), append(args, *v)- }- if v := find.DatabaseName; v != nil {- where, args = append(where, fmt.Sprintf("changelog.db_name = $%d", len(args)+1)), append(args, *v)- }- if v := find.ResourcesFilter; v != nil {- text, err := generateResourceFilter(*v, "changelog.payload")- if err != nil {- return nil, errors.Wrapf(err, "failed to generate resource filter from %q", *v)- }- if text != "" {- where = append(where, text)- }- }- if v := find.Status; v != nil {- where, args = append(where, fmt.Sprintf("changelog.status = $%d", len(args)+1)), append(args, string(*v))- }- if find.HasSyncHistory {- where = append(where, "changelog.sync_history_id IS NOT NULL")- }- if len(find.TypeList) > 0 {- where = append(where, fmt.Sprintf("changelog.payload->>'type' = ANY($%d)", len(args)+1))- args = append(args, find.TypeList)- }-truncateSize := 512if common.IsDev() {truncateSize = 4@@ -204,7 +175,7 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)sheetField = "sheet_blob.content"}- query := fmt.Sprintf(`+ q := qb.Q().Space(fmt.Sprintf(`SELECTchangelog.id,changelog.created_at,@@ -223,18 +194,52 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)LEFT JOIN sync_history sh_cur ON sh_cur.id = changelog.sync_history_idLEFT JOIN sheet ON sheet.id::text = split_part(changelog.payload->>'sheet', '/', 4)LEFT JOIN sheet_blob ON sheet.sha256 = sheet_blob.sha256- WHERE %s- ORDER BY changelog.id DESC+ WHERE TRUE`,shPreField,shCurField,sheetField,- strings.Join(where, " AND "))+ ))++ if v := find.UID; v != nil {+ q.And("changelog.id = ?", *v)+ }+ if v := find.InstanceID; v != nil {+ q.And("changelog.instance = ?", *v)+ }+ if v := find.DatabaseName; v != nil {+ q.And("changelog.db_name = ?", *v)+ }+ if v := find.ResourcesFilter; v != nil {+ text, err := generateResourceFilter(*v, "changelog.payload")+ if err != nil {+ return nil, errors.Wrapf(err, "failed to generate resource filter from %q", *v)+ }+ if text != "" {+ q.And(text)+ }+ }+ if v := find.Status; v != nil {+ q.And("changelog.status = ?", string(*v))+ }+ if find.HasSyncHistory {+ q.And("changelog.sync_history_id IS NOT NULL")+ }+ if len(find.TypeList) > 0 {+ q.And("changelog.payload->>'type' = ANY(?)", find.TypeList)+ }++ q.Space("ORDER BY changelog.id DESC")if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}rows, err := s.GetDB().QueryContext(ctx, query, args...)diff --git a/backend/store/common.go b/backend/store/common.goindex af4fe18f13..f5f1aca44c 100644--- a/backend/store/common.go+++ b/backend/store/common.go@@ -1,5 +1,9 @@package store+import (+ "regexp"+)+// RowStatus is the status for a row.type RowStatus string@@ -33,3 +37,11 @@ type ListResourceFilter struct {Args []anyWhere string}++var dollarPlaceholderRegex = regexp.MustCompile(`\$\d+`)++// ConvertDollarPlaceholders converts PostgreSQL $N placeholders to ? placeholders for qb.+// This is needed because the API layer creates filters with $1, $2, etc. but qb expects ?.+func ConvertDollarPlaceholders(where string) string {+ return dollarPlaceholderRegex.ReplaceAllString(where, "?")+}diff --git a/backend/store/database.go b/backend/store/database.goindex ba6af0f189..03b981b8f8 100644--- a/backend/store/database.go+++ b/backend/store/database.go@@ -3,7 +3,6 @@ package storeimport ("context""database/sql"- "fmt""strings""github.com/pkg/errors"@@ -11,6 +10,7 @@ import ("google.golang.org/protobuf/proto""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -150,26 +150,30 @@ func (s *Store) CreateDatabaseDefault(ctx context.Context, create *DatabaseMessa// createDatabaseDefault only creates a default database with charset, collation only in the default project.func (*Store) createDatabaseDefaultImpl(ctx context.Context, txn *sql.Tx, projectID, instanceID string, create *DatabaseMessage) (int, error) {- query := `+ q := qb.Q().Space(`INSERT INTO db (instance,project,name,deleted)- VALUES ($1, $2, $3, $4)+ VALUES (?, ?, ?, ?)ON CONFLICT (instance, name) DO UPDATE SETdeleted = EXCLUDED.deleted- RETURNING id`- var databaseUID int- if err := txn.QueryRowContext(ctx, query,+ RETURNING id`,instanceID,projectID,create.DatabaseName,false,- ).Scan(- &databaseUID,- ); err != nil {+ )++ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")+ }++ var databaseUID int+ if err := txn.QueryRowContext(ctx, query, args...).Scan(&databaseUID); err != nil {return 0, err}return databaseUID, nil@@ -192,7 +196,8 @@ func (s *Store) UpsertDatabase(ctx context.Context, create *DatabaseMessage) (*Dif create.EnvironmentID != nil && *create.EnvironmentID != "" {environment = create.EnvironmentID}- query := `++ q := qb.Q().Space(`INSERT INTO db (instance,project,@@ -201,24 +206,28 @@ func (s *Store) UpsertDatabase(ctx context.Context, create *DatabaseMessage) (*Ddeleted,metadata)- VALUES ($1, $2, $3, $4, $5, $6)+ VALUES (?, ?, ?, ?, ?, ?)ON CONFLICT (instance, name) DO UPDATE SETproject = EXCLUDED.project,environment = EXCLUDED.environment,name = EXCLUDED.name,metadata = EXCLUDED.metadata- RETURNING id`- var databaseUID int- if err := tx.QueryRowContext(ctx, query,+ RETURNING id`,create.InstanceID,create.ProjectID,environment,create.DatabaseName,create.Deleted,metadataString,- ).Scan(- &databaseUID,- ); err != nil {+ )++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ var databaseUID int+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&databaseUID); err != nil {return nil, err}if err := tx.Commit(); err != nil {@@ -232,19 +241,20 @@ func (s *Store) UpsertDatabase(ctx context.Context, create *DatabaseMessage) (*D// UpdateDatabase updates a database.func (s *Store) UpdateDatabase(ctx context.Context, patch *UpdateDatabaseMessage) (*DatabaseMessage, error) {- set, args := []string{}, []any{}+ set := qb.Q()+if v := patch.ProjectID; v != nil {- set, args = append(set, fmt.Sprintf("project = $%d", len(args)+1)), append(args, *v)+ set.Comma("project = ?", *v)}if v := patch.EnvironmentID; v != nil {if *v == "" {- set = append(set, "environment = NULL")+ set.Comma("environment = NULL")} else {- set, args = append(set, fmt.Sprintf("environment = $%d", len(args)+1)), append(args, *v)+ set.Comma("environment = ?", *v)}}if v := patch.Deleted; v != nil {- set, args = append(set, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, *v)+ set.Comma("deleted = ?", *v)}if fs := patch.MetadataUpdates; len(fs) > 0 {database, err := s.GetDatabaseV2(ctx, &FindDatabaseMessage{@@ -262,26 +272,28 @@ func (s *Store) UpdateDatabase(ctx context.Context, patch *UpdateDatabaseMessageif err != nil {return nil, err}- set, args = append(set, fmt.Sprintf("metadata = $%d", len(args)+1)), append(args, metadataBytes)+ set.Comma("metadata = ?", metadataBytes)+ }++ if set.Len() == 0 {+ return nil, errors.New("no fields to update")+ }++ q := qb.Q().Space("UPDATE db SET ? WHERE instance = ? AND name = ? RETURNING id", set, patch.InstanceID, patch.DatabaseName)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}- args = append(args, patch.InstanceID, patch.DatabaseName)tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, err}defer tx.Rollback()+var databaseUID int- if err := tx.QueryRowContext(ctx, fmt.Sprintf(`- UPDATE db- SET `+strings.Join(set, ", ")+`- WHERE instance = $%d AND name = $%d- RETURNING id- `, len(args)-1, len(args)),- args...,- ).Scan(- &databaseUID,- ); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&databaseUID); err != nil {return nil, err}if err := tx.Commit(); err != nil {@@ -295,44 +307,49 @@ func (s *Store) UpdateDatabase(ctx context.Context, patch *UpdateDatabaseMessage// BatchUpdateDatabases update databases in batch.func (s *Store) BatchUpdateDatabases(ctx context.Context, databases []*DatabaseMessage, update *BatchUpdateDatabases) ([]*DatabaseMessage, error) {- set, args, wheres := []string{}, []any{}, []string{}+ set := qb.Q()+if update.ProjectID != nil {- set, args = append(set, fmt.Sprintf("project = $%d", len(args)+1)), append(args, *update.ProjectID)+ set.Comma("project = ?", *update.ProjectID)}if v := update.EnvironmentID; v != nil {if *v == "" {- set = append(set, "environment = NULL")+ set.Comma("environment = NULL")} else {- set, args = append(set, fmt.Sprintf("environment = $%d", len(args)+1)), append(args, *v)+ set.Comma("environment = ?", *v)}}- if len(set) == 0 {+ if set.Len() == 0 {return nil, errors.New("no update field specified")}+ where := qb.Q()+if v := update.FindByEnvironmentID; v != nil {- wheres, args = append(wheres, fmt.Sprintf("environment = $%d", len(args)+1)), append(args, *v)+ where.Or("environment = ?", *v)}for _, database := range databases {- wheres = append(wheres, fmt.Sprintf("(db.instance = $%d AND db.name = $%d)", len(args)+1, len(args)+2))- args = append(args, database.InstanceID, database.DatabaseName)+ where.Or("(db.instance = ? AND db.name = ?)", database.InstanceID, database.DatabaseName)}- if len(wheres) == 0 {+ if where.Len() == 0 {return nil, errors.Errorf("empty where")}+ q := qb.Q().Space("UPDATE db SET ? WHERE ?", set, where)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, err}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, fmt.Sprintf(`- UPDATE db- SET `+strings.Join(set, ", ")+`- WHERE %s;`, strings.Join(wheres, " OR ")),- args...,- ); err != nil {++ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return nil, err}@@ -368,46 +385,46 @@ func (s *Store) BatchUpdateDatabases(ctx context.Context, databases []*DatabaseM}func (*Store) listDatabaseImplV2(ctx context.Context, txn *sql.Tx, find *FindDatabaseMessage) ([]*DatabaseMessage, error) {- where, args := []string{"TRUE"}, []any{}- joinQuery := ""- if filter := find.Filter; filter != nil {- where = append(where, filter.Where)- args = append(args, filter.Args...)+ from := qb.Q().Space("db")+ where := qb.Q().Space("TRUE")+ if filter := find.Filter; filter != nil {+ where.And(ConvertDollarPlaceholders(filter.Where), filter.Args...)if strings.Contains(filter.Where, "ds.metadata->'schemas'") {- joinQuery = "INNER JOIN db_schema ds ON db.instance = ds.instance AND db.name = ds.db_name"+ from.Space("INNER JOIN db_schema ds ON db.instance = ds.instance AND db.name = ds.db_name")}}++ from.Space("LEFT JOIN instance ON db.instance = instance.resource_id")+if v := find.ProjectID; v != nil {- where, args = append(where, fmt.Sprintf("db.project = $%d", len(args)+1)), append(args, *v)+ where.And("db.project = ?", *v)}if v := find.EffectiveEnvironmentID; v != nil {- where, args = append(where, fmt.Sprintf(`- COALESCE(+ where.And(`COALESCE(db.environment,instance.environment- ) = $%d`, len(args)+1)), append(args, *v)+ ) = ?`, *v)}if v := find.InstanceID; v != nil {- where, args = append(where, fmt.Sprintf("db.instance = $%d", len(args)+1)), append(args, *v)+ where.And("db.instance = ?", *v)}if v := find.DatabaseName; v != nil {if find.IsCaseSensitive {- where, args = append(where, fmt.Sprintf("db.name = $%d", len(args)+1)), append(args, *v)+ where.And("db.name = ?", *v)} else {- where, args = append(where, fmt.Sprintf("LOWER(db.name) = LOWER($%d)", len(args)+1)), append(args, *v)+ where.And("LOWER(db.name) = LOWER(?)", *v)}}if v := find.Engine; v != nil {- where, args = append(where, fmt.Sprintf("instance.metadata->>'engine' = $%d", len(args)+1)), append(args, *v)+ where.And("instance.metadata->>'engine' = ?", *v)}if !find.ShowDeleted {- where, args = append(where, fmt.Sprintf("instance.deleted = $%d", len(args)+1)), append(args, false)- // We don't show databases that are deleted by users already.- where, args = append(where, fmt.Sprintf("db.deleted = $%d", len(args)+1)), append(args, false)+ where.And("instance.deleted = ?", false)+ where.And("db.deleted = ?", false)}- query := fmt.Sprintf(`+ q := qb.Q().Space(`SELECTdb.project,COALESCE(@@ -419,22 +436,25 @@ func (*Store) listDatabaseImplV2(ctx context.Context, txn *sql.Tx, find *FindDatdb.name,db.deleted,db.metadata- FROM db- %s- LEFT JOIN instance ON db.instance = instance.resource_id- WHERE %s- ORDER BY db.project, db.instance, db.name`, joinQuery, strings.Join(where, " AND "))+ FROM ?+ WHERE ?+ ORDER BY db.project, db.instance, db.name+ `, from, where)+if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql %+v", q)}var databaseMessages []*DatabaseMessage- rows, err := txn.QueryContext(ctx, query,- args...,- )+ rows, err := txn.QueryContext(ctx, query, args...)if err != nil {return nil, err}diff --git a/backend/store/database_group.go b/backend/store/database_group.goindex 5ccf3b7025..5c4b4515fe 100644--- a/backend/store/database_group.go+++ b/backend/store/database_group.go@@ -3,14 +3,13 @@ package storeimport ("context""database/sql"- "fmt"- "strings""github.com/pkg/errors""google.golang.org/genproto/googleapis/type/expr""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb")// DatabaseGroupMessage is the message for database groups.@@ -35,17 +34,18 @@ type UpdateDatabaseGroupMessage struct {// DeleteDatabaseGroup deletes a database group.func (s *Store) DeleteDatabaseGroup(ctx context.Context, projectID, resourceID string) error {- query := `- DELETE- FROM db_group- WHERE project = $1 AND resource_id = $2- `+ q := qb.Q().Space("DELETE FROM db_group WHERE project = ? AND resource_id = ?", projectID, resourceID)+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return errors.Wrapf(err, "failed to begin transaction")}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, query, projectID, resourceID); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to execute")}if err := tx.Commit(); err != nil {@@ -111,20 +111,30 @@ func (s *Store) GetDatabaseGroup(ctx context.Context, find *FindDatabaseGroupMes}func (*Store) listDatabaseGroupImpl(ctx context.Context, txn *sql.Tx, find *FindDatabaseGroupMessage) ([]*DatabaseGroupMessage, error) {- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().Space(`+ SELECT+ project,+ resource_id,+ name,+ expression+ FROM db_group+ WHERE TRUE+ `)+if v := find.ProjectID; v != nil {- where, args = append(where, fmt.Sprintf("project = $%d", len(args)+1)), append(args, *v)+ q.And("project = ?", *v)}if v := find.ResourceID; v != nil {- where, args = append(where, fmt.Sprintf("resource_id = $%d", len(args)+1)), append(args, *v)+ q.And("resource_id = ?", *v)}- fields := []string{- "project",- "resource_id",- "name",- "expression",++ q.Space("ORDER BY project, resource_id ASC")++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}- query := fmt.Sprintf(`SELECT %s FROM db_group WHERE %s ORDER BY project, resource_id ASC;`, strings.Join(fields, ","), strings.Join(where, " AND "))+var databaseGroups []*DatabaseGroupMessagerows, err := txn.QueryContext(ctx, query, args...)if err != nil {@@ -158,24 +168,27 @@ func (*Store) listDatabaseGroupImpl(ctx context.Context, txn *sql.Tx, find *Find// UpdateDatabaseGroup updates a database group.func (s *Store) UpdateDatabaseGroup(ctx context.Context, projectID, resourceID string, patch *UpdateDatabaseGroupMessage) (*DatabaseGroupMessage, error) {- set, args := []string{}, []any{}+ set := qb.Q()if v := patch.Title; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set.Comma("name = ?", *v)}if v := patch.Expression; v != nil {exprBytes, err := protojson.Marshal(patch.Expression)if err != nil {return nil, errors.Wrapf(err, "failed to marshal expression")}- set, args = append(set, fmt.Sprintf("expression = $%d", len(args)+1)), append(args, exprBytes)+ set.Comma("expression = ?", exprBytes)+ }+ if set.Len() == 0 {+ return nil, errors.New("no fields to update")+ }++ q := qb.Q().Space("UPDATE db_group SET ? WHERE project = ? AND resource_id = ? RETURNING project, resource_id, name, expression", set, projectID, resourceID)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}- args = append(args, projectID, resourceID)- query := fmt.Sprintf(`- UPDATE db_group SET- %s- WHERE project = $%d AND resource_id = $%d- RETURNING project, resource_id, name, expression;- `, strings.Join(set, ", "), len(args)-1, len(args))tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -210,33 +223,32 @@ func (s *Store) UpdateDatabaseGroup(ctx context.Context, projectID, resourceID s// CreateDatabaseGroup creates a database group.func (s *Store) CreateDatabaseGroup(ctx context.Context, create *DatabaseGroupMessage) (*DatabaseGroupMessage, error) {- query := `- INSERT INTO db_group (- project,- resource_id,- name,- expression- ) VALUES ($1, $2, $3, $4);- `exprBytes, err := protojson.Marshal(create.Expression)if err != nil {return nil, errors.Wrapf(err, "failed to marshal expression")}+ q := qb.Q().Space(`+ INSERT INTO db_group (+ project,+ resource_id,+ name,+ expression+ ) VALUES (?, ?, ?, ?)+ `, create.ProjectID, create.ResourceID, create.Title, exprBytes)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, errors.Wrapf(err, "failed to begin transaction")}defer tx.Rollback()- if _, err := tx.ExecContext(- ctx,- query,- create.ProjectID,- create.ResourceID,- create.Title,- exprBytes,- ); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return nil, errors.Wrapf(err, "failed to scan")}if err := tx.Commit(); err != nil {diff --git a/backend/store/db_connection.go b/backend/store/db_connection.goindex 4d3ebee162..987b4a2090 100644--- a/backend/store/db_connection.go+++ b/backend/store/db_connection.go@@ -11,6 +11,8 @@ import ("github.com/fsnotify/fsnotify""github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/common/qb")// DBConnectionManager manages database connections with support for dynamic updates.@@ -204,11 +206,24 @@ func createConnection(ctx context.Context, pgURL string) (*sql.DB, error) {// Configure connection poolvar maxConns, reservedConns int- if err := db.QueryRowContext(ctx, `SHOW max_connections`).Scan(&maxConns); err != nil {+ q := qb.Q().Space("SHOW max_connections")+ sql, args, err := q.ToSQL()+ if err != nil {+ db.Close()+ return nil, errors.Wrap(err, "failed to build sql")+ }+ if err := db.QueryRowContext(ctx, sql, args...).Scan(&maxConns); err != nil {db.Close()return nil, errors.Wrap(err, "failed to get max_connections")}- if err := db.QueryRowContext(ctx, `SHOW superuser_reserved_connections`).Scan(&reservedConns); err != nil {++ q = qb.Q().Space("SHOW superuser_reserved_connections")+ sql, args, err = q.ToSQL()+ if err != nil {+ db.Close()+ return nil, errors.Wrap(err, "failed to build sql")+ }+ if err := db.QueryRowContext(ctx, sql, args...).Scan(&reservedConns); err != nil {db.Close()return nil, errors.Wrap(err, "failed to get superuser_reserved_connections")}diff --git a/backend/store/db_schema.go b/backend/store/db_schema.goindex 746561c641..e47639dc9a 100644--- a/backend/store/db_schema.go+++ b/backend/store/db_schema.go@@ -3,12 +3,12 @@ package storeimport ("context""database/sql"- "fmt"- "strings"+ "github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/store/model")@@ -29,9 +29,18 @@ func (s *Store) GetDBSchema(ctx context.Context, find *FindDBSchemaMessage) (*moreturn v, nil}- where, args := []string{"TRUE"}, []any{}- where, args = append(where, fmt.Sprintf("instance = $%d", len(args)+1)), append(args, find.InstanceID)- where, args = append(where, fmt.Sprintf("db_name = $%d", len(args)+1)), append(args, find.DatabaseName)+ q := qb.Q().Space(`+ SELECT+ metadata,+ raw_dump,+ config+ FROM db_schema+ WHERE instance = ? AND db_name = ?`, find.InstanceID, find.DatabaseName)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})if err != nil {@@ -40,15 +49,7 @@ func (s *Store) GetDBSchema(ctx context.Context, find *FindDBSchemaMessage) (*modefer tx.Rollback()var metadata, schema, config []byte- if err := tx.QueryRowContext(ctx, `- SELECT- metadata,- raw_dump,- config- FROM db_schema- WHERE `+strings.Join(where, " AND "),- args...,- ).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&metadata,&schema,&config,@@ -90,7 +91,7 @@ func (s *Store) UpsertDBSchema(return err}- query := `+ q := qb.Q().Space(`INSERT INTO db_schema (instance,db_name,@@ -99,14 +100,26 @@ func (s *Store) UpsertDBSchema(config,todo)- VALUES ($1, $2, $3, $4, $5, $6)+ VALUES (?, ?, ?, ?, ?, ?)ON CONFLICT(instance, db_name) DO UPDATE SETmetadata = EXCLUDED.metadata,raw_dump = EXCLUDED.raw_dump,config = EXCLUDED.config,todo = EXCLUDED.todo- RETURNING metadata, raw_dump, config- `+ RETURNING metadata, raw_dump, config`,+ instanceID,+ databaseName,+ metadataBytes,+ // Convert to string because []byte{} is null which violates db schema constraints.+ string(dbSchema),+ configBytes,+ todo)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return err@@ -114,15 +127,7 @@ func (s *Store) UpsertDBSchema(defer tx.Rollback()var metadata, schema, config []byte- if err := tx.QueryRowContext(ctx, query,- instanceID,- databaseName,- metadataBytes,- // Convert to string because []byte{} is null which violates db schema constraints.- string(dbSchema),- configBytes,- todo,- ).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&metadata,&schema,&config,@@ -143,29 +148,31 @@ func (s *Store) UpsertDBSchema(// UpdateDBSchema updates a database schema.func (s *Store) UpdateDBSchema(ctx context.Context, instanceID, databaseName string, patch *UpdateDBSchemaMessage) error {- set, args := []string{}, []any{}+ set := qb.Q()if v := patch.Config; v != nil {bytes, err := protojson.Marshal(v)if err != nil {return err}- set, args = append(set, fmt.Sprintf("config = $%d", len(args)+1)), append(args, bytes)+ set.Comma("config = ?", bytes)+ }+ if set.Len() == 0 {+ return errors.New("no fields to update")}- where := []string{"TRUE"}- where, args = append(where, fmt.Sprintf("instance = $%d", len(args)+1)), append(args, instanceID)- where, args = append(where, fmt.Sprintf("db_name = $%d", len(args)+1)), append(args, databaseName)+ q := qb.Q().Space("UPDATE db_schema SET ? WHERE instance = ? AND db_name = ?", set, instanceID, databaseName)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return err}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `- UPDATE db_schema- SET `+strings.Join(set, ", ")+`- WHERE `+strings.Join(where, " AND "), args...,- ); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}if err := tx.Commit(); err != nil {@@ -187,7 +194,7 @@ type DBSchemaWithTodo struct {// ListDBSchemasWithTodo lists all db_schemas with todo = true.func (s *Store) ListDBSchemasWithTodo(ctx context.Context, engineType storepb.Engine, limit int) ([]*DBSchemaWithTodo, error) {- rows, err := s.GetDB().QueryContext(ctx, `+ q := qb.Q().Space(`SELECTdb_schema.id,db_schema.instance,@@ -195,9 +202,15 @@ func (s *Store) ListDBSchemasWithTodo(ctx context.Context, engineType storepb.Endb_schema.metadataFROM db_schemaLEFT JOIN instance ON db_schema.instance = instance.resource_id- WHERE db_schema.todo = true AND instance.metadata->>'engine' = $1- LIMIT $2- `, engineType.String(), limit)+ WHERE db_schema.todo = true AND instance.metadata->>'engine' = ?+ LIMIT ?`, engineType.String(), limit)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := s.GetDB().QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -225,17 +238,20 @@ func (s *Store) ListDBSchemasWithTodo(ctx context.Context, engineType storepb.En// UpdateDBSchemaTodo updates the todo column of a db_schema.func (s *Store) UpdateDBSchemaTodo(ctx context.Context, id int, todo bool) error {+ q := qb.Q().Space("UPDATE db_schema SET todo = ? WHERE id = ?", todo, id)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return err}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `- UPDATE db_schema- SET todo = $1- WHERE id = $2- `, todo, id); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}@@ -244,17 +260,20 @@ func (s *Store) UpdateDBSchemaTodo(ctx context.Context, id int, todo bool) error// UpdateDBSchemaMetadata updates the metadata of a db_schema.func (s *Store) UpdateDBSchemaMetadata(ctx context.Context, id int, metadata string) error {+ q := qb.Q().Space("UPDATE db_schema SET metadata = ? WHERE id = ?", metadata, id)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return err}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `- UPDATE db_schema- SET metadata = $1- WHERE id = $2- `, metadata, id); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}@@ -265,17 +284,20 @@ func (s *Store) UpdateDBSchemaMetadata(ctx context.Context, id int, metadata str// This is used by the migrator to avoid race conditions with the sync process.// The WHERE condition ensures atomic check-and-update, preventing any race conditions.func (s *Store) UpdateDBSchemaMetadataIfTodo(ctx context.Context, id int, metadata string) error {+ q := qb.Q().Space("UPDATE db_schema SET metadata = ?, todo = false WHERE id = ? AND todo = true", metadata, id)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return err}defer tx.Rollback()- result, err := tx.ExecContext(ctx, `- UPDATE db_schema- SET metadata = $1, todo = false- WHERE id = $2 AND todo = true- `, metadata, id)+ result, err := tx.ExecContext(ctx, query, args...)if err != nil {return err}diff --git a/backend/store/environment.go b/backend/store/environment.goindex 694dca51e4..39eca60f46 100644--- a/backend/store/environment.go+++ b/backend/store/environment.go@@ -5,6 +5,7 @@ import ("github.com/pkg/errors"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -24,13 +25,20 @@ func (s *Store) GetEnvironmentByID(ctx context.Context, id string) (*storepb.Envfunc (s *Store) CheckDatabaseUseEnvironment(ctx context.Context, id string) (bool, error) {var exists bool- if err := s.GetDB().QueryRowContext(ctx, `+ q := qb.Q().Space(`SELECT EXISTS (SELECT 1- FROM db- WHERE db.environment = $1+ FROM db+ WHERE db.environment = ?)- `, id).Scan(&exists); err != nil {+ `, id)++ sql, args, err := q.ToSQL()+ if err != nil {+ return false, errors.Wrapf(err, "failed to build sql")+ }++ if err := s.GetDB().QueryRowContext(ctx, sql, args...).Scan(&exists); err != nil {return false, errors.Wrapf(err, "failed to check if databases uses environment %q", id)}diff --git a/backend/store/export_archive.go b/backend/store/export_archive.goindex 3b6134526a..be2b2a655f 100644--- a/backend/store/export_archive.go+++ b/backend/store/export_archive.go@@ -3,14 +3,13 @@ package storeimport ("context""database/sql"- "fmt"- "strings""time""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -43,10 +42,23 @@ func (s *Store) GetExportArchive(ctx context.Context, find *FindExportArchiveMes// ListExportArchives lists export archives.func (s *Store) ListExportArchives(ctx context.Context, find *FindExportArchiveMessage) ([]*ExportArchiveMessage, error) {- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().Space(`+ SELECT+ id,+ created_at,+ bytes,+ payload+ FROM export_archive+ WHERE TRUE+ `)if v := find.UID; v != nil {- where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)+ q.And("id = ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})@@ -55,17 +67,7 @@ func (s *Store) ListExportArchives(ctx context.Context, find *FindExportArchiveM}defer tx.Rollback()- query := fmt.Sprintf(`- SELECT- id,- created_at,- bytes,- payload- FROM export_archive- WHERE %s`, strings.Join(where, " AND "))- rows, err := tx.QueryContext(ctx, query,- args...,- )+ rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -108,26 +110,26 @@ func (s *Store) CreateExportArchive(ctx context.Context, create *ExportArchiveMereturn nil, err}- query := `+ q := qb.Q().Space(`INSERT INTO export_archive (bytes,payload)- VALUES ($1, $2)- RETURNING id;- `+ VALUES (?, ?)+ RETURNING id+ `, create.Bytes, payload)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, err}defer tx.Rollback()- if err := tx.QueryRowContext(ctx, query,- create.Bytes,- payload,- ).Scan(- &create.UID,- ); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&create.UID); err != nil {return nil, err}if err := tx.Commit(); err != nil {@@ -139,13 +141,19 @@ func (s *Store) CreateExportArchive(ctx context.Context, create *ExportArchiveMe// DeleteExportArchive deletes a export archive.func (s *Store) DeleteExportArchive(ctx context.Context, uid int) error {+ q := qb.Q().Space("DELETE FROM export_archive WHERE id = ?", uid)+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return err}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `DELETE FROM export_archive WHERE id = $1;`, uid); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}@@ -157,12 +165,13 @@ func (s *Store) DeleteExportArchive(ctx context.Context, uid int) error {func (s *Store) DeleteExpiredExportArchives(ctx context.Context, retentionPeriod time.Duration) (int64, error) {cutoffTime := time.Now().Add(-retentionPeriod)- query := `- DELETE FROM export_archive- WHERE created_at < $1- `+ q := qb.Q().Space("DELETE FROM export_archive WHERE created_at < ?", cutoffTime)+ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")+ }- result, err := s.GetDB().ExecContext(ctx, query, cutoffTime)+ result, err := s.GetDB().ExecContext(ctx, query, args...)if err != nil {return 0, err}diff --git a/backend/store/group.go b/backend/store/group.goindex 55c4033324..0f6aedb8e6 100644--- a/backend/store/group.go+++ b/backend/store/group.go@@ -3,14 +3,12 @@ package storeimport ("context""database/sql"- "fmt"- "strings"-- "google.golang.org/protobuf/encoding/protojson""github.com/pkg/errors"+ "google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -92,42 +90,57 @@ func (s *Store) ListGroups(ctx context.Context, find *FindGroupMessage) ([]*Grou}func (*Store) listGroupImpl(ctx context.Context, txn *sql.Tx, find *FindGroupMessage) ([]*GroupMessage, error) {- where, args := []string{"TRUE"}, []any{}- if filter := find.Filter; filter != nil {- where = append(where, filter.Where)- args = append(args, filter.Args...)- }- if v := find.Email; v != nil {- where, args = append(where, fmt.Sprintf("email = $%d", len(args)+1)), append(args, *v)- }+ with := qb.Q()+ from := qb.Q().Space("user_group")+ where := qb.Q().Space("TRUE")- var with, join string+ // Build CTE for project filtering if neededif v := find.ProjectID; v != nil {- with = `WITH all_members AS (+ with.Space(`WITH all_members AS (SELECTjsonb_array_elements_text(jsonb_array_elements(policy.payload->'bindings')->'members') AS member,jsonb_array_elements(policy.payload->'bindings')->>'role' AS roleFROM policy- WHERE ((resource_type = '` + storepb.Policy_PROJECT.String() + `' AND resource = 'projects/` + *v + `') OR resource_type = '` + storepb.Policy_WORKSPACE.String() + `') AND type = '` + storepb.Policy_IAM.String() + `'+ WHERE ((resource_type = ? AND resource = ?) OR resource_type = ?) AND type = ?),project_members AS (SELECT ARRAY_AGG(member) AS members FROM all_members WHERE role NOT LIKE 'roles/workspace%'- )`- join = `INNER JOIN project_members ON (CONCAT('groups/', user_group.email) = ANY(project_members.members) OR '` + common.AllUsers + `' = ANY(project_members.members))`+ )`, storepb.Policy_PROJECT.String(), "projects/"+*v, storepb.Policy_WORKSPACE.String(), storepb.Policy_IAM.String())+ from.Space(`INNER JOIN project_members ON (CONCAT('groups/', user_group.email) = ANY(project_members.members) OR ? = ANY(project_members.members))`, common.AllUsers)}- query := with + `- SELECT- user_group.email,- user_group.name,- user_group.description,- user_group.payload- FROM user_group ` + join + ` WHERE ` + strings.Join(where, " AND ") + ` ORDER BY email`+ if filter := find.Filter; filter != nil {+ where.And(ConvertDollarPlaceholders(filter.Where), filter.Args...)+ }+ if v := find.Email; v != nil {+ where.And("email = ?", *v)+ }++ q := qb.Q()+ if with.Len() > 0 {+ q.Space("?", with)+ }+ q.Space(`+ SELECT+ user_group.email,+ user_group.name,+ user_group.description,+ user_group.payload+ FROM ?+ WHERE ?+ ORDER BY email+ `, from, where)+if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}var groups []*GroupMessage@@ -168,17 +181,23 @@ func (s *Store) CreateGroup(ctx context.Context, create *GroupMessage) (*GroupMecreate.Payload = &storepb.GroupPayload{}}- query := `+ payloadBytes, err := protojson.Marshal(create.Payload)+ if err != nil {+ return nil, err+ }++ q := qb.Q().Space(`INSERT INTO user_group (email,name,description,payload- ) VALUES ($1, $2, $3, $4)- `- payloadBytes, err := protojson.Marshal(create.Payload)+ ) VALUES (?, ?, ?, ?)+ `, create.Email, create.Title, create.Description, payloadBytes)++ query, args, err := q.ToSQL()if err != nil {- return nil, err+ return nil, errors.Wrapf(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, nil)@@ -187,14 +206,7 @@ func (s *Store) CreateGroup(ctx context.Context, create *GroupMessage) (*GroupMe}defer tx.Rollback()- if _, err := tx.ExecContext(- ctx,- query,- create.Email,- create.Title,- create.Description,- payloadBytes,- ); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return nil, err}@@ -208,41 +220,48 @@ func (s *Store) CreateGroup(ctx context.Context, create *GroupMessage) (*GroupMe// UpdateGroup updates a group.func (s *Store) UpdateGroup(ctx context.Context, email string, patch *UpdateGroupMessage) (*GroupMessage, error) {- tx, err := s.GetDB().BeginTx(ctx, nil)- if err != nil {- return nil, errors.Wrapf(err, "failed to begin transaction")- }-- set, args := []string{}, []any{}+ set := qb.Q()if v := patch.Title; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set.Comma("name = ?", *v)}if v := patch.Description; v != nil {- set, args = append(set, fmt.Sprintf("description = $%d", len(args)+1)), append(args, *v)+ set.Comma("description = ?", *v)}if v := patch.Payload; v != nil {payload, err := protojson.Marshal(v)if err != nil {return nil, err}- set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, payload)+ set.Comma("payload = ?", payload)}- args = append(args, email)- var group GroupMessage- var payload []byte-- if err := tx.QueryRowContext(ctx, fmt.Sprintf(`+ q := qb.Q().Space(`UPDATE user_group- SET %s- WHERE email = $%d+ SET ?+ WHERE email = ?RETURNINGemail,name,description,payload- `, strings.Join(set, ", "), len(set)+1), args...).Scan(+ `, set, email)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to begin transaction")+ }+ defer tx.Rollback()++ var group GroupMessage+ var payload []byte++ if err := tx.QueryRowContext(ctx, query, args...).Scan(&group.Email,&group.Title,&group.Description,@@ -273,7 +292,13 @@ func (s *Store) DeleteGroup(ctx context.Context, email string) error {}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `DELETE FROM user_group WHERE email = $1`, email); err != nil {+ q := qb.Q().Space("DELETE FROM user_group WHERE email = ?", email)+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}diff --git a/backend/store/idp.go b/backend/store/idp.goindex ba6d05c2c6..ab7e01d3c3 100644--- a/backend/store/idp.go+++ b/backend/store/idp.go@@ -3,13 +3,12 @@ package storeimport ("context""database/sql"- "fmt"- "strings""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -69,7 +68,8 @@ func (s *Store) CreateIdentityProvider(ctx context.Context, create *IdentityProvif err != nil {return nil, errors.Wrap(err, "failed to marshal identity provider config")}- if _, err := tx.ExecContext(ctx, `++ q := qb.Q().Space(`INSERT INTO idp (resource_id,name,@@ -77,14 +77,15 @@ func (s *Store) CreateIdentityProvider(ctx context.Context, create *IdentityProvtype,config)- VALUES ($1, $2, $3, $4, $5)- `,- create.ResourceID,- create.Title,- create.Domain,- create.Type.String(),- configBytes,- ); err != nil {+ VALUES (?, ?, ?, ?, ?)+ `, create.ResourceID, create.Title, create.Domain, create.Type.String(), configBytes)++ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return nil, err}@@ -172,38 +173,45 @@ func (s *Store) UpdateIdentityProvider(ctx context.Context, patch *UpdateIdentit}func (*Store) updateIdentityProviderImpl(ctx context.Context, txn *sql.Tx, patch *UpdateIdentityProviderMessage) (*IdentityProviderMessage, error) {- set, args := []string{}, []any{}+ set := qb.Q()if v := patch.Title; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set.Comma("name = ?", *v)}if v := patch.Domain; v != nil {- set, args = append(set, fmt.Sprintf("domain = $%d", len(args)+1)), append(args, *v)+ set.Comma("domain = ?", *v)}if v := patch.Config; v != nil {configBytes, err := getConfigBytes(v)if err != nil {return nil, errors.Wrap(err, "failed to marshal identity provider config")}- set, args = append(set, fmt.Sprintf("config = $%d", len(args)+1)), append(args, string(configBytes))+ set.Comma("config = ?", string(configBytes))+ }+ if set.Len() == 0 {+ return nil, errors.New("no fields to update")}- args = append(args, patch.ResourceID)- identityProvider := &IdentityProviderMessage{}- var identityProviderType string- var identityProviderConfig string- if err := txn.QueryRowContext(ctx, fmt.Sprintf(`+ q := qb.Q().Space(`UPDATE idp- SET `+strings.Join(set, ", ")+`- WHERE resource_id = $%d+ SET ?+ WHERE resource_id = ?RETURNINGresource_id,name,domain,type,config- `, len(args)),- args...,- ).Scan(+ `, set, patch.ResourceID)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ identityProvider := &IdentityProviderMessage{}+ var identityProviderType string+ var identityProviderConfig string+ if err := txn.QueryRowContext(ctx, query, args...).Scan(&identityProvider.ResourceID,&identityProvider.Title,&identityProvider.Domain,@@ -222,12 +230,7 @@ func (*Store) updateIdentityProviderImpl(ctx context.Context, txn *sql.Tx, patch}func (*Store) listIdentityProvidersImpl(ctx context.Context, txn *sql.Tx, find *FindIdentityProviderMessage) ([]*IdentityProviderMessage, error) {- where, args := []string{"TRUE"}, []any{}- if v := find.ResourceID; v != nil {- where, args = append(where, fmt.Sprintf("resource_id = $%d", len(args)+1)), append(args, *v)- }-- rows, err := txn.QueryContext(ctx, `+ q := qb.Q().Space(`SELECTresource_id,name,@@ -235,9 +238,21 @@ func (*Store) listIdentityProvidersImpl(ctx context.Context, txn *sql.Tx, find *type,configFROM idp- WHERE `+strings.Join(where, " AND ")+` ORDER BY id ASC`,- args...,- )+ WHERE TRUE+ `)++ if v := find.ResourceID; v != nil {+ q.And("resource_id = ?", *v)+ }++ q.Space("ORDER BY id ASC")++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := txn.QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -275,10 +290,13 @@ func (s *Store) DeleteIdentityProvider(ctx context.Context, resourceID string) e}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `- DELETE FROM idp- WHERE resource_id = $1- `, resourceID); err != nil {+ q := qb.Q().Space("DELETE FROM idp WHERE resource_id = ?", resourceID)+ sql, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return err}diff --git a/backend/store/instance.go b/backend/store/instance.goindex 29ee811906..d2a0a8277f 100644--- a/backend/store/instance.go+++ b/backend/store/instance.go@@ -3,7 +3,6 @@ package storeimport ("context""database/sql"- "fmt""strings""github.com/pkg/errors"@@ -12,6 +11,7 @@ import ("google.golang.org/protobuf/proto""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -118,17 +118,18 @@ func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMeif instanceCreate.EnvironmentID != nil && *instanceCreate.EnvironmentID != "" {environment = instanceCreate.EnvironmentID}- if _, err := tx.ExecContext(ctx, `+ q := qb.Q().Space(`INSERT INTO instance (resource_id,environment,metadata- ) VALUES ($1, $2, $3)- `,- instanceCreate.ResourceID,- environment,- metadataBytes,- ); err != nil {+ ) VALUES (?, ?, ?)+ `, instanceCreate.ResourceID, environment, metadataBytes)+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return nil, err}@@ -147,18 +148,18 @@ func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMe// UpdateInstanceV2 updates an instance.func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessage) (*InstanceMessage, error) {- set, args, where := []string{}, []any{}, []string{}+ set := qb.Q()+if v := patch.EnvironmentID; v != nil {if *v == "" {// Unset the environment by setting it to NULL- set = append(set, fmt.Sprintf("environment = $%d", len(args)+1))- args = append(args, nil)+ set.Comma("environment = ?", nil)} else {- set, args = append(set, fmt.Sprintf("environment = $%d", len(args)+1)), append(args, *v)+ set.Comma("environment = ?", *v)}}if v := patch.Deleted; v != nil {- set, args = append(set, fmt.Sprintf(`deleted = $%d`, len(args)+1)), append(args, *v)+ set.Comma("deleted = ?", *v)}if v := patch.Metadata; v != nil {redacted, err := s.obfuscateInstance(ctx, v)@@ -169,38 +170,43 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessaif err != nil {return nil, err}- set, args = append(set, fmt.Sprintf("metadata = $%d", len(args)+1)), append(args, metadata)+ set.Comma("metadata = ?", metadata)}++ if set.Len() == 0 {+ return nil, errors.New("no update field specified")+ }++ where := qb.Q()if v := patch.ResourceID; v != nil {- where, args = append(where, fmt.Sprintf("resource_id = $%d", len(args)+1)), append(args, *v)+ where.And("resource_id = ?", *v)}if v := patch.FindByEnvironmentID; v != nil {- where, args = append(where, fmt.Sprintf("environment = $%d", len(args)+1)), append(args, *v)+ where.And("environment = ?", *v)}- if len(set) == 0 {- return nil, errors.New("no update field specified")- }- if len(where) == 0 {+ if where.Len() == 0 {return nil, errors.Errorf("empty where")}+ q := qb.Q().Space("UPDATE instance SET ?", set).+ Space("WHERE ?", where)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, err}defer tx.Rollback()- if len(set) > 0 {- query := fmt.Sprintf(`- UPDATE instance- SET `+strings.Join(set, ", ")+`- WHERE %s- `, strings.Join(where, " AND "))- if _, err := tx.ExecContext(ctx, query, args...); err != nil {- return nil, err- }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {+ return nil, err}+if err := tx.Commit(); err != nil {return nil, err}@@ -214,49 +220,51 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessa}func (s *Store) listInstanceImplV2(ctx context.Context, txn *sql.Tx, find *FindInstanceMessage) ([]*InstanceMessage, error) {- where, args := []string{"TRUE"}, []any{}- joinDSQuery := ""- joinDBQuery := ""+ where := qb.Q().Space("TRUE")+ from := qb.Q().Space("instance")if filter := find.Filter; filter != nil {- where = append(where, filter.Where)- args = append(args, filter.Args...)+ where.And(ConvertDollarPlaceholders(filter.Where), filter.Args...)if hasHostPortFilter(filter.Where) {- joinDSQuery = "CROSS JOIN jsonb_array_elements(instance.metadata -> 'dataSources') AS ds"+ from.Space("CROSS JOIN jsonb_array_elements(instance.metadata -> 'dataSources') AS ds")}if strings.Contains(filter.Where, "db.project") {- joinDBQuery = "LEFT JOIN db ON db.instance = instance.resource_id"+ from.Space("LEFT JOIN db ON db.instance = instance.resource_id")}}if v := find.ResourceID; v != nil {- where, args = append(where, fmt.Sprintf("instance.resource_id = $%d", len(args)+1)), append(args, *v)+ where.And("instance.resource_id = ?", *v)}if v := find.ResourceIDs; v != nil {- where, args = append(where, fmt.Sprintf("instance.resource_id = ANY($%d)", len(args)+1)), append(args, *v)+ where.And("instance.resource_id = ANY(?)", *v)}if !find.ShowDeleted {- where, args = append(where, fmt.Sprintf("instance.deleted = $%d", len(args)+1)), append(args, false)+ where.And("instance.deleted = ?", false)}- query := fmt.Sprintf(`+ q := qb.Q().Space(`SELECT DISTINCT ON (resource_id)instance.resource_id,instance.environment,instance.deleted,instance.metadata- FROM instance- %s- %s- WHERE %s- ORDER BY resource_id`, joinDSQuery, joinDBQuery, strings.Join(where, " AND "))+ FROM ?+ WHERE ?+ ORDER BY resource_id+ `, from, where)if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ query, queryArgs, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}var instanceMessages []*InstanceMessage- rows, err := txn.QueryContext(ctx, query, args...)+ rows, err := txn.QueryContext(ctx, query, queryArgs...)if err != nil {return nil, err}@@ -298,12 +306,15 @@ func hasHostPortFilter(where string) bool {return strings.Contains(where, "ds ->> 'host'") || strings.Contains(where, "ds ->> 'port'")}-var countActivateInstanceQuery = "SELECT COUNT(1) FROM instance WHERE (metadata ? 'activation') AND (metadata->>'activation')::boolean = TRUE AND deleted = FALSE"-// GetActivatedInstanceCount gets the number of activated instances.func (s *Store) GetActivatedInstanceCount(ctx context.Context) (int, error) {+ q := qb.Q().Space("SELECT COUNT(1) FROM instance WHERE (metadata ?? 'activation') AND (metadata->>'activation')::boolean = TRUE AND deleted = FALSE")+ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")+ }var count int- if err := s.GetDB().QueryRowContext(ctx, countActivateInstanceQuery).Scan(&count); err != nil {+ if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&count); err != nil {return 0, err}return count, nil@@ -511,91 +522,146 @@ func (s *Store) DeleteInstance(ctx context.Context, resourceID string) error {// Delete query_history entries that reference this instance// The database field contains instance reference like 'instances/{instance}/databases/{database}'- if _, err := tx.ExecContext(ctx, `+ q := qb.Q().Space(`DELETE FROM query_history- WHERE database LIKE 'instances/' || $1 || '/databases/%'- `, resourceID); err != nil {+ WHERE database LIKE 'instances/' || ? || '/databases/%'+ `, resourceID)+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to delete query_history for instance %s", resourceID)}// Delete task_run_log entries for tasks associated with this instance- if _, err := tx.ExecContext(ctx, `+ q = qb.Q().Space(`DELETE FROM task_run_logWHERE task_run_id IN (SELECT tr.id FROM task_run trJOIN task t ON tr.task_id = t.id- WHERE t.instance = $1+ WHERE t.instance = ?)- `, resourceID); err != nil {+ `, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to delete task_run_log for instance %s", resourceID)}// Delete task_run entries for tasks associated with this instance- if _, err := tx.ExecContext(ctx, `+ q = qb.Q().Space(`DELETE FROM task_runWHERE task_id IN (- SELECT id FROM task WHERE instance = $1+ SELECT id FROM task WHERE instance = ?)- `, resourceID); err != nil {+ `, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to delete task_run for instance %s", resourceID)}// Delete tasks associated with this instance- if _, err := tx.ExecContext(ctx, `- DELETE FROM task WHERE instance = $1- `, resourceID); err != nil {+ q = qb.Q().Space(`+ DELETE FROM task WHERE instance = ?+ `, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to delete tasks for instance %s", resourceID)}// Delete changelogs associated with this instance- if _, err := tx.ExecContext(ctx, `- DELETE FROM changelog WHERE instance = $1- `, resourceID); err != nil {+ q = qb.Q().Space(`+ DELETE FROM changelog WHERE instance = ?+ `, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to delete changelogs for instance %s", resourceID)}// Delete sync_history associated with this instance- if _, err := tx.ExecContext(ctx, `- DELETE FROM sync_history WHERE instance = $1- `, resourceID); err != nil {+ q = qb.Q().Space(`+ DELETE FROM sync_history WHERE instance = ?+ `, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to delete sync_history for instance %s", resourceID)}// Delete revisions associated with this instance- if _, err := tx.ExecContext(ctx, `- DELETE FROM revision WHERE instance = $1- `, resourceID); err != nil {+ q = qb.Q().Space(`+ DELETE FROM revision WHERE instance = ?+ `, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to delete revisions for instance %s", resourceID)}// Update worksheets to nullify instance and db_name references- if _, err := tx.ExecContext(ctx, `+ q = qb.Q().Space(`UPDATE worksheetSET instance = NULL, db_name = NULL- WHERE instance = $1- `, resourceID); err != nil {+ WHERE instance = ?+ `, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to update worksheets for instance %s", resourceID)}// Delete db_schema entries associated with databases on this instance- if _, err := tx.ExecContext(ctx, `- DELETE FROM db_schema WHERE instance = $1- `, resourceID); err != nil {+ q = qb.Q().Space(`+ DELETE FROM db_schema WHERE instance = ?+ `, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to delete db_schema for instance %s", resourceID)}// Delete databases associated with this instance- if _, err := tx.ExecContext(ctx, `- DELETE FROM db WHERE instance = $1- `, resourceID); err != nil {+ q = qb.Q().Space(`+ DELETE FROM db WHERE instance = ?+ `, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to delete databases for instance %s", resourceID)}// Finally, delete the instance itself (only if it's marked as deleted)- result, err := tx.ExecContext(ctx, `+ q = qb.Q().Space(`DELETE FROM instance- WHERE resource_id = $1 AND deleted = TRUE+ WHERE resource_id = ? AND deleted = TRUE`, resourceID)+ query, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ result, err := tx.ExecContext(ctx, query, args...)if err != nil {return errors.Wrapf(err, "failed to delete instance %s", resourceID)}diff --git a/backend/store/issue.go b/backend/store/issue.goindex 3bc60c23b5..5e549b0a1a 100644--- a/backend/store/issue.go+++ b/backend/store/issue.go@@ -13,6 +13,7 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -140,7 +141,8 @@ func (s *Store) CreateIssueV2(ctx context.Context, create *IssueMessage, creatorreturn nil, errors.Wrapf(err, "failed to marshal issue payload")}tsVector := getTSVector(fmt.Sprintf("%s %s", create.Title, create.Description))- query := `++ q := qb.Q().Space(`INSERT INTO issue (creator_id,project,@@ -153,16 +155,8 @@ func (s *Store) CreateIssueV2(ctx context.Context, create *IssueMessage, creatorpayload,ts_vector)- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)- RETURNING id`-- tx, err := s.GetDB().BeginTx(ctx, nil)- if err != nil {- return nil, err- }- defer tx.Rollback()-- if err := tx.QueryRowContext(ctx, query,+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)+ RETURNING id`,creatorID,create.Project.ResourceID,create.PipelineUID,@@ -173,9 +167,20 @@ func (s *Store) CreateIssueV2(ctx context.Context, create *IssueMessage, creatorcreate.Description,payload,tsVector,- ).Scan(- &create.UID,- ); err != nil {+ )++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return nil, err+ }+ defer tx.Rollback()++ if err := tx.QueryRowContext(ctx, query, args...).Scan(&create.UID); err != nil {if err == sql.ErrNoRows {return nil, common.FormatDBErrorEmptyRowWithQuery(query)}@@ -195,27 +200,29 @@ func (s *Store) UpdateIssueV2(ctx context.Context, uid int, patch *UpdateIssueMereturn nil, err}- set, args := []string{"updated_at = $1"}, []any{time.Now()}+ set := qb.Q()+ set.Comma("updated_at = ?", time.Now())+if v := patch.PipelineUID; v != nil {- set, args = append(set, fmt.Sprintf("pipeline_id = $%d", len(args)+1)), append(args, *v)+ set.Comma("pipeline_id = ?", *v)}if v := patch.Title; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set.Comma("name = ?", *v)}if v := patch.Status; v != nil {- set, args = append(set, fmt.Sprintf("status = $%d", len(args)+1)), append(args, v.String())+ set.Comma("status = ?", v.String())}if v := patch.Description; v != nil {- set, args = append(set, fmt.Sprintf("description = $%d", len(args)+1)), append(args, *v)+ set.Comma("description = ?", *v)}if v := patch.PayloadUpsert; v != nil {p, err := protojson.Marshal(v)if err != nil {return nil, errors.Wrapf(err, "failed to marshal patch.PayloadUpsert")}- set, args = append(set, fmt.Sprintf("payload = payload || $%d", len(args)+1)), append(args, p)+ set.Comma("payload = payload || ?", p)} else if patch.RemoveLabels {- set, args = append(set, fmt.Sprintf("payload = payload || jsonb_build_object('labels', $%d::JSONB)", len(args)+1)), append(args, nil)+ set.Comma("payload = payload || jsonb_build_object('labels', ?::JSONB)", nil)}if patch.Title != nil || patch.Description != nil {@@ -229,11 +236,15 @@ func (s *Store) UpdateIssueV2(ctx context.Context, uid int, patch *UpdateIssueMe}tsVector := getTSVector(fmt.Sprintf("%s %s", title, description))- set = append(set, fmt.Sprintf("ts_vector = $%d", len(args)+1))- args = append(args, tsVector)+ set.Comma("ts_vector = ?", tsVector)}- args = append(args, uid)+ q := qb.Q().Space("UPDATE issue SET ? WHERE id = ?", set, uid)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -241,12 +252,7 @@ func (s *Store) UpdateIssueV2(ctx context.Context, uid int, patch *UpdateIssueMe}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, fmt.Sprintf(`- UPDATE issue- SET `+strings.Join(set, ", ")+`- WHERE id = $%d`, len(args)),- args...,- ); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return nil, err}@@ -265,61 +271,59 @@ func (s *Store) UpdateIssueV2(ctx context.Context, uid int, patch *UpdateIssueMe// ListIssueV2 returns the list of issues by find query.func (s *Store) ListIssueV2(ctx context.Context, find *FindIssueMessage) ([]*IssueMessage, error) {orderByClause := "ORDER BY issue.id DESC"- from := "issue"- where, args := []string{"TRUE"}, []any{}+ from := qb.Q().Space("issue")+ where := qb.Q()+if v := find.UID; v != nil {- where, args = append(where, fmt.Sprintf("issue.id = $%d", len(args)+1)), append(args, *v)+ where.And("issue.id = ?", *v)}if v := find.PipelineID; v != nil {- where, args = append(where, fmt.Sprintf("issue.pipeline_id = $%d", len(args)+1)), append(args, *v)+ where.And("issue.pipeline_id = ?", *v)}if v := find.PlanUID; v != nil {- where, args = append(where, fmt.Sprintf("issue.plan_id = $%d", len(args)+1)), append(args, *v)+ where.And("issue.plan_id = ?", *v)}if v := find.ProjectID; v != nil {- where, args = append(where, fmt.Sprintf("issue.project = $%d", len(args)+1)), append(args, *v)+ where.And("issue.project = ?", *v)}if v := find.ProjectIDs; v != nil {- where, args = append(where, fmt.Sprintf("issue.project = ANY($%d)", len(args)+1)), append(args, *v)+ where.And("issue.project = ANY(?)", *v)}if v := find.InstanceResourceID; v != nil {- where, args = append(where, fmt.Sprintf("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.instance = $%d)", len(args)+1)), append(args, *v)+ where.And("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.instance = ?)", *v)}if find.InstanceID != nil && find.DatabaseName != nil {- where, args = append(where, fmt.Sprintf("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.instance = $%d AND task.db_name = $%d)", len(args)+1, len(args)+2)), append(args, *find.InstanceID, *find.DatabaseName)+ where.And("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.instance = ? AND task.db_name = ?)", *find.InstanceID, *find.DatabaseName)}if v := find.CreatorID; v != nil {- where, args = append(where, fmt.Sprintf("issue.creator_id = $%d", len(args)+1)), append(args, *v)+ where.And("issue.creator_id = ?", *v)}if v := find.CreatedAtBefore; v != nil {- where, args = append(where, fmt.Sprintf("issue.created_at < $%d", len(args)+1)), append(args, *v)+ where.And("issue.created_at < ?", *v)}if v := find.CreatedAtAfter; v != nil {- where, args = append(where, fmt.Sprintf("issue.created_at > $%d", len(args)+1)), append(args, *v)+ where.And("issue.created_at > ?", *v)}if v := find.Types; v != nil {typeStrings := make([]string, 0, len(*v))for _, t := range *v {typeStrings = append(typeStrings, t.String())}- where = append(where, fmt.Sprintf("issue.type = ANY($%d)", len(args)+1))- args = append(args, typeStrings)+ where.And("issue.type = ANY(?)", typeStrings)}if v := find.Query; v != nil && *v != "" {if tsQuery := getTSQuery(*v); tsQuery != "" {- from += fmt.Sprintf(` LEFT JOIN CAST($%d AS tsquery) AS query ON TRUE`, len(args)+1)- args = append(args, tsQuery)- where = append(where, "issue.ts_vector @@ query")+ from.Space("LEFT JOIN CAST(? AS tsquery) AS query ON TRUE", tsQuery)+ where.And("issue.ts_vector @@ query")orderByClause = "ORDER BY ts_rank(issue.ts_vector, query) DESC, issue.id DESC"}}if len(find.StatusList) != 0 {- var list []string+ statusStrings := make([]string, 0, len(find.StatusList))for _, status := range find.StatusList {- list = append(list, fmt.Sprintf("$%d", len(args)+1))- args = append(args, status.String())+ statusStrings = append(statusStrings, status.String())}- where = append(where, fmt.Sprintf("issue.status IN (%s)", strings.Join(list, ", ")))+ where.And("issue.status = ANY(?)", statusStrings)}if v := find.TaskTypes; v != nil {taskTypeStrings := make([]string, 0, len(*v))@@ -327,35 +331,80 @@ func (s *Store) ListIssueV2(ctx context.Context, find *FindIssueMessage) ([]*IsstaskTypeStrings = append(taskTypeStrings, t.String())}if find.MigrateTypes != nil && len(*find.MigrateTypes) > 0 {- // Filter by both task type and migrate typemigrateTypeStrings := make([]string, 0, len(*find.MigrateTypes))for _, mt := range *find.MigrateTypes {migrateTypeStrings = append(migrateTypeStrings, mt.String())}- where = append(where, fmt.Sprintf("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.type = ANY($%d) AND task.payload->>'migrateType' = ANY($%d))", len(args)+1, len(args)+2))- args = append(args, taskTypeStrings, migrateTypeStrings)+ where.And("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.type = ANY(?) AND task.payload->>'migrateType' = ANY(?))", taskTypeStrings, migrateTypeStrings)} else {- where = append(where, fmt.Sprintf("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.type = ANY($%d))", len(args)+1))- args = append(args, taskTypeStrings)+ where.And("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.type = ANY(?))", taskTypeStrings)}}if v := find.EnvironmentID; v != nil {- where = append(where, fmt.Sprintf("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.environment = $%d)", len(args)+1))- args = append(args, *v)+ where.And("EXISTS (SELECT 1 FROM task WHERE task.pipeline_id = issue.pipeline_id AND task.environment = ?)", *v)+ }+ if len(find.LabelList) != 0 {+ where.And("payload->'labels' ??& ?::TEXT[]", find.LabelList)}- limitOffsetClause := ""+ if find.NoPipeline {+ where.And("issue.pipeline_id IS NULL")+ }++ q := qb.Q().Space(`+ SELECT+ issue.id,+ issue.creator_id,+ issue.created_at,+ issue.updated_at,+ issue.project,+ issue.pipeline_id,+ issue.plan_id,+ COALESCE(plan.name, issue.name) AS name,+ issue.status,+ issue.type,+ COALESCE(plan.description, issue.description) AS description,+ issue.payload,+ COALESCE(task_run_status_count.status_count, '{}'::jsonb)+ FROM ?+ LEFT JOIN plan ON issue.plan_id = plan.id+ LEFT JOIN LATERAL (+ SELECT+ jsonb_object_agg(t.status, t.count) AS status_count+ FROM (+ SELECT+ t.status,+ count(*) AS count+ FROM (+ SELECT+ CASE COALESCE((task.payload->>'skipped')::BOOLEAN, FALSE)+ WHEN TRUE THEN 'SKIPPED'+ ELSE latest_task_run.status+ END AS status+ FROM task+ LEFT JOIN LATERAL(+ SELECT COALESCE(+ (SELECT task_run.status FROM task_run WHERE task_run.task_id = task.id ORDER BY task_run.id DESC LIMIT 1), 'NOT_STARTED'+ ) AS status+ ) AS latest_task_run ON TRUE+ WHERE task.pipeline_id = issue.pipeline_id+ ) AS t+ GROUP BY t.status+ ) AS t+ ) AS task_run_status_count ON TRUE+ WHERE ?+ `, from, where)+ q.Space(orderByClause)+if v := find.Limit; v != nil {- limitOffsetClause = fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- limitOffsetClause += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)}- if len(find.LabelList) != 0 {- where = append(where, fmt.Sprintf("payload->'labels' ?& $%d::TEXT[]", len(args)+1))- args = append(args, find.LabelList)- }- if find.NoPipeline {- where = append(where, "issue.pipeline_id IS NULL")++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}var issues []*IssueMessage@@ -365,51 +414,6 @@ func (s *Store) ListIssueV2(ctx context.Context, find *FindIssueMessage) ([]*Iss}defer tx.Rollback()- query := fmt.Sprintf(`- SELECT- issue.id,- issue.creator_id,- issue.created_at,- issue.updated_at,- issue.project,- issue.pipeline_id,- issue.plan_id,- COALESCE(plan.name, issue.name) AS name,- issue.status,- issue.type,- COALESCE(plan.description, issue.description) AS description,- issue.payload,- COALESCE(task_run_status_count.status_count, '{}'::jsonb)- FROM %s- LEFT JOIN plan ON issue.plan_id = plan.id- LEFT JOIN LATERAL (- SELECT- jsonb_object_agg(t.status, t.count) AS status_count- FROM (- SELECT- t.status,- count(*) AS count- FROM (- SELECT- CASE COALESCE((task.payload->>'skipped')::BOOLEAN, FALSE)- WHEN TRUE THEN 'SKIPPED'- ELSE latest_task_run.status- END AS status- FROM task- LEFT JOIN LATERAL(- SELECT COALESCE(- (SELECT task_run.status FROM task_run WHERE task_run.task_id = task.id ORDER BY task_run.id DESC LIMIT 1), 'NOT_STARTED'- ) AS status- ) AS latest_task_run ON TRUE- WHERE task.pipeline_id = issue.pipeline_id- ) AS t- GROUP BY t.status- ) AS t- ) AS task_run_status_count ON TRUE- WHERE %s- %s- %s`, from, strings.Join(where, " AND "), orderByClause, limitOffsetClause)-rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return nil, err@@ -490,16 +494,12 @@ func (s *Store) ListIssueV2(ctx context.Context, find *FindIssueMessage) ([]*Iss// BatchUpdateIssueStatuses updates the status of multiple issues.func (s *Store) BatchUpdateIssueStatuses(ctx context.Context, issueUIDs []int, status storepb.Issue_Status) error {- var ids []string- for _, id := range issueUIDs {- ids = append(ids, fmt.Sprintf("%d", id))+ q := qb.Q().Space("UPDATE issue SET status = ? WHERE id = ANY(?) RETURNING id, pipeline_id", status.String(), issueUIDs)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")}- query := fmt.Sprintf(`- UPDATE issue- SET status = $1- WHERE id IN (%s)- RETURNING id, pipeline_id;- `, strings.Join(ids, ","))tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -507,7 +507,7 @@ func (s *Store) BatchUpdateIssueStatuses(ctx context.Context, issueUIDs []int, s}defer tx.Rollback()- rows, err := tx.QueryContext(ctx, query, status.String())+ rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return errors.Wrapf(err, "failed to query")}@@ -580,23 +580,6 @@ func getTSQuery(text string) string {func (s *Store) BackfillIssueTSVector(ctx context.Context) error {chunkSize := 50offset := 0- selectQuery := `- SELECT- issue.id,- COALESCE(plan.name, issue.name) AS name,- COALESCE(plan.description, issue.description) AS description- FROM issue- LEFT JOIN plan ON issue.plan_id = plan.id- WHERE issue.ts_vector IS NULL- ORDER BY issue.id- LIMIT $1- OFFSET $2- `- updateStatement := `- UPDATE issue- SET ts_vector = $1- WHERE id = $2- `tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -605,9 +588,26 @@ func (s *Store) BackfillIssueTSVector(ctx context.Context) error {defer tx.Rollback()for {+ selectQuery := qb.Q().Space(`+ SELECT+ issue.id,+ COALESCE(plan.name, issue.name) AS name,+ COALESCE(plan.description, issue.description) AS description+ FROM issue+ LEFT JOIN plan ON issue.plan_id = plan.id+ WHERE issue.ts_vector IS NULL+ ORDER BY issue.id+ LIMIT ?+ OFFSET ?`, chunkSize, offset)++ selectSQL, selectArgs, err := selectQuery.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build select sql")+ }+var issues []*IssueMessageif err := func() error {- rows, err := tx.QueryContext(ctx, selectQuery, chunkSize, offset)+ rows, err := tx.QueryContext(ctx, selectSQL, selectArgs...)if err != nil {return errors.Wrapf(err, "failed to query")}@@ -634,7 +634,12 @@ func (s *Store) BackfillIssueTSVector(ctx context.Context) error {for _, issue := range issues {tsVector := getTSVector(fmt.Sprintf("%s %s", issue.Title, issue.Description))- if _, err := tx.ExecContext(ctx, updateStatement, tsVector, issue.UID); err != nil {+ updateQuery := qb.Q().Space("UPDATE issue SET ts_vector = ? WHERE id = ?", tsVector, issue.UID)+ updateSQL, updateArgs, err := updateQuery.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build update sql")+ }+ if _, err := tx.ExecContext(ctx, updateSQL, updateArgs...); err != nil {return errors.Wrapf(err, "failed to update")}}diff --git a/backend/store/issue_comment.go b/backend/store/issue_comment.goindex 38fbb168f6..27c07a6b6d 100644--- a/backend/store/issue_comment.go+++ b/backend/store/issue_comment.go@@ -2,14 +2,13 @@ package storeimport ("context"- "fmt"- "strings""time""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -53,39 +52,40 @@ func (s *Store) GetIssueComment(ctx context.Context, find *FindIssueCommentMessa}func (s *Store) ListIssueComment(ctx context.Context, find *FindIssueCommentMessage) ([]*IssueCommentMessage, error) {- where := []string{"TRUE"}- args := []any{}+ q := qb.Q().Space(`+ SELECT+ id,+ creator_id,+ created_at,+ updated_at,+ issue_id,+ payload+ FROM+ issue_comment+ WHERE TRUE+ `)+if v := find.UID; v != nil {- where = append(where, fmt.Sprintf("id = $%d", len(args)+1))- args = append(args, *v)+ q.And("id = ?", *v)}if v := find.IssueUID; v != nil {- where = append(where, fmt.Sprintf("issue_id = $%d", len(args)+1))- args = append(args, *v)+ q.And("issue_id = ?", *v)}- limitOffsetClause := ""+ q.Space("ORDER BY id ASC")if v := find.Limit; v != nil {- limitOffsetClause = fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- limitOffsetClause += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)}- rows, err := s.GetDB().QueryContext(ctx, fmt.Sprintf(`- SELECT- id,- creator_id,- created_at,- updated_at,- issue_id,- payload- FROM- issue_comment- WHERE %s- ORDER BY id ASC- %s- `, strings.Join(where, " AND "), limitOffsetClause), args...)+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := s.GetDB().QueryContext(ctx, query, args...)if err != nil {return nil, errors.Wrapf(err, "failed to query context")}@@ -147,24 +147,29 @@ func (s *Store) CreateIssueCommentTaskUpdateStatus(ctx context.Context, issueUID}func (s *Store) CreateIssueComment(ctx context.Context, create *IssueCommentMessage, creatorUID int) (*IssueCommentMessage, error) {- query := `+ payload, err := protojson.Marshal(create.Payload)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to marshal payload")+ }++ q := qb.Q().Space(`INSERT INTO issue_comment (creator_id,issue_id,payload) VALUES (- $1,- $2,- $3+ ?,+ ?,+ ?) RETURNING id, created_at, updated_at- `+ `, creatorUID, create.IssueUID, payload)- payload, err := protojson.Marshal(create.Payload)+ query, args, err := q.ToSQL()if err != nil {- return nil, errors.Wrapf(err, "failed to marshal payload")+ return nil, errors.Wrapf(err, "failed to build sql")}- if err := s.GetDB().QueryRowContext(ctx, query, creatorUID, create.IssueUID, payload).Scan(&create.UID, &create.CreatedAt, &create.UpdatedAt); err != nil {+ if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&create.UID, &create.CreatedAt, &create.UpdatedAt); err != nil {return nil, errors.Wrapf(err, "failed to insert")}@@ -179,13 +184,19 @@ func (s *Store) CreateIssueComment(ctx context.Context, create *IssueCommentMess}func (s *Store) UpdateIssueComment(ctx context.Context, patch *UpdateIssueCommentMessage) error {- set, args := []string{"updated_at = $1"}, []any{time.Now()}+ q := qb.Q().Space("UPDATE issue_comment SET updated_at = ?", time.Now())if v := patch.Comment; v != nil {- set, args = append(set, fmt.Sprintf("payload = payload || jsonb_build_object('comment',$%d::TEXT)", len(args)+1)), append(args, *v)+ q.Join(", ", "payload = payload || jsonb_build_object('comment',?::TEXT)", *v)+ }++ q.Space("WHERE id = ?", patch.UID)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")}- args = append(args, patch.UID)- query := `UPDATE issue_comment SET ` + strings.Join(set, ", ") + fmt.Sprintf(` WHERE id = $%d`, len(args))+if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to update issue comment")}diff --git a/backend/store/pipeline.go b/backend/store/pipeline.goindex 1a20fcdf6c..d1809d6869 100644--- a/backend/store/pipeline.go+++ b/backend/store/pipeline.go@@ -3,13 +3,12 @@ package storeimport ("context""database/sql"- "fmt"- "strings""time""github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb")// PipelineMessage is the message for pipelines.@@ -125,20 +124,31 @@ func (s *Store) CreatePipelineAIO(ctx context.Context, planUID int64, pipeline *// returns func() to invalidate cache.func (s *Store) updatePipelineUIDOfIssueAndPlan(ctx context.Context, txn *sql.Tx, planUID int64, pipelineUID int) (func(), error) {- if _, err := txn.ExecContext(ctx, `+ q := qb.Q().Space(`UPDATE plan- SET pipeline_id = $1- WHERE id = $2- `, pipelineUID, planUID); err != nil {+ SET pipeline_id = ?+ WHERE id = ?+ `, pipelineUID, planUID)+ querySQL, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+ if _, err := txn.ExecContext(ctx, querySQL, args...); err != nil {return nil, errors.Wrapf(err, "failed to update plan pipeline_id")}- var issueUID int- if err := txn.QueryRowContext(ctx, `++ q = qb.Q().Space(`UPDATE issue- SET pipeline_id = $1- WHERE plan_id = $2+ SET pipeline_id = ?+ WHERE plan_id = ?RETURNING id- `, pipelineUID, planUID).Scan(&issueUID); err != nil {+ `, pipelineUID, planUID)+ querySQL, args, err = q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+ var issueUID int+ if err := txn.QueryRowContext(ctx, querySQL, args...).Scan(&issueUID); err != nil {if err != sql.ErrNoRows {return nil, errors.Wrapf(err, "failed to update issue pipeline_id")}@@ -152,11 +162,13 @@ func (s *Store) updatePipelineUIDOfIssueAndPlan(ctx context.Context, txn *sql.Tx}func lockPlanAndGetPipelineUID(ctx context.Context, txn *sql.Tx, planUID int64) (*int, error) {- query := `- SELECT pipeline_id FROM plan WHERE id = $1 FOR UPDATE- `+ q := qb.Q().Space("SELECT pipeline_id FROM plan WHERE id = ? FOR UPDATE", planUID)+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }var uid sql.NullInt32- if err := txn.QueryRowContext(ctx, query, planUID).Scan(&uid); err != nil {+ if err := txn.QueryRowContext(ctx, query, args...).Scan(&uid); err != nil {if err == sql.ErrNoRows {return nil, errors.Errorf("plan %d not found", planUID)}@@ -171,25 +183,26 @@ func lockPlanAndGetPipelineUID(ctx context.Context, txn *sql.Tx, planUID int64)}func (*Store) createPipeline(ctx context.Context, txn *sql.Tx, create *PipelineMessage, creatorUID int) (*PipelineMessage, error) {- query := `+ q := qb.Q().Space(`INSERT INTO pipeline (project,creator_id)VALUES (- $1,- $2+ ?,+ ?)RETURNING id, created_at- `+ `, create.ProjectID, creatorUID)+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }pipeline := &PipelineMessage{ProjectID: create.ProjectID,CreatorUID: creatorUID,}- if err := txn.QueryRowContext(ctx, query,- create.ProjectID,- creatorUID,- ).Scan(+ if err := txn.QueryRowContext(ctx, query, args...).Scan(&pipeline.ID,&pipeline.CreatedAt,); err != nil {@@ -225,18 +238,7 @@ func (s *Store) GetPipelineV2ByID(ctx context.Context, id int) (*PipelineMessage// ListPipelineV2 lists pipelines.func (s *Store) ListPipelineV2(ctx context.Context, find *PipelineFind) ([]*PipelineMessage, error) {- where, args := []string{"TRUE"}, []any{}- if filter := find.Filter; filter != nil {- where = append(where, filter.Where)- args = append(args, filter.Args...)- }- if v := find.ID; v != nil {- where, args = append(where, fmt.Sprintf("pipeline.id = $%d", len(args)+1)), append(args, *v)- }- if v := find.ProjectID; v != nil {- where, args = append(where, fmt.Sprintf("pipeline.project = $%d", len(args)+1)), append(args, *v)- }- query := fmt.Sprintf(`+ q := qb.Q().Space(`SELECTpipeline.id,pipeline.creator_id,@@ -254,13 +256,30 @@ func (s *Store) ListPipelineV2(ctx context.Context, find *PipelineFind) ([]*Pipe) AS updated_atFROM pipelineLEFT JOIN issue ON pipeline.id = issue.pipeline_id- WHERE %s- ORDER BY pipeline.id DESC`, strings.Join(where, " AND "))+ WHERE TRUE+ `)++ if filter := find.Filter; filter != nil {+ q.And(ConvertDollarPlaceholders(filter.Where), filter.Args...)+ }+ if v := find.ID; v != nil {+ q.And("pipeline.id = ?", *v)+ }+ if v := find.ProjectID; v != nil {+ q.And("pipeline.project = ?", *v)+ }++ q.Space("ORDER BY pipeline.id DESC")if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})diff --git a/backend/store/plan.go b/backend/store/plan.goindex 0ecb84d4ff..9845adb78c 100644--- a/backend/store/plan.go+++ b/backend/store/plan.go@@ -13,6 +13,7 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -56,7 +57,12 @@ type UpdatePlanMessage struct {// CreatePlan creates a new plan.func (s *Store) CreatePlan(ctx context.Context, plan *PlanMessage, creatorUID int) (*PlanMessage, error) {- query := `+ config, err := protojson.Marshal(plan.Config)+ if err != nil {+ return nil, errors.Wrap(err, "failed to marshal plan config")+ }++ q := qb.Q().Space(`INSERT INTO plan (creator_id,project,@@ -65,19 +71,15 @@ func (s *Store) CreatePlan(ctx context.Context, plan *PlanMessage, creatorUID indescription,config) VALUES (- $1,- $2,- $3,- $4,- $5,- $6+ ?, ?, ?, ?, ?, ?) RETURNING id, created_at, updated_at- `+ `, creatorUID, plan.ProjectID, plan.PipelineUID, plan.Name, plan.Description, config)- config, err := protojson.Marshal(plan.Config)+ query, args, err := q.ToSQL()if err != nil {- return nil, errors.Wrap(err, "failed to marshal plan config")+ return nil, errors.Wrap(err, "failed to build sql")}+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, errors.Wrap(err, "failed to begin transaction")@@ -85,14 +87,7 @@ func (s *Store) CreatePlan(ctx context.Context, plan *PlanMessage, creatorUID indefer tx.Rollback()var id int64- if err := tx.QueryRowContext(ctx, query,- creatorUID,- plan.ProjectID,- plan.PipelineUID,- plan.Name,- plan.Description,- config,- ).Scan(&id, &plan.CreatedAt, &plan.UpdatedAt); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&id, &plan.CreatedAt, &plan.UpdatedAt); err != nil {return nil, errors.Wrap(err, "failed to insert plan")}@@ -130,50 +125,55 @@ func (s *Store) GetPlan(ctx context.Context, find *FindPlanMessage) (*PlanMessag// ListPlans retrieves a list of plans.func (s *Store) ListPlans(ctx context.Context, find *FindPlanMessage) ([]*PlanMessage, error) {- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().Space(`+ SELECT+ plan.id,+ plan.creator_id,+ plan.created_at,+ plan.updated_at,+ plan.project,+ plan.pipeline_id,+ plan.name,+ plan.description,+ plan.config,+ plan.deleted+ FROM plan+ LEFT JOIN issue on plan.id = issue.plan_id+ WHERE TRUE+ `)+if filter := find.Filter; filter != nil {- where = append(where, filter.Where)- args = append(args, filter.Args...)+ // Convert $1, $2, etc. to ? for qb+ q.And(ConvertDollarPlaceholders(filter.Where), filter.Args...)}if v := find.UID; v != nil {- where, args = append(where, fmt.Sprintf("plan.id = $%d", len(args)+1)), append(args, *v)+ q.And("plan.id = ?", *v)}if v := find.ProjectID; v != nil {- where, args = append(where, fmt.Sprintf("plan.project = $%d", len(args)+1)), append(args, *v)+ q.And("plan.project = ?", *v)}if v := find.ProjectIDs; v != nil {if len(*v) == 0 {- where = append(where, "FALSE")+ q.And("FALSE")} else {- where, args = append(where, fmt.Sprintf("plan.project = ANY($%d)", len(args)+1)), append(args, *v)+ q.And("plan.project = ANY(?)", *v)}}if v := find.PipelineID; v != nil {- where, args = append(where, fmt.Sprintf("plan.pipeline_id = $%d", len(args)+1)), append(args, *v)+ q.And("plan.pipeline_id = ?", *v)}- query := fmt.Sprintf(`- SELECT- plan.id,- plan.creator_id,- plan.created_at,- plan.updated_at,- plan.project,- plan.pipeline_id,- plan.name,- plan.description,- plan.config,- plan.deleted- FROM plan- LEFT JOIN issue on plan.id = issue.plan_id- WHERE %s- ORDER BY id DESC- `, strings.Join(where, " AND "))+ q.Space("ORDER BY id DESC")if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrap(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})@@ -226,47 +226,52 @@ func (s *Store) ListPlans(ctx context.Context, find *FindPlanMessage) ([]*PlanMe// UpdatePlan updates an existing plan.func (s *Store) UpdatePlan(ctx context.Context, patch *UpdatePlanMessage) error {- set, args := []string{"updated_at = $1"}, []any{time.Now()}+ set := []string{"updated_at = ?"}+ args := []any{time.Now()}+if v := patch.Name; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set = append(set, "name = ?")+ args = append(args, *v)}if v := patch.Description; v != nil {- set, args = append(set, fmt.Sprintf("description = $%d", len(args)+1)), append(args, *v)+ set = append(set, "description = ?")+ args = append(args, *v)}if v := patch.Deleted; v != nil {- set, args = append(set, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, *v)+ set = append(set, "deleted = ?")+ args = append(args, *v)}- {- var payloadSets []string- if v := patch.Specs; v != nil {- config, err := protojson.Marshal(&storepb.PlanConfig{- Specs: *v,- })- if err != nil {- return errors.Wrapf(err, "failed to marshal plan config")- }- payloadSets = append(payloadSets, fmt.Sprintf("jsonb_build_object('specs', ($%d)::JSONB->'specs')", len(args)+1))- args = append(args, config)- }- if v := patch.Deployment; v != nil {- p, err := protojson.Marshal(*v)- if err != nil {- return errors.Wrapf(err, "failed to marshal deployment")- }- payloadSets = append(payloadSets, fmt.Sprintf("jsonb_build_object('deployment', ($%d)::JSONB)", len(args)+1))- args = append(args, p)++ var payloadSets []string+ if v := patch.Specs; v != nil {+ config, err := protojson.Marshal(&storepb.PlanConfig{+ Specs: *v,+ })+ if err != nil {+ return errors.Wrapf(err, "failed to marshal plan config")}- if len(payloadSets) > 0 {- set = append(set, fmt.Sprintf("config = config || %s", strings.Join(payloadSets, " || ")))+ payloadSets = append(payloadSets, "jsonb_build_object('specs', (?)::JSONB->'specs')")+ args = append(args, config)+ }+ if v := patch.Deployment; v != nil {+ p, err := protojson.Marshal(*v)+ if err != nil {+ return errors.Wrapf(err, "failed to marshal deployment")}+ payloadSets = append(payloadSets, "jsonb_build_object('deployment', (?)::JSONB)")+ args = append(args, p)+ }+ if len(payloadSets) > 0 {+ set = append(set, fmt.Sprintf("config = config || %s", strings.Join(payloadSets, " || ")))}args = append(args, patch.UID)- query := fmt.Sprintf(`- UPDATE plan- SET `+strings.Join(set, ", ")+`- WHERE id = $%d- `, len(args))+ q := qb.Q().Space(fmt.Sprintf("UPDATE plan SET %s WHERE id = ?", strings.Join(set, ", ")), args...)++ query, finalArgs, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -274,7 +279,7 @@ func (s *Store) UpdatePlan(ctx context.Context, patch *UpdatePlanMessage) error}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, query, args...); err != nil {+ if _, err := tx.ExecContext(ctx, query, finalArgs...); err != nil {return errors.Wrapf(err, "failed to update plan")}diff --git a/backend/store/plan_check_run.go b/backend/store/plan_check_run.goindex 1439510d26..2d3f98bcba 100644--- a/backend/store/plan_check_run.go+++ b/backend/store/plan_check_run.go@@ -2,14 +2,13 @@ package storeimport ("context"- "fmt"- "strings""time""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -74,18 +73,24 @@ func (s *Store) CreatePlanCheckRuns(ctx context.Context, plan *PlanMessage, creareturn nil}- var query strings.Builder- var values []any- if _, err := query.WriteString(`INSERT INTO plan_check_run (- plan_id,- status,- type,- config,- result- ) VALUES- `); err != nil {+ txn, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {return err}+ defer txn.Rollback()++ // Delete existing plan check runs+ q := qb.Q().Space("DELETE FROM plan_check_run WHERE plan_id = ?", plan.UID)+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build delete sql")+ }+ if _, err := txn.ExecContext(ctx, query, args...); err != nil {+ return errors.Wrapf(err, "failed to delete plan check run for plan %d", plan.UID)+ }++ // Insert new plan check runs+ q = qb.Q().Space("INSERT INTO plan_check_run (plan_id, status, type, config, result) VALUES")for i, create := range creates {config, err := protojson.Marshal(create.Config)if err != nil {@@ -95,32 +100,16 @@ func (s *Store) CreatePlanCheckRuns(ctx context.Context, plan *PlanMessage, creaif err != nil {return errors.Wrapf(err, "failed to marshal create result %v", create.Result)}- values = append(values,- create.PlanUID,- create.Status,- create.Type,- config,- result,- )- if i != 0 {- if _, err := query.WriteString(","); err != nil {- return err- }- }- count := 5- if _, err := query.WriteString(getPlaceholders(i*count+1, count)); err != nil {- return err+ if i > 0 {+ q.Space(",")}+ q.Space("(?, ?, ?, ?, ?)", create.PlanUID, create.Status, create.Type, config, result)}- txn, err := s.GetDB().BeginTx(ctx, nil)+ query, args, err = q.ToSQL()if err != nil {- return err+ return errors.Wrapf(err, "failed to build insert sql")}- defer txn.Rollback()- if _, err := txn.ExecContext(ctx, "DELETE FROM plan_check_run WHERE plan_id = $1", plan.UID); err != nil {- return errors.Wrapf(err, "failed to delete plan check run for plan %d", plan.UID)- }- if _, err := txn.ExecContext(ctx, query.String(), values...); err != nil {+ if _, err := txn.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to execute insert")}return txn.Commit()@@ -128,43 +117,42 @@ func (s *Store) CreatePlanCheckRuns(ctx context.Context, plan *PlanMessage, crea// ListPlanCheckRuns returns a list of plan check runs based on find.func (s *Store) ListPlanCheckRuns(ctx context.Context, find *FindPlanCheckRunMessage) ([]*PlanCheckRunMessage, error) {- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().Space(`+SELECT+ plan_check_run.id,+ plan_check_run.created_at,+ plan_check_run.updated_at,+ plan_check_run.plan_id,+ plan_check_run.status,+ plan_check_run.type,+ plan_check_run.config,+ plan_check_run.result+FROM plan_check_run+WHERE TRUE`)if v := find.PlanUID; v != nil {- where = append(where, fmt.Sprintf("plan_check_run.plan_id = $%d", len(args)+1))- args = append(args, *v)+ q.Space("AND plan_check_run.plan_id = ?", *v)}if v := find.UIDs; v != nil {- where, args = append(where, fmt.Sprintf("plan_check_run.id = ANY($%d)", len(args)+1)), append(args, *v)+ q.Space("AND plan_check_run.id = ANY(?)", *v)}if v := find.Status; v != nil {- where = append(where, fmt.Sprintf("plan_check_run.status = ANY($%d)", len(args)+1))- args = append(args, *v)+ q.Space("AND plan_check_run.status = ANY(?)", *v)}if v := find.Type; v != nil {- where = append(where, fmt.Sprintf("plan_check_run.type = ANY($%d)", len(args)+1))- args = append(args, *v)+ q.Space("AND plan_check_run.type = ANY(?)", *v)}if v := find.ResultStatus; v != nil {statusStrings := make([]string, len(*v))for i, status := range *v {statusStrings[i] = status.String()}- where = append(where, fmt.Sprintf("EXISTS (SELECT 1 FROM jsonb_array_elements(plan_check_run.result->'results') AS elem WHERE elem->>'status' = ANY($%d))", len(args)+1))- args = append(args, statusStrings)+ q.Space("AND EXISTS (SELECT 1 FROM jsonb_array_elements(plan_check_run.result->'results') AS elem WHERE elem->>'status' = ANY(?))", statusStrings)+ }+ q.Space("ORDER BY plan_check_run.id ASC")+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}- query := fmt.Sprintf(`-SELECT- plan_check_run.id,- plan_check_run.created_at,- plan_check_run.updated_at,- plan_check_run.plan_id,- plan_check_run.status,- plan_check_run.type,- plan_check_run.config,- plan_check_run.result-FROM plan_check_run-WHERE %s-ORDER BY plan_check_run.id ASC`, strings.Join(where, " AND "))rows, err := s.GetDB().QueryContext(ctx, query, args...)if err != nil {return nil, err@@ -212,14 +200,18 @@ func (s *Store) UpdatePlanCheckRun(ctx context.Context, status PlanCheckRunStatuif err != nil {return errors.Wrapf(err, "failed to marshal result %v", result)}- query := `+ q := qb.Q().Space(`UPDATE plan_check_runSET- updated_at = $1,- status = $2,- result = $3- WHERE id = $4`- if _, err := s.GetDB().ExecContext(ctx, query, time.Now(), status, resultBytes, uid); err != nil {+ updated_at = ?,+ status = ?,+ result = ?+ WHERE id = ?`, time.Now(), status, resultBytes, uid)+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to update plan check run")}return nil@@ -227,13 +219,17 @@ func (s *Store) UpdatePlanCheckRun(ctx context.Context, status PlanCheckRunStatu// BatchCancelPlanCheckRuns updates the status of planCheckRuns to CANCELED.func (s *Store) BatchCancelPlanCheckRuns(ctx context.Context, planCheckRunUIDs []int) error {- query := `+ q := qb.Q().Space(`UPDATE plan_check_run- SET- status = $1,- updated_at = $2- WHERE id = ANY($3)`- if _, err := s.GetDB().ExecContext(ctx, query, PlanCheckRunStatusCanceled, time.Now(), planCheckRunUIDs); err != nil {+ SET+ status = ?,+ updated_at = ?+ WHERE id = ANY(?)`, PlanCheckRunStatusCanceled, time.Now(), planCheckRunUIDs)+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {return err}return nildiff --git a/backend/store/policy.go b/backend/store/policy.goindex 921c8fed40..11a1c937af 100644--- a/backend/store/policy.go+++ b/backend/store/policy.go@@ -6,7 +6,6 @@ import ("fmt""log/slog""slices"- "strings""time""github.com/pkg/errors"@@ -15,6 +14,7 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -439,17 +439,22 @@ func (s *Store) CreatePolicyV2(ctx context.Context, create *PolicyMessage) (*Pol// UpdatePolicyV2 updates the policy.func (s *Store) UpdatePolicyV2(ctx context.Context, patch *UpdatePolicyMessage) (*PolicyMessage, error) {- set, args := []string{"updated_at = $1"}, []any{time.Now()}+ set := qb.Q()+ set.Comma("updated_at = ?", time.Now())if v := patch.InheritFromParent; v != nil {- set, args = append(set, fmt.Sprintf("inherit_from_parent = $%d", len(args)+1)), append(args, *v)+ set.Comma("inherit_from_parent = ?", *v)}if v := patch.Payload; v != nil {- set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, *v)+ set.Comma("payload = ?", *v)}if v := patch.Enforce; v != nil {- set, args = append(set, fmt.Sprintf(`enforce = $%d`, len(args)+1)), append(args, *v)+ set.Comma("enforce = ?", *v)+ }++ query, args, err := qb.Q().Space("UPDATE policy SET ? WHERE resource_type = ? AND resource = ? AND type = ? RETURNING payload, inherit_from_parent, enforce, updated_at", set, patch.ResourceType, patch.Resource, patch.Type.String()).ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}- args = append(args, patch.ResourceType, patch.Resource, patch.Type.String())tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -463,18 +468,7 @@ func (s *Store) UpdatePolicyV2(ctx context.Context, patch *UpdatePolicyMessage)Type: patch.Type,}- if err := tx.QueryRowContext(ctx, fmt.Sprintf(`- UPDATE policy- SET `+strings.Join(set, ", ")+`- WHERE resource_type = $%d AND resource = $%d AND type =$%d- RETURNING- payload,- inherit_from_parent,- enforce,- updated_at- `, len(args)-2, len(args)-1, len(args)),- args...,- ).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&policy.Payload,&policy.InheritFromParent,&policy.Enforce,@@ -497,18 +491,24 @@ func (s *Store) UpdatePolicyV2(ctx context.Context, patch *UpdatePolicyMessage)// DeletePolicyV2 deletes the policy.func (s *Store) DeletePolicyV2(ctx context.Context, policy *PolicyMessage) error {+ q := qb.Q().Space("DELETE FROM policy WHERE resource_type = ? AND resource = ? AND type = ?",+ policy.ResourceType,+ policy.Resource,+ policy.Type.String(),+ )++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return err}defer tx.Rollback()- if _, err := tx.ExecContext(ctx,- `DELETE FROM policy WHERE resource_type = $1 AND resource = $2 AND type = $3`,- policy.ResourceType,- policy.Resource,- policy.Type.String(),- ); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}@@ -522,7 +522,8 @@ func (s *Store) DeletePolicyV2(ctx context.Context, policy *PolicyMessage) errorfunc upsertPolicyV2Impl(ctx context.Context, txn *sql.Tx, create *PolicyMessage) (*PolicyMessage, error) {create.UpdatedAt = time.Now()- if _, err := txn.ExecContext(ctx, `++ q := qb.Q().Space(`INSERT INTO policy (resource_type,resource,@@ -532,13 +533,13 @@ func upsertPolicyV2Impl(ctx context.Context, txn *sql.Tx, create *PolicyMessage)enforce,updated_at)- VALUES ($1, $2, $3, $4, $5, $6, $7)+ VALUES (?, ?, ?, ?, ?, ?, ?)ON CONFLICT(resource_type, resource, type) DO UPDATE SETinherit_from_parent = EXCLUDED.inherit_from_parent,payload = EXCLUDED.payload,enforce = EXCLUDED.enforce,updated_at = EXCLUDED.updated_at- `,+ `,create.ResourceType.String(),create.Resource,create.InheritFromParent,@@ -546,28 +547,21 @@ func upsertPolicyV2Impl(ctx context.Context, txn *sql.Tx, create *PolicyMessage)create.Payload,create.Enforce,create.UpdatedAt,- ); err != nil {+ )++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ if _, err := txn.ExecContext(ctx, query, args...); err != nil {return nil, err}return create, nil}func (*Store) listPolicyImplV2(ctx context.Context, txn *sql.Tx, find *FindPolicyMessage) ([]*PolicyMessage, error) {- where, args := []string{"TRUE"}, []any{}- if v := find.ResourceType; v != nil {- where, args = append(where, fmt.Sprintf("resource_type = $%d", len(args)+1)), append(args, v.String())- }- if v := find.Resource; v != nil {- where, args = append(where, fmt.Sprintf("resource = $%d", len(args)+1)), append(args, *v)- }- if v := find.Type; v != nil {- where, args = append(where, fmt.Sprintf("type = $%d", len(args)+1)), append(args, v.String())- }- if !find.ShowAll {- where, args = append(where, fmt.Sprintf("enforce = $%d", len(args)+1)), append(args, true)- }-- rows, err := txn.QueryContext(ctx, `+ q := qb.Q().Space(`SELECTupdated_at,resource_type,@@ -577,9 +571,28 @@ func (*Store) listPolicyImplV2(ctx context.Context, txn *sql.Tx, find *FindPolicpayload,enforceFROM policy- WHERE `+strings.Join(where, " AND "),- args...,- )+ WHERE TRUE+ `)++ if v := find.ResourceType; v != nil {+ q.And("resource_type = ?", v.String())+ }+ if v := find.Resource; v != nil {+ q.And("resource = ?", *v)+ }+ if v := find.Type; v != nil {+ q.And("type = ?", v.String())+ }+ if !find.ShowAll {+ q.And("enforce = ?", true)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := txn.QueryContext(ctx, query, args...)if err != nil {return nil, err}diff --git a/backend/store/principal.go b/backend/store/principal.goindex 24be20dd74..5cfc8ff23a 100644--- a/backend/store/principal.go+++ b/backend/store/principal.go@@ -3,7 +3,6 @@ package storeimport ("context""database/sql"- "fmt""log/slog""strings""time"@@ -15,6 +14,7 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -115,13 +115,20 @@ func (s *Store) GetUserByEmail(ctx context.Context, email string) (*UserMessage,}func (s *Store) StatUsers(ctx context.Context) ([]*UserStat, error) {- rows, err := s.GetDB().QueryContext(ctx, `- SELECT- COUNT(*),- type,- deleted- FROM principal- GROUP BY type, deleted`)+ q := qb.Q().Space(`+ SELECT+ COUNT(*),+ type,+ deleted+ FROM principal+ GROUP BY type, deleted+ `)+ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := s.GetDB().QueryContext(ctx, sql, args...)if err != nil {return nil, err}@@ -202,51 +209,30 @@ func (s *Store) listAndCacheAllUsers(ctx context.Context) error {}func listUserImpl(ctx context.Context, txn *sql.Tx, find *FindUserMessage) ([]*UserMessage, error) {- where, args := []string{"TRUE"}, []any{}- if filter := find.Filter; filter != nil {- where = append(where, filter.Where)- args = append(args, filter.Args...)- }- if v := find.ID; v != nil {- where, args = append(where, fmt.Sprintf("principal.id = $%d", len(args)+1)), append(args, *v)- }- if v := find.Email; v != nil {- if *v == common.AllUsers {- where, args = append(where, fmt.Sprintf("principal.email = $%d", len(args)+1)), append(args, *v)- } else {- where, args = append(where, fmt.Sprintf("principal.email = $%d", len(args)+1)), append(args, strings.ToLower(*v))- }- }- if v := find.Type; v != nil {- where, args = append(where, fmt.Sprintf("principal.type = $%d", len(args)+1)), append(args, v.String())- }- if !find.ShowDeleted {- where, args = append(where, fmt.Sprintf("principal.deleted = $%d", len(args)+1)), append(args, false)- }+ with := qb.Q()+ from := qb.Q().Space("principal INNER JOIN user_groups ON principal.id = user_groups.user_id")+ where := qb.Q().Space("TRUE")- var with, join string+ // Build CTE for project filtering if neededif v := find.ProjectID; v != nil {- with = `WITH all_members AS (+ with.Space(`WITH all_members AS (SELECTjsonb_array_elements_text(jsonb_array_elements(policy.payload->'bindings')->'members') AS member,jsonb_array_elements(policy.payload->'bindings')->>'role' AS roleFROM policy- WHERE ((resource_type = '` + storepb.Policy_PROJECT.String() + `' AND resource = 'projects/` + *v + `') OR resource_type = '` + storepb.Policy_WORKSPACE.String() + `') AND type = '` + storepb.Policy_IAM.String() + `'+ WHERE ((resource_type = ? AND resource = ?) OR resource_type = ?) AND type = ?),project_members AS (SELECT ARRAY_AGG(member) AS members FROM all_members WHERE role NOT LIKE 'roles/workspace%'- )`- join = `INNER JOIN project_members ON (CONCAT('users/', principal.id) = ANY(project_members.members) OR '` + common.AllUsers + `' = ANY(project_members.members))`+ ),`, storepb.Policy_PROJECT.String(), "projects/"+*v, storepb.Policy_WORKSPACE.String(), storepb.Policy_IAM.String())+ from.Space(`INNER JOIN project_members ON (CONCAT('users/', principal.id) = ANY(project_members.members) OR ? = ANY(project_members.members))`, common.AllUsers)+ } else {+ with.Space(`WITH`)}// Join the user_group table to find groups for each user.// The user will be stored in the user_group.payload.members.member field, the member is in the "users/{id}" format- if strings.HasPrefix(with, "WITH") {- with += ","- } else {- with = "WITH"- }- query := with + ` user_groups AS (+ with.Space(`user_groups AS (SELECTprincipal.id AS user_id,COALESCE(ARRAY_AGG(user_group.email ORDER BY user_group.email) FILTER (WHERE user_group.email IS NOT NULL), '{}') AS groups@@ -256,32 +242,61 @@ func listUserImpl(ctx context.Context, txn *sql.Tx, find *FindUserMessage) ([]*UWHERE m->>'member' = CONCAT('users/', principal.id))GROUP BY principal.id- )- SELECT- principal.id AS user_id,- principal.deleted,- principal.email,- principal.name,- principal.type,- principal.password_hash,- principal.mfa_config,- principal.phone,- principal.profile,- principal.created_at,- user_groups.groups- FROM principal- INNER JOIN user_groups ON principal.id = user_groups.user_id- ` + join + ` WHERE ` + strings.Join(where, " AND ") + ` ORDER BY type DESC, created_at ASC`+ )`)++ if filter := find.Filter; filter != nil {+ where.And(ConvertDollarPlaceholders(filter.Where), filter.Args...)+ }+ if v := find.ID; v != nil {+ where.And("principal.id = ?", *v)+ }+ if v := find.Email; v != nil {+ if *v == common.AllUsers {+ where.And("principal.email = ?", *v)+ } else {+ where.And("principal.email = ?", strings.ToLower(*v))+ }+ }+ if v := find.Type; v != nil {+ where.And("principal.type = ?", v.String())+ }+ if !find.ShowDeleted {+ where.And("principal.deleted = ?", false)+ }++ q := qb.Q().Space("?", with)+ q.Space(`+ SELECT+ principal.id AS user_id,+ principal.deleted,+ principal.email,+ principal.name,+ principal.type,+ principal.password_hash,+ principal.mfa_config,+ principal.phone,+ principal.profile,+ principal.created_at,+ user_groups.groups+ FROM ?+ WHERE ?+ ORDER BY type DESC, created_at ASC+ `, from, where)if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}var userMessages []*UserMessage- rows, err := txn.QueryContext(ctx, query, args...)+ rows, err := txn.QueryContext(ctx, sql, args...)if err != nil {return nil, err}@@ -356,23 +371,26 @@ func (s *Store) CreateUser(ctx context.Context, create *UserMessage) (*UserMessareturn nil, err}- set := []string{"email", "name", "type", "password_hash", "phone", "profile"}- args := []any{create.Email, create.Name, create.Type.String(), create.PasswordHash, create.Phone, profileBytes}- placeholder := []string{}- for index := range set {- placeholder = append(placeholder, fmt.Sprintf("$%d", index+1))+ q := qb.Q().Space(`+ INSERT INTO principal (+ email,+ name,+ type,+ password_hash,+ phone,+ profile+ )+ VALUES (?, ?, ?, ?, ?, ?)+ RETURNING id, created_at+ `, create.Email, create.Name, create.Type.String(), create.PasswordHash, create.Phone, profileBytes)++ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}var userID int- if err := tx.QueryRowContext(ctx, fmt.Sprintf(`- INSERT INTO principal (- %s- )- VALUES (%s)- RETURNING id, created_at- `, strings.Join(set, ","), strings.Join(placeholder, ",")),- args...,- ).Scan(&userID, &create.CreatedAt); err != nil {+ if err := tx.QueryRowContext(ctx, sql, args...).Scan(&userID, &create.CreatedAt); err != nil {return nil, err}@@ -402,59 +420,57 @@ func (s *Store) UpdateUser(ctx context.Context, currentUser *UserMessage, patchreturn nil, errors.Errorf("cannot update system bot")}- principalSet, principalArgs := []string{}, []any{}+ set := qb.Q()if v := patch.Delete; v != nil {- principalSet, principalArgs = append(principalSet, fmt.Sprintf("deleted = $%d", len(principalArgs)+1)), append(principalArgs, *v)+ set.Comma("deleted = ?", *v)}if v := patch.Email; v != nil {- principalSet, principalArgs = append(principalSet, fmt.Sprintf("email = $%d", len(principalArgs)+1)), append(principalArgs, strings.ToLower(*v))+ set.Comma("email = ?", strings.ToLower(*v))}if v := patch.Name; v != nil {- principalSet, principalArgs = append(principalSet, fmt.Sprintf("name = $%d", len(principalArgs)+1)), append(principalArgs, *v)+ set.Comma("name = ?", *v)}if v := patch.PasswordHash; v != nil {- principalSet, principalArgs = append(principalSet, fmt.Sprintf("password_hash = $%d", len(principalArgs)+1)), append(principalArgs, *v)+ set.Comma("password_hash = ?", *v)if patch.Profile == nil {patch.Profile = currentUser.Profilepatch.Profile.LastChangePasswordTime = timestamppb.New(time.Now())}}if v := patch.Phone; v != nil {- principalSet, principalArgs = append(principalSet, fmt.Sprintf("phone = $%d", len(principalArgs)+1)), append(principalArgs, *v)+ set.Comma("phone = ?", *v)}if v := patch.MFAConfig; v != nil {mfaConfigBytes, err := protojson.Marshal(v)if err != nil {return nil, err}- principalSet, principalArgs = append(principalSet, fmt.Sprintf("mfa_config = $%d", len(principalArgs)+1)), append(principalArgs, mfaConfigBytes)+ set.Comma("mfa_config = ?", mfaConfigBytes)}if v := patch.Profile; v != nil {profileBytes, err := protojson.Marshal(v)if err != nil {return nil, err}- principalSet, principalArgs = append(principalSet, fmt.Sprintf("profile = $%d", len(principalArgs)+1)), append(principalArgs, profileBytes)+ set.Comma("profile = ?", profileBytes)}- principalArgs = append(principalArgs, currentUser.ID)- if len(principalSet) == 0 {+ if set.Len() == 0 {return currentUser, nil}+ sql, args, err := qb.Q().Space("UPDATE principal SET ? WHERE id = ?", set, currentUser.ID).ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, err}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, fmt.Sprintf(`- UPDATE principal- SET `+strings.Join(principalSet, ", ")+`- WHERE id = $%d- `, len(principalArgs)),- principalArgs...,- ); err != nil {+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return nil, err}diff --git a/backend/store/project.go b/backend/store/project.goindex 3a7eff8a0a..d3d5419811 100644--- a/backend/store/project.go+++ b/backend/store/project.go@@ -4,13 +4,13 @@ import ("context""database/sql""fmt"- "strings""github.com/pkg/errors""google.golang.org/genproto/googleapis/type/expr""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -133,20 +133,13 @@ func (s *Store) CreateProjectV2(ctx context.Context, create *ProjectMessage, creDataClassificationConfigID: create.DataClassificationConfigID,Setting: create.Setting,}- if _, err := tx.ExecContext(ctx, `- INSERT INTO project (- resource_id,- name,- data_classification_config_id,- setting- )- VALUES ($1, $2, $3, $4)- `,- create.ResourceID,- create.Title,- create.DataClassificationConfigID,- payload,- ); err != nil {+ q := qb.Q().Space("INSERT INTO project (resource_id, name, data_classification_config_id, setting)")+ q.Space("VALUES (?, ?, ?, ?)", create.ResourceID, create.Title, create.DataClassificationConfigID, payload)+ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, err+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return nil, err}@@ -248,68 +241,68 @@ func (s *Store) BatchUpdateProjectsV2(ctx context.Context, patches []*UpdateProj}func updateProjectImplV2(ctx context.Context, txn *sql.Tx, patch *UpdateProjectMessage) error {- set, args := []string{}, []any{}+ set := qb.Q()+if v := patch.Title; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set.Comma("name = ?", *v)}if v := patch.Delete; v != nil {- set, args = append(set, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, *v)+ set.Comma("deleted = ?", *v)}if v := patch.DataClassificationConfigID; v != nil {- set, args = append(set, fmt.Sprintf("data_classification_config_id = $%d", len(args)+1)), append(args, *v)+ set.Comma("data_classification_config_id = ?", *v)}if v := patch.Setting; v != nil {payload, err := protojson.Marshal(patch.Setting)if err != nil {return err}- set, args = append(set, fmt.Sprintf("setting = $%d", len(args)+1)), append(args, payload)+ set.Comma("setting = ?", payload)}- args = append(args, patch.ResourceID)- if _, err := txn.ExecContext(ctx, fmt.Sprintf(`- UPDATE project- SET `+strings.Join(set, ", ")+`- WHERE resource_id = $%d`, len(args)),- args...,- ); err != nil {+ if set.Len() == 0 {+ return errors.New("no fields to update")+ }++ q := qb.Q().Space("UPDATE project SET ?", set).+ Space("WHERE resource_id = ?", patch.ResourceID)++ sql, args, err := q.ToSQL()+ if err != nil {+ return err+ }+ if _, err := txn.ExecContext(ctx, sql, args...); err != nil {return err}return nil}func (s *Store) listProjectImplV2(ctx context.Context, txn *sql.Tx, find *FindProjectMessage) ([]*ProjectMessage, error) {- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().Space("SELECT resource_id, name, data_classification_config_id, setting, deleted FROM project WHERE TRUE")if filter := find.Filter; filter != nil {- where = append(where, filter.Where)- args = append(args, filter.Args...)+ q.And(ConvertDollarPlaceholders(filter.Where), filter.Args...)}if v := find.ResourceID; v != nil {- where, args = append(where, fmt.Sprintf("resource_id = $%d", len(args)+1)), append(args, *v)+ q.And("resource_id = ?", *v)}if !find.ShowDeleted {- where, args = append(where, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, false)- }-- query := fmt.Sprintf(`- SELECT- resource_id,- name,- data_classification_config_id,- setting,- deleted- FROM project- WHERE %s- ORDER BY project.resource_id`, strings.Join(where, " AND "))+ q.And("deleted = ?", false)+ }+ q.Space("ORDER BY project.resource_id")if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, err}var projectMessages []*ProjectMessage- rows, err := txn.QueryContext(ctx, query, args...)+ rows, err := txn.QueryContext(ctx, sql, args...)if err != nil {return nil, err}@@ -370,155 +363,195 @@ func (s *Store) DeleteProject(ctx context.Context, resourceID string) error {defer tx.Rollback()// Delete query_history entries that reference this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM query_history- WHERE project_id = $1- `, resourceID); err != nil {+ q := qb.Q().Space("DELETE FROM query_history WHERE project_id = ?", resourceID)+ sql, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build query_history delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete query_history for project %s", resourceID)}// Delete policy entries that reference this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM policy- WHERE (resource_type = $1 AND resource = 'projects/' || $2)- `, storepb.Policy_PROJECT.String(), resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM policy")+ q.Space("WHERE (resource_type = ? AND resource = 'projects/' || ?)", storepb.Policy_PROJECT.String(), resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build policy delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete policies for project %s", resourceID)}// Delete worksheets associated with this project- if _, err := tx.ExecContext(ctx, `- UPDATE worksheet- SET project = $1- WHERE project = $2- `, common.DefaultProjectID, resourceID); err != nil {+ q = qb.Q().Space("UPDATE worksheet SET project = ? WHERE project = ?", common.DefaultProjectID, resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build worksheet update query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to update worksheets for project %s", resourceID)}// Delete issue_comment entries for issues in this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM issue_comment- WHERE issue_id IN (- SELECT id FROM issue WHERE project = $1- )- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM issue_comment")+ q.Space("WHERE issue_id IN (SELECT id FROM issue WHERE project = ?)", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build issue_comment delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete issue_comment for project %s", resourceID)}// Delete issues associated with this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM issue WHERE project = $1- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM issue WHERE project = ?", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build issue delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete issues for project %s", resourceID)}// Delete plan_check_run entries for plans in this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM plan_check_run- WHERE plan_id IN (- SELECT id FROM plan WHERE project = $1- )- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM plan_check_run")+ q.Space("WHERE plan_id IN (SELECT id FROM plan WHERE project = ?)", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build plan_check_run delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete plan_check_run for project %s", resourceID)}// Delete plans associated with this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM plan WHERE project = $1- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM plan WHERE project = ?", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build plan delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete plans for project %s", resourceID)}// Delete task_run_log entries for tasks in pipelines of this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM task_run_log- WHERE task_run_id IN (- SELECT tr.id FROM task_run tr- JOIN task t ON tr.task_id = t.id- JOIN pipeline p ON t.pipeline_id = p.id- WHERE p.project = $1- )- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM task_run_log")+ q.Space("WHERE task_run_id IN (")+ q.Space("SELECT tr.id FROM task_run tr")+ q.Space("JOIN task t ON tr.task_id = t.id")+ q.Space("JOIN pipeline p ON t.pipeline_id = p.id")+ q.Space("WHERE p.project = ?)", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build task_run_log delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete task_run_log for project %s", resourceID)}// Delete task_run entries for tasks in pipelines of this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM task_run- WHERE task_id IN (- SELECT t.id FROM task t- JOIN pipeline p ON t.pipeline_id = p.id- WHERE p.project = $1- )- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM task_run")+ q.Space("WHERE task_id IN (")+ q.Space("SELECT t.id FROM task t")+ q.Space("JOIN pipeline p ON t.pipeline_id = p.id")+ q.Space("WHERE p.project = ?)", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build task_run delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete task_run for project %s", resourceID)}// Delete tasks in pipelines of this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM task- WHERE pipeline_id IN (- SELECT id FROM pipeline WHERE project = $1- )- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM task")+ q.Space("WHERE pipeline_id IN (SELECT id FROM pipeline WHERE project = ?)", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build task delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete tasks for project %s", resourceID)}// Delete pipelines associated with this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM pipeline WHERE project = $1- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM pipeline WHERE project = ?", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build pipeline delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete pipelines for project %s", resourceID)}// Delete sheets associated with this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM sheet WHERE project = $1- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM sheet WHERE project = ?", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build sheet delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete sheets for project %s", resourceID)}// Delete releases associated with this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM release WHERE project = $1- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM release WHERE project = ?", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build release delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete releases for project %s", resourceID)}// Delete changelists associated with this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM changelist WHERE project = $1- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM changelist WHERE project = ?", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build changelist delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete changelists for project %s", resourceID)}// Delete db_groups associated with this project- if _, err := tx.ExecContext(ctx, `- DELETE FROM db_group WHERE project = $1- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM db_group WHERE project = ?", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build db_group delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete db_groups for project %s", resourceID)}// Move databases to the default project instead of deleting them- if _, err := tx.ExecContext(ctx, `- UPDATE db- SET project = $1- WHERE project = $2- `, common.DefaultProjectID, resourceID); err != nil {+ q = qb.Q().Space("UPDATE db SET project = ? WHERE project = ?", common.DefaultProjectID, resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build db update query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to move databases to default project for project %s", resourceID)}// Delete project webhooks- if _, err := tx.ExecContext(ctx, `- DELETE FROM project_webhook WHERE project = $1- `, resourceID); err != nil {+ q = qb.Q().Space("DELETE FROM project_webhook WHERE project = ?", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build project_webhook delete query")+ }+ if _, err := tx.ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to delete project_webhook for project %s", resourceID)}// Finally, delete the project itself (only if it's marked as deleted)- result, err := tx.ExecContext(ctx, `- DELETE FROM project- WHERE resource_id = $1 AND deleted = TRUE- `, resourceID)+ q = qb.Q().Space("DELETE FROM project WHERE resource_id = ? AND deleted = TRUE", resourceID)+ sql, args, err = q.ToSQL()+ if err != nil {+ return errors.Wrap(err, "failed to build project delete query")+ }+ result, err := tx.ExecContext(ctx, sql, args...)if err != nil {return errors.Wrapf(err, "failed to delete project %s", resourceID)}diff --git a/backend/store/project_webhook.go b/backend/store/project_webhook.goindex 498a8f52fa..718846c458 100644--- a/backend/store/project_webhook.go+++ b/backend/store/project_webhook.go@@ -3,13 +3,12 @@ package storeimport ("context""database/sql"- "fmt"- "strings""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -39,14 +38,6 @@ type FindProjectWebhookMessage struct {// CreateProjectWebhookV2 creates an instance of ProjectWebhook.func (s *Store) CreateProjectWebhookV2(ctx context.Context, projectID string, create *ProjectWebhookMessage) (*ProjectWebhookMessage, error) {- query := `- INSERT INTO project_webhook (- project,- payload- )- VALUES ($1, $2)- RETURNING id- `payload := []byte("{}")if create.Payload != nil {p, err := protojson.Marshal(create.Payload)@@ -61,15 +52,26 @@ func (s *Store) CreateProjectWebhookV2(ctx context.Context, projectID string, crPayload: create.Payload,}+ q := qb.Q().Space(`+ INSERT INTO project_webhook (+ project,+ payload+ )+ VALUES (?, ?)+ RETURNING id+ `, projectID, payload)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, errors.Wrapf(err, "failed to begin transaction")}- if err := tx.QueryRowContext(ctx, query,- projectID,- payload,- ).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&projectWebhook.ID,); err != nil {if err == sql.ErrNoRows {@@ -145,20 +147,24 @@ func (s *Store) UpdateProjectWebhookV2(ctx context.Context, projectResourceID stpayload = p}+ q := qb.Q().Space(`+ UPDATE project_webhook+ SET payload = ?+ WHERE id = ?+ RETURNING id, project, payload+ `, payload, projectWebhookID)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+projectWebhook := ProjectWebhookMessage{Payload: &storepb.ProjectWebhook{},}var returnedPayload []byte// Execute update query with RETURNING.- if err := tx.QueryRowContext(ctx, `- UPDATE project_webhook- SET payload = $1- WHERE id = $2- RETURNING id, project, payload- `,- payload,- projectWebhookID,- ).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&projectWebhook.ID,&projectWebhook.ProjectID,&returnedPayload,@@ -183,12 +189,19 @@ func (s *Store) UpdateProjectWebhookV2(ctx context.Context, projectResourceID st// DeleteProjectWebhookV2 deletes an existing projectWebhook by projectUID and url.func (s *Store) DeleteProjectWebhookV2(ctx context.Context, projectResourceID string, projectWebhookUID int) error {+ q := qb.Q().Space("DELETE FROM project_webhook WHERE id = ?", projectWebhookUID)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return errors.Wrapf(err, "failed to begin transaction")}- if _, err := tx.ExecContext(ctx, `DELETE FROM project_webhook WHERE id = $1`, projectWebhookUID); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}@@ -201,27 +214,31 @@ func (s *Store) DeleteProjectWebhookV2(ctx context.Context, projectResourceID st}func (*Store) findProjectWebhookImplV2(ctx context.Context, txn *sql.Tx, find *FindProjectWebhookMessage) ([]*ProjectWebhookMessage, error) {- // Build WHERE clause.- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().Space(`+ SELECT+ id,+ project,+ payload+ FROM project_webhook+ WHERE TRUE+ `)+if v := find.ID; v != nil {- where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)+ q.And("id = ?", *v)}if v := find.ProjectID; v != nil {- where, args = append(where, fmt.Sprintf("project = $%d", len(args)+1)), append(args, *v)+ q.And("project = ?", *v)}if v := find.URL; v != nil {- where, args = append(where, fmt.Sprintf("payload->>'url' = $%d", len(args)+1)), append(args, *v)+ q.And("payload->>'url' = ?", *v)}- rows, err := txn.QueryContext(ctx, `- SELECT- id,- project,- payload- FROM project_webhook- WHERE `+strings.Join(where, " AND "),- args...,- )+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := txn.QueryContext(ctx, query, args...)if err != nil {return nil, err}diff --git a/backend/store/query_history.go b/backend/store/query_history.goindex 3ead44411b..27dad6d694 100644--- a/backend/store/query_history.go+++ b/backend/store/query_history.go@@ -2,8 +2,6 @@ package storeimport ("context"- "fmt"- "strings""time""google.golang.org/protobuf/encoding/protojson"@@ -11,6 +9,7 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -64,13 +63,7 @@ func (s *Store) CreateQueryHistory(ctx context.Context, create *QueryHistoryMessreturn nil, err}- tx, err := s.GetDB().BeginTx(ctx, nil)- if err != nil {- return nil, err- }- defer tx.Rollback()-- if err := tx.QueryRowContext(ctx, `+ q := qb.Q().Space(`INSERT INTO query_history (creator_id,project_id,@@ -79,7 +72,7 @@ func (s *Store) CreateQueryHistory(ctx context.Context, create *QueryHistoryMesstype,payload)- VALUES ($1, $2, $3, $4, $5, $6)+ VALUES (?, ?, ?, ?, ?, ?)RETURNINGid,created_at@@ -90,7 +83,20 @@ func (s *Store) CreateQueryHistory(ctx context.Context, create *QueryHistoryMesscreate.Statement,create.Type,payload,- ).Scan(+ )++ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return nil, err+ }+ defer tx.Rollback()++ if err := tx.QueryRowContext(ctx, sql, args...).Scan(&create.UID,&create.CreatedAt,); err != nil {@@ -105,25 +111,50 @@ func (s *Store) CreateQueryHistory(ctx context.Context, create *QueryHistoryMess// ListQueryHistories lists the query history.func (s *Store) ListQueryHistories(ctx context.Context, find *FindQueryHistoryMessage) ([]*QueryHistoryMessage, error) {- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().Space(`+ SELECT+ query_history.id,+ query_history.creator_id,+ query_history.created_at,+ query_history.project_id,+ query_history.database,+ query_history.statement,+ query_history.type,+ query_history.payload+ FROM query_history+ WHERE TRUE+ `)+if filter := find.Filter; filter != nil {- where = append(where, filter.Where)- args = append(args, filter.Args...)+ // Convert $1, $2, etc. to ? for qb+ q.And(ConvertDollarPlaceholders(filter.Where), filter.Args...)}-if v := find.CreatorUID; v != nil {- where, args = append(where, fmt.Sprintf("query_history.creator_id = $%d", len(args)+1)), append(args, *v)+ q.And("query_history.creator_id = ?", *v)}if v := find.ProjectID; v != nil {- where, args = append(where, fmt.Sprintf("query_history.project_id = $%d", len(args)+1)), append(args, *v)+ q.And("query_history.project_id = ?", *v)}if v := find.Instance; v != nil {- where, args = append(where, fmt.Sprintf("query_history.database LIKE $%d", len(args)+1)), append(args, *v)+ q.And("query_history.database LIKE ?", *v)} else if v := find.Database; v != nil {- where, args = append(where, fmt.Sprintf("query_history.database = $%d", len(args)+1)), append(args, *v)+ q.And("query_history.database = ?", *v)}if v := find.Type; v != nil {- where, args = append(where, fmt.Sprintf("query_history.type = $%d", len(args)+1)), append(args, *v)+ q.And("query_history.type = ?", *v)+ }++ q.Space("ORDER BY id DESC")+ if v := find.Limit; v != nil {+ q.Space("LIMIT ?", *v)+ }+ if v := find.Offset; v != nil {+ q.Space("OFFSET ?", *v)+ }++ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, nil)@@ -132,29 +163,8 @@ func (s *Store) ListQueryHistories(ctx context.Context, find *FindQueryHistoryMe}defer tx.Rollback()- query := fmt.Sprintf(`- SELECT- query_history.id,- query_history.creator_id,- query_history.created_at,- query_history.project_id,- query_history.database,- query_history.statement,- query_history.type,- query_history.payload- FROM query_history- WHERE %s- ORDER BY id DESC- `, strings.Join(where, " AND "))- if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)- }- if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)- }-var queryHistories []*QueryHistoryMessage- rows, err := tx.QueryContext(ctx, query, args...)+ rows, err := tx.QueryContext(ctx, sql, args...)if err != nil {return nil, err}diff --git a/backend/store/release.go b/backend/store/release.goindex 1e73a2430b..ee3c3fb63e 100644--- a/backend/store/release.go+++ b/backend/store/release.go@@ -3,14 +3,13 @@ package storeimport ("context""database/sql"- "fmt"- "strings""time""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -43,23 +42,28 @@ type UpdateReleaseMessage struct {}func (s *Store) CreateRelease(ctx context.Context, release *ReleaseMessage, creatorUID int) (*ReleaseMessage, error) {- query := `+ p, err := protojson.Marshal(release.Payload)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to marshal release payload")+ }++ q := qb.Q().Space(`INSERT INTO release (creator_id,project,digest,payload) VALUES (- $1,- $2,- $3,- $4+ ?,+ ?,+ ?,+ ?) RETURNING id, created_at- `+ `, creatorUID, release.ProjectID, release.Digest, p)- p, err := protojson.Marshal(release.Payload)+ query, args, err := q.ToSQL()if err != nil {- return nil, errors.Wrapf(err, "failed to marshal release payload")+ return nil, errors.Wrapf(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, nil)@@ -70,12 +74,7 @@ func (s *Store) CreateRelease(ctx context.Context, release *ReleaseMessage, creavar id int64var createdTime time.Time- if err := tx.QueryRowContext(ctx, query,- creatorUID,- release.ProjectID,- release.Digest,- p,- ).Scan(&id, &createdTime); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&id, &createdTime); err != nil {return nil, errors.Wrapf(err, "failed to insert release")}@@ -105,24 +104,7 @@ func (s *Store) GetRelease(ctx context.Context, uid int64) (*ReleaseMessage, err}func (s *Store) ListReleases(ctx context.Context, find *FindReleaseMessage) ([]*ReleaseMessage, error) {- where, args := []string{"TRUE"}, []any{}- if v := find.ProjectID; v != nil {- where = append(where, fmt.Sprintf("project = $%d", len(args)+1))- args = append(args, *v)- }- if v := find.UID; v != nil {- where = append(where, fmt.Sprintf("id= $%d", len(args)+1))- args = append(args, *v)- }- if v := find.Digest; v != nil {- where = append(where, fmt.Sprintf("digest = $%d", len(args)+1))- args = append(args, *v)- }- if !find.ShowDeleted {- where, args = append(where, fmt.Sprintf("deleted = $%d", len(args)+1)), append(args, false)- }-- query := fmt.Sprintf(`+ q := qb.Q().Space(`SELECTid,deleted,@@ -132,15 +114,33 @@ func (s *Store) ListReleases(ctx context.Context, find *FindReleaseMessage) ([]*created_at,payloadFROM release- WHERE %s- ORDER BY release.id DESC- `, strings.Join(where, " AND "))+ WHERE TRUE+ `)+ if v := find.ProjectID; v != nil {+ q.And("project = ?", *v)+ }+ if v := find.UID; v != nil {+ q.And("id = ?", *v)+ }+ if v := find.Digest; v != nil {+ q.And("digest = ?", *v)+ }+ if !find.ShowDeleted {+ q.And("deleted = ?", false)+ }++ q.Space("ORDER BY release.id DESC")if v := find.Limit; v != nil {- query += fmt.Sprintf(" LIMIT %d", *v)+ q.Space("LIMIT ?", *v)}if v := find.Offset; v != nil {- query += fmt.Sprintf(" OFFSET %d", *v)+ q.Space("OFFSET ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})@@ -193,23 +193,26 @@ func (s *Store) ListReleases(ctx context.Context, find *FindReleaseMessage) ([]*}func (s *Store) UpdateRelease(ctx context.Context, update *UpdateReleaseMessage) (*ReleaseMessage, error) {- set, args := []string{}, []any{}-+ set := qb.Q()if v := update.Deleted; v != nil {- set, args = append(set, fmt.Sprintf(`deleted = $%d`, len(args)+1)), append(args, *v)+ set.Comma("deleted = ?", *v)}if v := update.Payload; v != nil {payload, err := protojson.Marshal(update.Payload)if err != nil {return nil, errors.Wrapf(err, "failed to marshal payload")}- set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, payload)+ set.Comma("payload = ?", payload)}- if len(set) == 0 {++ if set.Len() == 0 {return nil, errors.New("no update field provided")}- args = append(args, update.UID)+ query, args, err := qb.Q().Space("UPDATE release SET ? WHERE id = ?", set, update.UID).ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -217,7 +220,7 @@ func (s *Store) UpdateRelease(ctx context.Context, update *UpdateReleaseMessage)}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, fmt.Sprintf(`UPDATE release SET %s WHERE id = $%d`, strings.Join(set, ", "), len(args)), args...); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return nil, errors.Wrapf(err, "failed to query row")}diff --git a/backend/store/review_config.go b/backend/store/review_config.goindex 48c58868ed..951980ba1b 100644--- a/backend/store/review_config.go+++ b/backend/store/review_config.go@@ -3,13 +3,12 @@ package storeimport ("context""database/sql"- "fmt"- "strings""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -52,10 +51,23 @@ func (s *Store) GetReviewConfig(ctx context.Context, id string) (*ReviewConfigMe// ListReviewConfigs lists all sql review.func (s *Store) ListReviewConfigs(ctx context.Context, find *FindReviewConfigMessage) ([]*ReviewConfigMessage, error) {- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().Space(`+ SELECT+ id,+ enabled,+ name,+ payload+ FROM review_config+ WHERE TRUE+ `)if v := find.ID; v != nil {- where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, *v)+ q.And("id = ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})@@ -64,16 +76,7 @@ func (s *Store) ListReviewConfigs(ctx context.Context, find *FindReviewConfigMes}defer tx.Rollback()- rows, err := tx.QueryContext(ctx, fmt.Sprintf(`- SELECT- id,- enabled,- name,- payload- FROM review_config- WHERE %s`, strings.Join(where, " AND ")),- args...,- )+ rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -121,14 +124,19 @@ func (s *Store) CreateReviewConfig(ctx context.Context, create *ReviewConfigMessreturn nil, err}- query := `+ q := qb.Q().Space(`INSERT INTO review_config (id,name,payload)- VALUES ($1, $2, $3)- `+ VALUES (?, ?, ?)+ `, create.ID, create.Name, payload)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -136,11 +144,7 @@ func (s *Store) CreateReviewConfig(ctx context.Context, create *ReviewConfigMess}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, query,- create.ID,- create.Name,- payload,- ); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {if err == sql.ErrNoRows {return nil, common.FormatDBErrorEmptyRowWithQuery(query)}@@ -157,13 +161,20 @@ func (s *Store) CreateReviewConfig(ctx context.Context, create *ReviewConfigMess// DeleteReviewConfig deletes sql review by ID.func (s *Store) DeleteReviewConfig(ctx context.Context, id string) error {+ q := qb.Q().Space("DELETE FROM review_config WHERE id = ?", id)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return err}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `DELETE FROM review_config WHERE id = $1`, id); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}@@ -172,38 +183,45 @@ func (s *Store) DeleteReviewConfig(ctx context.Context, id string) error {// UpdateReviewConfig updates the sql review.func (s *Store) UpdateReviewConfig(ctx context.Context, patch *PatchReviewConfigMessage) (*ReviewConfigMessage, error) {- set, args := []string{}, []any{}+ set := qb.Q()if v := patch.Enforce; v != nil {- set, args = append(set, fmt.Sprintf("enabled = $%d", len(args)+1)), append(args, *v)+ set.Comma("enabled = ?", *v)}if v := patch.Name; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set.Comma("name = ?", *v)}if v := patch.Payload; v != nil {payload, err := protojson.Marshal(v)if err != nil {return nil, err}- set, args = append(set, fmt.Sprintf("payload = $%d", len(args)+1)), append(args, payload)+ set.Comma("payload = ?", payload)}- args = append(args, patch.ID)-- tx, err := s.GetDB().BeginTx(ctx, nil)- if err != nil {- return nil, err+ if set.Len() == 0 {+ return nil, errors.New("no fields to update")}- defer tx.Rollback()- query := fmt.Sprintf(`+ q := qb.Q().Space(`UPDATE review_config- SET `+strings.Join(set, ", ")+`- WHERE id = $%d+ SET ?+ WHERE id = ?RETURNINGid,enabled,name,payload- `, len(args))+ `, set, patch.ID)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ tx, err := s.GetDB().BeginTx(ctx, nil)+ if err != nil {+ return nil, err+ }+ defer tx.Rollback()var sqlReview ReviewConfigMessagevar payload []bytediff --git a/backend/store/risk.go b/backend/store/risk.goindex f357cc2fc6..98bcd769e5 100644--- a/backend/store/risk.go+++ b/backend/store/risk.go@@ -3,14 +3,13 @@ package storeimport ("context""database/sql"- "fmt"- "strings""github.com/pkg/errors""google.golang.org/genproto/googleapis/type/expr""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -55,7 +54,7 @@ type UpdateRiskMessage struct {// GetRisk gets a risk.func (s *Store) GetRisk(ctx context.Context, id int64) (*RiskMessage, error) {- query := `+ q := qb.Q().Space(`SELECTid,source,@@ -64,7 +63,12 @@ func (s *Store) GetRisk(ctx context.Context, id int64) (*RiskMessage, error) {active,expressionFROM risk- WHERE id = $1`+ WHERE id = ?`, id)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})if err != nil {@@ -75,7 +79,7 @@ func (s *Store) GetRisk(ctx context.Context, id int64) (*RiskMessage, error) {var risk RiskMessagevar expressionBytes []bytevar levelStr string- if err := tx.QueryRowContext(ctx, query, id).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&risk.ID,&risk.Source,&levelStr,@@ -115,7 +119,7 @@ func (s *Store) ListRisks(ctx context.Context) ([]*RiskMessage, error) {return v, nil}- query := `+ q := qb.Q().Space(`SELECTid,source,@@ -125,7 +129,12 @@ func (s *Store) ListRisks(ctx context.Context) ([]*RiskMessage, error) {expressionFROM riskORDER BY source, level DESC, id- `+ `)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})if err != nil {@@ -133,7 +142,7 @@ func (s *Store) ListRisks(ctx context.Context) ([]*RiskMessage, error) {}defer tx.Rollback()- rows, err := tx.QueryContext(ctx, query)+ rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return nil, errors.Wrapf(err, "failed to query %s", query)}@@ -183,19 +192,25 @@ func (s *Store) ListRisks(ctx context.Context) ([]*RiskMessage, error) {// CreateRisk creates a risk.func (s *Store) CreateRisk(ctx context.Context, risk *RiskMessage) (*RiskMessage, error) {- query := `+ expressionBytes, err := protojson.Marshal(risk.Expression)+ if err != nil {+ return nil, err+ }++ q := qb.Q().Space(`INSERT INTO risk (source,level,name,active,expression- ) VALUES ($1, $2, $3, $4, $5)+ ) VALUES (?, ?, ?, ?, ?)RETURNING id- `- expressionBytes, err := protojson.Marshal(risk.Expression)+ `, risk.Source, risk.Level.String(), risk.Name, risk.Active, string(expressionBytes))++ query, args, err := q.ToSQL()if err != nil {- return nil, err+ return nil, errors.Wrapf(err, "failed to build sql")}tx, err := s.GetDB().BeginTx(ctx, nil)@@ -206,7 +221,7 @@ func (s *Store) CreateRisk(ctx context.Context, risk *RiskMessage) (*RiskMessagevar id int64// Convert enum to database string using .String() method- if err := tx.QueryRowContext(ctx, query, risk.Source, risk.Level.String(), risk.Name, risk.Active, string(expressionBytes)).Scan(&id); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&id); err != nil {return nil, err}@@ -227,28 +242,36 @@ func (s *Store) CreateRisk(ctx context.Context, risk *RiskMessage) (*RiskMessage// UpdateRisk updates a risk.func (s *Store) UpdateRisk(ctx context.Context, patch *UpdateRiskMessage, id int64) (*RiskMessage, error) {- set, args := []string{}, []any{}+ set := qb.Q()if v := patch.Name; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set.Comma("name = ?", *v)}if v := patch.Active; v != nil {- set, args = append(set, fmt.Sprintf("active = $%d", len(args)+1)), append(args, *v)+ set.Comma("active = ?", *v)}if v := patch.Level; v != nil {// Convert enum to database string using .String() method- set, args = append(set, fmt.Sprintf("level = $%d", len(args)+1)), append(args, v.String())+ set.Comma("level = ?", v.String())}if v := patch.Source; v != nil {- set, args = append(set, fmt.Sprintf("source = $%d", len(args)+1)), append(args, *v)+ set.Comma("source = ?", *v)}if v := patch.Expression; v != nil {expressionBytes, err := protojson.Marshal(patch.Expression)if err != nil {return nil, err}- set, args = append(set, fmt.Sprintf("expression = $%d", len(args)+1)), append(args, string(expressionBytes))+ set.Comma("expression = ?", string(expressionBytes))+ }+ if set.Len() == 0 {+ return nil, errors.New("no fields to update")+ }+ q := qb.Q().Space("UPDATE risk SET ? WHERE id = ?", set, id)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}- args = append(args, id)tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -256,12 +279,7 @@ func (s *Store) UpdateRisk(ctx context.Context, patch *UpdateRiskMessage, id int}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `- UPDATE risk- SET `+strings.Join(set, ", ")+`- WHERE id = `+fmt.Sprintf("$%d", len(args)),- args...,- ); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return nil, err}@@ -274,13 +292,20 @@ func (s *Store) UpdateRisk(ctx context.Context, patch *UpdateRiskMessage, id int}func (s *Store) DeleteRisk(ctx context.Context, id int64) error {+ q := qb.Q().Space("DELETE FROM risk WHERE id = ?", id)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return errors.Wrap(err, "failed to begin tx")}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `DELETE FROM risk WHERE id = $1`, id); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}diff --git a/backend/store/role.go b/backend/store/role.goindex dcb8ab4859..0c4837e18f 100644--- a/backend/store/role.go+++ b/backend/store/role.go@@ -3,12 +3,12 @@ package storeimport ("context""database/sql"- "fmt"- "strings"+ "github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -35,16 +35,22 @@ type RoleUsedByResource struct {}func (s *Store) GetResourcesUsedByRole(ctx context.Context, role string) ([]*RoleUsedByResource, error) {- query := `+ q := qb.Q().Space(`SELECT resource, resource_type FROM policyCROSS JOIN LATERAL jsonb_array_elements(payload->'bindings') AS bindingWHERE- type = $1 AND+ type = ? ANDCOALESCE(jsonb_array_length(binding->'members'), 0) > 0 AND- binding->>'role' = $2- GROUP BY resource, resource_type;- `- rows, err := s.GetDB().QueryContext(ctx, query, storepb.Policy_IAM.String(), role)+ binding->>'role' = ?+ GROUP BY resource, resource_type+ `, storepb.Policy_IAM.String(), role)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := s.GetDB().QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -69,11 +75,6 @@ func (s *Store) GetResourcesUsedByRole(ctx context.Context, role string) ([]*Rol// CreateRole creates a new role.func (s *Store) CreateRole(ctx context.Context, create *RoleMessage) (*RoleMessage, error) {- query := `- INSERT INTO- role (resource_id, name, description, permissions)- VALUES ($1, $2, $3, $4)- `p := &storepb.RolePermissions{}for k := range create.Permissions {p.Permissions = append(p.Permissions, k)@@ -82,7 +83,19 @@ func (s *Store) CreateRole(ctx context.Context, create *RoleMessage) (*RoleMessaif err != nil {return nil, err}- if _, err := s.GetDB().ExecContext(ctx, query, create.ResourceID, create.Name, create.Description, permissionBytes); err != nil {++ q := qb.Q().Space(`+ INSERT INTO+ role (resource_id, name, description, permissions)+ VALUES (?, ?, ?, ?)+ `, create.ResourceID, create.Name, create.Description, permissionBytes)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {return nil, err}s.rolesCache.Add(create.ResourceID, create)@@ -94,17 +107,24 @@ func (s *Store) GetRole(ctx context.Context, resourceID string) (*RoleMessage, eif v, ok := s.rolesCache.Get(resourceID); ok && s.enableCache {return v, nil}- query := `++ q := qb.Q().Space(`SELECTname, description, permissionsFROM role- WHERE resource_id = $1- `+ WHERE resource_id = ?+ `, resourceID)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+role := &RoleMessage{Permissions: map[string]bool{},}var permissions []byte- if err := s.GetDB().QueryRowContext(ctx, query, resourceID).Scan(&role.Name, &role.Description, &permissions); err != nil {+ if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&role.Name, &role.Description, &permissions); err != nil {if err == sql.ErrNoRows {return nil, nil}@@ -124,12 +144,18 @@ func (s *Store) GetRole(ctx context.Context, resourceID string) (*RoleMessage, e// ListRoles returns a list of roles.func (s *Store) ListRoles(ctx context.Context) ([]*RoleMessage, error) {- query := `+ q := qb.Q().Space(`SELECTresource_id, name, description, permissionsFROM role- `- rows, err := s.GetDB().QueryContext(ctx, query)+ `)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := s.GetDB().QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -164,12 +190,12 @@ func (s *Store) ListRoles(ctx context.Context) ([]*RoleMessage, error) {// UpdateRole updates an existing role.func (s *Store) UpdateRole(ctx context.Context, patch *UpdateRoleMessage) (*RoleMessage, error) {- set, args := []string{}, []any{}+ set := qb.Q()if v := patch.Name; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set.Comma("name = ?", *v)}if v := patch.Description; v != nil {- set, args = append(set, fmt.Sprintf("description = $%d", len(args)+1)), append(args, *v)+ set.Comma("description = ?", *v)}if v := patch.Permissions; v != nil {p := &storepb.RolePermissions{}@@ -180,16 +206,23 @@ func (s *Store) UpdateRole(ctx context.Context, patch *UpdateRoleMessage) (*Roleif err != nil {return nil, err}- set, args = append(set, fmt.Sprintf("permissions = $%d", len(args)+1)), append(args, permissionBytes)+ set.Comma("permissions = ?", permissionBytes)+ }+ if set.Len() == 0 {+ return nil, errors.New("no fields to update")}- args = append(args, patch.ResourceID)- query := fmt.Sprintf(`+ q := qb.Q().Space(`UPDATE role- SET `+strings.Join(set, ", ")+`- WHERE resource_id = $%d+ SET ?+ WHERE resource_id = ?RETURNING name, description, permissions- `, len(args))+ `, set, patch.ResourceID)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }role := &RoleMessage{ResourceID: patch.ResourceID,@@ -214,11 +247,17 @@ func (s *Store) UpdateRole(ctx context.Context, patch *UpdateRoleMessage) (*Role// DeleteRole deletes an existing role.func (s *Store) DeleteRole(ctx context.Context, resourceID string) error {- query := `+ q := qb.Q().Space(`DELETE FROM role- WHERE resource_id = $1- `- if _, err := s.GetDB().ExecContext(ctx, query, resourceID); err != nil {+ WHERE resource_id = ?+ `, resourceID)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {return err}s.rolesCache.Remove(resourceID)diff --git a/backend/store/setting.go b/backend/store/setting.goindex 61feb04b17..95864d6109 100644--- a/backend/store/setting.go+++ b/backend/store/setting.go@@ -3,12 +3,11 @@ package storeimport ("context""database/sql"- "fmt"- "strings""github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -266,14 +265,17 @@ func (s *Store) GetSecret(ctx context.Context) (string, error) {// UpsertSettingV2 upserts the setting by name.func (s *Store) UpsertSettingV2(ctx context.Context, update *SetSettingMessage) (*SettingMessage, error) {- fields := []string{"name", "value"}- updateFields := []string{"value = EXCLUDED.value"}- valuePlaceholders, args := []string{"$1", "$2"}, []any{update.Name.String(), update.Value}-- query := `INSERT INTO setting (` + strings.Join(fields, ", ") + `)- VALUES (` + strings.Join(valuePlaceholders, ", ") + `)- ON CONFLICT (name) DO UPDATE SET ` + strings.Join(updateFields, ", ") + `- RETURNING name, value`+ q := qb.Q().Space(`+ INSERT INTO setting (name, value)+ VALUES (?, ?)+ ON CONFLICT (name) DO UPDATE SET value = EXCLUDED.value+ RETURNING name, value+ `, update.Name.String(), update.Value)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -328,12 +330,17 @@ func (s *Store) CreateSettingIfNotExistV2(ctx context.Context, create *SettingMereturn settings[0], false, nil}- fields := []string{"name", "value"}- valuesPlaceholders, args := []string{"$1", "$2"}, []any{create.Name.String(), create.Value}+ q := qb.Q().Space(`+ INSERT INTO setting (name, value)+ VALUES (?, ?)+ RETURNING name, value+ `, create.Name.String(), create.Value)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, false, errors.Wrapf(err, "failed to build sql")+ }- query := `INSERT INTO setting (` + strings.Join(fields, ",") + `)- VALUES (` + strings.Join(valuesPlaceholders, ",") + `)- RETURNING name, value`var setting SettingMessagevar nameString stringif err := tx.QueryRowContext(ctx, query, args...).Scan(@@ -357,13 +364,19 @@ func (s *Store) CreateSettingIfNotExistV2(ctx context.Context, create *SettingMe// DeleteSettingV2 deletes a setting by the name.func (s *Store) DeleteSettingV2(ctx context.Context, name storepb.SettingName) error {+ q := qb.Q().Space("DELETE FROM setting WHERE name = ?", name.String())+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return errors.Wrap(err, "failed to begin transaction")}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `DELETE FROM setting WHERE name = $1`, name.String()); err != nil {+ if _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}@@ -376,16 +389,21 @@ func (s *Store) DeleteSettingV2(ctx context.Context, name storepb.SettingName) e}func listSettingV2Impl(ctx context.Context, txn *sql.Tx, find *FindSettingMessage) ([]*SettingMessage, error) {- where, args := []string{"TRUE"}, []any{}- if v := find.Name; v != nil {- where, args = append(where, fmt.Sprintf("name = $%d", len(args)+1)), append(args, v.String())- }- rows, err := txn.QueryContext(ctx, `+ q := qb.Q().Space(`SELECTname,valueFROM setting- WHERE `+strings.Join(where, " AND "), args...)+ WHERE TRUE+ `)+ if v := find.Name; v != nil {+ q.And("name = ?", v.String())+ }+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+ rows, err := txn.QueryContext(ctx, query, args...)if err != nil {return nil, err}diff --git a/backend/store/sheet.go b/backend/store/sheet.goindex e1906ba5ea..bc9c7528af 100644--- a/backend/store/sheet.go+++ b/backend/store/sheet.go@@ -6,13 +6,13 @@ import ("database/sql""encoding/hex""fmt"- "strings""time""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -111,29 +111,12 @@ func (s *Store) GetSheet(ctx context.Context, find *FindSheetMessage) (*SheetMes// listSheets returns a list of sheets.func (s *Store) listSheets(ctx context.Context, find *FindSheetMessage) ([]*SheetMessage, error) {- where, args := []string{"TRUE"}, []any{}-- if v := find.UID; v != nil {- where, args = append(where, fmt.Sprintf("sheet.id = $%d", len(args)+1)), append(args, *v)- }- if v := find.CreatorID; v != nil {- where, args = append(where, fmt.Sprintf("sheet.creator_id = $%d", len(args)+1)), append(args, *v)- }- if v := find.ProjectID; v != nil {- where, args = append(where, fmt.Sprintf("sheet.project = $%d", len(args)+1)), append(args, *v)- }statementField := fmt.Sprintf("LEFT(sheet_blob.content, %d)", common.MaxSheetSize)if find.LoadFull {statementField = "sheet_blob.content"}- tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})- if err != nil {- return nil, err- }- defer tx.Rollback()-- rows, err := tx.QueryContext(ctx, fmt.Sprintf(`+ q := qb.Q().Space(fmt.Sprintf(`SELECTsheet.id,sheet.creator_id,@@ -146,9 +129,30 @@ func (s *Store) listSheets(ctx context.Context, find *FindSheetMessage) ([]*SheeOCTET_LENGTH(sheet_blob.content)FROM sheetLEFT JOIN sheet_blob ON sheet.sha256 = sheet_blob.sha256- WHERE %s`, statementField, strings.Join(where, " AND ")),- args...,- )+ WHERE TRUE`, statementField))++ if v := find.UID; v != nil {+ q.And("sheet.id = ?", *v)+ }+ if v := find.CreatorID; v != nil {+ q.And("sheet.creator_id = ?", *v)+ }+ if v := find.ProjectID; v != nil {+ q.And("sheet.project = ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})+ if err != nil {+ return nil, err+ }+ defer tx.Rollback()++ rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -208,7 +212,7 @@ func (s *Store) CreateSheet(ctx context.Context, create *SheetMessage) (*SheetMereturn nil, errors.Wrapf(err, "failed to create sheet blobs")}- query := `+ q := qb.Q().Space(`INSERT INTO sheet (creator_id,project,@@ -216,22 +220,21 @@ func (s *Store) CreateSheet(ctx context.Context, create *SheetMessage) (*SheetMesha256,payload)- VALUES ($1, $2, $3, $4, $5)+ VALUES (?, ?, ?, ?, ?)RETURNING id, created_at- `+ `, create.CreatorID, create.ProjectID, create.Title, create.Sha256, payload)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, err}defer tx.Rollback()- if err := tx.QueryRowContext(ctx, query,- create.CreatorID,- create.ProjectID,- create.Title,- create.Sha256,- payload,- ).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&create.UID,&create.CreatedAt,); err != nil {@@ -278,7 +281,7 @@ func (s *Store) BatchCreateSheet(ctx context.Context, projectID string, createsreturn nil, errors.Wrapf(err, "failed to create sheet blobs")}- query := `+ q := qb.Q().Space(`INSERT INTO sheet (creator_id,project,@@ -286,13 +289,18 @@ func (s *Store) BatchCreateSheet(ctx context.Context, projectID string, createssha256,payload) SELECT- $1,- $2,- unnest(CAST($3 AS TEXT[])),- unnest(CAST($4 AS BYTEA[])),- unnest(CAST($5 AS JSONB[]))+ ?,+ ?,+ unnest(CAST(? AS TEXT[])),+ unnest(CAST(? AS BYTEA[])),+ unnest(CAST(? AS JSONB[]))RETURNING id, created_at- `+ `, creatorUID, projectID, names, sha256s, payloads)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -300,7 +308,7 @@ func (s *Store) BatchCreateSheet(ctx context.Context, projectID string, creates}defer tx.Rollback()- rows, err := tx.QueryContext(ctx, query, creatorUID, projectID, names, sha256s, payloads)+ rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return nil, errors.Wrapf(err, "failed to query")}@@ -331,17 +339,22 @@ func (s *Store) BatchCreateSheet(ctx context.Context, projectID string, creates}func (s *Store) BatchCreateSheetBlob(ctx context.Context, sha256s [][]byte, contents []string) error {- query := `+ q := qb.Q().Space(`INSERT INTO sheet_blob (sha256,content) SELECT- unnest(CAST($1 AS BYTEA[])),- unnest(CAST($2 AS TEXT[]))+ unnest(CAST(? AS BYTEA[])),+ unnest(CAST(? AS TEXT[]))ON CONFLICT DO NOTHING- `+ `, sha256s, contents)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }- if _, err := s.GetDB().ExecContext(ctx, query, sha256s, contents); err != nil {+ if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to exec")}@@ -360,17 +373,21 @@ func (s *Store) PatchSheet(ctx context.Context, patch *PatchSheetMessage) (*Sheereturn nil, errors.Wrapf(err, "failed to create sheet blobs")}- var uid int- if err := s.GetDB().QueryRowContext(ctx, `+ q := qb.Q().Space(`UPDATE sheetSET- sha256 = $1- WHERE id = $2+ sha256 = ?+ WHERE id = ?RETURNING id- `,- h[:],- patch.UID,- ).Scan(+ `, h[:], patch.UID)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ var uid int+ if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&uid,); err != nil {if err == sql.ErrNoRows {diff --git a/backend/store/stats.go b/backend/store/stats.goindex 4c349e0b31..16acc9a83a 100644--- a/backend/store/stats.go+++ b/backend/store/stats.go@@ -3,12 +3,11 @@ package storeimport ("context""database/sql"- "fmt"- "strings""github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/metric")@@ -28,11 +27,13 @@ func (s *Store) CountUsers(ctx context.Context, userType storepb.PrincipalType)count := 0- if err := tx.QueryRowContext(ctx, `- SELECT COUNT(*)- FROM principal- WHERE principal.type = $1`,- userType.String()).Scan(&count); err != nil {+ q := qb.Q().Space("SELECT COUNT(*) FROM principal WHERE principal.type = ?", userType.String())+ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")+ }++ if err := tx.QueryRowContext(ctx, query, args...).Scan(&count); err != nil {return 0, err}@@ -45,15 +46,15 @@ func (s *Store) CountUsers(ctx context.Context, userType storepb.PrincipalType)// CountInstance counts the number of instances.func (s *Store) CountInstance(ctx context.Context, find *CountInstanceMessage) (int, error) {- where, args := []string{"instance.deleted = $1"}, []any{false}+ q := qb.Q().Space("SELECT count(1) FROM instance WHERE instance.deleted = ?", false)if v := find.EnvironmentID; v != nil {- where, args = append(where, fmt.Sprintf("instance.environment = $%d", len(args)+1)), append(args, *v)+ q = q.Space("AND instance.environment = ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")}- query := `- SELECT- count(1)- FROM instance- WHERE ` + strings.Join(where, " AND ")tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -79,13 +80,14 @@ func (s *Store) CountActiveUsers(ctx context.Context) (int, error) {}defer tx.Rollback()- query := `- SELECT- count(DISTINCT principal.id)- FROM principal- WHERE principal.deleted = $1 AND principal.type = $2`+ q := qb.Q().Space("SELECT count(DISTINCT principal.id) FROM principal WHERE principal.deleted = ? AND principal.type = ?", false, storepb.PrincipalType_END_USER.String())+ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")+ }+var count int- if err := tx.QueryRowContext(ctx, query, false, storepb.PrincipalType_END_USER.String()).Scan(&count); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&count); err != nil {if err == sql.ErrNoRows {return 0, common.FormatDBErrorEmptyRowWithQuery(query)}@@ -106,13 +108,14 @@ func (s *Store) CountProjects(ctx context.Context) (int, error) {}defer tx.Rollback()+ q := qb.Q().Space("SELECT COUNT(1) FROM project WHERE deleted = FALSE")+ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")+ }+var count int- if err := tx.QueryRowContext(ctx, `- SELECT- COUNT(1)- FROM project- WHERE deleted = FALSE`,- ).Scan(&count); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&count); err != nil {return 0, err}if err := tx.Commit(); err != nil {@@ -130,12 +133,14 @@ func (s *Store) CountIssues(ctx context.Context) (int, error) {}defer tx.Rollback()+ q := qb.Q().Space("SELECT COUNT(1) FROM issue WHERE id > ?", 101)+ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")+ }+var count int- if err := tx.QueryRowContext(ctx, `- SELECT COUNT(1)- FROM issue- WHERE id > 101- `).Scan(&count); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&count); err != nil {return 0, err}if err := tx.Commit(); err != nil {@@ -153,12 +158,13 @@ func (s *Store) CountInstanceGroupByEngineAndEnvironmentID(ctx context.Context)}defer tx.Rollback()- rows, err := tx.QueryContext(ctx, `- SELECT metadata->>'engine' AS engine, environment, COUNT(1)- FROM instance- WHERE id > 101 AND deleted = FALSE- GROUP BY engine, environment`,- )+ q := qb.Q().Space("SELECT metadata->>'engine' AS engine, environment, COUNT(1) FROM instance WHERE id > ? AND deleted = FALSE GROUP BY engine, environment", 101)+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return nil, err}diff --git a/backend/store/store.go b/backend/store/store.goindex 7b5b682cb0..f06a7d639d 100644--- a/backend/store/store.go+++ b/backend/store/store.go@@ -5,7 +5,6 @@ import ("context""database/sql""fmt"- "strings"lru "github.com/hashicorp/golang-lru/v2"@@ -176,11 +175,3 @@ func getDatabaseCacheKey(instanceID, databaseName string) string {func getDatabaseGroupCacheKey(projectID, resourceID string) string {return fmt.Sprintf("%s/%s", projectID, resourceID)}--func getPlaceholders(start int, count int) string {- var placeholders []string- for i := start; i < start+count; i++ {- placeholders = append(placeholders, fmt.Sprintf("$%d", i))- }- return fmt.Sprintf("(%s)", strings.Join(placeholders, ","))-}diff --git a/backend/store/sync_history.go b/backend/store/sync_history.goindex 7616263b38..9da4bdff3f 100644--- a/backend/store/sync_history.go+++ b/backend/store/sync_history.go@@ -4,11 +4,11 @@ import ("context""time"- "google.golang.org/protobuf/encoding/protojson"-"github.com/pkg/errors"+ "google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -23,7 +23,7 @@ type SyncHistory struct {}func (s *Store) GetSyncHistoryByUID(ctx context.Context, uid int64) (*SyncHistory, error) {- query := `+ q := qb.Q().Space(`SELECTid,created_at,@@ -32,14 +32,20 @@ func (s *Store) GetSyncHistoryByUID(ctx context.Context, uid int64) (*SyncHistormetadata,raw_dumpFROM sync_history- WHERE id = $1- `+ WHERE id = ?+ `, uid)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+h := SyncHistory{Metadata: &storepb.DatabaseSchemaMetadata{},}var m []byte- if err := s.GetDB().QueryRowContext(ctx, query, uid).Scan(+ if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&h.UID,&h.CreatedAt,&h.InstanceID,@@ -64,16 +70,22 @@ func (s *Store) CreateSyncHistory(ctx context.Context, instanceID, databaseNamereturn 0, errors.Wrapf(err, "failed to marshal")}- query := `+ q := qb.Q().Space(`INSERT INTO sync_history (instance,db_name,metadata,raw_dump)- VALUES ($1, $2, $3, $4)+ VALUES (?, ?, ?, ?)RETURNING id- `+ `, instanceID, databaseName, metadataBytes, schema)++ query, args, err := q.ToSQL()+ if err != nil {+ return 0, errors.Wrapf(err, "failed to build sql")+ }+tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return 0, errors.Wrapf(err, "failed to begin tx")@@ -81,14 +93,7 @@ func (s *Store) CreateSyncHistory(ctx context.Context, instanceID, databaseNamedefer tx.Rollback()var id int64- if err := tx.QueryRowContext(ctx, query,- instanceID,- databaseName,- metadataBytes,- schema,- ).Scan(- &id,- ); err != nil {+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&id); err != nil {return 0, errors.Wrapf(err, "failed to insert")}if err := tx.Commit(); err != nil {diff --git a/backend/store/task.go b/backend/store/task.goindex 84076e8637..c90ba6e0fa 100644--- a/backend/store/task.go+++ b/backend/store/task.go@@ -4,15 +4,14 @@ import ("context""database/sql""encoding/json"- "fmt""slices"- "strings""time""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/store/model")@@ -111,32 +110,35 @@ func (s *Store) FindBlockingTaskByVersion(ctx context.Context, pipelineUID int,if err != nil {return nil, err}- query := `- SELECT- task.id,- task.payload->>'schemaVersion'- FROM task- LEFT JOIN pipeline ON task.pipeline_id = pipeline.id- LEFT JOIN issue ON pipeline.id = issue.pipeline_id- LEFT JOIN LATERAL (- SELECT COALESCE(- (SELECT- task_run.status- FROM task_run- WHERE task_run.task_id = task.id- ORDER BY task_run.id DESC- LIMIT 1- ), 'NOT_STARTED'- ) AS status- ) AS latest_task_run ON TRUE- WHERE task.pipeline_id = $1 AND task.instance = $2 AND task.db_name = $3- AND task.payload->>'schemaVersion' IS NOT NULL- AND (task.payload->>'skipped')::BOOLEAN IS NOT TRUE- AND latest_task_run.status != 'DONE'- AND COALESCE(issue.status, 'OPEN') = 'OPEN'- ORDER BY task.id ASC- `- rows, err := s.GetDB().QueryContext(ctx, query, pipelineUID, instanceID, databaseName)+ q := qb.Q().+ Space("SELECT").+ Space("task.id,").+ Space("task.payload->>'schemaVersion'").+ Space("FROM task").+ Space("LEFT JOIN pipeline ON task.pipeline_id = pipeline.id").+ Space("LEFT JOIN issue ON pipeline.id = issue.pipeline_id").+ Space("LEFT JOIN LATERAL (").+ Space("SELECT COALESCE(").+ Space("(SELECT").+ Space("task_run.status").+ Space("FROM task_run").+ Space("WHERE task_run.task_id = task.id").+ Space("ORDER BY task_run.id DESC").+ Space("LIMIT 1").+ Space("), 'NOT_STARTED'").+ Space(") AS status").+ Space(") AS latest_task_run ON TRUE").+ Space("WHERE task.pipeline_id = ? AND task.instance = ? AND task.db_name = ?", pipelineUID, instanceID, databaseName).+ Space("AND task.payload->>'schemaVersion' IS NOT NULL").+ Space("AND (task.payload->>'skipped')::BOOLEAN IS NOT TRUE").+ Space("AND latest_task_run.status != 'DONE'").+ Space("AND COALESCE(issue.status, 'OPEN') = 'OPEN'").+ Space("ORDER BY task.id ASC")+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+ rows, err := s.GetDB().QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -162,22 +164,6 @@ func (s *Store) FindBlockingTaskByVersion(ctx context.Context, pipelineUID int,}func (*Store) createTasks(ctx context.Context, txn *sql.Tx, creates ...*TaskMessage) ([]*TaskMessage, error) {- query := `INSERT INTO task (- pipeline_id,- instance,- db_name,- environment,- type,- payload- ) SELECT- unnest(CAST($1 AS INTEGER[])),- unnest(CAST($2 AS TEXT[])),- unnest(CAST($3 AS TEXT[])),- unnest(CAST($4 AS TEXT[])),- unnest(CAST($5 AS TEXT[])),- unnest(CAST($6 AS JSONB[]))- RETURNING id, pipeline_id, instance, db_name, environment, type, payload`-var (pipelineIDs []intinstances []string@@ -202,15 +188,29 @@ func (*Store) createTasks(ctx context.Context, txn *sql.Tx, creates ...*TaskMesspayloads = append(payloads, payload)}+ q := qb.Q().+ Space("INSERT INTO task (").+ Space("pipeline_id,").+ Space("instance,").+ Space("db_name,").+ Space("environment,").+ Space("type,").+ Space("payload").+ Space(") SELECT").+ Space("unnest(CAST(? AS INTEGER[])),", pipelineIDs).+ Space("unnest(CAST(? AS TEXT[])),", instances).+ Space("unnest(CAST(? AS TEXT[])),", databases).+ Space("unnest(CAST(? AS TEXT[])),", environments).+ Space("unnest(CAST(? AS TEXT[])),", types).+ Space("unnest(CAST(? AS JSONB[]))", payloads).+ Space("RETURNING id, pipeline_id, instance, db_name, environment, type, payload")+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+var tasks []*TaskMessage- rows, err := txn.QueryContext(ctx, query,- pipelineIDs,- instances,- databases,- environments,- types,- payloads,- )+ rows, err := txn.QueryContext(ctx, query, args...)if err != nil {return nil, errors.Wrapf(err, "failed to query")}@@ -250,70 +250,69 @@ func (*Store) createTasks(ctx context.Context, txn *sql.Tx, creates ...*TaskMess}func (*Store) listTasksTx(ctx context.Context, txn *sql.Tx, find *TaskFind) ([]*TaskMessage, error) {- where, args := []string{"TRUE"}, []any{}+ q := qb.Q().+ Space("SELECT").+ Space("task.id,").+ Space("task.pipeline_id,").+ Space("task.instance,").+ Space("task.db_name,").+ Space("task.environment,").+ Space("COALESCE(latest_task_run.status, ?) AS latest_task_run_status,", storepb.TaskRun_NOT_STARTED.String()).+ Space("task.type,").+ Space("task.payload,").+ Space("latest_task_run.updated_at,").+ Space("latest_task_run.run_at").+ Space("FROM task").+ Space("LEFT JOIN LATERAL (").+ Space("SELECT").+ Space("task_run.status,").+ Space("task_run.updated_at,").+ Space("task_run.run_at").+ Space("FROM task_run").+ Space("WHERE task_run.task_id = task.id").+ Space("ORDER BY task_run.id DESC").+ Space("LIMIT 1").+ Space(") AS latest_task_run ON TRUE").+ Space("WHERE TRUE")if v := find.ID; v != nil {- where, args = append(where, fmt.Sprintf("task.id = $%d", len(args)+1)), append(args, *v)+ q.Space("AND task.id = ?", *v)}if v := find.IDs; v != nil {- where, args = append(where, fmt.Sprintf("task.id = ANY($%d)", len(args)+1)), append(args, *v)+ q.Space("AND task.id = ANY(?)", *v)}if v := find.PipelineID; v != nil {- where, args = append(where, fmt.Sprintf("task.pipeline_id = $%d", len(args)+1)), append(args, *v)+ q.Space("AND task.pipeline_id = ?", *v)}if v := find.Environment; v != nil {- where, args = append(where, fmt.Sprintf("task.environment = $%d", len(args)+1)), append(args, *v)+ q.Space("AND task.environment = ?", *v)}if v := find.InstanceID; v != nil {- where, args = append(where, fmt.Sprintf("task.instance = $%d", len(args)+1)), append(args, *v)+ q.Space("AND task.instance = ?", *v)}if v := find.DatabaseName; v != nil {- where, args = append(where, fmt.Sprintf("task.db_name = $%d", len(args)+1)), append(args, *v)+ q.Space("AND task.db_name = ?", *v)}if v := find.LatestTaskRunStatusList; v != nil {var statusStrings []stringfor _, status := range *v {statusStrings = append(statusStrings, status.String())}- where = append(where, fmt.Sprintf("latest_task_run.status = ANY($%d)", len(args)+1))- args = append(args, statusStrings)+ q.Space("AND latest_task_run.status = ANY(?)", statusStrings)}if v := find.TypeList; v != nil {- var list []string+ typeStrings := []string{}for _, taskType := range *v {- list = append(list, fmt.Sprintf("$%d", len(args)+1))- args = append(args, taskType.String())+ typeStrings = append(typeStrings, taskType.String())}- where = append(where, fmt.Sprintf("task.type in (%s)", strings.Join(list, ",")))- }-- args = append(args, storepb.TaskRun_NOT_STARTED.String())- rows, err := txn.QueryContext(ctx, fmt.Sprintf(`- SELECT- task.id,- task.pipeline_id,- task.instance,- task.db_name,- task.environment,- COALESCE(latest_task_run.status, $%d) AS latest_task_run_status,- task.type,- task.payload,- latest_task_run.updated_at,- latest_task_run.run_at- FROM task- LEFT JOIN LATERAL (- SELECT- task_run.status,- task_run.updated_at,- task_run.run_at- FROM task_run- WHERE task_run.task_id = task.id- ORDER BY task_run.id DESC- LIMIT 1- ) AS latest_task_run ON TRUE- WHERE %s- ORDER BY task.id ASC`, len(args), strings.Join(where, " AND ")),- args...,- )+ q.Space("AND task.type = ANY(?)", typeStrings)+ }+ q.Space("ORDER BY task.id ASC")++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+ rows, err := txn.QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -384,43 +383,52 @@ func (s *Store) ListTasks(ctx context.Context, find *TaskFind) ([]*TaskMessage,// UpdateTaskV2 updates an existing task.// Returns ENOTFOUND if task does not exist.func (s *Store) UpdateTaskV2(ctx context.Context, patch *TaskPatch) (*TaskMessage, error) {- set, args := []string{}, []any{}+ set := qb.Q()if v := patch.DatabaseName; v != nil {- set, args = append(set, fmt.Sprintf("db_name = $%d", len(args)+1)), append(args, *v)+ set.Comma("db_name = ?", *v)}if v := patch.Type; v != nil {- set, args = append(set, fmt.Sprintf("type = $%d", len(args)+1)), append(args, v.String())+ set.Comma("type = ?", v.String())}- var payloadSet []string++ payloadParts := qb.Q()if v := patch.SheetID; v != nil {- payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('sheetId', $%d::INT)`, len(args)+1)), append(args, *v)+ payloadParts.Join(" || ", "jsonb_build_object('sheetId', ?::INT)", *v)}if v := patch.SchemaVersion; v != nil {- payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('schemaVersion', $%d::TEXT)`, len(args)+1)), append(args, *v)+ payloadParts.Join(" || ", "jsonb_build_object('schemaVersion', ?::TEXT)", *v)}if v := patch.ExportFormat; v != nil {- payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('format', $%d::INT)`, len(args)+1)), append(args, *v)+ payloadParts.Join(" || ", "jsonb_build_object('format', ?::INT)", *v)}if v := patch.ExportPassword; v != nil {- payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('password', $%d::TEXT)`, len(args)+1)), append(args, *v)+ payloadParts.Join(" || ", "jsonb_build_object('password', ?::TEXT)", *v)}if v := patch.EnablePriorBackup; v != nil {- payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('enablePriorBackup', $%d::BOOLEAN)`, len(args)+1)), append(args, *v)+ payloadParts.Join(" || ", "jsonb_build_object('enablePriorBackup', ?::BOOLEAN)", *v)}if v := patch.MigrateType; v != nil {- payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('migrateType', $%d::TEXT)`, len(args)+1)), append(args, v.String())+ payloadParts.Join(" || ", "jsonb_build_object('migrateType', ?::TEXT)", v.String())}if v := patch.Flags; v != nil {jsonb, err := json.Marshal(v)if err != nil {return nil, errors.Wrapf(err, "failed to marshal flags")}- payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('flags', $%d::JSONB)`, len(args)+1)), append(args, jsonb)+ payloadParts.Join(" || ", "jsonb_build_object('flags', ?::JSONB)", jsonb)+ }+ if payloadParts.Len() > 0 {+ set.Comma("payload = payload || ?", payloadParts)}- if len(payloadSet) != 0 {- set = append(set, fmt.Sprintf(`payload = payload || %s`, strings.Join(payloadSet, "||")))++ if set.Len() == 0 {+ return nil, errors.Errorf("no fields to update")+ }++ query, args, err := qb.Q().Space(`UPDATE task SET ? WHERE id = ? RETURNING id, pipeline_id, instance, db_name, environment, type, payload`, set, patch.ID).ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")}- args = append(args, patch.ID)tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {@@ -431,14 +439,7 @@ func (s *Store) UpdateTaskV2(ctx context.Context, patch *TaskPatch) (*TaskMessagtask := &TaskMessage{}var payload []bytevar typeString string- if err := tx.QueryRowContext(ctx, fmt.Sprintf(`- UPDATE task- SET `+strings.Join(set, ", ")+`- WHERE id = $%d- RETURNING id, pipeline_id, instance, db_name, environment, type, payload- `, len(args)),- args...,- ).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&task.ID,&task.PipelineID,&task.InstanceID,@@ -472,11 +473,14 @@ func (s *Store) UpdateTaskV2(ctx context.Context, patch *TaskPatch) (*TaskMessag// BatchSkipTasks batch skip tasks.func (s *Store) BatchSkipTasks(ctx context.Context, taskUIDs []int, comment string) error {- query := `- UPDATE task- SET payload = payload || jsonb_build_object('skipped', $1::BOOLEAN) || jsonb_build_object('skippedReason', $2::TEXT)- WHERE id = ANY($3)`- args := []any{true, comment, taskUIDs}+ q := qb.Q().+ Space("UPDATE task").+ Space("SET payload = payload || jsonb_build_object('skipped', ?::BOOLEAN) || jsonb_build_object('skippedReason', ?::TEXT)", true, comment).+ Space("WHERE id = ANY(?)", taskUIDs)+ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to batch skip tasks")@@ -493,20 +497,25 @@ func (s *Store) BatchSkipTasks(ctx context.Context, taskUIDs []int, comment stri// 5. are the first task in the pipeline for their environment// 6. are not data export tasks.func (s *Store) ListTasksToAutoRollout(ctx context.Context, environments []string) ([]int, error) {- rows, err := s.GetDB().QueryContext(ctx, `- SELECT- task.pipeline_id,- task.environment,- task.id- FROM task- LEFT JOIN pipeline ON pipeline.id = task.pipeline_id- LEFT JOIN issue ON issue.pipeline_id = pipeline.id- WHERE NOT EXISTS (SELECT 1 FROM task_run WHERE task_run.task_id = task.id)- AND task.type != 'DATABASE_EXPORT'- AND COALESCE((task.payload->>'skipped')::BOOLEAN, FALSE) IS FALSE- AND COALESCE(issue.status, 'OPEN') = 'OPEN'- AND task.environment = ANY($1)- ORDER BY task.pipeline_id, task.id`, environments)+ q := qb.Q().+ Space("SELECT").+ Space("task.pipeline_id,").+ Space("task.environment,").+ Space("task.id").+ Space("FROM task").+ Space("LEFT JOIN pipeline ON pipeline.id = task.pipeline_id").+ Space("LEFT JOIN issue ON issue.pipeline_id = pipeline.id").+ Space("WHERE NOT EXISTS (SELECT 1 FROM task_run WHERE task_run.task_id = task.id)").+ Space("AND task.type != 'DATABASE_EXPORT'").+ Space("AND COALESCE((task.payload->>'skipped')::BOOLEAN, FALSE) IS FALSE").+ Space("AND COALESCE(issue.status, 'OPEN') = 'OPEN'").+ Space("AND task.environment = ANY(?)", environments).+ Space("ORDER BY task.pipeline_id, task.id")+ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+ rows, err := s.GetDB().QueryContext(ctx, query, args...)if err != nil {return nil, err}diff --git a/backend/store/task_run.go b/backend/store/task_run.goindex cf04b6a4a5..1603a3af53 100644--- a/backend/store/task_run.go+++ b/backend/store/task_run.go@@ -3,14 +3,13 @@ package storeimport ("context""database/sql"- "fmt""slices"- "strings""time""github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -72,32 +71,7 @@ type TaskRunStatusPatch struct {// ListTaskRunsV2 lists task runs.func (s *Store) ListTaskRunsV2(ctx context.Context, find *FindTaskRunMessage) ([]*TaskRunMessage, error) {- where, args := []string{"TRUE"}, []any{}- if v := find.UID; v != nil {- where, args = append(where, fmt.Sprintf("task_run.id = $%d", len(args)+1)), append(args, *v)- }- if v := find.UIDs; v != nil {- where, args = append(where, fmt.Sprintf("task_run.id = ANY($%d)", len(args)+1)), append(args, *v)- }- if v := find.TaskUID; v != nil {- where, args = append(where, fmt.Sprintf("task_run.task_id = $%d", len(args)+1)), append(args, *v)- }- if v := find.Environment; v != nil {- where, args = append(where, fmt.Sprintf("task.environment = $%d", len(args)+1)), append(args, *v)- }- if v := find.PipelineUID; v != nil {- where, args = append(where, fmt.Sprintf("task.pipeline_id = $%d", len(args)+1)), append(args, *v)- }- if v := find.Status; v != nil {- list := []string{}- for _, status := range *v {- list = append(list, fmt.Sprintf("$%d", len(args)+1))- args = append(args, status.String())- }- where = append(where, fmt.Sprintf("task_run.status in (%s)", strings.Join(list, ",")))- }-- rows, err := s.GetDB().QueryContext(ctx, fmt.Sprintf(`+ q := qb.Q().Space(`SELECTtask_run.id,task_run.creator_id,@@ -117,10 +91,40 @@ func (s *Store) ListTaskRunsV2(ctx context.Context, find *FindTaskRunMessage) ([LEFT JOIN task ON task.id = task_run.task_idLEFT JOIN pipeline ON pipeline.id = task.pipeline_idLEFT JOIN project ON project.resource_id = pipeline.project- WHERE %s- ORDER BY task_run.id ASC`, strings.Join(where, " AND ")),- args...,- )+ WHERE TRUE+ `)++ if v := find.UID; v != nil {+ q.And("task_run.id = ?", *v)+ }+ if v := find.UIDs; v != nil {+ q.And("task_run.id = ANY(?)", *v)+ }+ if v := find.TaskUID; v != nil {+ q.And("task_run.task_id = ?", *v)+ }+ if v := find.Environment; v != nil {+ q.And("task.environment = ?", *v)+ }+ if v := find.PipelineUID; v != nil {+ q.And("task.pipeline_id = ?", *v)+ }+ if v := find.Status; v != nil {+ var statusStrings []string+ for _, status := range *v {+ statusStrings = append(statusStrings, status.String())+ }+ q.And("task_run.status = ANY(?)", statusStrings)+ }++ q.Space("ORDER BY task_run.id ASC")++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := s.GetDB().QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -213,10 +217,14 @@ func (s *Store) UpdateTaskRunStatus(ctx context.Context, patch *TaskRunStatusPat}// Get the pipeline ID for cache invalidation+ q := qb.Q().Space("SELECT pipeline_id FROM task WHERE id = ?", taskRun.TaskUID)+ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+var pipelineID int- if err := tx.QueryRowContext(ctx, `- SELECT pipeline_id FROM task WHERE id = $1- `, taskRun.TaskUID).Scan(&pipelineID); err != nil {+ if err := tx.QueryRowContext(ctx, sql, args...).Scan(&pipelineID); err != nil {return nil, errors.Wrapf(err, "failed to get pipeline ID for task %d", taskRun.TaskUID)}@@ -232,13 +240,20 @@ func (s *Store) UpdateTaskRunStatus(ctx context.Context, patch *TaskRunStatusPatfunc (s *Store) UpdateTaskRunStartAt(ctx context.Context, taskRunID int) error {// Get the pipeline ID for cache invalidation- var pipelineID int- if err := s.GetDB().QueryRowContext(ctx, `+ q := qb.Q().Space(`UPDATE task_runSET started_at = now(), updated_at = now()- WHERE id = $1+ WHERE id = ?RETURNING (SELECT pipeline_id FROM task WHERE task.id = task_run.task_id)- `, taskRunID).Scan(&pipelineID); err != nil {+ `, taskRunID)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ var pipelineID int+ if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(&pipelineID); err != nil {return errors.Wrapf(err, "failed to update task run start at")}@@ -294,16 +309,21 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creatorID int, create}func (*Store) getTaskNextAttempt(ctx context.Context, txn *sql.Tx, taskIDs []int) ([]int, error) {- query := `- WITH tasks AS (- SELECT id FROM unnest(CAST($1 AS INTEGER[])) AS id- )- SELECT- (SELECT COALESCE(MAX(attempt)+1, 0) FROM task_run WHERE task_run.task_id = tasks.id)- FROM tasks ORDER BY tasks.id ASC;- `-- rows, err := txn.QueryContext(ctx, query, taskIDs)+ q := qb.Q().Space(`+ WITH tasks AS (+ SELECT id FROM unnest(CAST(? AS INTEGER[])) AS id+ )+ SELECT+ (SELECT COALESCE(MAX(attempt)+1, 0) FROM task_run WHERE task_run.task_id = tasks.id)+ FROM tasks ORDER BY tasks.id ASC+ `, taskIDs)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := txn.QueryContext(ctx, query, args...)if err != nil {return nil, errors.Wrap(err, "failed to query")}@@ -337,7 +357,7 @@ func (*Store) createPendingTaskRunsTx(ctx context.Context, txn *sql.Tx, creatorIrunAts = append(runAts, create.RunAt)}- query := `+ q := qb.Q().Space(`INSERT INTO task_run (creator_id,task_id,@@ -346,40 +366,46 @@ func (*Store) createPendingTaskRunsTx(ctx context.Context, txn *sql.Tx, creatorIattempt,status) SELECT- $1,- unnest(CAST($2 AS INTEGER[])),- unnest(CAST($3 AS INTEGER[])),- unnest(CAST($4 AS TIMESTAMPTZ[])),- unnest(CAST($5 AS INTEGER[])),- $6- `- if _, err := txn.ExecContext(ctx, query,- creatorID,- taskUIDs,- sheetUIDs,- runAts,- attempts,- storepb.TaskRun_PENDING.String(),- ); err != nil {+ ?,+ unnest(CAST(? AS INTEGER[])),+ unnest(CAST(? AS INTEGER[])),+ unnest(CAST(? AS TIMESTAMPTZ[])),+ unnest(CAST(? AS INTEGER[])),+ ?+ `, creatorID, taskUIDs, sheetUIDs, runAts, attempts, storepb.TaskRun_PENDING.String())++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ if _, err := txn.ExecContext(ctx, query, args...); err != nil {return errors.Wrapf(err, "failed to create pending task runs")}return nil}func (*Store) checkTaskRunsExist(ctx context.Context, txn *sql.Tx, taskIDs []int, statuses []storepb.TaskRun_Status) (bool, error) {- query := `- SELECT EXISTS (- SELECT 1- FROM task_run- WHERE task_run.task_id = ANY($1) AND task_run.status = ANY($2)- )`-- var exist boolvar statusStrings []stringfor _, status := range statuses {statusStrings = append(statusStrings, status.String())}- if err := txn.QueryRowContext(ctx, query, taskIDs, statusStrings).Scan(&exist); err != nil {++ q := qb.Q().Space(`+ SELECT EXISTS (+ SELECT 1+ FROM task_run+ WHERE task_run.task_id = ANY(?) AND task_run.status = ANY(?)+ )+ `, taskIDs, statusStrings)++ query, args, err := q.ToSQL()+ if err != nil {+ return false, errors.Wrapf(err, "failed to build sql")+ }++ var exist bool+ if err := txn.QueryRowContext(ctx, query, args...).Scan(&exist); err != nil {return false, errors.Wrapf(err, "failed to query if task runs exist")}@@ -388,32 +414,33 @@ func (*Store) checkTaskRunsExist(ctx context.Context, txn *sql.Tx, taskIDs []int// patchTaskRunStatusImpl updates a taskRun status. Returns the new state of the taskRun after update.func (*Store) patchTaskRunStatusImpl(ctx context.Context, txn *sql.Tx, patch *TaskRunStatusPatch) (*TaskRunMessage, error) {- set, args := []string{"updated_at = $1", "status = $2"}, []any{time.Now(), patch.Status.String()}+ set := qb.Q()++ set.Comma("updated_at = ?, status = ?", time.Now(), patch.Status.String())+if v := patch.Code; v != nil {- set, args = append(set, fmt.Sprintf("code = $%d", len(args)+1)), append(args, *v)+ set.Comma("code = ?", *v)}if v := patch.Result; v != nil {result := "{}"if *v != "" {result = *v}- set, args = append(set, fmt.Sprintf("result = $%d", len(args)+1)), append(args, result)+ set.Comma("result = ?", result)}- // Build WHERE clause.- where := []string{"TRUE"}- where, args = append(where, fmt.Sprintf("id = $%d", len(args)+1)), append(args, patch.ID)+ q := qb.Q().Space("UPDATE task_run SET ?", set).+ Space("WHERE id = ?", patch.ID).+ Space("RETURNING id, creator_id, created_at, updated_at, task_id, status, code, result")++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }var taskRun TaskRunMessagevar statusString string- if err := txn.QueryRowContext(ctx, `- UPDATE task_run- SET `+strings.Join(set, ", ")+`- WHERE `+strings.Join(where, " AND ")+`- RETURNING id, creator_id, created_at, updated_at, task_id, status, code, result- `,- args...,- ).Scan(+ if err := txn.QueryRowContext(ctx, query, args...).Scan(&taskRun.ID,&taskRun.CreatorID,&taskRun.CreatedAt,@@ -457,27 +484,7 @@ func (s *Store) ListTaskRun(ctx context.Context, find *TaskRunFind) ([]*TaskRunM}func (*Store) findTaskRunImpl(ctx context.Context, txn *sql.Tx, find *TaskRunFind) ([]*TaskRunMessage, error) {- where, args := []string{"TRUE"}, []any{}- if v := find.TaskID; v != nil {- where, args = append(where, fmt.Sprintf("task_run.task_id = $%d", len(args)+1)), append(args, *v)- }- if v := find.Environment; v != nil {- where, args = append(where, fmt.Sprintf("task.environment = $%d", len(args)+1)), append(args, *v)- }- if v := find.PipelineID; v != nil {- where, args = append(where, fmt.Sprintf("task.pipeline_id = $%d", len(args)+1)), append(args, *v)- }-- if v := find.StatusList; v != nil {- list := []string{}- for _, status := range *v {- list = append(list, fmt.Sprintf("$%d", len(args)+1))- args = append(args, status)- }- where = append(where, fmt.Sprintf("task_run.status in (%s)", strings.Join(list, ",")))- }-- rows, err := txn.QueryContext(ctx, fmt.Sprintf(`+ q := qb.Q().Space(`SELECTtask_run.id,task_run.creator_id,@@ -491,10 +498,34 @@ func (*Store) findTaskRunImpl(ctx context.Context, txn *sql.Tx, find *TaskRunFintask.environmentFROM task_runJOIN task ON task.id = task_run.task_id- WHERE %s- ORDER BY task_run.id ASC`, strings.Join(where, " AND ")),- args...,- )+ WHERE TRUE+ `)++ if v := find.TaskID; v != nil {+ q.And("task_run.task_id = ?", *v)+ }+ if v := find.Environment; v != nil {+ q.And("task.environment = ?", *v)+ }+ if v := find.PipelineID; v != nil {+ q.And("task.pipeline_id = ?", *v)+ }+ if v := find.StatusList; v != nil {+ var statusValues []any+ for _, status := range *v {+ statusValues = append(statusValues, status)+ }+ q.And("task_run.status = ANY(?)", statusValues)+ }++ q.Space("ORDER BY task_run.id ASC")++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ rows, err := txn.QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -534,12 +565,19 @@ func (s *Store) BatchCancelTaskRuns(ctx context.Context, taskRunIDs []int) error}// Get affected pipeline IDs for cache invalidation- rows, err := s.GetDB().QueryContext(ctx, `+ q := qb.Q().Space(`SELECT DISTINCT task.pipeline_idFROM task_runJOIN task ON task.id = task_run.task_id- WHERE task_run.id = ANY($1)+ WHERE task_run.id = ANY(?)`, taskRunIDs)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ rows, err := s.GetDB().QueryContext(ctx, query, args...)if err != nil {return errors.Wrapf(err, "failed to get pipeline IDs")}@@ -557,11 +595,18 @@ func (s *Store) BatchCancelTaskRuns(ctx context.Context, taskRunIDs []int) errorreturn errors.Wrapf(err, "failed to iterate pipeline IDs")}- query := `+ q2 := qb.Q().Space(`UPDATE task_run- SET status = $1, updated_at = now()- WHERE id = ANY($2)`- if _, err := s.GetDB().ExecContext(ctx, query, storepb.TaskRun_CANCELED.String(), taskRunIDs); err != nil {+ SET status = ?, updated_at = now()+ WHERE id = ANY(?)+ `, storepb.TaskRun_CANCELED.String(), taskRunIDs)++ query2, args2, err := q2.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ if _, err := s.GetDB().ExecContext(ctx, query2, args2...); err != nil {return err}diff --git a/backend/store/task_run_log.go b/backend/store/task_run_log.goindex 1f585693e4..e4ff844f2b 100644--- a/backend/store/task_run_log.go+++ b/backend/store/task_run_log.go@@ -10,6 +10,7 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -25,39 +26,51 @@ func (s *Store) CreateTaskRunLogS(ctx context.Context, taskRunUID int, t time.Ti}func (s *Store) CreateTaskRunLog(ctx context.Context, taskRunUID int, t time.Time, deployID string, e *storepb.TaskRunLog) error {- query := `+ e.DeployId = deployID+ p, err := protojson.Marshal(e)+ if err != nil {+ return errors.Wrapf(err, "failed to marshal task run log")+ }++ q := qb.Q().Space(`INSERT INTO task_run_log (task_run_id,created_at,payload) VALUES (- $1,- $2,- $3+ ?,+ ?,+ ?)- `- e.DeployId = deployID- p, err := protojson.Marshal(e)+ `, taskRunUID, t, p)++ sql, args, err := q.ToSQL()if err != nil {- return errors.Wrapf(err, "failed to marshal task run log")+ return errors.Wrapf(err, "failed to build sql")}- if _, err := s.GetDB().ExecContext(ctx, query, taskRunUID, t, p); err != nil {++ if _, err := s.GetDB().ExecContext(ctx, sql, args...); err != nil {return errors.Wrapf(err, "failed to create task run log")}return nil}func (s *Store) ListTaskRunLogs(ctx context.Context, taskRunUID int) ([]*TaskRunLog, error) {- query := `+ q := qb.Q().Space(`SELECTcreated_at,payloadFROM task_run_log- WHERE task_run_log.task_run_id = $1+ WHERE task_run_log.task_run_id = ?ORDER BY id- `+ `, taskRunUID)++ sql, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }- rows, err := s.GetDB().QueryContext(ctx, query, taskRunUID)+ rows, err := s.GetDB().QueryContext(ctx, sql, args...)if err != nil {return nil, errors.Wrapf(err, "failed to query task run log")}diff --git a/backend/store/worksheet.go b/backend/store/worksheet.goindex 06bf6c2a90..ad245a3925 100644--- a/backend/store/worksheet.go+++ b/backend/store/worksheet.go@@ -4,13 +4,13 @@ import ("context""database/sql""fmt"- "strings""time""github.com/pkg/errors""google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -88,28 +88,12 @@ func (s *Store) GetWorkSheet(ctx context.Context, find *FindWorkSheetMessage, cu// ListWorkSheets returns a list of sheets.func (s *Store) ListWorkSheets(ctx context.Context, find *FindWorkSheetMessage, currentPrincipalID int) ([]*WorkSheetMessage, error) {- where, args := []string{"TRUE"}, []any{}- if filter := find.Filter; filter != nil {- where = append(where, filter.Where)- args = append(args, filter.Args...)- }-- if v := find.UID; v != nil {- where, args = append(where, fmt.Sprintf("worksheet.id = $%d", len(args)+1)), append(args, *v)- }-statementField := fmt.Sprintf("LEFT(worksheet.statement, %d)", common.MaxSheetSize)if find.LoadFull {statementField = "worksheet.statement"}- tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})- if err != nil {- return nil, err- }- defer tx.Rollback()-- rows, err := tx.QueryContext(ctx, fmt.Sprintf(`+ q := qb.Q().Space(fmt.Sprintf(`SELECTworksheet.id,worksheet.creator_id,@@ -125,9 +109,28 @@ func (s *Store) ListWorkSheets(ctx context.Context, find *FindWorkSheetMessage,COALESCE(worksheet_organizer.starred, FALSE)FROM worksheetLEFT JOIN worksheet_organizer ON worksheet_organizer.worksheet_id = worksheet.id AND worksheet_organizer.principal_id = %d- WHERE %s`, statementField, currentPrincipalID, strings.Join(where, " AND ")),- args...,- )+ WHERE TRUE`, statementField, currentPrincipalID))++ if filter := find.Filter; filter != nil {+ q.And(ConvertDollarPlaceholders(filter.Where), filter.Args...)+ }++ if v := find.UID; v != nil {+ q.And("worksheet.id = ?", *v)+ }++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }++ tx, err := s.GetDB().BeginTx(ctx, &sql.TxOptions{ReadOnly: true})+ if err != nil {+ return nil, err+ }+ defer tx.Rollback()++ rows, err := tx.QueryContext(ctx, query, args...)if err != nil {return nil, err}@@ -172,7 +175,7 @@ func (s *Store) CreateWorkSheet(ctx context.Context, create *WorkSheetMessage) (return nil, err}- query := `+ q := qb.Q().Space(`INSERT INTO worksheet (creator_id,project,@@ -183,25 +186,21 @@ func (s *Store) CreateWorkSheet(ctx context.Context, create *WorkSheetMessage) (visibility,payload)- VALUES ($1, $2, $3, $4, $5, $6, $7, $8)+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)RETURNING id, created_at, updated_at, OCTET_LENGTH(statement)- `+ `, create.CreatorID, create.ProjectID, create.InstanceID, create.DatabaseName, create.Title, create.Statement, create.Visibility, payload)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }tx, err := s.GetDB().BeginTx(ctx, nil)if err != nil {return nil, err}defer tx.Rollback()- if err := tx.QueryRowContext(ctx, query,- create.CreatorID,- create.ProjectID,- create.InstanceID,- create.DatabaseName,- create.Title,- create.Statement,- create.Visibility,- payload,- ).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&create.UID,&create.CreatedAt,&create.UpdatedAt,@@ -244,10 +243,21 @@ func (s *Store) DeleteWorkSheet(ctx context.Context, sheetUID int) error {}defer tx.Rollback()- if _, err := tx.ExecContext(ctx, `DELETE FROM worksheet_organizer WHERE worksheet_id = $1`, sheetUID); err != nil {+ q1 := qb.Q().Space(`DELETE FROM worksheet_organizer WHERE worksheet_id = ?`, sheetUID)+ query1, args1, err := q1.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query1, args1...); err != nil {return err}- if _, err := tx.ExecContext(ctx, `DELETE FROM worksheet WHERE id = $1`, sheetUID); err != nil {++ q2 := qb.Q().Space(`DELETE FROM worksheet WHERE id = ?`, sheetUID)+ query2, args2, err := q2.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := tx.ExecContext(ctx, query2, args2...); err != nil {return err}@@ -256,30 +266,29 @@ func (s *Store) DeleteWorkSheet(ctx context.Context, sheetUID int) error {// patchWorkSheetImpl updates a sheet's name/statement/visibility/instance/db_name/project.func patchWorkSheetImpl(ctx context.Context, txn *sql.Tx, patch *PatchWorkSheetMessage) error {- set, args := []string{"updated_at = $1"}, []any{time.Now()}+ set := qb.Q()+ set.Comma("updated_at = ?", time.Now())if v := patch.Title; v != nil {- set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)+ set.Comma("name = ?", *v)}if v := patch.Statement; v != nil {- set, args = append(set, fmt.Sprintf("statement = $%d", len(args)+1)), append(args, *v)+ set.Comma("statement = ?", *v)}if v := patch.Visibility; v != nil {- set, args = append(set, fmt.Sprintf("visibility = $%d", len(args)+1)), append(args, *v)+ set.Comma("visibility = ?", *v)}if v := patch.InstanceID; v != nil {- set, args = append(set, fmt.Sprintf("instance = $%d", len(args)+1)), append(args, *v)+ set.Comma("instance = ?", *v)}if v := patch.DatabaseName; v != nil {- set, args = append(set, fmt.Sprintf("db_name = $%d", len(args)+1)), append(args, *v)+ set.Comma("db_name = ?", *v)}- args = append(args, patch.UID)- query := fmt.Sprintf(`- UPDATE worksheet- SET `+strings.Join(set, ", ")+`- WHERE id = $%d`, len(args))- if _, err := txn.ExecContext(ctx, query, args...,- ); err != nil {+ query, args, err := qb.Q().Space("UPDATE worksheet SET ? WHERE id = ?", set, patch.UID).ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }+ if _, err := txn.ExecContext(ctx, query, args...); err != nil {return err}return nil@@ -303,23 +312,25 @@ func (s *Store) UpsertWorksheetOrganizer(ctx context.Context, organizer *Workshe}defer tx.Rollback()- query := `+ q := qb.Q().Space(`INSERT INTO worksheet_organizer (worksheet_id,principal_id,starred)- VALUES ($1, $2, $3)+ VALUES (?, ?, ?)ON CONFLICT(worksheet_id, principal_id) DO UPDATE SETstarred = EXCLUDED.starredRETURNING id, worksheet_id, principal_id, starred- `+ `, organizer.WorksheetUID, organizer.PrincipalUID, organizer.Starred)++ query, args, err := q.ToSQL()+ if err != nil {+ return nil, errors.Wrapf(err, "failed to build sql")+ }+var worksheetOrganizer WorksheetOrganizerMessage- if err := tx.QueryRowContext(ctx, query,- organizer.WorksheetUID,- organizer.PrincipalUID,- organizer.Starred,- ).Scan(+ if err := tx.QueryRowContext(ctx, query, args...).Scan(&worksheetOrganizer.UID,&worksheetOrganizer.WorksheetUID,&worksheetOrganizer.PrincipalUID,