Skip to content

Instantly share code, notes, and snippets.

@dimsmol
Created March 29, 2012 10:45
Show Gist options
  • Save dimsmol/2235773 to your computer and use it in GitHub Desktop.
Save dimsmol/2235773 to your computer and use it in GitHub Desktop.
why pg doesn't need next example
BEGIN
INSERT
ROLLBACK
BEGIN
INSERT
COMMIT
BEGIN
INSERT
COMMIT
BEGIN
INSERT
ROLLBACK
BEGIN
INSERT
COMMIT
0.430576
0.0382059
0.0331782
function insertRandomNumber(cb) {
async.waterfall([
function (cb) {
console.log('BEGIN');
client.query('BEGIN', cb);
},
function (result, cb) {
console.log('INSERT');
client.query('INSERT INTO foo VALUES (random()) RETURNING bar', cb);
},
function (result, cb) {
if (result.rows[0].bar < 0.5) {
console.log('COMMIT');
client.query('COMMIT', cb);
} else {
console.log('ROLLBACK');
client.query('ROLLBACK', cb);
}
}
], cb);
}
async.waterfall([
function (cb) { client.query('CREATE TEMP TABLE foo (bar real)', cb); },
function (result, cb) {
var i = 0;
async.whilst(
function () { return i < 5; },
function (cb) { i++; insertRandomNumber(cb); },
function (err) {
client.query('SELECT bar FROM foo',
function (err, results) {
client.end();
for (var i = 0; i < results.rowCount; i++)
console.log(results.rows[i].bar);
cb();
}
);
}
);
}
]);
function insertRandomNumber(cb) {
console.log('BEGIN');
client.query('BEGIN', function () {
console.log('INSERT');
client.query('INSERT INTO foo VALUES (random()) RETURNING bar', function (err, result) {
if (result.rows[0].bar < 0.5) {
console.log('COMMIT');
client.query('COMMIT', cb);
} else {
console.log('ROLLBACK');
client.query('ROLLBACK', cb);
}
});
});
}
client.query('CREATE TEMP TABLE foo (bar real)', function () {
var i = 0;
var doInserts = function () {
insertRandomNumber(function () {
i++;
if (i < 5) {
doInserts();
} else {
client.query('SELECT bar FROM foo', function (err, results) {
client.end();
for (var i = 0; i < results.rowCount; i++)
console.log(results.rows[i].bar);
});
}
});
};
doInserts();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment