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