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

Add: Use a 3 arg iso_time for GET iterators #2109

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,34 @@ manage_create_sql_functions ()

sql ("DROP FUNCTION IF EXISTS iso_time (seconds integer);");

sql ("CREATE OR REPLACE FUNCTION iso_time (seconds bigint, user_zone text, user_offset interval)"
" RETURNS text AS $$"
" BEGIN"
" RETURN CASE"
" WHEN $1 IS NULL OR $1 = 0"
" THEN ''"
" WHEN user_zone IS NULL"
" OR EXTRACT (EPOCH FROM user_offset) = 0"
" THEN to_char (to_timestamp ($1) AT TIME ZONE 'UTC',"
" 'FMYYYY-MM-DD')"
" || to_char (to_timestamp ($1) AT TIME ZONE 'UTC',"
" 'FMTHH24:MI:SSZ')"
" ELSE to_char (to_timestamp ($1) AT TIME ZONE user_zone,"
" 'FMYYYY-MM-DD')"
" || to_char (to_timestamp ($1) AT TIME ZONE user_zone,"
" 'FMTHH24:MI:SS')"
" || CASE WHEN (extract (epoch FROM user_offset) > 0)"
" THEN '+' ELSE '' END"
" || to_char (extract (hours FROM user_offset)::integer,"
" 'FM00')"
" || ':'"
" || to_char (abs (extract (minutes FROM user_offset)"
" ::integer),"
" 'FM00')"
" END;"
" END;"
"$$ LANGUAGE plpgsql;");

sql ("CREATE OR REPLACE FUNCTION iso_time (seconds bigint, user_zone text)"
" RETURNS text AS $$"
" DECLARE"
Expand Down
27 changes: 27 additions & 0 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -4998,6 +4998,33 @@ init_get_iterator2_with (iterator_t* iterator, const char *type,
with_clause = g_strdup_printf ("WITH %s", extra_with);
}


extra_with = " iterator_user_zone"
" AS (SELECT coalesce (NULLIF ((SELECT current_setting ('gvmd.tz_override')),"
" ''),"
" (SELECT timezone FROM users"
" WHERE id = gvmd_user ()))"
" AS user_zone),"
" iterator_user_offset"
" AS (SELECT age (now () AT TIME ZONE (SELECT user_zone"
" FROM iterator_user_zone"
" LIMIT 1),"
" now () AT TIME ZONE 'UTC')"
" AS user_offset)";
if (extra_with)
{
if (with_clause)
{
gchar *old_with;

old_with = with_clause;
with_clause = g_strdup_printf ("%s, %s", old_with, extra_with);
g_free (old_with);
}
else
with_clause = g_strdup_printf ("WITH %s", extra_with);
}

g_free (owner_filter);
array_free (permissions);

Expand Down
11 changes: 7 additions & 4 deletions src/manage_sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,12 @@ typedef struct
*
* @param[in] prefix Column prefix.
*/
// FIX takes CPEs from 9s to 7s
#define GET_ITERATOR_COLUMNS_STRING \
"id, uuid, name, comment, iso_time (creation_time)," \
" iso_time (modification_time), creation_time AS created," \
"id, uuid, name, comment," \
" iso_time (creation_time, (SELECT user_zone FROM iterator_user_zone LIMIT 1), (SELECT user_offset FROM iterator_user_offset LIMIT 1))," \
" iso_time (modification_time, (SELECT user_zone FROM iterator_user_zone LIMIT 1), (SELECT user_offset FROM iterator_user_offset LIMIT 1))," \
" creation_time AS created," \
" modification_time AS modified"

/**
Expand All @@ -263,8 +266,8 @@ typedef struct
{ prefix "uuid", NULL, KEYWORD_TYPE_STRING }, \
{ prefix "name", NULL, KEYWORD_TYPE_STRING }, \
{ prefix "comment", NULL, KEYWORD_TYPE_STRING }, \
{ " iso_time (" prefix "creation_time)", NULL, KEYWORD_TYPE_STRING }, \
{ " iso_time (" prefix "modification_time)", NULL, KEYWORD_TYPE_STRING }, \
{ " iso_time (" prefix "creation_time, (SELECT user_zone FROM iterator_user_zone LIMIT 1), (SELECT user_offset FROM iterator_user_offset LIMIT 1))", NULL, KEYWORD_TYPE_STRING }, \
{ " iso_time (" prefix "modification_time, (SELECT user_zone FROM iterator_user_zone LIMIT 1), (SELECT user_offset FROM iterator_user_offset LIMIT 1))", NULL, KEYWORD_TYPE_STRING }, \
{ prefix "creation_time", "created", KEYWORD_TYPE_INTEGER }, \
{ prefix "modification_time", "modified", KEYWORD_TYPE_INTEGER }

Expand Down