package dialect import "fmt" // OrderedIDConcat returns the SQL fragment for a scalar subquery that // comma-joins the `id` column of `SELECT id FROM tasks WHERE `, in // ascending id order. The two dialects place the ordering differently: // SQLite's GROUP_CONCAT has no ORDER BY of its own, so ordering comes from // an ORDER BY inside the subquery; PostgreSQL's string_agg takes an ORDER BY // inside the aggregate call itself. Writing one and assuming the other // matches is how the two dialects silently diverge on row order — see // docs/specs/office/system-design/parent-wake-wave-identity.md, "Backstop // admission". // // where is the WHERE clause body (no leading "WHERE", no trailing // "ORDER BY") of the `tasks` subquery — the caller supplies the predicate, // this helper only owns the aggregate/order-by placement split. // // SQLite: (SELECT GROUP_CONCAT(w.id, ',') FROM (SELECT id FROM tasks WHERE ORDER BY id) w) // Postgres: (SELECT string_agg(w.id, ',' ORDER BY w.id) FROM (SELECT id FROM tasks WHERE ) w) func OrderedIDConcat(driver, where string) string { if IsPostgres(driver) { return fmt.Sprintf( "(SELECT string_agg(w.id, ',' ORDER BY w.id) FROM (SELECT id FROM tasks WHERE %s) w)", where) } return fmt.Sprintf( "(SELECT GROUP_CONCAT(w.id, ',') FROM (SELECT id FROM tasks WHERE %s ORDER BY id) w)", where) }