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

Provide new dateStrings option: "DATE" #1481

Closed
wants to merge 7 commits into from
Closed

Conversation

bbito
Copy link
Contributor

@bbito bbito commented Jul 21, 2016

Adding checks to allow true OR "DATE" as dateStrings values "DATE" should only effect DATE fields whereas true retains original behavior, effecting DATE, DATETIME and TIMESTAMP fields. See: #605 , loopbackio/loopback-connector-mysql#120 , loopbackio/loopback-connector-mysql#149

@bbito
Copy link
Contributor Author

bbito commented Jul 21, 2016

My apologies, I didn't mean to leave that big comment block in there! I'll clean that up in 12 hours.

@bbito
Copy link
Contributor Author

bbito commented Jul 21, 2016

This hopes to be a low-impact method to satisfy users wishing to handle only DATE fields as strings while retaining the default behavior for TIMESTAMP and DATETIME fields and providing backwards compatibility with existing code. Another and perhaps more efficient approach would be to add yet another property to connection.config with boolean options, e.g. connection.config.dateOnlyStrings.

Copy link
Member

@dougwilson dougwilson left a comment

Choose a reason for hiding this comment

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

Please add tests for the added functionality :) !

@@ -61,7 +61,7 @@ function typeCast(field, parser, timeZone, supportBigNumbers, bigNumberStrings,
case Types.DATETIME2:
case Types.NEWDATE:
var dateString = parser.parseLengthCodedString();
if (dateStrings) {
if (dateStrings === true || field.type === Types.DATE && dateStrings.toString().toLowerCase() === "date") {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this is backwards-compatible, because code today with dateStrings: 1 will no longer work after this change, since we allowed any truthy value before to mean date strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback, I'll look at supporting any truthy value - except dateStrings.toString().toLowerCase() === "date" - for backward compatible behavior and also look to adding the requested tests.
I may be back to ask for help on setting up the test.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good! You may want to rebase your changes to the current master as well, as I just saw the GitHub is saying the PR is not mergable due to conflicts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dougwilson , I was hoping to find an existing test for the current dateStrings connection option to either add to or use as a base, but I'm not finding it. Does such a test exist?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RE: Requested tests, @dougwilson could you take a quick look at the more exhaustive tests on my branch: https://github.com/bbito/node-mysql/blob/test-datestrings-options/test/integration/connection/test-datestrings-options.js and let me know if I should merge that into this PR?

Copy link
Member

Choose a reason for hiding this comment

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

Hi @bbito sorry I haven't had the time to revisit this PR just yet. As for additional tests, I'm all for it :) If you are making tests just as general additions to test the functionality that already exists, that is awesome! It would be nice to have it as a separate PR, ideally, but you can add it to this PR if you like.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As it stands the new test file will fail without the rest of the PR, so I'll merge it for now and if the PR is rejected and you want the test for the current code just let me know and I'll strip out the dateStrings: 'date' stuff and submit it as a separate PR.

@bbito
Copy link
Contributor Author

bbito commented Sep 29, 2016

I feel like I've got a good PR now and an additional improvement for ths, more backwards compatible code is that as in the pre-feature code, there is a simple evaluation for truthiness of dateStrings and with this PR, now the additional checks only affect projects with a truthy setting for dateStrings. My earlier attempt would impact performance of both truthy and non-truthy or unset dateStrings configurations.

  • So thanks for prodding for that change!
    Now for testing, @dougwilson - is there an example you might point me to that performs a similar test for different connection options?

@dougwilson
Copy link
Member

I can't tell what is in the PR anymore, as it seems like the GitHub changes tab is not working and showing a while lot of other changes: https://github.com/mysqljs/mysql/pull/1481/files

@bbito
Copy link
Contributor Author

bbito commented Sep 29, 2016

Wow, I wouldn't doubt that I messed up the rebase (my first one!), but I think I would have noticed it was now showing 33 files changed instead of 2! I'll see if I can figure out what went haywire!

@dougwilson
Copy link
Member

is there an example you might point me to that performs a similar test for different connection options?

All you need to do is create the connection with option you are trying to test in the test. THe current main test that is supposed to be testing all the type casting from the database to JavaScript is https://github.com/mysqljs/mysql/blob/c19c867d032a89263dc8717a84596275b3e4dfa5/test/integration/connection/test-type-casting.js

@bbito
Copy link
Contributor Author

bbito commented Sep 29, 2016

Thanks @dougwilson , I'll look a that existing test.
I'm a bit stumped about the status of the PR... If I look at my repo it looks as I'd expect with 13 commits and 2 files changed: master...bbito:master

@dougwilson
Copy link
Member

Weird. I have a comment I want to make, but not sure if it'll get lost or not. Can you do like a squash rebase on top of the current master, perhaps? The following commands may get you out of the hole:

$ git fetch origin
$ git reset --hard origin/master
$ git checkout HEAD@{1} -- .
$ git commit -a
# validate the current commit contains all your changes
$ git push -f

@bbito
Copy link
Contributor Author

bbito commented Sep 30, 2016

Thanks for the git help - I'll try that now

@dougwilson
Copy link
Member

Sorry, I forgot you're a fork :) The first two should be the name of your remote instead of origin. By default, this is usually upstream:

$ git fetch upstream
$ git reset --hard upstream/master

@bbito
Copy link
Contributor Author

bbito commented Sep 30, 2016

Okay, commits squashed down to 1 and back to 2 files changed as hoped - thanks.
Coding is in addition to full-time+ job, so it may take a day or two to get the test going.

Add 'date' as dateStrings value option. The connection option dateStrings: 'date' will only affect DATE fields whereas any truthy value (except 'date') retains original behavior, effecting DATE, DATETIME and TIMESTAMP fields. See: mysqljs#605 , loopbackio/loopback-connector-mysql#120 , loopbackio/loopback-connector-mysql#149
This test confirms that DATETIME and TIMESTAMP columns are still
exhibiting the default behavior, accepting and returning javascript
date objects while DATE columns will accept and return a string value.
@bbito
Copy link
Contributor Author

bbito commented Oct 2, 2016

Test added, please let me know if there are any additional tasks to sew up this PR.

@bbito
Copy link
Contributor Author

bbito commented Oct 3, 2016

Hi @dougwilson , I have a new test that I haven't pushed yet (test-datestrings-options.js), that loops through various truthy, falsy and 'date'-ish values for dateStrings:. Should I delete test-datestrings-option-date.js and add test-datestrings-options.js?

This test loops through various truthy, falsy and 'date'-ish values
for connection option dateStrings: and runs corresponding typecasting
checks.
@bbito
Copy link
Contributor Author

bbito commented Oct 3, 2016

@dougwilson I stuck the test-datestrings-options.js test onto a branch: https://github.com/bbito/node-mysql/blob/test-datestrings-options/test/integration/connection/test-datestrings-options.js
Just let me know if I should merge into master.

Add tests where JavaScript Date objects are supplied when
dateStrings: truthy or 'date' and check that strings are returned
as well as supplying strings when dateStrings: falsy and check that
JavaScript Date objects are returned.
@bbito
Copy link
Contributor Author

bbito commented Oct 4, 2016

Latest commit in test-datestrings-options branch includes 'mis-matched' tests - that is supplying JavaScript Date objects to the module when dateStrings: is truthy or 'date' and conversely supplying strings when dateStrings: is falsy. Although I don't think it is clearly documented, it seems that it is by design that this behavior is supported.

@dougwilson
Copy link
Member

I swear I though I replied here at some point in the last few days, but I guess I'm just making stuff up in my mind :) Anyway, I need to take a look. Seems every time I turn around, more stuff is added to this PR, so please bear with me in trying to look it over :)

@@ -62,7 +62,11 @@ function typeCast(field, parser, timeZone, supportBigNumbers, bigNumberStrings,
case Types.NEWDATE:
var dateString = parser.parseLengthCodedString();
if (dateStrings) {
return dateString;
if (field.type === Types.DATE) {
Copy link
Member

Choose a reason for hiding this comment

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

Should this also include Types.NEWDATE? I believe that is a date string with no time component as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm happy to include it. When I first started poking into this module I found NEWDATE in the constants: https://github.com/mysqljs/mysql/blob/master/lib/protocol/constants/types.js , but I couldn't find it in mysql docs. Today I find 2 references to it in current docs: http://dev.mysql.com/doc/connector-net/en/connector-net-ref-mysqlclient-mysqlcommandmembers.html

Newdate: Obsolete. Use Datetime or Date type.

http://dev.mysql.com/doc/connectors/en/connector-net-ref-mysqlclient-mysqlcommand.html

Newdate: Obsolete. Use Datetime or Date type.

So unless there is a decision for this module to remove support for this obsolete Member/Type and it is removed from the switch block in this file, I think I should amend this PR to support it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Types.NEWDATE support added with Oct. 7 2016 commit: 975b2ae

@bbito
Copy link
Contributor Author

bbito commented Oct 18, 2016

@dougwilson Is there anything more you want for this PR?

@cfilby
Copy link

cfilby commented Oct 27, 2016

Is there anything I could do to assist with this? Seems like the PR is just about done, but I'm definitely interested in this functionality and would be happy to help out.

@dougwilson
Copy link
Member

This is on my backlog, but is low priority since it doesn't add any functionality that is not already in this module; it simply adds some sugar to what you can already do using a custom typecast.

@bbito
Copy link
Contributor Author

bbito commented Oct 27, 2016

@dougwilson in my scenario I don't think I have the ability to pass a custom typecast because I'm not using this module directly, but rather through Strongloop/Loopback: https://github.com/strongloop/loopback.
In this scenario I can pass connection options through the \server\datasources.json configuration file, but I haven't found another way to customize a connection in Loopback.

@dougwilson
Copy link
Member

Hi @bbito, you may also want to make a PR to that wrapper in parallel to this PR, because the typecast is a very essential feature. I add things to this module from the perspective of using this module; if you are using a wrapper and it's forcing you to alter the module it's wrapping just to use it, you may want to use a different wrapper.

@dougwilson
Copy link
Member

Hi @bbito thanks for all your hard work. I am working on merging this now, for inclusion in the next release. The API would be an array similar to debug rather than just a string, thus dateStrings: 'DATE' would become dateStrings: ['DATE'], which allows people to fine tune whatever types they want casted or not for whatever reason.

@dougwilson dougwilson closed this in 5cc6c68 Nov 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants