[smartpl] Refactor appending date expression to sql query

This commit is contained in:
chme 2019-07-14 10:02:17 +02:00 committed by whatdoineed2do/Ray
parent 02d4ba1eaa
commit 772b18479d

View File

@ -38,34 +38,44 @@ 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, "last week") == 0) else if (strcmp((char *)datval, "yesterday") == 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', '-1 day'");
} }
else if (strcmp((char *)datval, "last month") == 0) else if (strcmp((char *)datval, "last week") == 0)
{ {
result->append8(result, "strftime('\%s', datetime('now', 'start of month', '-1 month', "); result->append8(result, "strftime('\%s', datetime('now', 'start of day', 'weekday 0', '-13 days'");
} }
else if (strcmp((char *)datval, "last year") == 0) else if (strcmp((char *)datval, "last month") == 0)
{ {
result->append8(result, "strftime('\%s', datetime('now', 'start of year', '-1 year', "); result->append8(result, "strftime('\%s', datetime('now', 'start of month', '-1 month'");
} }
else else if (strcmp((char *)datval, "last year") == 0)
{ {
result->append8(result, "strftime('\%s', datetime('now', 'start of day', "); result->append8(result, "strftime('\%s', datetime('now', 'start of year', '-1 year'");
} }
else
{
result->append8(result, "strftime('\%s', datetime(\'");
result->append8(result, datval);
result->append8(result, "\'");
}
result->addc(result, '\''); if (beforeorafter)
result->addc(result, beforeorafter); {
result->append8(result, (const char *)interval); result->append8(result, ", '");
result->append8(result, "', 'localtime'))"); result->addc(result, beforeorafter);
} result->append8(result, interval);
result->addc(result, '\'');
}
result->append8(result, ", 'utc'))");
}
} }
playlist returns [ pANTLR3_STRING title, pANTLR3_STRING query, pANTLR3_STRING orderby, pANTLR3_STRING having, int limit ] playlist returns [ pANTLR3_STRING title, pANTLR3_STRING query, pANTLR3_STRING orderby, pANTLR3_STRING having, int limit ]
@ -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'))");
} }
; ;
@ -397,7 +379,7 @@ interval returns [ pANTLR3_STRING result ]
@init { $result = NULL; } @init { $result = NULL; }
: INT DATINTERVAL : INT DATINTERVAL
{ {
pANTLR3_UINT8 interval; pANTLR3_UINT8 interval;
int intval; int intval;
char buf[25]; char buf[25];
@ -407,16 +389,16 @@ interval returns [ pANTLR3_STRING result ]
interval = $DATINTERVAL.text->chars; interval = $DATINTERVAL.text->chars;
if (strcmp((char *)interval, "weeks") == 0) if (strcmp((char *)interval, "weeks") == 0)
{ {
intval = atoi((const char *)$INT.text->chars) * 7; intval = atoi((const char *)$INT.text->chars) * 7;
snprintf(buf, sizeof(buf), "\%d days", intval); snprintf(buf, sizeof(buf), "\%d days", intval);
$result->append8($result, buf); $result->append8($result, buf);
} }
else else
{ {
$result->append8($result, (const char *)$INT.text->chars); $result->append8($result, (const char *)$INT.text->chars);
$result->append8($result, " "); $result->append8($result, " ");
$result->append8($result, (const char *)$DATINTERVAL.text->chars); $result->append8($result, (const char *)$DATINTERVAL.text->chars);
} }
return $result; return $result;
} }