From ed43ed4c8a0251e6461fa6c8c377c6125b5bb0e9 Mon Sep 17 00:00:00 2001 From: Jacob Brewer Date: Sun, 6 Oct 2024 09:02:17 +0100 Subject: [PATCH] Using a strings builder to build the base of the SQL query --- sql.go | 25 +++++++++++++++++-------- sql_test.go | 8 ++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/sql.go b/sql.go index a013938..5be1090 100644 --- a/sql.go +++ b/sql.go @@ -97,17 +97,26 @@ func (s *SQLPatch) GenerateSQL() (string, []any, error) { return "", nil, fmt.Errorf("validate perform patch: %w", err) } - sqlStr := fmt.Sprintf(` - UPDATE %s - %s - SET %s - WHERE 1 - %s - `, s.table, s.joinSql.String(), strings.Join(s.fields, ", "), s.where.String()) + sqlBuilder := new(strings.Builder) + sqlBuilder.WriteString("UPDATE ") + sqlBuilder.WriteString(s.table) + sqlBuilder.WriteString("\n") + + if s.joinSql.String() != "" { + sqlBuilder.WriteString(s.joinSql.String()) + } + + sqlBuilder.WriteString("SET ") + sqlBuilder.WriteString(strings.Join(s.fields, ", ")) + sqlBuilder.WriteString("\n") + + sqlBuilder.WriteString("WHERE 1\n") + sqlBuilder.WriteString(s.where.String()) + args := append(s.joinArgs, s.args...) args = append(args, s.whereArgs...) - return sqlStr, args, nil + return sqlBuilder.String(), args, nil } func PerformPatch(resource any, opts ...PatchOpt) (sql.Result, error) { diff --git a/sql_test.go b/sql_test.go index 3c1d84a..6d5ca80 100644 --- a/sql_test.go +++ b/sql_test.go @@ -129,7 +129,7 @@ func (s *generateSQLSuite) TestGenerateSQL_Success() { WithWhere(mw), ) s.NoError(err) - s.Equal("\n\t\tUPDATE test_table\n\t\t\n\t\tSET id = ?, name = ?\n\t\tWHERE 1\n\t\tAND age = ?\n\n\t", sqlStr) + s.Equal("UPDATE test_table\nSET id = ?, name = ?\nWHERE 1\nAND age = ?\n", sqlStr) s.Equal([]any{int64(1), "test", 18}, args) mw.AssertExpectations(s.T()) @@ -158,7 +158,7 @@ func (s *generateSQLSuite) TestGenerateSQL_Success_multipleWhere() { WithWhere(mw2), ) s.NoError(err) - s.Equal("\n\t\tUPDATE test_table\n\t\t\n\t\tSET id = ?, name = ?\n\t\tWHERE 1\n\t\tAND age = ?\nAND name = ?\n\n\t", sqlStr) + s.Equal("UPDATE test_table\nSET id = ?, name = ?\nWHERE 1\nAND age = ?\nAND name = ?\n", sqlStr) s.Equal([]any{int64(1), "test", 18, "john"}, args) mw.AssertExpectations(s.T()) @@ -187,7 +187,7 @@ func (s *generateSQLSuite) TestGenerateSQL_Success_withJoin() { WithJoin(mj), ) s.NoError(err) - s.Equal("\n\t\tUPDATE test_table\n\t\tJOIN table2 ON table1.id = table2.id\n\n\t\tSET id = ?, name = ?\n\t\tWHERE 1\n\t\tAND age = ?\n\n\t", sqlStr) + s.Equal("UPDATE test_table\nJOIN table2 ON table1.id = table2.id\nSET id = ?, name = ?\nWHERE 1\nAND age = ?\n", sqlStr) s.Equal([]any{int64(1), "test", 18}, args) mw.AssertExpectations(s.T()) @@ -220,7 +220,7 @@ func (s *generateSQLSuite) TestGenerateSQL_Success_multipleJoin() { WithJoin(mj2), ) s.NoError(err) - s.Equal("\n\t\tUPDATE test_table\n\t\tJOIN table2 ON table1.id = table2.id\nJOIN table3 ON table1.id = table3.id\n\n\t\tSET id = ?, name = ?\n\t\tWHERE 1\n\t\tAND age = ?\n\n\t", sqlStr) + s.Equal("UPDATE test_table\nJOIN table2 ON table1.id = table2.id\nJOIN table3 ON table1.id = table3.id\nSET id = ?, name = ?\nWHERE 1\nAND age = ?\n", sqlStr) s.Equal([]any{int64(1), "test", 18}, args) mw.AssertExpectations(s.T())