mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.29k stars 2.53k forks source link

add transaction handling to Client prototype #39

Closed billywhizz closed 13 years ago

billywhizz commented 13 years ago

would it be okay to add transaction handling to the client as per the following patch, or something along these lines?

Client.prototype.begin = function(callback) {
    var client = this;
    this.query("SET autocommit=0", function(err, info) {
        if(err) {
            callback(err);
        }
        else {
            client.query("START TRANSACTION", function(err, info) {
                if(err) {
                    callback(err);
                }
                else {
                    callback(null, info);
                }
            });
        }
    });
};

Client.prototype.commit = function(callback) {
    var client = this;
    this.query("COMMIT", function(err, info) {
        if(err) {
            callback(err);
        }
        else {
            client.query("SET autocommit=1", function(err, info) {
                if(err) {
                    callback(err);
                }
                else {
                    callback(null, info);
                }
            });
        }
    });
};

Client.prototype.rollback = function(callback) {
    var client = this;
    this.query("ROLLBACK", function(err, info) {
        if(err) {
            callback(err);
        }
        else {
            client.query("SET autocommit=1", function(err, info) {
                if(err) {
                    callback(err);
                }
                else {
                    callback(null, info);
                }
            });
        }
    });
};
felixge commented 13 years ago

Hm, I think this kind of stuff would be better suited in a high-level wrapper. My goal with node-mysql is to only provide minimal abstraction on top of the low-level protocol handling.

Also, you probably want to issue both queries in your functions at the same time, otherwise a query could be inserted into the queue in between which would probably lead to undesirable side-affects.

billywhizz commented 13 years ago

OK. Thanks for the feedback.

bminer commented 12 years ago

I have added transaction support to node-mysql. Check out this project:

https://github.com/bminer/node-mysql-queues