@@ -70,6 +70,8 @@ An object with the following following optional fields:
70
70
For example in MySQL using ` paramTypes: {quoted: [':']} ` would allow you to use `` :`name` `` syntax,
71
71
while in Transact-SQL ` :"name" ` and ` :[name] ` would work instead.
72
72
See [ identifier syntax wiki page] [ ] for information about differences in support quoted identifiers.
73
+ - ** ` custom ` ** : ` Array<{ regex: string, key?: (text: string) => string }> ` .
74
+ An option to implement custom syntax for parameter placeholders. See below for details.
73
75
74
76
Note that using this config will override the by default supported placeholders types.
75
77
For example PL/SQL supports numbered (` :1 ` ) and named (` :name ` ) placeholders by default.
@@ -89,5 +91,53 @@ The result will be:
89
91
90
92
This config option can be used together with [ params] [ ] to substitute the placeholders with actual values.
91
93
94
+ ## Custom parameter syntax
95
+
96
+ Say, you'd like to support the ` {name} ` parameter placeholders in this SQL:
97
+
98
+ ``` sql
99
+ SELECT id, fname, age FROM person WHERE lname = {lname} AND age > {age};
100
+ ```
101
+
102
+ You can define a regex pattern to match the custom parameters:
103
+
104
+ ``` js
105
+ paramTypes: {
106
+ custom: [{ regex: ' \\ {[a-zA-Z0-9_]+\\ }' }];
107
+ }
108
+ ```
109
+
110
+ Note the double backslashes. You can get around the double-escaping problem by using ` String.raw ` :
111
+
112
+ ``` js
113
+ paramTypes: {
114
+ custom: [{ regex: String .raw ` \{ [a-zA-Z0-9_]+\} ` }];
115
+ }
116
+ ```
117
+
118
+ You can also use the [ params] [ ] option to substitute values of these parameters.
119
+ However by default the parameter names contain the whole string that is matched by the regex:
120
+
121
+ ``` js
122
+ params: { ' {lname}' : ' Doe' , ' {age}' : ' 25' },
123
+ ```
124
+
125
+ To get around this, you can also specify the ` key ` function to extract the name of the parameter:
126
+
127
+ ``` js
128
+ paramTypes: {
129
+ custom: [{
130
+ regex: String .raw ` \{ [a-zA-Z0-9_]+\} `
131
+ key : (text ) => text .slice (1 , - 1 ), // discard first and last char
132
+ }]
133
+ }
134
+ ```
135
+
136
+ Now you can refer to the parameters by their actual name:
137
+
138
+ ``` js
139
+ params: { ' lname' : ' Doe' , ' age' : ' 25' },
140
+ ```
141
+
92
142
[ params ] : ./params.md
93
143
[ identifier syntax wiki page ] : https://github.com/sql-formatter-org/sql-formatter/wiki/identifiers
0 commit comments