@@ -16,7 +16,7 @@ $ npm install sqlstring
16
16
17
17
## Usage
18
18
19
- <!-- eslint-disable no-unused-vars -->
19
+ <!-- eslint-disable no-undef, no- unused-vars -->
20
20
21
21
``` js
22
22
var SqlString = require (' sqlstring' );
@@ -32,6 +32,8 @@ In order to avoid SQL Injection attacks, you should always escape any user
32
32
provided data before using it inside a SQL query. You can do so using the
33
33
` SqlString.escape() ` method:
34
34
35
+ <!-- eslint-disable no-undef -->
36
+
35
37
``` js
36
38
var userId = ' some user provided value' ;
37
39
var sql = ' SELECT * FROM users WHERE id = ' + SqlString .escape (userId);
@@ -41,6 +43,8 @@ console.log(sql); // SELECT * FROM users WHERE id = 'some user provided value'
41
43
Alternatively, you can use ` ? ` characters as placeholders for values you would
42
44
like to have escaped like this:
43
45
46
+ <!-- eslint-disable no-undef -->
47
+
44
48
``` js
45
49
var userId = 1 ;
46
50
var sql = SqlString .format (' SELECT * FROM users WHERE id = ?' , [userId]);
@@ -51,6 +55,8 @@ Multiple placeholders are mapped to values in the same order as passed. For exam
51
55
in the following query ` foo ` equals ` a ` , ` bar ` equals ` b ` , ` baz ` equals ` c ` , and
52
56
` id ` will be ` userId ` :
53
57
58
+ <!-- eslint-disable no-undef -->
59
+
54
60
``` js
55
61
var userId = 1 ;
56
62
var sql = SqlString .format (' UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?' ,
@@ -87,6 +93,8 @@ Different value types are escaped differently, here is how:
87
93
88
94
You may have noticed that this escaping allows you to do neat things like this:
89
95
96
+ <!-- eslint-disable no-undef -->
97
+
90
98
``` js
91
99
var post = {id: 1 , title: ' Hello MySQL' };
92
100
var sql = SqlString .format (' INSERT INTO posts SET ?' , post);
@@ -95,6 +103,8 @@ console.log(sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
95
103
96
104
And the ` toSqlString ` method allows you to form complex queries with functions:
97
105
106
+ <!-- eslint-disable no-undef -->
107
+
98
108
``` js
99
109
var CURRENT_TIMESTAMP = { toSqlString : function () { return ' CURRENT_TIMESTAMP()' ; } };
100
110
var sql = SqlString .format (' UPDATE posts SET modified = ? WHERE id = ?' , [CURRENT_TIMESTAMP , 42 ]);
@@ -108,6 +118,8 @@ placeholder, useful for using functions as dynamic values:
108
118
** Caution** The string provided to ` SqlString.raw() ` will skip all escaping
109
119
functions when used, so be careful when passing in unvalidated input.
110
120
121
+ <!-- eslint-disable no-undef -->
122
+
111
123
``` js
112
124
var CURRENT_TIMESTAMP = SqlString .raw (' CURRENT_TIMESTAMP()' );
113
125
var sql = SqlString .format (' UPDATE posts SET modified = ? WHERE id = ?' , [CURRENT_TIMESTAMP , 42 ]);
@@ -117,6 +129,8 @@ console.log(sql); // UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id =
117
129
If you feel the need to escape queries by yourself, you can also use the escaping
118
130
function directly:
119
131
132
+ <!-- eslint-disable no-undef -->
133
+
120
134
``` js
121
135
var sql = ' SELECT * FROM posts WHERE title=' + SqlString .escape (' Hello MySQL' );
122
136
console .log (sql); // SELECT * FROM posts WHERE title='Hello MySQL'
@@ -127,6 +141,8 @@ console.log(sql); // SELECT * FROM posts WHERE title='Hello MySQL'
127
141
If you can't trust an SQL identifier (database / table / column name) because it is
128
142
provided by a user, you should escape it with ` SqlString.escapeId(identifier) ` like this:
129
143
144
+ <!-- eslint-disable no-undef -->
145
+
130
146
``` js
131
147
var sorter = ' date' ;
132
148
var sql = ' SELECT * FROM posts ORDER BY ' + SqlString .escapeId (sorter);
@@ -135,6 +151,8 @@ console.log(sql); // SELECT * FROM posts ORDER BY `date`
135
151
136
152
It also supports adding qualified identifiers. It will escape both parts.
137
153
154
+ <!-- eslint-disable no-undef -->
155
+
138
156
``` js
139
157
var sorter = ' date' ;
140
158
var sql = ' SELECT * FROM posts ORDER BY ' + SqlString .escapeId (' posts.' + sorter);
@@ -144,6 +162,8 @@ console.log(sql); // SELECT * FROM posts ORDER BY `posts`.`date`
144
162
If you do not want to treat ` . ` as qualified identifiers, you can set the second
145
163
argument to ` true ` in order to keep the string as a literal identifier:
146
164
165
+ <!-- eslint-disable no-undef -->
166
+
147
167
``` js
148
168
var sorter = ' date.2' ;
149
169
var sql = ' SELECT * FROM posts ORDER BY ' + SqlString .escapeId (sorter, true );
@@ -153,6 +173,8 @@ console.log(sql); // SELECT * FROM posts ORDER BY `date.2`
153
173
Alternatively, you can use ` ?? ` characters as placeholders for identifiers you would
154
174
like to have escaped like this:
155
175
176
+ <!-- eslint-disable no-undef -->
177
+
156
178
``` js
157
179
var userId = 1 ;
158
180
var columns = [' username' , ' email' ];
@@ -168,6 +190,8 @@ When you pass an Object to `.escape()` or `.format()`, `.escapeId()` is used to
168
190
You can use ` SqlString.format ` to prepare a query with multiple insertion points,
169
191
utilizing the proper escaping for ids and values. A simple example of this follows:
170
192
193
+ <!-- eslint-disable no-undef -->
194
+
171
195
``` js
172
196
var userId = 1 ;
173
197
var inserts = [' users' , ' id' , userId];
@@ -184,6 +208,8 @@ location-specific/timezone-aware `Date`.
184
208
This can be further combined with the ` SqlString.raw() ` helper to generate SQL
185
209
that includes MySQL functions as dynamic vales:
186
210
211
+ <!-- eslint-disable no-undef -->
212
+
187
213
``` js
188
214
var userId = 1 ;
189
215
var data
= { email
: ' [email protected] ' , modified
: SqlString .
raw (
' NOW()' ) };
0 commit comments