@@ -16,7 +16,7 @@ $ npm install sqlstring
1616
1717## Usage
1818
19- <!-- eslint-disable no-unused-vars -->
19+ <!-- eslint-disable no-undef, no- unused-vars -->
2020
2121``` js
2222var SqlString = require (' sqlstring' );
@@ -32,6 +32,8 @@ In order to avoid SQL Injection attacks, you should always escape any user
3232provided data before using it inside a SQL query. You can do so using the
3333` SqlString.escape() ` method:
3434
35+ <!-- eslint-disable no-undef -->
36+
3537``` js
3638var userId = ' some user provided value' ;
3739var 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'
4143Alternatively, you can use ` ? ` characters as placeholders for values you would
4244like to have escaped like this:
4345
46+ <!-- eslint-disable no-undef -->
47+
4448``` js
4549var userId = 1 ;
4650var 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
5155in the following query ` foo ` equals ` a ` , ` bar ` equals ` b ` , ` baz ` equals ` c ` , and
5256` id ` will be ` userId ` :
5357
58+ <!-- eslint-disable no-undef -->
59+
5460``` js
5561var userId = 1 ;
5662var sql = SqlString .format (' UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?' ,
@@ -87,6 +93,8 @@ Different value types are escaped differently, here is how:
8793
8894You may have noticed that this escaping allows you to do neat things like this:
8995
96+ <!-- eslint-disable no-undef -->
97+
9098``` js
9199var post = {id: 1 , title: ' Hello MySQL' };
92100var sql = SqlString .format (' INSERT INTO posts SET ?' , post);
@@ -95,6 +103,8 @@ console.log(sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
95103
96104And the ` toSqlString ` method allows you to form complex queries with functions:
97105
106+ <!-- eslint-disable no-undef -->
107+
98108``` js
99109var CURRENT_TIMESTAMP = { toSqlString : function () { return ' CURRENT_TIMESTAMP()' ; } };
100110var sql = SqlString .format (' UPDATE posts SET modified = ? WHERE id = ?' , [CURRENT_TIMESTAMP , 42 ]);
@@ -108,6 +118,8 @@ placeholder, useful for using functions as dynamic values:
108118** Caution** The string provided to ` SqlString.raw() ` will skip all escaping
109119functions when used, so be careful when passing in unvalidated input.
110120
121+ <!-- eslint-disable no-undef -->
122+
111123``` js
112124var CURRENT_TIMESTAMP = SqlString .raw (' CURRENT_TIMESTAMP()' );
113125var 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 =
117129If you feel the need to escape queries by yourself, you can also use the escaping
118130function directly:
119131
132+ <!-- eslint-disable no-undef -->
133+
120134``` js
121135var sql = ' SELECT * FROM posts WHERE title=' + SqlString .escape (' Hello MySQL' );
122136console .log (sql); // SELECT * FROM posts WHERE title='Hello MySQL'
@@ -127,6 +141,8 @@ console.log(sql); // SELECT * FROM posts WHERE title='Hello MySQL'
127141If you can't trust an SQL identifier (database / table / column name) because it is
128142provided by a user, you should escape it with ` SqlString.escapeId(identifier) ` like this:
129143
144+ <!-- eslint-disable no-undef -->
145+
130146``` js
131147var sorter = ' date' ;
132148var sql = ' SELECT * FROM posts ORDER BY ' + SqlString .escapeId (sorter);
@@ -135,6 +151,8 @@ console.log(sql); // SELECT * FROM posts ORDER BY `date`
135151
136152It also supports adding qualified identifiers. It will escape both parts.
137153
154+ <!-- eslint-disable no-undef -->
155+
138156``` js
139157var sorter = ' date' ;
140158var sql = ' SELECT * FROM posts ORDER BY ' + SqlString .escapeId (' posts.' + sorter);
@@ -144,6 +162,8 @@ console.log(sql); // SELECT * FROM posts ORDER BY `posts`.`date`
144162If you do not want to treat ` . ` as qualified identifiers, you can set the second
145163argument to ` true ` in order to keep the string as a literal identifier:
146164
165+ <!-- eslint-disable no-undef -->
166+
147167``` js
148168var sorter = ' date.2' ;
149169var sql = ' SELECT * FROM posts ORDER BY ' + SqlString .escapeId (sorter, true );
@@ -153,6 +173,8 @@ console.log(sql); // SELECT * FROM posts ORDER BY `date.2`
153173Alternatively, you can use ` ?? ` characters as placeholders for identifiers you would
154174like to have escaped like this:
155175
176+ <!-- eslint-disable no-undef -->
177+
156178``` js
157179var userId = 1 ;
158180var columns = [' username' , ' email' ];
@@ -168,6 +190,8 @@ When you pass an Object to `.escape()` or `.format()`, `.escapeId()` is used to
168190You can use ` SqlString.format ` to prepare a query with multiple insertion points,
169191utilizing the proper escaping for ids and values. A simple example of this follows:
170192
193+ <!-- eslint-disable no-undef -->
194+
171195``` js
172196var userId = 1 ;
173197var inserts = [' users' , ' id' , userId];
@@ -184,6 +208,8 @@ location-specific/timezone-aware `Date`.
184208This can be further combined with the ` SqlString.raw() ` helper to generate SQL
185209that includes MySQL functions as dynamic vales:
186210
211+ <!-- eslint-disable no-undef -->
212+
187213``` js
188214var userId = 1 ;
189215var data = { email: ' foobar@example.com' , modified: SqlString .raw (' NOW()' ) };
0 commit comments