@@ -208,18 +208,91 @@ register("sql.optimize", async (params) => {
208208// ---------------------------------------------------------------------------
209209// sql.format
210210// ---------------------------------------------------------------------------
211+
212+ /**
213+ * Pretty-print SQL by inserting line breaks before major clauses
214+ * and indenting sub-clauses. Used when core.formatSql returns single-line.
215+ */
216+ function prettyPrintSql ( sql : string , indent : number = 2 ) : string {
217+ const pad = " " . repeat ( indent )
218+
219+ // Major clause keywords that start a new line (not indented)
220+ const majorClauses = / \b ( S E L E C T | F R O M | W H E R E | G R O U P \s + B Y | O R D E R \s + B Y | H A V I N G | L I M I T | O F F S E T | U N I O N \s + A L L | U N I O N | I N T E R S E C T | E X C E P T | I N S E R T \s + I N T O | U P D A T E | D E L E T E \s + F R O M | S E T | V A L U E S | R E T U R N I N G ) \b / gi
221+
222+ // Sub-clauses that should be indented
223+ const subClauses = / \b ( L E F T \s + J O I N | R I G H T \s + J O I N | I N N E R \s + J O I N | F U L L \s + (?: O U T E R \s + ) ? J O I N | C R O S S \s + J O I N | J O I N | O N | A N D | O R | W H E N | T H E N | E L S E | E N D ) \b / gi
224+
225+ // First, normalize whitespace
226+ let formatted = sql . replace ( / \s + / g, " " ) . trim ( )
227+
228+ // Break before major clauses
229+ formatted = formatted . replace ( majorClauses , "\n$1" )
230+
231+ // Break before sub-clauses and indent
232+ formatted = formatted . replace ( subClauses , ( match ) => {
233+ const upper = match . toUpperCase ( ) . trim ( )
234+ // AND/OR get indented under WHERE/ON
235+ if ( upper === "AND" || upper === "OR" ) return `\n${ pad } ${ pad } ${ match } `
236+ // WHEN/THEN/ELSE/END for CASE expressions
237+ if ( [ "WHEN" , "THEN" , "ELSE" , "END" ] . includes ( upper ) ) return `\n${ pad } ${ pad } ${ match } `
238+ // JOINs get single indent
239+ return `\n${ match } `
240+ } )
241+
242+ // Indent SELECT column list: break after commas in SELECT clause
243+ const lines = formatted . split ( "\n" )
244+ const result : string [ ] = [ ]
245+ let inSelect = false
246+ for ( const line of lines ) {
247+ const trimmed = line . trim ( )
248+ if ( ! trimmed ) continue
249+
250+ if ( / ^ S E L E C T \b / i. test ( trimmed ) ) {
251+ inSelect = true
252+ // Split comma-separated columns onto separate lines
253+ const afterSelect = trimmed . replace ( / ^ S E L E C T \s + / i, "" ) . trim ( )
254+ if ( afterSelect . includes ( "," ) ) {
255+ result . push ( "SELECT" )
256+ const cols = afterSelect . split ( "," ) . map ( ( c ) => c . trim ( ) )
257+ for ( let i = 0 ; i < cols . length ; i ++ ) {
258+ result . push ( `${ pad } ${ cols [ i ] } ${ i < cols . length - 1 ? "," : "" } ` )
259+ }
260+ } else {
261+ result . push ( trimmed )
262+ }
263+ continue
264+ }
265+
266+ if ( / ^ (?: F R O M | W H E R E | G R O U P | O R D E R | H A V I N G | L I M I T | U N I O N | I N S E R T | U P D A T E | D E L E T E | S E T | V A L U E S ) \b / i. test ( trimmed ) ) {
267+ inSelect = false
268+ }
269+
270+ result . push ( trimmed )
271+ }
272+
273+ return result . join ( "\n" )
274+ }
275+
211276register ( "sql.format" , async ( params ) => {
212277 try {
213278 const raw = core . formatSql ( params . sql , params . dialect )
214279 const result = JSON . parse ( JSON . stringify ( raw ) )
280+ let formattedSql = result . formatted_sql ?? params . sql
281+
282+ // If the core formatter returned single-line SQL, apply pretty-printing
283+ if ( formattedSql && ! formattedSql . includes ( "\n" ) ) {
284+ formattedSql = prettyPrintSql ( formattedSql , params . indent ?? 2 )
285+ }
286+
215287 return {
216288 success : result . success ?? true ,
217- formatted_sql : result . formatted_sql ?? params . sql ,
289+ formatted_sql : formattedSql ,
290+ statement_count : result . statement_count ?? 1 ,
218291 dialect : params . dialect ?? "generic" ,
219292 error : result . error ,
220293 }
221294 } catch ( e ) {
222- return { success : false , formatted_sql : params . sql , dialect : params . dialect ?? "generic" , error : String ( e ) }
295+ return { success : false , formatted_sql : params . sql , statement_count : 0 , dialect : params . dialect ?? "generic" , error : String ( e ) }
223296 }
224297} )
225298
0 commit comments