2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-22 01:13:44 +08:00
|
|
|
const COPY_FORMATS = ['TEXT', 'CSV', 'BINARY'];
|
|
|
|
|
|
|
|
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 regex = /\bFORMAT\s+(\w+)/;
|
|
|
|
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];
|
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;
|
|
|
|
}
|
|
|
|
};
|