diff --git a/lib/adapters/adapter--pg.js b/lib/adapters/adapter--pg.js index 235ecba..719abdc 100644 --- a/lib/adapters/adapter--pg.js +++ b/lib/adapters/adapter--pg.js @@ -127,6 +127,12 @@ Adapter.prototype.query = function( sql, bind, callback ) var origSql = sql; sql = this.ensureStandardSql(sql); sql = this._format(sql, bind); + if ( sql.substr(0,6).toLowerCase() === 'insert' ) { + var isInsert = true; + if (sql.substr(sql.length - 15).toLowerCase().indexOf('returning id') === -1) { + sql += ' RETURNING id'; + } + } if( this._debug ) console.log('sql='+sql); @@ -165,8 +171,9 @@ Adapter.prototype.query = function( sql, bind, callback ) else { - //if( this.lastID && ! isNaN(this.lastID) && this.lastID>0 ) - // me._lastInsertId = this.lastID; + if( isInsert && res.rows[0] && res.rows[0].id && !isNaN(res.rows[0].id) && res.rows[0].id>0 ) { + me._lastInsertId = res.rows[0].id; + } // TODO could save OID here diff --git a/test/adapters-simple-operations.js b/test/adapters-simple-operations.js index da2de9d..299ba6a 100644 --- a/test/adapters-simple-operations.js +++ b/test/adapters-simple-operations.js @@ -224,10 +224,6 @@ var adapterTestSuite = function (adapterName) { }); it('should be able to retrieve the last inserted id', function () { - // pg doesn't support getLastInsertId() - if (-1 < adapterName.indexOf('pg')) { - return; - } expect(dbWrapper.getLastInsertId()).to.equal(2); }); @@ -346,4 +342,4 @@ describe('PgSQL adapter', function () { // ------------------------------------------ end tests -// vim: ts=2 sw=2 et \ No newline at end of file +// vim: ts=2 sw=2 et