segmentio / pg-escape

sprintf-style postgres query escaping and helper functions
80 stars 30 forks source link

handle non-strings #4

Closed rpedela closed 8 years ago

rpedela commented 10 years ago

Below is how Postgres format() handles timestamps, numbers, booleans. Either pg-escape crashes or does not do the right thing for these data types. I did not test arrays. Also how should objects be handled? Should they be converted to JSON strings?

Identifier formatting
select format('SELECT %I', TRUE);
--------------------------------------
 SELECT "t"

select format('SELECT %I', FALSE);
--------------------------------------
 SELECT "f"

select format('SELECT %I', now());
--------------------------------------
 SELECT "2014-09-11 00:02:43.486377+00"

select format('SELECT %I', 45);      
--------------------------------------
 SELECT "45"

select format('SELECT %I', 45.13);          
--------------------------------------
 SELECT "45.13"
Literal formatting
select format('SELECT %L', TRUE);         
--------------------------------------
 SELECT 't'

select format('SELECT %L', FALSE);          
--------------------------------------
 SELECT 'f'

select format('SELECT %L', now());            
--------------------------------------
 SELECT '2014-09-11 00:02:43.486377+00'

select format('SELECT %L', 45);          
--------------------------------------
 SELECT '45'

select format('SELECT %L', 45.13);
--------------------------------------
 SELECT '45.13'
String formatting
select format('SELECT %s', TRUE);
--------------------------------------
 SELECT t

select format('SELECT %s', FALSE); 
--------------------------------------
 SELECT f

select format('SELECT %s', now());           
--------------------------------------
 SELECT 2014-09-11 00:02:43.486377+00

select format('SELECT %s', 45);           
--------------------------------------
 SELECT 45

select format('SELECT %s', 45.13);          
--------------------------------------
 SELECT 45.13
rpedela commented 8 years ago

For anyone who needs this: https://github.com/datalanche/node-pg-format