1
1
import _ from "lodash" ;
2
- import sqlTokenTypes from "./tokenTypes" ;
2
+ import tokenTypes from "./tokenTypes" ;
3
3
import Indentation from "./Indentation" ;
4
4
import InlineBlock from "./InlineBlock" ;
5
+ import Params from "./Params" ;
5
6
6
7
export default class Formatter {
7
8
/**
8
9
* @param {Object } cfg
9
- * @param {Object } cfg.indent
10
+ * @param {Object } cfg.indent
11
+ * @param {Object } cfg.params
10
12
* @param {Tokenizer } tokenizer
11
13
*/
12
14
constructor ( cfg , tokenizer ) {
13
15
this . cfg = cfg || { } ;
14
16
this . indentation = new Indentation ( this . cfg . indent ) ;
15
17
this . inlineBlock = new InlineBlock ( ) ;
18
+ this . params = new Params ( this . cfg . params ) ;
16
19
this . tokenizer = tokenizer ;
17
20
this . previousReservedWord = { } ;
18
21
}
19
22
20
23
/**
21
- * Format the whitespace in a SQL string to make it easier to read.
24
+ * Formats whitespaces in a SQL string to make it easier to read.
22
25
*
23
26
* @param {String } query The SQL query string
24
27
* @return {String } formatted query
@@ -34,33 +37,36 @@ export default class Formatter {
34
37
let formattedQuery = "" ;
35
38
36
39
tokens . forEach ( ( token , index ) => {
37
- if ( token . type === sqlTokenTypes . WHITESPACE ) {
40
+ if ( token . type === tokenTypes . WHITESPACE ) {
38
41
return ;
39
42
}
40
- else if ( token . type === sqlTokenTypes . LINE_COMMENT ) {
43
+ else if ( token . type === tokenTypes . LINE_COMMENT ) {
41
44
formattedQuery = this . formatLineComment ( token , formattedQuery ) ;
42
45
}
43
- else if ( token . type === sqlTokenTypes . BLOCK_COMMENT ) {
46
+ else if ( token . type === tokenTypes . BLOCK_COMMENT ) {
44
47
formattedQuery = this . formatBlockComment ( token , formattedQuery ) ;
45
48
}
46
- else if ( token . type === sqlTokenTypes . RESERVED_TOPLEVEL ) {
49
+ else if ( token . type === tokenTypes . RESERVED_TOPLEVEL ) {
47
50
formattedQuery = this . formatToplevelReservedWord ( token , formattedQuery ) ;
48
51
this . previousReservedWord = token ;
49
52
}
50
- else if ( token . type === sqlTokenTypes . RESERVED_NEWLINE ) {
53
+ else if ( token . type === tokenTypes . RESERVED_NEWLINE ) {
51
54
formattedQuery = this . formatNewlineReservedWord ( token , formattedQuery ) ;
52
55
this . previousReservedWord = token ;
53
56
}
54
- else if ( token . type === sqlTokenTypes . RESERVED ) {
57
+ else if ( token . type === tokenTypes . RESERVED ) {
55
58
formattedQuery = this . formatWithSpaces ( token , formattedQuery ) ;
56
59
this . previousReservedWord = token ;
57
60
}
58
- else if ( token . type === sqlTokenTypes . OPEN_PAREN ) {
61
+ else if ( token . type === tokenTypes . OPEN_PAREN ) {
59
62
formattedQuery = this . formatOpeningParentheses ( tokens , index , formattedQuery ) ;
60
63
}
61
- else if ( token . type === sqlTokenTypes . CLOSE_PAREN ) {
64
+ else if ( token . type === tokenTypes . CLOSE_PAREN ) {
62
65
formattedQuery = this . formatClosingParentheses ( token , formattedQuery ) ;
63
66
}
67
+ else if ( token . type === tokenTypes . PLACEHOLDER ) {
68
+ formattedQuery = this . formatPlaceholder ( token , formattedQuery ) ;
69
+ }
64
70
else if ( token . value === "," ) {
65
71
formattedQuery = this . formatComma ( token , formattedQuery ) ;
66
72
}
@@ -113,7 +119,7 @@ export default class Formatter {
113
119
formatOpeningParentheses ( tokens , index , query ) {
114
120
// Take out the preceding space unless there was whitespace there in the original query or another opening parens
115
121
const previousToken = tokens [ index - 1 ] ;
116
- if ( previousToken && previousToken . type !== sqlTokenTypes . WHITESPACE && previousToken . type !== sqlTokenTypes . OPEN_PAREN ) {
122
+ if ( previousToken && previousToken . type !== tokenTypes . WHITESPACE && previousToken . type !== tokenTypes . OPEN_PAREN ) {
117
123
query = _ . trimEnd ( query ) ;
118
124
}
119
125
query += tokens [ index ] . value ;
@@ -139,6 +145,10 @@ export default class Formatter {
139
145
}
140
146
}
141
147
148
+ formatPlaceholder ( token , query ) {
149
+ return query + this . params . get ( token ) + " " ;
150
+ }
151
+
142
152
// Commas start a new line (unless within inline parentheses or SQL "LIMIT" clause)
143
153
formatComma ( token , query ) {
144
154
query = _ . trimEnd ( query ) + token . value + " " ;
0 commit comments