CartoDB-SQL-API/lib/utils/query-info.js

33 lines
804 B
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
2018-05-22 01:13:44 +08:00
const COPY_FORMATS = ['TEXT', 'CSV', 'BINARY'];
2020-12-17 18:11:49 +08:00
const regex = /\bFORMAT\s+(\w+)/;
2018-05-22 01:13:44 +08:00
module.exports = {
2019-12-24 01:19:08 +08:00
getFormatFromCopyQuery (copyQuery) {
2018-05-22 01:13:44 +08:00
let format = 'TEXT'; // Postgres default format
copyQuery = copyQuery.toUpperCase();
2019-12-24 01:19:08 +08:00
if (!copyQuery.startsWith('COPY ')) {
2018-05-22 01:13:44 +08:00
return false;
}
2018-10-24 21:42:33 +08:00
2019-12-24 01:19:08 +08:00
if (copyQuery.includes(' WITH') && copyQuery.includes('FORMAT ')) {
2018-05-22 20:16:22 +08:00
const result = regex.exec(copyQuery);
2018-10-24 21:42:33 +08:00
2018-05-22 20:16:22 +08:00
if (result && result.length === 2) {
if (COPY_FORMATS.includes(result[1])) {
format = result[1];
format = format.toUpperCase();
2018-05-22 20:16:22 +08:00
} else {
format = false;
}
}
2018-05-22 01:13:44 +08:00
}
return format;
}
};