Masterminds / squirrel

Fluent SQL generation for golang
Other
7.01k stars 465 forks source link

Add USING clause to delete builder #361

Closed ejjdejong closed 1 year ago

ejjdejong commented 1 year ago

Adds possibility to use the USING keyword in a delete query. This way, you can add an extra security check before deleting a record. For example, the table you want to delete from is has an ID as foreign key, but you want to check on a unique identifier as well, which is only present in the referenced table:

DELETE FROM playlist
USING song
WHERE 
    playlist.song_id = song.id AND
    song.id = 123 AND
    song.uuid = 'some-song-uuid'

You can use either one or multiple tables in the Using() builder:

sql, args, err := Delete("playlist").
Using("song", "user").
Where(And{
    Eq{"playlist.song_id": "song.id"},
    Eq{"playlist.user_id": "user.id"},
    Eq{"song.id": 123},
    Eq{"song.uuid": "some-song-uuid"},
    Eq{"user.id": 456},
    Eq{"user.uuid": "some-user-uuid"},
}).ToSql()