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

Support date ranges being converted to SQL and MongoDB queries with jQuery-QueryBuilder #37

Open
VoordeMensen opened this issue Jun 27, 2019 · 1 comment

Comments

@VoordeMensen
Copy link

I have a database-design where the date I want to query is of the datetime-type. But when I try to select all orders that equal to todays date for example, the parser will add 00:00:00 and I will not get any results.

I usually solve this to use the mysql date function (select * from orders where order_date = date('2019-06-28')) - is there any way to do this with the QueryBuilderParser?

I now manually loop through the rules and rebuild an equals to to between and manipulate fields, but I hope this can be done more easily?

@timgws
Copy link
Owner

timgws commented Jul 12, 2019

This is not something that is currently possible, but I would not mind a pull request if the functionality is appropriately implemented.

Normally, with the querybuilder, you could do something like:

$q->whereDate('created_at', '=', Carbon::today()->toDateString());

to just get the date.

We are using Carbon to convert the input from QueryBuilderParser:

/**
* Convert a Datetime field to Carbon items to be used for comparisons.
*
* @param $value
* @return \Carbon\Carbon
* @throws QBParseException
*/
protected function convertDatetimeToCarbon($value)
{
if (is_array($value)) {
return array_map(function ($v) {
return new Carbon($v);
}, $value);
}
return new Carbon($value);
}

but we do not provide any options.

This could be fixed by changing the way getRuleValue parses the date:

/**
* get a value for a given rule.
*
* throws an exception if the rule is not correct.
*
* @param stdClass $rule
* @throws QBRuleException
*/
private function getRuleValue(stdClass $rule)
{
if (!$this->checkRuleCorrect($rule)) {
throw new QBRuleException();
}
return $rule->value;
}

So that it creates a correct DateBasedWhere when using different formats of dates:

https://github.com/laravel/framework/blob/15917aa3a2616344a1ffb896610878b929544f77/src/Illuminate/Database/Query/Builder.php#L1104-L1124

You would need a way to be able to specify what format the date is desired to be in. A quick browse of Laravel shows it supports the following:

  • Y-m-d (which you want)
  • H:i:s
  • Y
  • m
  • d

@timgws timgws changed the title mysql date function? Support date ranges being converted to SQL queries with jQuery-QueryBuilder Feb 20, 2020
@timgws timgws changed the title Support date ranges being converted to SQL queries with jQuery-QueryBuilder Support date ranges being converted to SQL and MongoDB queries with jQuery-QueryBuilder Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants