2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-04 17:26:31 +08:00
|
|
|
// Quote an PostgreSQL identifier if ncecessary
|
2019-10-22 01:07:24 +08:00
|
|
|
function quote_identifier_if_needed (txt) {
|
|
|
|
if (txt && !txt.match(/^[a-z_][a-z_0-9]*$/)) {
|
2019-10-25 01:18:47 +08:00
|
|
|
return '"' + txt.replace(/"/g, '""') + '"';
|
2016-02-04 17:26:31 +08:00
|
|
|
} else {
|
|
|
|
return txt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse PostgreSQL table name (possibly quoted and with optional schema).+
|
|
|
|
// Returns { schema: 'schema_name', table: 'table_name' }
|
2019-10-22 01:07:24 +08:00
|
|
|
function parse_table_name (table) {
|
|
|
|
function split_as_quoted_parts (table_name) {
|
|
|
|
// parse table into 'parts' that may be quoted, each part
|
|
|
|
// in the parts array being an object { part: 'text', quoted: false/true }
|
|
|
|
var parts = [];
|
2019-10-25 01:18:47 +08:00
|
|
|
var splitted = table_name.split(/"/);
|
2019-10-22 01:07:24 +08:00
|
|
|
for (var i = 0; i < splitted.length; i++) {
|
|
|
|
if (splitted[i] === '') {
|
|
|
|
if (parts.length > 0 && i < splitted.length - 1) {
|
|
|
|
i++;
|
|
|
|
parts[parts.length - 1].part += '"' + splitted[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var is_quoted = (i > 0 && splitted[i - 1] === '') ||
|
|
|
|
(i < splitted.length - 1 && splitted[i + 1] === '');
|
|
|
|
parts.push({ part: splitted[i], quoted: is_quoted });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parts;
|
2016-02-04 17:26:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var parts = split_as_quoted_parts(table);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function split_single_part (part) {
|
2016-02-04 17:26:31 +08:00
|
|
|
var schema_part = null;
|
|
|
|
var table_part = null;
|
2019-10-22 01:07:24 +08:00
|
|
|
if (part.quoted) {
|
2016-02-04 17:26:31 +08:00
|
|
|
table_part = part.part;
|
|
|
|
} else {
|
|
|
|
var parts = part.part.split('.');
|
2019-10-22 01:07:24 +08:00
|
|
|
if (parts.length === 1) {
|
2016-02-04 17:26:31 +08:00
|
|
|
schema_part = null;
|
|
|
|
table_part = parts[0];
|
2019-10-22 01:07:24 +08:00
|
|
|
} else if (parts.length === 2) {
|
2016-02-04 17:26:31 +08:00
|
|
|
schema_part = parts[0];
|
|
|
|
table_part = parts[1];
|
|
|
|
} // else invalid table name
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
schema: schema_part,
|
2019-10-22 01:07:24 +08:00
|
|
|
table: table_part
|
2016-02-04 17:26:31 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function split_two_parts (part1, part2) {
|
2016-02-04 17:26:31 +08:00
|
|
|
var schema_part = null;
|
|
|
|
var table_part = null;
|
2019-10-22 01:07:24 +08:00
|
|
|
if (part1.quoted && !part2.quoted) {
|
|
|
|
if (part2.part[0] === '.') {
|
2016-02-04 17:26:31 +08:00
|
|
|
schema_part = part1.part;
|
|
|
|
table_part = part2.part.slice(1);
|
|
|
|
} // else invalid table name (missing dot)
|
2019-10-22 01:07:24 +08:00
|
|
|
} else if (!part1.quoted && part2.quoted) {
|
|
|
|
if (part1.part[part1.part.length - 1] === '.') {
|
2016-02-04 17:26:31 +08:00
|
|
|
schema_part = part1.part.slice(0, -1);
|
|
|
|
table_part = part2.part;
|
|
|
|
} // else invalid table name (missing dot)
|
|
|
|
} // else invalid table name (missing dot)
|
|
|
|
return {
|
|
|
|
schema: schema_part,
|
2019-10-22 01:07:24 +08:00
|
|
|
table: table_part
|
2016-02-04 17:26:31 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
if (parts.length === 1) {
|
2016-02-04 17:26:31 +08:00
|
|
|
return split_single_part(parts[0]);
|
2019-10-22 01:07:24 +08:00
|
|
|
} else if (parts.length === 2) {
|
2016-02-04 17:26:31 +08:00
|
|
|
return split_two_parts(parts[0], parts[1]);
|
2019-10-22 01:07:24 +08:00
|
|
|
} else if (parts.length === 3 && parts[1].part === '.') {
|
2016-02-04 17:26:31 +08:00
|
|
|
return {
|
|
|
|
schema: parts[0].part,
|
2019-10-22 01:07:24 +08:00
|
|
|
table: parts[2].part
|
2016-02-04 17:26:31 +08:00
|
|
|
};
|
|
|
|
} // else invalid table name
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function table_identifier (parsed_name) {
|
|
|
|
if (parsed_name && parsed_name.table) {
|
|
|
|
if (parsed_name.schema) {
|
2016-02-04 17:26:31 +08:00
|
|
|
return quote_identifier_if_needed(parsed_name.schema) + '.' + quote_identifier_if_needed(parsed_name.table);
|
|
|
|
} else {
|
|
|
|
return quote_identifier_if_needed(parsed_name.table);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2019-10-22 01:07:24 +08:00
|
|
|
parse: parse_table_name,
|
|
|
|
quote: quote_identifier_if_needed,
|
|
|
|
table_identifier: table_identifier
|
2016-02-04 17:26:31 +08:00
|
|
|
};
|