dataflow/api/lib/sql.js
Paul Trowbridge 4e477420ad Fix lit() truncating decimals; restore last selected stack on load
- lit() was calling Math.trunc() on numbers, dropping decimals from balance_offset and any other numeric SQL params
- Stacks page now saves last selected stack to localStorage and restores it on load

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 16:38:02 -04:00

30 lines
1.2 KiB
JavaScript

/**
* SQL literal helpers
* Build literal SQL values for direct interpolation into query strings.
* Queries are fully formed strings — easy to copy and run in psql for debugging.
*/
// Escape a single value for safe interpolation into SQL
function lit(val) {
if (val === null || val === undefined) return 'NULL';
if (typeof val === 'boolean') return val ? 'TRUE' : 'FALSE';
if (typeof val === 'number') return String(val);
if (typeof val === 'object') return `'${JSON.stringify(val).replace(/'/g, "''")}'`;
return `'${String(val).replace(/'/g, "''")}'`;
}
// Format a value for a JSONB parameter — always JSON.stringifies first so that
// plain strings become valid JSON ("ROBLOX" not ROBLOX), arrays and objects work too.
function jsonLit(val) {
if (val === null || val === undefined) return 'NULL';
return `'${JSON.stringify(val).replace(/'/g, "''")}'`;
}
// Format a JS array as a PostgreSQL text array literal: ARRAY['a','b']
function arr(val) {
if (!Array.isArray(val) || val.length === 0) return "ARRAY[]::text[]";
return `ARRAY[${val.map(s => `'${String(s).replace(/'/g, "''")}'`).join(',')}]`;
}
module.exports = { lit, jsonLit, arr };