Skip to content

Instantly share code, notes, and snippets.

@chrisvaughn
Created May 22, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvaughn/b670b883938799fbd052669e4ab7f7c6 to your computer and use it in GitHub Desktop.
Save chrisvaughn/b670b883938799fbd052669e4ab7f7c6 to your computer and use it in GitHub Desktop.
-- HELPER FUNCTIONS
-- returns the day of the month from a date
CREATE OR REPLACE FUNCTION day_of_month(dt date) RETURNS INTEGER AS
$$
SELECT DATE_PART('days', dt)::INTEGER
$$ LANGUAGE SQL;
-- returns the numeric (1-12) value for a month from a date
CREATE OR REPLACE FUNCTION month_index(dt date) RETURNS INTEGER AS
$$
SELECT DATE_PART('month', dt)::INTEGER
$$ LANGUAGE SQL;
-- updates the month checkin record for a given date
CREATE OR REPLACE FUNCTION set_checkin(x integer, checkin_dt date) RETURNS INTEGER AS
$$
SELECT (x | (1 << 31 - day_of_month(checkin_dt)))
$$ LANGUAGE SQL;
-- check-in for date
UPDATE streaks_checkins
SET checkins[month_index(%(date)s)] = set_checkin(checkins[month_index(%(date)s)], %(date)s)
WHERE user_id = %(user_id)s and year = %(year)s;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment