8000 Support for insertId, doc updates · mysqljs/mysql@d6ffdb5 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6ffdb5

Browse files
committed
Support for insertId, doc updates
1 parent 9adc7a8 commit d6ffdb5

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

Readme.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ connection.destroy();
120120

121121
Unlike `end()` the `destroy()` method does not take a callback argument.
122122

123-
## Escaping Query Values
123+
## Escaping query values
124124

125125
In order to avoid SQL Injection attacks, you should always escape any user
126126
provided data before using it inside a SQL query. You can do so using the
@@ -170,7 +170,20 @@ console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQ
170170

171171
```
172172

173-
## Executing Queries in Parallel
173+
## Getting the id of an inserted row
174+
175+
If you are inserting a row into a table with an auto increment primary key, you
176+
can retrieve the insert id like this:
177+
178+
```js
179+
connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result) {
180+
if (err) throw err;
181+
182+
console.log(result.insertId);
183+
});
184+
```
185+
186+
## Executing queries in parallel
174187

175188
The MySQL protocol is sequential, this means that you need multiple connections
176189
to execute queries in parallel. Future version of this module may ship with a
@@ -180,7 +193,7 @@ parallel.
180193

181194
One simple approach is to create one connection per incoming http request.
182195

183-
## Streaming Query Rows
196+
## Streaming query rows
184197

185198
Sometimes you may want to select large quantities of rows and process each of
186199
them as they are received. This can be done like this:
@@ -220,7 +233,7 @@ stream individual row columns, they will always be buffered up entirely. If you
220233
have a good use case for streaming large fields to and from MySQL, I'd love to
221234
get your thoughts and conributions on this.
222235

223-
## Error Handling
236+
## Error handling
224237

225238
This module comes with a consistent approach to error handling that you should
226239
review carefully in order to write solid applications.
@@ -297,7 +310,7 @@ this advice and suppress unhandled errors, you can do this:
297310
connection.on('error', function() {});
298311
```
299312

300-
## Type Casting
313+
## Type casting
301314

302315
For your convenience, this driver will cast mysql types into native JavaScript
303316
types by default. The following mappings exist:
@@ -357,7 +370,7 @@ var query = connection.query('...'):
357370
query.typeCast = false;
358371
```
359372

360-
## Debugging and Reporting Problems
373+
## Debugging and reporting problems
361374

362375
If you are running into problems, one thing that may help is enabling the
363376
`debug` mode for the connection:

lib/protocol/sequences/Sequence.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Sequence.prototype.end = function(err) {
8282
};
8383

8484
Sequence.prototype['OkPacket'] = function(packet) {
85-
this.end(null);
85+
this.end(null, packet);
8686
};
8787

8888
Sequence.prototype['ErrorPacket'] = function(packet) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var common = require('../common');
2+
var connection = common.createConnection();
3+
var assert = require('assert');
4+
5+
common.useTestDb(connection);
6+
7+
var table = 'insert_test';
8+
connection.query([
9+
'CREATE TEMPORARY TABLE `' + table + '` (',
10+
'`id` int(11) unsigned NOT NULL AUTO_INCREMENT,',
11+
'`title` varchar(255),',
12+
'PRIMARY KEY (`id`)',
13+
') ENGINE=InnoDB DEFAULT CHARSET=utf8'
14+
].join('\n'));
15+
16+
var result;
17+
connection.query('INSERT INTO ' + table + ' SET ?', {title: 'test'}, function(err, _result) {
18+
if (err) throw err;
19+
20+
result = _result;
21+
});
22+
connection.end();
23+
24+
process.on('exit', function() {
25+
assert.strictEqual(result.insertId, 1);
26+
});

0 commit comments

Comments
 (0)
0