2018-05-22 01:13:44 +08:00
|
|
|
const COPY_FORMATS = ['TEXT', 'CSV', 'BINARY'];
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getFormatFromCopyQuery(copyQuery) {
|
|
|
|
let format = 'TEXT'; // Postgres default format
|
|
|
|
|
|
|
|
copyQuery = copyQuery.toUpperCase();
|
|
|
|
|
2018-05-22 20:16:22 +08:00
|
|
|
if (!copyQuery.startsWith("COPY ")) {
|
2018-05-22 01:13:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-22 20:16:22 +08:00
|
|
|
|
2018-06-05 00:15:28 +08:00
|
|
|
if(copyQuery.includes(' WITH') && copyQuery.includes('FORMAT ')) {
|
2018-05-22 20:16:22 +08:00
|
|
|
const regex = /\bFORMAT\s+(\w+)/;
|
|
|
|
const result = regex.exec(copyQuery);
|
|
|
|
|
|
|
|
if (result && result.length === 2) {
|
|
|
|
if (COPY_FORMATS.includes(result[1])) {
|
|
|
|
format = result[1];
|
2018-06-05 04:12:06 +08:00
|
|
|
format = format.toUpperCase();
|
2018-05-22 20:16:22 +08:00
|
|
|
} else {
|
|
|
|
format = false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 01:13:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
};
|