Efcore: SQLite Query: Simplify composed DateTime functions

Created on 18 May 2018  路  4Comments  路  Source: dotnet/efcore

Today (pun intended) when we generate SQL for DateTime.Now.Date, for example, we generate this:

strftime('%Y-%m-%d %H:%M:%f', strftime('%Y-%m-%d %H:%M:%f', 'now', 'localtime'), 'start of day')

However, it can simplify to just this: (not surprisingly the same SQL we generate for DateTime.Today)

strftime('%Y-%m-%d %H:%M:%f', 'now', 'localtime', 'start of day')

The logic to do this is fairly simple. Whenever we generate a strftime(format, timestring, modifier...) call, if the timestring argument is another strftime() call:

  1. Ignore its format parameter
  2. Use its timestring parameter directly in its place
  3. Prepend its modifier arguments (if any) to the current call
closed-fixed good first issue help wanted punted-for-2.2 type-enhancement

All 4 comments

@bricelam Threw together a PR for this, it's not super slick but it does the job. Let me know if you have any thoughts on ways of making less verbose.

It's more robust than I would've made it. (probably a good thing) 馃槈 Thanks! Will merge when I get a chance...

The rtrim calls force it to be a bit verbose. I can't think of any obvious ways to simplify it.

Thanks, @Muppets! There's just something cool about seeing an O/RM generate such tidy SQL...

Was this page helpful?
0 / 5 - 0 ratings