? ? ? Pending

User tests: Successful: Unsuccessful:

avatar alikon
alikon
25 Aug 2018

Pull Request for Issue
use the db api dateAdd() instead of mysql dialect DATE_SUB()

Summary of Changes

fixed dateAdd() for postgresql & mysql
switched DATE_SUB() to dateAdd() in model articles

Testing Instructions

on Modules: Articles - Category on tab filtering options
enable date filtering
screenshot from 2018-08-25 11-49-12

Expected result

works as before as expected even on mysql

Actual result

don't work on postgresql

avatar alikon alikon - open - 25 Aug 2018
avatar alikon alikon - change - 25 Aug 2018
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 25 Aug 2018
Category Front End com_content Postgresql Libraries
avatar alikon alikon - change - 25 Aug 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - change - 25 Aug 2018
Category Front End com_content Postgresql Libraries Front End com_content Postgresql Libraries Unit Tests
avatar alikon alikon - change - 26 Aug 2018
Labels Added: ?
avatar alikon alikon - change - 26 Aug 2018
The description was changed
avatar alikon alikon - edited - 26 Aug 2018
avatar richard67
richard67 - comment - 26 Aug 2018

Hmm what I don't understand with this PR is: Doesn't DATE_SUB do a subtraction while dateAdd does an addition, and there is no minus in your new code in file components/com_content/models/articles.php? Did you really test if it works the same way as before?

avatar richard67
richard67 - comment - 26 Aug 2018

I have tested this item ? unsuccessfully on a69e062

Tested with patch tester on MySQL.

Before patch I had to enter a positive number (900), after patch a negative number (-900) to have the same result.

For me this change of filter behavior would be ok as it is, but then 1. the testing instructions of this PR, and 2. the documentation for the relative date filtering would have to be changed to reflect this change of behavior of the filter.

The reason for the behavior change is line 464 of file components/com_content/models/articles.php, where it should be $query->dateAdd($nowDate, '-' . $relativeDate, 'DAY') because a DATE_SUB is replaced, which does a subtraction, and not a DATE_ADD, which does an addition.

Hint for other testers: Had to refresh cache because I use module caching and page caching.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67 richard67 - test_item - 26 Aug 2018 - Tested unsuccessfully
avatar richard67
richard67 - comment - 26 Aug 2018

The other thing is what happens if someone choses a negative value. My proposed solution '-' . $relativeDate might not work when $relativeDate is negative.
So maybe this PR does it in the right way.
But it would be a bc break and also would need change of documentation. So maybe do this for 4.0 because of bc break?


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67
richard67 - comment - 26 Aug 2018

P.S.: I tested with MySQL.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar alikon
alikon - comment - 26 Aug 2018

there is no b/c imho , before this pr, it was simply wrong and not cross-database, even don't using the API
but yes true no one cares about different db's so closing

avatar alikon alikon - change - 26 Aug 2018
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2018-08-26 10:11:13
Closed_By alikon
avatar alikon alikon - close - 26 Aug 2018
avatar richard67
richard67 - comment - 26 Aug 2018

@alikon Sorry, I did not want to discourage you from a useful change. I just followed the testing instructions which said everything should work like before on MySQL.

avatar alikon
alikon - comment - 26 Aug 2018

no problem @richard67, please forgive me for this istinctive reaction....

maybe should i've to write everything should work as "expected" ?

avatar alikon alikon - change - 26 Aug 2018
Status Closed New
Closed_Date 2018-08-26 10:11:13
Closed_By alikon
avatar alikon alikon - change - 26 Aug 2018
Status New Pending
avatar alikon alikon - reopen - 26 Aug 2018
avatar richard67
richard67 - comment - 26 Aug 2018

A bit more work but really cool would be to add a function dateSub to the api, and let both dateAdd and dateSub accept negative values so dateAdd for a negative value would be a subtraction, and dateSub for a negative value would be an addition? Then the api functions would work the same like DATE_ADD and DATE_SUB, and then every appearance of DATE_ADD and DATE_SUB could be replaced by the corresponding api function without any change of behavior.

I would understand if you won't go this way because it would be more work.

