File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed
Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -26,13 +26,24 @@ export default class Formatter {
2626 throw new Error ( 'tokenizer() not implemented by subclass' ) ;
2727 }
2828
29+ // Cache the tokenizer for each class (each SQL dialect)
30+ // So we wouldn't need to recreate the tokenizer, which is kinda expensive,
31+ // for each call to format() function.
32+ private cachedTokenizer ( ) : Tokenizer {
33+ const cls : Function & { cachedTokenizer ?: Tokenizer } = this . constructor ;
34+ if ( ! cls . cachedTokenizer ) {
35+ cls . cachedTokenizer = this . tokenizer ( ) ;
36+ }
37+ return cls . cachedTokenizer ;
38+ }
39+
2940 /**
3041 * Formats an SQL query.
3142 * @param {string } query - The SQL query string to be formatted
3243 * @return {string } The formatter query
3344 */
3445 public format ( query : string ) : string {
35- const tokens = this . tokenizer ( ) . tokenize ( query ) ;
46+ const tokens = this . cachedTokenizer ( ) . tokenize ( query ) ;
3647 const ast = new Parser ( tokens ) . parse ( ) ;
3748 const formattedQuery = this . formatAst ( ast , tokens ) ;
3849 const finalQuery = this . postFormat ( formattedQuery ) ;
You can’t perform that action at this time.
0 commit comments