Skip to content

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

Use QueryParams for formatting where clauses in prepared statement.

spinningboteb5144b39a6f
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index c063b1b885..ab0b77b5a8 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -356,3 +356,49 @@ func Open(dbType Type, driverConfig DriverConfig, connectionConfig ConnectionCon
return driver, nil
}
+
+// QueryParams is a list of query parameters for prepared query.
+type QueryParams struct {
+ DatabaseType Type
+ Names []string
+ Params []interface{}
+}
+
+// AddParam adds a parameter to QueryParams.
+func (p *QueryParams) AddParam(name string, param interface{}) {
+ p.Names = append(p.Names, name)
+ p.Params = append(p.Params, param)
+}
+
+// QueryString returns the query where clause string for the query parameters.
+func (p *QueryParams) QueryString() string {
+ mysqlQuery := func(params []string) string {
+ if len(params) == 0 {
+ return ""
+ }
+ var parts []string
+ for _, param := range params {
+ parts = append(parts, fmt.Sprintf("%s = ?", param))
+ }
+ return fmt.Sprintf("WHERE %s ", strings.Join(parts, " AND "))
+ }
+ pgQuery := func(params []string) string {
+ if len(params) == 0 {
+ return ""
+ }
+ var parts []string
+ for i, param := range params {
+ parts = append(parts, fmt.Sprintf("%s=$%v", param, i+1))
+ }
+ return fmt.Sprintf("WHERE %s ", strings.Join(parts, " AND "))
+ }
+ switch p.DatabaseType {
+ case MySQL:
+ return mysqlQuery(p.Names)
+ case TiDB:
+ return mysqlQuery(p.Names)
+ case Postgres:
+ return pgQuery(p.Names)
+ }
+ return ""
+}
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index 4056d36737..634de4ae0b 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -522,7 +522,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
}
func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return util.FindMigrationHistoryList(ctx, driver, find, "bytebase.")
+ return util.FindMigrationHistoryList(ctx, db.MySQL, driver, find, "bytebase.")
}
// Dump and restore
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index d9ac62201f..5ec30c7ff1 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -565,7 +565,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
}
func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return util.FindMigrationHistoryList(ctx, driver, find, "")
+ return util.FindMigrationHistoryList(ctx, db.Postgres, driver, find, "")
}
// Dump and restore
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index e201bf0a66..d203321b7b 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -295,7 +295,7 @@ func findNextSequence(ctx context.Context, tx *sql.Tx, namespace string, require
}
// FindMigrationHistoryList will find the list of migration history.
-func FindMigrationHistoryList(ctx context.Context, driver db.Driver, find *db.MigrationHistoryFind, tablePrefix string) ([]*db.MigrationHistory, error) {
+func FindMigrationHistoryList(ctx context.Context, dbType db.Type, driver db.Driver, find *db.MigrationHistoryFind, tablePrefix string) ([]*db.MigrationHistory, error) {
sqldb, err := driver.GetDbConnection(ctx, bytebaseDatabase)
if err != nil {
return nil, err
@@ -306,45 +306,45 @@ func FindMigrationHistoryList(ctx context.Context, driver db.Driver, find *db.Mi
}
defer tx.Rollback()
- where, args := []string{"1 = 1"}, []interface{}{}
+ queryParams := &db.QueryParams{DatabaseType: dbType}
if v := find.ID; v != nil {
- where, args = append(where, "id = ?"), append(args, *v)
+ // queryParams.AddParam("id", *v)
}
if v := find.Database; v != nil {
- where, args = append(where, "namespace = ?"), append(args, *v)
+ // queryParams.AddParam("namespace", *v)
}
if v := find.Version; v != nil {
- where, args = append(where, "version = ?"), append(args, *v)
+ // queryParams.AddParam("version", *v)
}
var query = `
- SELECT
- id,
+ SELECT
+ id,
created_by,
- created_ts,
- updated_by,
- updated_ts,
+ created_ts,
+ updated_by,
+ updated_ts,
namespace,
sequence,
- ` + "`engine`," + `
- ` + "`type`," + `
- ` + "`status`," + `
+ engine,
+ type,
+ status,
version,
description,
- statement,
- ` + "`schema`," + `
- execution_duration,
+ statement,
+ "schema",
+ execution_duration,
issue_id,
payload
FROM ` +
tablePrefix + `migration_history ` +
- `WHERE ` + strings.Join(where, " AND ") + `
- ORDER BY created_ts DESC`
+ queryParams.QueryString() +
+ `ORDER BY created_ts DESC`
if v := find.Limit; v != nil {
query += fmt.Sprintf(" LIMIT %d", *v)
}
- rows, err := tx.QueryContext(ctx, query, args...)
+ rows, err := tx.QueryContext(ctx, query, queryParams.Params...)
if err != nil {
return nil, FormatErrorWithQuery(err, query)
}