To me it feels wrong to specify a negative value in the UI, because it does not make sense to search for future intervals, so for me is clear that "30" means "from now on until 30 days in past", so for me the current behavior (on MySQL) without this PR is ok.

avatar richard67
richard67 - comment - 26 Aug 2018

If you want i can make a PR against your branch so you have less work with it.

avatar alikon
alikon - comment - 26 Aug 2018

add(1, -1) should be the same as add(1, 1) if we are talking with relative integer number
it's not a matter of a more work even if i'm quite much more lazy than others....
but i'm open .... so PR are warmly welcommed

avatar alikon alikon - change - 26 Aug 2018
The description was changed
avatar alikon alikon - edited - 26 Aug 2018
avatar richard67
richard67 - comment - 26 Aug 2018

@alikon Am just testing my changes, then will make PR against your branch.

avatar richard67
richard67 - comment - 26 Aug 2018

@alikon I've just made a PR against the branch of this PR. I tested with success with MySQL that behavior is like before. Can you test for PostgreSQL?
And I have not added unit tests for the new function dateSub(). Can you do that? Or shall I try?

avatar richard67
richard67 - comment - 26 Aug 2018

@alikon I also noticed that the @since tag in the new function's documentation needs to be changed, but I don't remember the constant text to be added, which will trhen be replaced with the Joomla! version when being released. Do you know that constant text for the @since tag?

avatar richard67
richard67 - comment - 26 Aug 2018

I have not tested this item.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67 richard67 - test_item - 26 Aug 2018 - Not tested
avatar wojsmol
wojsmol - comment - 26 Aug 2018

@richard67 That constant for @since tag is __DEPLOY_VERSION__

avatar HLeithner
HLeithner - comment - 26 Aug 2018

@richard67 correct quote makes no sense there

avatar richard67
richard67 - comment - 26 Aug 2018

@HLeithner Sorry but I don't understand.

avatar richard67
richard67 - comment - 26 Aug 2018

@alikon I've made a new PR against your branch, the other one I closed. The new one is simple and just contains the -1 * $relativeDate as proposed by @ggppdk .
I also have set up a test environment with postgresql and have tested your PR plus this change on both MySQL and PostgreSQL with success.
So if you just merge my new PR I can set the test result here as success.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar HLeithner
HLeithner - comment - 26 Aug 2018

@richard67 it was my fault. Everything is ok.

avatar alikon
alikon - comment - 27 Aug 2018

@richard67 merged thanks

avatar richard67
richard67 - comment - 27 Aug 2018

I have tested this item successfully on 65f3169

Tested with MySQL and with PostgreSQL.

With mySQL it works like before, with PostgreSQL it does not work before and now works like with MySQL.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.
avatar richard67 richard67 - test_item - 27 Aug 2018 - Tested successfully
avatar richard67
richard67 - comment - 27 Aug 2018

@alikon I made a PR against your repo to make @csthomas happy. Please check and if ok merge, then I can test again.

avatar alikon
alikon - comment - 27 Aug 2018

@richard67 merged thanks

avatar richard67
richard67 - comment - 27 Aug 2018

I have tested this item successfully on 99190b1

Tested with success on both MySQL and PostgreSQL.
On MySQL the filter works as well as before (no idea why it worked before this PR because date parameter was quoted 2 times).
On PostgreSQL it work with this PR in the same way as with MySQL. Without this PR the filter does not work on PostgreSQL, i.e. no article links are shown.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67
richard67 - comment - 27 Aug 2018

I have tested this item successfully on 99190b1

Tested with success on both MySQL and PostgreSQL.
On MySQL the filter works as well as before (no idea why it worked before this PR because date parameter was quoted 2 times).
On PostgreSQL it work with this PR in the same way as with MySQL. Without this PR the filter does not work, i.e. no article links are shown.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67 richard67 - test_item - 27 Aug 2018 - Tested successfully
avatar richard67
richard67 - comment - 27 Aug 2018

It seems there are code style errors reported by Drone, maybe my fault:
FILE: ...ub.com/joomla/joomla-cms/libraries/joomla/database/query/postgresql.php
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
691 | ERROR | Expected 2 spaces after the longest type

