Merge branch 'eslint' of github.com:CartoDB/Windshaft-cartodb into eslint
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
// Quote an PostgreSQL identifier if ncecessary
|
||||
function quote_identifier_if_needed (txt) {
|
||||
function quoteIdentifierIfNeeded (txt) {
|
||||
if (txt && !txt.match(/^[a-z_][a-z_0-9]*$/)) {
|
||||
return '"' + txt.replace(/"/g, '""') + '"';
|
||||
} else {
|
||||
@@ -10,13 +10,13 @@ function quote_identifier_if_needed (txt) {
|
||||
}
|
||||
|
||||
// Parse PostgreSQL table name (possibly quoted and with optional schema).+
|
||||
// Returns { schema: 'schema_name', table: 'table_name' }
|
||||
function parse_table_name (table) {
|
||||
function split_as_quoted_parts (table_name) {
|
||||
// Returns { schema: 'schema_name', table: 'tableName' }
|
||||
function parseTableName (table) {
|
||||
function splitAsQuotedParts (tableName) {
|
||||
// parse table into 'parts' that may be quoted, each part
|
||||
// in the parts array being an object { part: 'text', quoted: false/true }
|
||||
var parts = [];
|
||||
var splitted = table_name.split(/"/);
|
||||
var splitted = tableName.split(/"/);
|
||||
for (var i = 0; i < splitted.length; i++) {
|
||||
if (splitted[i] === '') {
|
||||
if (parts.length > 0 && i < splitted.length - 1) {
|
||||
@@ -24,61 +24,61 @@ function parse_table_name (table) {
|
||||
parts[parts.length - 1].part += '"' + splitted[i];
|
||||
}
|
||||
} else {
|
||||
var is_quoted = (i > 0 && splitted[i - 1] === '') ||
|
||||
var isQuoted = (i > 0 && splitted[i - 1] === '') ||
|
||||
(i < splitted.length - 1 && splitted[i + 1] === '');
|
||||
parts.push({ part: splitted[i], quoted: is_quoted });
|
||||
parts.push({ part: splitted[i], quoted: isQuoted });
|
||||
}
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
var parts = split_as_quoted_parts(table);
|
||||
var parts = splitAsQuotedParts(table);
|
||||
|
||||
function split_single_part (part) {
|
||||
var schema_part = null;
|
||||
var table_part = null;
|
||||
function splitSinglePart (part) {
|
||||
var schemaPart = null;
|
||||
var tablePart = null;
|
||||
if (part.quoted) {
|
||||
table_part = part.part;
|
||||
tablePart = part.part;
|
||||
} else {
|
||||
var parts = part.part.split('.');
|
||||
if (parts.length === 1) {
|
||||
schema_part = null;
|
||||
table_part = parts[0];
|
||||
schemaPart = null;
|
||||
tablePart = parts[0];
|
||||
} else if (parts.length === 2) {
|
||||
schema_part = parts[0];
|
||||
table_part = parts[1];
|
||||
schemaPart = parts[0];
|
||||
tablePart = parts[1];
|
||||
} // else invalid table name
|
||||
}
|
||||
return {
|
||||
schema: schema_part,
|
||||
table: table_part
|
||||
schema: schemaPart,
|
||||
table: tablePart
|
||||
};
|
||||
}
|
||||
|
||||
function split_two_parts (part1, part2) {
|
||||
var schema_part = null;
|
||||
var table_part = null;
|
||||
function splitTwoParts (part1, part2) {
|
||||
var schemaPart = null;
|
||||
var tablePart = null;
|
||||
if (part1.quoted && !part2.quoted) {
|
||||
if (part2.part[0] === '.') {
|
||||
schema_part = part1.part;
|
||||
table_part = part2.part.slice(1);
|
||||
schemaPart = part1.part;
|
||||
tablePart = part2.part.slice(1);
|
||||
} // else invalid table name (missing dot)
|
||||
} else if (!part1.quoted && part2.quoted) {
|
||||
if (part1.part[part1.part.length - 1] === '.') {
|
||||
schema_part = part1.part.slice(0, -1);
|
||||
table_part = part2.part;
|
||||
schemaPart = part1.part.slice(0, -1);
|
||||
tablePart = part2.part;
|
||||
} // else invalid table name (missing dot)
|
||||
} // else invalid table name (missing dot)
|
||||
return {
|
||||
schema: schema_part,
|
||||
table: table_part
|
||||
schema: schemaPart,
|
||||
table: tablePart
|
||||
};
|
||||
}
|
||||
|
||||
if (parts.length === 1) {
|
||||
return split_single_part(parts[0]);
|
||||
return splitSinglePart(parts[0]);
|
||||
} else if (parts.length === 2) {
|
||||
return split_two_parts(parts[0], parts[1]);
|
||||
return splitTwoParts(parts[0], parts[1]);
|
||||
} else if (parts.length === 3 && parts[1].part === '.') {
|
||||
return {
|
||||
schema: parts[0].part,
|
||||
@@ -87,12 +87,12 @@ function parse_table_name (table) {
|
||||
} // else invalid table name
|
||||
}
|
||||
|
||||
function table_identifier (parsed_name) {
|
||||
if (parsed_name && parsed_name.table) {
|
||||
if (parsed_name.schema) {
|
||||
return quote_identifier_if_needed(parsed_name.schema) + '.' + quote_identifier_if_needed(parsed_name.table);
|
||||
function tableIdentifier (parsedName) {
|
||||
if (parsedName && parsedName.table) {
|
||||
if (parsedName.schema) {
|
||||
return quoteIdentifierIfNeeded(parsedName.schema) + '.' + quoteIdentifierIfNeeded(parsedName.table);
|
||||
} else {
|
||||
return quote_identifier_if_needed(parsed_name.table);
|
||||
return quoteIdentifierIfNeeded(parsedName.table);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
@@ -100,7 +100,7 @@ function table_identifier (parsed_name) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parse: parse_table_name,
|
||||
quote: quote_identifier_if_needed,
|
||||
table_identifier: table_identifier
|
||||
parse: parseTableName,
|
||||
quote: quoteIdentifierIfNeeded,
|
||||
table_identifier: tableIdentifier
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user