En mi opinión, la mejor manera de hacer esto es usar expresiones regulares o SED/AWK para formatear todo, nos da la ventaja de reemplazar mapas sobre la marcha. Sin embargo, la posibilidad de que tenga errores de código es alta, por lo que es un poco difícil.
déjame trabajar en ello un poco y puedo ver si se me ocurre una buena solución. ¿Está garantizado que está encapsulando todas las comillas dobles de SQL?
EDITAR
Prueba esto
cd {{directory}} && find . -type f -print0 |
xargs -0 perl -i.bak -pe 's/select/SELECT/g ; s/from/\n\tFROM/g ; s/where/\n\t\tWHERE/g ; s/and/\n\t\tAND/g ; s/order by/\n\t\tORDER BY/g ; s/asc/ASC/g ; s/desc/DESC/g ;'
Aquí hay un ejemplo
$ printf '"select * from whatever where this = that and active = 1 order by something asc";\n' |
> perl -pe 's/select/SELECT/g ; s/from/\n\tFROM/g ; s/where/\n\t\tWHERE/g ; s/and/\n\t\tAND/g ; s/order by/\n\t\tORDER BY/g ; s/asc/ASC/g ; s/desc/DESC/g ;'
"SELECT *
FROM whatever
WHERE this = that
AND active = 1
ORDER BY something ASC";
¿Es bonito? no, para nada, funciona... Sí.
Intentaré crear un archivo de filtro y tal vez un pequeño programa bash o algo así cuando tenga tiempo para ejecutar este lío.
EDITAR
Aquí hay un código revisado, se ve más bonito (más o menos)
printf '$request1 = "select * from whatever where this = that and active = 1 order by something asc";\n' |
perl -pe 's/select/SELECT/gi ; s/from/\n FROM/gi ; s/where/\n WHERE/gi ; s/and/\n AND/gi ; s/order by/\n ORDER BY/gi ; s/asc/ASC/gi ; s/desc/DESC/gi ;' |
awk 'NR == 1 {pad = length($0)/2; print} NR > 1 {gsub(/\r/,""); printf "%*s%s\n", pad, " ", $0}'
__OUTPUTS__
$request1 = "SELECT *
FROM whatever
WHERE this = that
AND active = 1
ORDER BY something ASC";