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

Where with parametrized INTERVAL #379

Open
ikawalec opened this issue May 28, 2024 · 9 comments
Open

Where with parametrized INTERVAL #379

ikawalec opened this issue May 28, 2024 · 9 comments

Comments

@ikawalec
Copy link

ikawalec commented May 28, 2024

Hi,

I'm having an issue with using where expr with parametrized INTERVAL.

Code:

d := 3

builder := r.StatementBuilderType.
  Delete("test").
  Where(squirrel.Expr("expiry <= now()::timestamptz - INTERVAL '? DAYS'", d))
		
query, args, err = builder.ToSql()
	
logrus.Infof("query: %s, args: %+v", query, args)
	
// db execute

I'm getting the following error:

ERROR: could not parse "$1 DAYS" as type interval: interval: missing number at position 0: "$1 DAYS" (SQLSTATE 22007)

Generated query and args:

query: DELETE FROM test WHERE expiry \u003c= now()::timestamptz - INTERVAL '$1 DAYS'
args: [3]

Expected SQL:

delete from test where expiry <= now()::timestamptz - INTERVAL '3 days';

Is this a bug? Is there any other way this can be achieved?

Thanks!

@lann
Copy link
Member

lann commented May 28, 2024

Assuming you are using postgres, I believe the quotes are the problem: INTERVAL '? DAYS' -> INTERVAL ? DAYS

@ikawalec
Copy link
Author

Assuming you are using postgres, I believe the quotes are the problem: INTERVAL '? DAYS' -> INTERVAL ? DAYS

That's correct, I'm using postgres. Without quotes, it's not a valid SQL statement:

delete from test where expiry <= now()::timestamptz - INTERVAL 3 days;

at or near "3": syntax error
DETAIL:  source SQL:
delete from test where expiry <= now()::timestamptz - INTERVAL 3 days

@lann
Copy link
Member

lann commented May 28, 2024

It looks like postgres expects the number to be a string, so you may also need to e.g. strconv.Itoa the Go int before passing it to the database.

@ikawalec
Copy link
Author

Thanks but still even with days converted usingstrconv.Itoa I still get the same error (assuming quotes are mandatory).

squirrel.Expr("expiry <= now()::timestamptz - INTERVAL '? DAYS'", strconv.Itoa(days)),
ERROR: could not parse "$1 DAYS" as type interval: interval: missing number at position 0: "$1 DAYS" (SQLSTATE 22007)

@lann
Copy link
Member

lann commented May 28, 2024

The quotes mean you are passing the literal string "? DAYS"; Squirrel converts the ? to $1 because its placeholder replacement doesn't know how to deal with strings.

I believe what you need is:

squirrel.Expr("expiry <= now()::timestamptz - INTERVAL ? DAYS", strconv.Itoa(days)),

@ikawalec
Copy link
Author

The quotes mean you are passing the literal string "? DAYS"; Squirrel converts the ? to $1 because its placeholder replacement doesn't know how to deal with strings.

I believe what you need is:

squirrel.Expr("expiry <= now()::timestamptz - INTERVAL ? DAYS", strconv.Itoa(days)),

The example that you provided will generate the following SQL query:

DELETE FROM test WHERE expiry \u003c= now()::timestamptz - INTERVAL 3 DAYS

In order for that to work with postgres: https://www.postgresql.org/docs/8.0/functions-datetime.html, the INTERVAL should use quotes '3 DAYS':

DELETE FROM test WHERE expiry \u003c= now()::timestamptz - INTERVAL '3 DAYS'

@lann
Copy link
Member

lann commented May 28, 2024

Ah sorry, I got bad information from Google. In that case I would expect something like this to work:

squirrel.Expr("expiry <= now()::timestamptz - INTERVAL ?", fmt.Sprintf("%d DAYS", days))

@ikawalec
Copy link
Author

Ah sorry, I got bad information from Google. In that case I would expect something like this to work:

squirrel.Expr("expiry <= now()::timestamptz - INTERVAL ?", fmt.Sprintf("%d DAYS", days))

Unfortunately, this would also fail:

 query: DELETE FROM test WHERE expiry \u003c= now()::timestamptz - INTERVAL $1
 args: [3 DAYS]
 error: ERROR: at or near "1": syntax error (SQLSTATE 42601)

@efitzkiwi
Copy link

Only way I've gotten this to work is by doing something such as the following

Where(`"timestamp" >= (recent_close."timestamp" - interval '1 day' * ?)`, finalInterval)

Where finalInterval is a number.

The key part being the multiplier, unfortunately seems like a driver issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants