Skip to content

feat(query-generator): Generate INSERTs using bind parameters - #9371

Closed
gazoakley wants to merge 1 commit into
sequelize:masterfrom
gazoakley:feat-insert-bind-ii
Closed

gazoakley wants to merge 1 commit into
sequelize:masterfrom
gazoakley:feat-insert-bind-ii

Conversation

@gazoakley

@gazoakley gazoakley commented Apr 29, 2018 •

Copy link
Copy Markdown
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

This provides very basic support for using bind parameters when generating INSERT queries and partly addresses #1608. Re-do of #8923

@codecov

codecov Bot commented Apr 29, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #9371 into master will increase coverage by <.01%.
The diff coverage is 96%.

@gazoakley
gazoakley force-pushed the feat-insert-bind-ii branch 3 times, most recently from 2173633 to e86705a Compare April 30, 2018 09:02

@janmeier janmeier left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

First of all, very nice and well worked out PR!

Is there any reason for hiding this behind a flag? I realize enabling it for all inserts would require a fair amount of tests to be rewritten, but I still think it would be a good idea.

Comment thread test/unit/sql/insert.test.js Outdated
};
expectsql(sql.insertQuery(User.tableName, values, User.rawAttributes, options),
{
mssql: 'INSERT INTO [users] ([user],[name]) VALUES ($1,UPPER(N\'bindtest\'));',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ideally I'd like to assert on bind parameters as well. Something along the lines of

expectsql(sql..., {
 query: {
 ....
  }.
  bind: [...]
})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call - I'll do some tweaks shortly

@gazoakley

Copy link
Copy Markdown
Contributor Author

Thanks @janmeier . I've put in a flag out of cautiousness - it also means anyone enabling the flag will be aware of the limitations of the current implementation. It could be enabled by default in a future version.

@janmeier
janmeier requested a review from a team May 9, 2018 14:14
@janmeier

janmeier commented May 9, 2018

Copy link
Copy Markdown
Member

@sequelize/orm WDYT? Should we hide this behind a flag?

@janmeier

janmeier commented May 9, 2018

Copy link
Copy Markdown
Member

What are the limitations exactly? Of the top of my head I can't come up with any other than functions that might cause problems for inserts and you are already handling that

@eseliger

eseliger commented May 9, 2018

Copy link
Copy Markdown
Member

Hmm if this is a flag, we theoretically would have to run the whole integration test suite twice, once enabled and once disabled. If it doesn't break any of the existing tests, I would have no fears to use it without a flag, or am I missing some edge-cases?

@gazoakley

Copy link
Copy Markdown
Contributor Author

Whilst I've named the option/flag bindParams it currently only supports bind parameters for non-bulk INSERTs - I've documented this as a limitation here:

https://github.com/sequelize/sequelize/pull/9371/files#diff-4be2078a125fe364f7198675d51bc688R123

// Generate queries with bind parameters
// Currently only has an effect for non-bulk INSERTs
// - default: false
bindParams: true

Merging this won't add support for bind parameters to everywhere in the query generator, but having this behind a documented option gives users the benefit of bind parameters whilst ensuring they are aware of where it is supported. There's always a possibility to enable this option by default in the future.

I don't think there should be a need to rerun the whole integration test suite twice - there are specific tests that target this option.

@gazoakley
gazoakley force-pushed the feat-insert-bind-ii branch from e86705a to a530a2f Compare May 9, 2018 15:04
@janmeier

janmeier commented May 9, 2018

Copy link
Copy Markdown
Member

I think what Erik is getting at is, that you remove the flag and run the full integration test suite with all its inserts again the new could we would have no problems releasing this without a flag.
If it can run against all our integration tests I'm sure 99% of cases are covered :)

@sushantdhiman

Copy link
Copy Markdown
Contributor

I think we should just remove this flag, even if we only support this for basic insert, there is no point hiding this behind the flag. If this works let's start using this feature.

In next steps we can keep converting more parts of query generator to support bind parameters.

@gazoakley

Copy link
Copy Markdown
Contributor Author

At a brief glance 67 unit tests need updating to use the new format across the different dialects - I haven't looked at updating the integration tests yet. I'll see if I can get some time to make the changes.

@janmeier

janmeier commented May 18, 2018 •

Copy link
Copy Markdown
Member

I've been playing a bit around with this and some changes will be required.

Values don't need to be escaped when used as bind parameters. This applies to range (before: VALUES('[1, 2)'), now: VALUES($x), bind: [1,2)), geometry (before VALUES(ST_GeomFromGeoJSON({ "type": "Point" ...})), now: VALUES(ST_GeomFromGeoJSON($x)), bind: { "type": "Point" ...}) and probably also some other types. I can take a look at this but not sure when I can find the time :)

@gazoakley

Copy link
Copy Markdown
Contributor Author

@janmeier I'd come to the same conclusion. I'm thinking of passing a function as a third param to the _stringify methods that returns a $n placeholder and updates the bind params array.

@janmeier

Copy link
Copy Markdown
Member

@janmeier I'd come to the same conclusion. I'm thinking of passing a function as a third param to the _stringify methods that returns a $n placeholder and updates the bind params array.

Sounds like a good idea! Ping me when you have something ready for review (or let me know if you need my help)

@sushantdhiman

Copy link
Copy Markdown
Contributor

Closing this PR as work is now continued in #9431

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.

4 participants