mirror of
https://github.com/owntone/owntone-server.git
synced 2024-12-28 16:15:57 -05:00
[smartpl] Refactor appending date expression to sql query
This commit is contained in:
parent
02d4ba1eaa
commit
772b18479d
@ -38,33 +38,43 @@ options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@members {
|
@members {
|
||||||
static void interval_date(pANTLR3_STRING result, pANTLR3_UINT8 interval, pANTLR3_UINT8 datval, char beforeorafter)
|
static void append_date(pANTLR3_STRING result, const char *datval, char beforeorafter, const char *interval)
|
||||||
{
|
{
|
||||||
if (strcmp((char *)datval, "yesterday") == 0)
|
if (strcmp((char *)datval, "today") == 0)
|
||||||
{
|
{
|
||||||
result->append8(result, "strftime('\%s', datetime('now', 'start of day', '-1 day', ");
|
result->append8(result, "strftime('\%s', datetime('now', 'start of day'");
|
||||||
|
}
|
||||||
|
else if (strcmp((char *)datval, "yesterday") == 0)
|
||||||
|
{
|
||||||
|
result->append8(result, "strftime('\%s', datetime('now', 'start of day', '-1 day'");
|
||||||
}
|
}
|
||||||
else if (strcmp((char *)datval, "last week") == 0)
|
else if (strcmp((char *)datval, "last week") == 0)
|
||||||
{
|
{
|
||||||
result->append8(result, "strftime('\%s', datetime('now', 'start of day', 'weekday 0', '-13 days', ");
|
result->append8(result, "strftime('\%s', datetime('now', 'start of day', 'weekday 0', '-13 days'");
|
||||||
}
|
}
|
||||||
else if (strcmp((char *)datval, "last month") == 0)
|
else if (strcmp((char *)datval, "last month") == 0)
|
||||||
{
|
{
|
||||||
result->append8(result, "strftime('\%s', datetime('now', 'start of month', '-1 month', ");
|
result->append8(result, "strftime('\%s', datetime('now', 'start of month', '-1 month'");
|
||||||
}
|
}
|
||||||
else if (strcmp((char *)datval, "last year") == 0)
|
else if (strcmp((char *)datval, "last year") == 0)
|
||||||
{
|
{
|
||||||
result->append8(result, "strftime('\%s', datetime('now', 'start of year', '-1 year', ");
|
result->append8(result, "strftime('\%s', datetime('now', 'start of year', '-1 year'");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result->append8(result, "strftime('\%s', datetime('now', 'start of day', ");
|
result->append8(result, "strftime('\%s', datetime(\'");
|
||||||
|
result->append8(result, datval);
|
||||||
|
result->append8(result, "\'");
|
||||||
}
|
}
|
||||||
|
|
||||||
result->addc(result, '\'');
|
if (beforeorafter)
|
||||||
|
{
|
||||||
|
result->append8(result, ", '");
|
||||||
result->addc(result, beforeorafter);
|
result->addc(result, beforeorafter);
|
||||||
result->append8(result, (const char *)interval);
|
result->append8(result, interval);
|
||||||
result->append8(result, "', 'localtime'))");
|
result->addc(result, '\'');
|
||||||
|
}
|
||||||
|
result->append8(result, ", 'utc'))");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,52 +354,24 @@ dateval returns [ pANTLR3_STRING result ]
|
|||||||
pANTLR3_UINT8 datval;
|
pANTLR3_UINT8 datval;
|
||||||
|
|
||||||
datval = $DATE.text->chars;
|
datval = $DATE.text->chars;
|
||||||
|
|
||||||
$result = $DATE.text->factory->newRaw($DATE.text->factory);
|
$result = $DATE.text->factory->newRaw($DATE.text->factory);
|
||||||
|
|
||||||
if (strcmp((char *)datval, "today") == 0)
|
append_date($result, (const char *)datval, 0, NULL);
|
||||||
{
|
|
||||||
$result->append8($result, "strftime('\%s', datetime('now', 'start of day', 'localtime'))");
|
|
||||||
}
|
|
||||||
else if (strcmp((char *)datval, "yesterday") == 0)
|
|
||||||
{
|
|
||||||
$result->append8($result, "strftime('\%s', datetime('now', 'start of day', '-1 day', 'localtime'))");
|
|
||||||
}
|
|
||||||
else if (strcmp((char *)datval, "last week") == 0)
|
|
||||||
{
|
|
||||||
$result->append8($result, "strftime('\%s', datetime('now', 'start of day', 'weekday 0', '-13 days', 'localtime'))");
|
|
||||||
}
|
|
||||||
else if (strcmp((char *)datval, "last month") == 0)
|
|
||||||
{
|
|
||||||
$result->append8($result, "strftime('\%s', datetime('now', 'start of month', '-1 month', 'localtime'))");
|
|
||||||
}
|
|
||||||
else if (strcmp((char *)datval, "last year") == 0)
|
|
||||||
{
|
|
||||||
$result->append8($result, "strftime('\%s', datetime('now', 'start of year', '-1 year', 'localtime'))");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$result->append8($result, "strftime('\%s', datetime('YYYY-MM-DD', (char*)dateval, 'localtime'))");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
| interval BEFORE DATE
|
| interval BEFORE DATE
|
||||||
{
|
{
|
||||||
$result = $DATE.text->factory->newRaw($DATE.text->factory);
|
$result = $DATE.text->factory->newRaw($DATE.text->factory);
|
||||||
interval_date($result, $interval.result->chars, $DATE.text->chars, '-');
|
append_date($result, (const char *)$DATE.text->chars, '-', (const char *)$interval.result->chars);
|
||||||
}
|
}
|
||||||
| interval AFTER DATE
|
| interval AFTER DATE
|
||||||
{
|
{
|
||||||
$result = $DATE.text->factory->newRaw($DATE.text->factory);
|
$result = $DATE.text->factory->newRaw($DATE.text->factory);
|
||||||
interval_date($result, $interval.result->chars, $DATE.text->chars, '+');
|
append_date($result, (const char *)$DATE.text->chars, '+', (const char *)$interval.result->chars);
|
||||||
}
|
}
|
||||||
| interval AGO
|
| interval AGO
|
||||||
{
|
{
|
||||||
$result = $AGO.text->factory->newRaw($AGO.text->factory);
|
$result = $AGO.text->factory->newRaw($AGO.text->factory);
|
||||||
|
append_date($result, "today", '-', (const char *)$interval.result->chars);
|
||||||
$result->append8($result, "strftime('\%s', datetime('now', 'start of day', ");
|
|
||||||
$result->append8($result, "'-");
|
|
||||||
$result->append8($result, (const char *)$interval.result->chars);
|
|
||||||
$result->append8($result, "', 'localtime'))");
|
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user