Skip to content

Commit

Permalink
Using a strings builder to build the base of the SQL query
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacobbrewer1 committed Oct 6, 2024
1 parent 89b3ed9 commit ed43ed4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
25 changes: 17 additions & 8 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit ed43ed4

Please sign in to comment.