diff --git a/sql-bricks.js b/sql-bricks.js index 33942f6..486b134 100644 --- a/sql-bricks.js +++ b/sql-bricks.js @@ -938,19 +938,33 @@ sql._handleTables = handleTables; function handleTable(table, opts) { - return handleColumn(expandAlias(table), opts); + if (typeof table === 'string') { + return sql._prepareTableIdentifier(table, opts); + } + return prepareTableIdentifier(table, opts); + } + + function prepareTableIdentifier(table, opts) { + return prepareColumnIdentifier(expandAlias(table), opts); } - sql._handleTable = handleTable; + sql._prepareTableIdentifier = prepareTableIdentifier; function handleColumns(cols, opts) { return cols.map(function(col) { return handleColumn(col, opts); }).join(', '); } sql._handleColumns = handleColumns; + 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); + } // handles prefixes before a '.' and suffixes after a ' ' // for example: 'tbl.order AS tbl_order' -> 'tbl."order" AS tbl_order' var unquoted_regex = /^[\w\.]+(( AS)? \w+)?$/i; - function handleColumn(expr, opts) { + function prepareColumnIdentifier(expr, opts) { if (expr instanceof Statement) return expr._toNestedString(opts); @@ -965,7 +979,7 @@ else return expr; } - sql._handleColumn = handleColumn; + sql._prepareColumnIdentifier = prepareColumnIdentifier; function quoteColOrTbl(expr) { var prefix = ''; diff --git a/tests/tests.js b/tests/tests.js index c834045..f5f3493 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -944,6 +944,83 @@ describe('SQL Bricks', function() { }); }); + describe('overriding _prepareTableIdentifier', function() { + var sql; + var oldFunc; + before('override _prepareTableIdentifier', function() { + sql = is_common_js ? require('../sql-bricks.js') : window.SqlBricks; + oldFunc = sql._prepareTableIdentifier; + sql._prepareTableIdentifier = function(expr, opts) { + if (typeof expr === 'string') { + var snake = expr.replace(/[A-Z]/g, l => `_${l.toLowerCase()}`).replace(/^_/, ''); + return oldFunc(snake, opts); + } + return oldFunc(expr, opts); + }; + }); + + after('restore _prepareTableIdentifier', function() { + sql._prepareTableIdentifier = oldFunc; + }); + it('should allow camelCase conversions', function() { + check(sql.select('myColumn').from('myTable'), + 'SELECT "myColumn" FROM my_table') + }); + }); + + describe('overriding _prepareColumnIdentifier', 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) { + var snake = expr.replace(/[A-Z]/g, l => `_${l.toLowerCase()}`).replace(/^_/, ''); + return oldFunc(snake, opts); + }; + }); + + after('restore _prepareColumnIdentifier', function() { + sql._prepareColumnIdentifier = oldFunc; + }); + it('should allow camelCase conversions', function() { + check(sql.select('myColumn').from('my_table'), + 'SELECT my_column FROM my_table') + }); + it('should not affect table names', function() { + check(sql.select('myColumn').from('myTable'), + 'SELECT my_column FROM "myTable"') + }); + it('should not affect functions', function() { + check(sql.select('COUNT(*)').from('myTable'), + 'SELECT COUNT(*) FROM "myTable"') + }); + }); + + 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();