avatar alikon
alikon - comment - 27 Aug 2018

no idea why it worked before this PR because date parameter was quoted 2 times

in the core before this PR there was no code using dateAdd() ?
but it will be used in the 3.9 for example in the privacy-framework or #20800

avatar richard67
richard67 - comment - 27 Aug 2018

I have tested this item successfully on a09b53b

Tested with success on both MySQL and PostgreSQL.
On MySQL the filter works as well as before (no idea why it worked before this PR because date parameter was quoted 2 times).
On PostgreSQL it work with this PR in the same way as with MySQL. Without this PR the filter does not work on PostgreSQL, i.e. no article links are shown.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67
richard67 - comment - 27 Aug 2018

I have tested this item successfully on a09b53b

Tested with success on both MySQL and PostgreSQL.
On MySQL the filter works as well as before (no idea why it worked before this PR because date parameter was quoted 2 times).
On PostgreSQL it work with this PR in the same way as with MySQL. Without this PR the filter does not work on PostgreSQL, i.e. no article links are shown.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67 richard67 - test_item - 27 Aug 2018 - Tested successfully
avatar richard67
richard67 - comment - 27 Aug 2018

No idea why my test result appears 2 times in the issue tracker.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67
richard67 - comment - 27 Aug 2018

No idea why my test result appears 2 times.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67 richard67 - test_item - 27 Aug 2018 - Tested successfully
avatar richard67
richard67 - comment - 27 Aug 2018

I have tested this item successfully on 8436f31

Tested with success on both MySQL and PostgreSQL.
On MySQL the filter works as well as before (no idea why it worked before this PR because date parameter was quoted 2 times).
On PostgreSQL it work with this PR in the same way as with MySQL. Without this PR the filter does not work on PostgreSQL, i.e. no article links are shown.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar csthomas
csthomas - comment - 27 Aug 2018

I have tested this item successfully on 8436f31


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar csthomas csthomas - test_item - 27 Aug 2018 - Tested successfully
avatar Quy Quy - change - 27 Aug 2018
Status Pending Ready to Commit
avatar Quy
Quy - comment - 27 Aug 2018

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar Quy
Quy - comment - 27 Aug 2018

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21844.

avatar richard67
richard67 - comment - 27 Aug 2018

This should be merged before 3.8.12 is released because it fixes a bug for PostgreSQL.

avatar mbabker
mbabker - comment - 27 Aug 2018

Looking at this, I think I'd rather have the dateAdd method quote date strings versus relying on the downstream caller to quote them (just because the current use in core is quoting them doesn't mean everyone's going to). If the expectation is that it should produce a string with quoted values, we should do the quoting here.

This should be merged before 3.8.12 is released because it fixes a bug for PostgreSQL.

Unfortunately that's not going to happen without delaying the release as we're already in the RC phase, personally I don't see this being urgent enough to do that.

Also, someone needs to ensure the appropriate changes are ported to https://github.com/joomla-framework/database

avatar csthomas
csthomas - comment - 27 Aug 2018

Looking at this, I think I'd rather have the dateAdd method quote date strings versus relying on the downstream caller to quote them ...

The problem may be to determine how to quote this parameter.

Which use case is valid?

  • $query->dateAdd('publish_up', ...)
  • $query->dateAdd('CURRENT_TIMESTAMP()', ...) - probably not an important case
  • $query->dateAdd('2018-08-27 00:00:00', ...)
avatar richard67
richard67 - comment - 27 Aug 2018

Exactly that. It may be a date or timestamp literal value with string quoting, or a column name with name quoting, or a function like "CURRENT_TIMESTAMP which should not be quoted at all.

avatar mbabker
mbabker - comment - 27 Aug 2018

Can't say I like it any, but if that's what it is then so be it. This kind of hits the same problem that joomla-framework/database#136 vaguely tries to mask/fix in that it still leaves too much of the decision on whether a value should be quoted to whomever is implementing the method. To me, a query building API should either be ensuring values get quoted consistently or not dealing with it at all (outside of explicitly named quoting methods) and I feel like when we start introducing value objects or leaving this type of implementation detail to the API consumer it's just setting us up for trouble down the line.

