简体   繁体   中英

Golang Postgres syntax error in If/Else statement

I have a postgres exec string. I have rows of users, with arrays of weight lifting data. I want to either update the current day log (ie replace last array element if there's already a lift logged for today), or append the lifting data to these arrays.

BEGIN
IF (SELECT date[array_upper(date, 1)] FROM userdata WHERE email = $8) = $7 THEN
UPDATE userdata SET age = $1,
weight[array_upper(weight, 1)] = $2,
deadlift[array_upper(deadlift, 1)] = $3,
squat[array_upper(squat, 1)] = $4,
bench[array_upper(bench, 1)] = $5,
overhead[array_upper(overhead, 1)] = $6,
WHERE email = $8;
ELSE
UPDATE userdata SET age = $1,
weight = array_append(weight, $2),
deadlift = array_append(deadlift, $3),
squat = array_append(squat, $4),
bench = array_append(bench, $5),
overhead = array_append(overhead, $6),
date = array_append(date, $7)
WHERE email = $8;
END IF;
END;

First checks if date (ie last date array element) == today's date, if true, THEN set last element to given values.

ELSE, append given values to the ends of all these arrays

My Golang code is the following:

_, err := d.conn.Exec(context.Background(), execstring, user.Age, user.Weight, user.Deadlift, user.Squat,
        user.Bench, user.Overhead, fmt.Sprint(time.Now().Date()), user.Email)

Executes execstring (the 1st code block), with the following variables.

Error: ERROR: syntax error at or near "IF"

New to both golang and postgres, would love some help (problem likely lies with my execstring)

EDIT (CASE VERSION BELOW):

SELECT date[array_upper(date, 1)]
CASE
WHEN date[array_upper(date,1)] = $7 THEN
UPDATE userdata SET age = $1,
weight[array_upper(weight, 1)] = $2,
deadlift[array_upper(deadlift, 1)] = $3,
squat[array_upper(squat, 1)] = $4,
bench[array_upper(bench, 1)] = $5,
overhead[array_upper(overhead, 1)] = $6,
WHERE email = $8;
ELSE
UPDATE userdata SET age = $1,
weight = array_append(weight, $2),
deadlift = array_append(deadlift, $3),
squat = array_append(squat, $4),
bench = array_append(bench, $5),
overhead = array_append(overhead, $6),
date = array_append(date, $7)
WHERE email = $8;
END
FROM userdata WHERE email = $8

Error: ERROR: syntax error at or near "CASE"

Here is what I did to fix my problem (hope it helps anybody else trying to do something similar)

It seems that CASE can't be used to decide which statement to run, but rather, seems to only decide which text to put into the query/exec string. Therefore, I had to move things around to do this, using CASE to decide what to set my arrays equal to, rather than deciding what operation to perform on the arrays.

UPDATE userdata
SET age = $1,
weight = CASE WHEN date[array_upper(date,1)] = $7 THEN array_replace(weight, weight[array_upper(weight, 1)], $2) 
ELSE array_append(weight, $2) END,
deadlift = CASE WHEN date[array_upper(date,1)] = $7 THEN array_replace(deadlift, deadlift[array_upper(deadlift, 1)], $3) 
ELSE array_append(deadlift, $3) END,
squat = CASE WHEN date[array_upper(date,1)] = $7 THEN array_replace(squat, squat[array_upper(squat, 1)], $4) 
ELSE array_append(squat, $4) END,
bench = CASE WHEN date[array_upper(date,1)] = $7 THEN array_replace(bench, bench[array_upper(bench, 1)], $5) 
ELSE array_append(bench, $5) END,
overhead = CASE WHEN date[array_upper(date,1)] = $7 THEN array_replace(overhead, overhead[array_upper(overhead, 1)], $6) 
ELSE array_append(overhead, $6) END,
date = CASE WHEN date[array_upper(date,1)] = $7 THEN array_replace(date, date[array_upper(date, 1)], $7) 
ELSE array_append(date, $7) END
WHERE email = $8;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM