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 {
26
26
throw new Error ( 'tokenizer() not implemented by subclass' ) ;
27
27
}
28
28
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
+
29
40
/**
30
41
* Formats an SQL query.
31
42
* @param {string } query - The SQL query string to be formatted
32
43
* @return {string } The formatter query
33
44
*/
34
45
public format ( query : string ) : string {
35
- const tokens = this . tokenizer ( ) . tokenize ( query ) ;
46
+ const tokens = this . cachedTokenizer ( ) . tokenize ( query ) ;
36
47
const ast = new Parser ( tokens ) . parse ( ) ;
37
48
const formattedQuery = this . formatAst ( ast , tokens ) ;
38
49
const finalQuery = this . postFormat ( formattedQuery ) ;
You can’t perform that action at this time.
0 commit comments