Skip to content

To handle cases where an insert statement is created with empty insert data#197

Merged
brianc merged 5 commits into
brianc:masterfrom
paytm:master
Jan 14, 2015
Merged

To handle cases where an insert statement is created with empty insert data#197
brianc merged 5 commits into
brianc:masterfrom
paytm:master

Conversation

@Hiteshm01
Copy link
Copy Markdown
Contributor

Fix for insert statement with empty array without object i.e. []. currently it is issuing select * from table_name in this case.
For array with empty object, it will behave as default.

@brianc
Copy link
Copy Markdown
Owner

brianc commented Jan 2, 2015

Nice! Thanks @Hiteshm01

Would you be able to put together a test case for this?

@Hiteshm01
Copy link
Copy Markdown
Contributor Author

@brianc I found the root cause of it but could only manage a hackish handling in code. Could you please have a look at my latest commit?

@Hiteshm01
Copy link
Copy Markdown
Contributor Author

@brianc , Were you able to verify it?

@brianc
Copy link
Copy Markdown
Owner

brianc commented Jan 14, 2015

Sorry I missed this earlier! Your code looks good to me - there's a merge conflict currently. :( Once you get that resolved I'll happily pull it in! 👍

@Hiteshm01
Copy link
Copy Markdown
Contributor Author

@brianc , conflict resolved.

@brianc
Copy link
Copy Markdown
Owner

brianc commented Jan 14, 2015

Awesome! Thanks! Merging & pushing new patch version now.

brianc added a commit that referenced this pull request Jan 14, 2015
To handle cases where an insert statement is created with empty insert data
@brianc brianc merged commit 8ed6679 into brianc:master Jan 14, 2015
@spion
Copy link
Copy Markdown
Contributor

spion commented Jan 16, 2015

This doesn't always work in pg; will produce a test case

edit: it does actually work, it generates INSERT INTO "post" DEFAULT VALUES.

@Hiteshm01 I don't think this behavior is correct - if an empty array is passed, the query should not try to insert anything at all...

At the very least, this requires a major version bump as it causes existing code that did nothing to try and insert data.

@Hiteshm01
Copy link
Copy Markdown
Contributor Author

@spion , I agree. It should not. But earlier implementation was returning a "select * from table" in this case which was disastrous in any production table. My fix isn't ideal as it is only avoiding trouble.
Suggestions/improvements welcome.

@spion
Copy link
Copy Markdown
Contributor

spion commented Jan 16, 2015

@Hiteshm01 a harmless query like select (1 = 1) would probably be ideal there. Even throwing from node-sql would be better as it would not result with random inserts happening in the DB (which could be even more disastrous - e.g. what if a new AccountState is inserted with the default value of accountId = 1 and money = 0)

In any case, major version (in terms of npm i.e. 0.44) should definitely be bumped.

@Hiteshm01
Copy link
Copy Markdown
Contributor Author

@spion , That will be a better fix but do you think returning a select statement when requested for an insert statement is correct behavior?

@spion
Copy link
Copy Markdown
Contributor

spion commented Jan 16, 2015

Well, not really - its just backward-compatible behavior. The correct backward-incompatible behavior would be to not generate a query.

@Hiteshm01
Copy link
Copy Markdown
Contributor Author

Agreed. I will try to implement it over the weekend.

@Hiteshm01
Copy link
Copy Markdown
Contributor Author

Attempted fix @ #203

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.

3 participants