Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add support for LIMIT query in UPDATE for sql/sqlite #452

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Hasaber8
Copy link

@Hasaber8 Hasaber8 commented Feb 8, 2025

Closes #448

@Hasaber8
Copy link
Author

Hasaber8 commented Feb 8, 2025

Fixed the failing tests.

* For multiple-table syntax, ORDER BY and LIMIT cannot be used.
Copy link

codecov bot commented Feb 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.28%. Comparing base (00b8155) to head (be13aae).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #452      +/-   ##
==========================================
+ Coverage   91.26%   91.28%   +0.01%     
==========================================
  Files         134      134              
  Lines        7765     7782      +17     
==========================================
+ Hits         7087     7104      +17     
  Misses        509      509              
  Partials      169      169              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Hasaber8
Copy link
Author

Hasaber8 commented Feb 12, 2025

@go-jet Ping on this^, can I get some review comments so I can refactor the change if needed.

@@ -12,15 +12,19 @@ type UpdateStatement interface {
MODEL(data interface{}) UpdateStatement

WHERE(expression BoolExpression) UpdateStatement
LIMIT(limit int64) UpdateStatement
INNER_JOIN(table ReadableTable, onCondition BoolExpression) UpdateStatement
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi table update is already supported, check this test:

func TestUpdateWithJoin(t *testing.T) {
statement := Staff.INNER_JOIN(Address, Address.AddressID.EQ(Staff.AddressID)).
UPDATE(Staff.LastName).
SET(String("New staff name")).
WHERE(Staff.StaffID.EQ(Int(1)))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this.

@@ -76,3 +84,11 @@ func (u *updateStatementImpl) RETURNING(projections ...Projection) UpdateStateme
u.Returning.ProjectionList = projections
return u
}

func (u *updateStatementImpl) LIMIT(limit int64) UpdateStatement {
if u.hasFrom {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if len(u.From.Tables) > 0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -64,6 +69,9 @@ func (u *updateStatementImpl) MODEL(data interface{}) UpdateStatement {

func (u *updateStatementImpl) FROM(tables ...ReadableTable) UpdateStatement {
u.From.Tables = readableTablesToSerializerList(tables)
u.hasFrom = true
// Remove any existing LIMIT since it's not allowed with FROM
u.Limit.Count = -1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would introduce hidden behavior. It's better to fail at runtime rather than silently remove clauses.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a straight panic for this.

}

func TestUpdateWithMultiTableAndLimit(t *testing.T) {
defer func() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use testutils.AssertPanicErr to catch and compare panic errors.

@@ -23,6 +24,8 @@ type updateStatementImpl struct {
SetNew jet.SetClauseNew
Where jet.ClauseWhere
Returning jet.ClauseReturning
Limit jet.ClauseLimit
hasFrom bool
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasFrom can be removed.


func (u *updateStatementImpl) LIMIT(limit int64) UpdateStatement {
if len(u.From.Tables) > 1 {
panic("jet: SQLite does not support LIMIT with multi-table UPDATE statements")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we verify this? I haven't find any golang sqlite driver that compiles sqlite with SQLITE_ENABLE_UPDATE_DELETE_LIMIT.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I wasn't aware of this. I shall read up on it. Do you have anything in your mind for this?

}

func TestUpdateWithMultiTableAndLimit(t *testing.T) {
defer func() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertPanicErr

WHERE(Link.Name.NOT_EQ(String(""))).
LIMIT(2)

testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Execute can be removed, since panic will occur before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LIMIT ... for UPDATE statements
2 participants