Skip to content

Commit

Permalink
Fix #737, proper escaping for identifiers
Browse files Browse the repository at this point in the history
Conflicts:
	lib/dialects/oracle/formatter.js
  • Loading branch information
tgriesser committed Mar 13, 2015
1 parent 222ac82 commit 77f2275
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/dialects/postgres/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 25,7 @@ Formatter_PG.prototype.wrapValue = function(value) {
if (value === '*') return value;
var matched = value.match(/(.*?)(\[[0-9]\])/);
if (matched) return this.wrapValue(matched[1]) matched[2];
return '"' value '"';
return '"' value.replace(/"/g, '""') '"';
};

// Memoize the calls to "wrap" for a little extra perf.
Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/sqlite3/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 21,7 @@ Formatter_SQLite3.prototype.operators = [

// Wraps a value (column, tableName) with the correct ticks.
Formatter_SQLite3.prototype.wrapValue = function(value) {
return (value !== '*' ? '"' value '"' : '*');
return (value !== '*' ? '"' value.replace(/"/g, '""') '"' : '*');
};

// Memoize the calls to "wrap" for a little extra perf.
Expand Down
8 changes: 8 additions & 0 deletions test/integration/builder/selects.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 436,14 @@ module.exports = function(knex) {
}
});

it('properly escapes identifiers, #737', function() {
if (knex.client.dialect === 'postgresql') {
var query = knex.select('id","name').from('test').toSQL();
assert(query.sql === 'select "id"",""name" from "test"');
}
});


});

};
16 changes: 16 additions & 0 deletions test/unit/query/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 749,22 @@ module.exports = function(pgclient, mysqlclient, sqlite3client) {
expect(str).to.equal('select "e"."lastname", "e"."salary", (select "avg(salary)" from "employee" where dept_no = e.dept_no) as "avg_sal_dept" from "employee" as "e" where "dept_no" = \'e.dept_no\'');
});

it('escapes queries properly, #737', function() {
testsql(qb()
.select('id","name')
.from('test'),
{
mysql: {
sql: 'select `id","name` from `test`',
bindings: []
},
default: {
sql: 'select "id"",""name" from "test"',
bindings: []
}
});
});

});

};

0 comments on commit 77f2275

Please sign in to comment.