Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overrideable identifier preparation methods #124

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test
ClaireNeveu committed Sep 20, 2020
commit 0b7af4358b7ac12f921fb8d24b760d6602e374e2
17 changes: 8 additions & 9 deletions sql-bricks.js
Original file line number Diff line number Diff line change
@@ -933,15 +933,14 @@
};

function handleTables(tables, opts) {
var foo = tables.map(function(tbl) { return handleTable(tbl, opts); }).join(', ');
return foo;
return tables.map(function(tbl) { return handleTable(tbl, opts); }).join(', ');
}
sql._handleTables = handleTables;

function handleTable(table, opts) {
if (typeof table === 'string') {
return sql._prepareTableIdentifier(table, opts);
}
if (typeof table === 'string') {
return sql._prepareTableIdentifier(table, opts);
}
return prepareTableIdentifier(table, opts);
}

@@ -957,10 +956,10 @@

var function_regex = /\(/g;
function handleColumn(expr, opts) {
if (typeof expr === 'string' && !function_regex.test(expr)) {
return sql._prepareColumnIdentifier(expr, opts);
}
return prepareColumnIdentifier(expr, opts);
if (typeof expr === 'string' && !function_regex.test(expr)) {
return sql._prepareColumnIdentifier(expr, opts);
}
return prepareColumnIdentifier(expr, opts);
}
// handles prefixes before a '.' and suffixes after a ' '
// for example: 'tbl.order AS tbl_order' -> 'tbl."order" AS tbl_order'
24 changes: 24 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -997,6 +997,30 @@ describe('SQL Bricks', function() {
});
});

describe('overriding _prepareColumnIdentifier 2', function() {
var sql;
var oldFunc;
before('override _prepareColumnIdentifier', function() {
sql = is_common_js ? require('../sql-bricks.js') : window.SqlBricks;
oldFunc = sql._prepareColumnIdentifier;
sql._prepareColumnIdentifier = function(expr, opts) {
if (/[A-Z]/g.test(expr)) {
var snake = expr.replace(/[A-Z]/g, l => `_${l.toLowerCase()}`).replace(/^_/, '');
return oldFunc(`${snake} AS "${expr}"`, opts);
}
return oldFunc(expr, opts);
};
});

after('restore _prepareColumnIdentifier', function() {
sql._prepareColumnIdentifier = oldFunc;
});
it('should allow emitting complex expressions', function() {
check(sql.select('myColumn').from('my_table'),
'SELECT my_column AS "myColumn" FROM my_table')
});
});

describe('_extension()', function() {
it('should shield base', function() {
var ext = sql._extension();