You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simple transaction support is available at the connection level:
707
+
708
+
```js
709
+
connection.beginTransaction(function(err) {
710
+
if (err) { throw err; }
711
+
connection.query('INSERT INTO posts SET title=?', title, function(err, result) {
712
+
if (err) {
713
+
connection.rollback(function() {
714
+
throw err;
715
+
});
716
+
}
717
+
718
+
var log ='Post '+result.insertId+' added';
719
+
720
+
connection.query('INSERT INTO log SET data=?', log, function(err, result) {
721
+
if (err) {
722
+
connection.rollback(function() {
723
+
throw err;
724
+
});
725
+
}
726
+
connection.commit(function(err) {
727
+
if (err) {
728
+
connection.rollback(function() {
729
+
throw err;
730
+
});
731
+
}
732
+
console.log('success!');
733
+
});
734
+
});
735
+
});
736
+
});
737
+
```
738
+
Please note that beginTransaction(), commit() and rollback() are simply convenience
739
+
functions that execute the START TRANSACTION, COMMIT, and ROLLBACK commands respectively.
740
+
It is important to understand that many commands in MySQL can cause an implicit commit,
741
+
as described [in the MySQL documentation](http://dev.mysql.com/doc/refman/5.5/en/implicit-commit.html)
742
+
636
743
## Error handling
637
744
638
745
This module comes with a consistent approach to error handling that you should
@@ -775,7 +882,7 @@ Or on the query level:
775
882
var options = {sql:'...', typeCast:false};
776
883
var query =connection.query(options, function(err, results) {
777
884
778
-
}):
885
+
});
779
886
```
780
887
781
888
You can also pass a function and handle type casting yourself. You're given some
@@ -792,11 +899,23 @@ connection.query({
792
899
}
793
900
returnnext();
794
901
}
795
-
})
902
+
});
903
+
```
904
+
__WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once.( see #539 for discussion)__
905
+
906
+
```
907
+
field.string()
908
+
field.buffer()
909
+
field.geometry()
910
+
```
911
+
are aliases for
912
+
```
913
+
parser.parseLengthCodedString()
914
+
parser.parseLengthCodedBuffer()
915
+
parser.parseGeometryValue()
796
916
```
917
+
__You can find which field function you need to use by looking at: [RowDataPacket.prototype._typeCast](https://github.com/felixge/node-mysql/blob/master/lib/protocol/packets/RowDataPacket.js#L41)__
797
918
798
-
If you need a buffer there's also a `.buffer()` function and also a `.geometry()` one
799
-
both used by the default type cast that you can use.
800
919
801
920
## Connection Flags
802
921
@@ -813,7 +932,7 @@ prepend the flag with a minus sign. To add a flag that is not in the default lis
813
932
The next example blacklists FOUND_ROWS flag from default connection flags.
814
933
815
934
```js
816
-
var connection =mysql.createConnection("mysql://localhost/test?flags=-FOUND_ROWS")
935
+
var connection =mysql.createConnection("mysql://localhost/test?flags=-FOUND_ROWS");
817
936
```
818
937
819
938
### Default Flags
@@ -886,11 +1005,10 @@ For example, if you have an installation of mysql running on localhost:3306 and
886
1005
* Make sure the database (e.g. 'test') you want to use exists and the user you entered has the proper rights to use the test database. (E.g. do not forget to execute the SQL-command ```FLUSH PRIVILEGES``` after you have created the user.)
887
1006
* In a DOS-box (or CMD-shell) in the folder of your application run ```npm install mysql --dev``` or in the mysql folder (```node_modules\mysql```), run ```npm install --dev```. (This will install additional developer-dependencies for node-mysql.)
888
1007
* Run ```npm test mysql``` in your applications folder or ```npm test``` in the mysql subfolder.
889
-
* If you want to log the output into a file use ```npm test mysql > test.log``` or ```npm test > test.log```.
1008
+
* If you want to log the output into a file use ```npm test mysql > test.log``` or ```npm test > test.log```.
890
1009
891
1010
## Todo
892
1011
893
1012
* Prepared statements
894
1013
* setTimeout() for Connection / Query
895
1014
* Support for encodings other than UTF-8 / ASCII
896
-
* API support for transactions, similar to [php](http://www.php.net/manual/en/mysqli.quickstart.transactions.php)
0 commit comments