(Granted, these days I personally don't write code that makes liberal use of quoting like the Joomla API does (I leave it only to reserved keywords basically, as even parameterized values shouldn't be quoted), but to each their own I suppose)

avatar csthomas
csthomas - comment - 27 Aug 2018

The parameter in $query-> dateAdd() is problematic.

This is just my first idea, 3 use cases:

  • $query->dateAdd(new Column('publish_up'), ...)
  • $query->dateAdd(new Expression('CURRENT_TIMESTAMP()'), ...)
  • $query->dateAdd('2018-08-27 00:00:00', ...) - if string then use auto quoted

or split $query->dateAdd() into separate methods.

avatar mbabker
mbabker - comment - 27 Aug 2018

I'd rather just leave it as it is right now instead of trying to add a bunch of complexity with a mixed argument. TBH there's a lot of the API that needs re-thinking, especially with forced parameterized query support at the interface level.

avatar alikon
alikon - comment - 28 Aug 2018

so before to port this change to https://github.com/joomla-framework/database

what's the final decision:
a) is up to the caller to quote the date like now
or
b) must be done inside the dateAdd() method

avatar HLeithner
HLeithner - comment - 28 Aug 2018

iirc no other database function in jdatabasequery escapes the $value on its own.

It's already complex to create a database query without magically escaping. IMHO we should focus on a the prepared statement driver.

$query->dateAdd(new Column('publish_up'), ...)
$query->dateAdd(new Expression('CURRENT_TIMESTAMP()'), ...)
$query->dateAdd('2018-08-27 00:00:00', ...)

Do we get any gain from the new? classes, adding $this->quoteName() results in the same output with less overhead.

If this is changed for dateAdd it has to be changed for all functions.

avatar richard67
richard67 - comment - 28 Aug 2018

Whatever you will change (if so), please keep in mind that the parameter for calling dateAdd() is already quoted here, and this has not been changed by this PR:

$nowDate = $db->quote(JFactory::getDate()->toSql());

And the quoted string variable $nowDate is then used at a few other places, not only for the call to dateAdd(). So there might be more changes necessary than just changing the call to dateAdd when the dateAdd function will be changed.

I think this is out of scope of this PR, and I think we should leave it as it is for now, as @mbabker suggested in his last comment, but that's just my personal opinion as a contributor, it is not my PR.

avatar alikon
alikon - comment - 28 Aug 2018

even for me is better option a), but to avoid to make more work than needed (ie BEFORE port this to the database framework) i've asked to the contributors....(don't forget i'm lazy ?)

avatar csthomas
csthomas - comment - 28 Aug 2018

Only my thoughts for J4.

use \Joomla\Database\Function\Date;
use \Joomla\Database\Expression;

/* Date::add(string|expression $date, int $interval, enum $datePart, array $params = []) */

// For prepared statement
Date::add(':date', -1, Date::DAY, ['date': '2018-01-01 00:00:00']);
Date::add('?', -1, Date::DAY, ['2018-01-01 00:00:00']);

// Call $db->quoteName() inside the method
Date::add('publish_up', -1, Date::DAY);

// Clearly ask for trouble if you do not know what you are doing
Date::add(new Expression('CURRENT_TIMESTAMP()'), -1, Date::DAY);
avatar mbabker
mbabker - comment - 28 Aug 2018

Just leave this as is before we start overengineering our API.

avatar alikon
alikon - comment - 29 Aug 2018

changes have been ported to joomla-framework/database#138

avatar mbabker mbabker - change - 3 Sep 2018
Status Ready to Commit Fixed in Code Base
Closed_Date 0000-00-00 00:00:00 2018-09-03 17:13:07
Closed_By mbabker
Labels Added: ?
avatar mbabker mbabker - close - 3 Sep 2018
avatar mbabker mbabker - merge - 3 Sep 2018
avatar alikon
alikon - comment - 3 Sep 2018

thanks all

Add a Comment

Login with GitHub to post a comment