Created
March 29, 2012 10:45
-
-
Save dimsmol/2235773 to your computer and use it in GitHub Desktop.
why pg doesn't need next example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN | |
INSERT | |
ROLLBACK | |
BEGIN | |
INSERT | |
COMMIT | |
BEGIN | |
INSERT | |
COMMIT | |
BEGIN | |
INSERT | |
ROLLBACK | |
BEGIN | |
INSERT | |
COMMIT | |
0.430576 | |
0.0382059 | |
0.0331782 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
); | |
} | |
); | |
} | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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