Skip to content

Functions in prepared statements #85

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

Closed
Hebo opened this issue Jan 14, 2012 · 2 comments
Closed

Functions in prepared statements #85

Hebo opened this issue Jan 14, 2012 · 2 comments

Comments

@Hebo
Copy link
Contributor

Hebo commented Jan 14, 2012

Hey

I'm trying to figure out the syntax for having postgres functions in my prepared statements. Here's the SQL query:

PREPARE insert (int, timestamp, int, varchar) AS
INSERT INTO hours (count, view_period, countable_id, countable_type)
VALUES($1, $2, $3, $4);
EXECUTE insert(1, date_trunc('hour',timestamp '2012-01-13T20:19:35.945Z'), '1234', 'foo');
// INSERT 0 1

As best as I can tell, the translation of that into node-postgres would be:

var insert = {
    name: "insert",
    text: "INSERT INTO hours (count, view_period, countable_id, countable_type) "+
    "VALUES ($1,$2,$3,$4);",
    values: [1, "date_trunc('hour',timestamp '2012-01-13T20:19:35.945Z')", 1234, 'foo']
}
client.query(insert, function(e,r){console.log(e)})

However, that returns the following error:

{ [error: invalid input syntax for type timestamp: "date_trunc('hour',timestamp '2012-01-13T20:19:35.945Z')"]
length: 157,
name: 'error',
severity: 'ERROR',
code: '22007',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
file: 'datetime.c',
line: '3557',
routine: 'DateTimeParseError' }

Any ideas what's going on?

@booo
Copy link
Contributor

booo commented Jan 14, 2012

Maybe you can try this one:

  var insert = {
    name: "insert",
    text: "INSERT INTO hours (count, view_period, countable_id, countable_type) VALUES ($1, date_trunc('hour', $2::timestamp), $3, $4);",
    values: [1, "2012-01-13T20:19:35.945Z", 1234, "foo"]
  };

@Hebo
Copy link
Contributor Author

Hebo commented Jan 16, 2012

This worked beautifully, thanks!

@Hebo Hebo closed this as completed Jan 16, 2012
brianc pushed a commit that referenced this issue Dec 27, 2019
* Add failing test for correct removal from checkout queue on timeout

* Remove timed-out checkouts from queue correctly

Fixes #85.
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

2 participants