Created
May 22, 2018 14:33
-
-
Save chrisvaughn/b670b883938799fbd052669e4ab7f7c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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