示例#1
0
func UpsertArticle(dbmap *gorp.DbMap, accountId int64, key string) (int64, error) {
	id, err := dbmap.SelectNullInt(`
        with s as (
            update articles set updated_at = $3 where account_id = $1 and key = $2 returning id
        ), i as (
            insert into articles ("account_id", "key", "created_at", "updated_at")
            select $1, $2, $3, $3
            where not exists (select 1 from s)
            returning id
        )
        select id from i union all select id from s;
    `, accountId,
		key,
		time.Now().UTC(),
	)
	if err != nil {
		return -1, err
	}

	iid, err := id.Value()

	return iid.(int64), err
}
示例#2
0
func InsertExpectedReader(dbmap *gorp.DbMap, aid, rid int64) (int64, error) {
	id, err := dbmap.SelectNullInt(`
	        with s as (
	            select id from read_receipts where article_id = $1 and reader_id = $2
	        ), i as (
	            insert into read_receipts ("article_id", "reader_id", "created_at")
	            select $1, $2, $3
	            where not exists (select 1 from s)
	            returning id
	        )
	        select id from i union all select id from s;`,
		aid,
		rid,
		time.Now().UTC(),
	)
	if err != nil {
		return -1, err
	}

	iid, err := id.Value()

	return iid.(int64), err
}
示例#3
0
func InsertReader(dbmap *gorp.DbMap, accountId int64, distinctId string) (int64, error) {
	id, err := dbmap.SelectNullInt(`
        with s as (
            select id from readers where account_id = $1 and distinct_id = $2
        ), i as (
            insert into readers ("account_id", "distinct_id", "created_at")
            select $1, $2, $3
            where not exists (select 1 from s)
            returning id
        )
        select id from i union all select id from s;
    `, accountId,
		distinctId,
		time.Now().UTC(),
	)
	if err != nil {
		return -1, err
	}

	iid, err := id.Value()

	return iid.(int64), err
}
示例#4
0
func UpsertReadReceipt(dbmap *gorp.DbMap, articleId, readerId int64) (int64, error) {
	defer UpdateArticleCounts(dbmap, articleId)
	at := time.Now().UTC()
	_, err := dbmap.SelectNullInt(`
				update read_receipts set first_read_at = $1
				where article_id=$2 and reader_id=$3 and first_read_at is null`,
		at,
		articleId,
		readerId,
	)

	id, err := dbmap.SelectNullInt(`
        update read_receipts set last_read_at = $1, read_count = read_count + 1
				where article_id=$2 and reader_id=$3
				returning id;
    `, at,
		articleId,
		readerId,
	)
	if err != nil {
		return -1, err
	}
	if id.Valid {
		return id.Int64, nil
	}

	id, err = dbmap.SelectNullInt(`
        with s as (
            select id from read_receipts where article_id = $1 and reader_id = $2
        ), i as (
            insert into read_receipts (
							"article_id", "reader_id",
							"created_at", "first_read_at", "last_read_at",
							"read_count")
            select $1, $2, $3, $3, $3, 1
            where not exists (select 1 from s)
            returning id
        )
        select id from i union all select id from s;
    `, articleId,
		readerId,
		at,
	)
	if err != nil {
		return 0, err
	}

	iid, err := id.Value()
	if err != nil {
		return 0, err
	}

	return iid.(int64), err
}