|
1 | 1 | const fs = require('node:fs/promises'); |
2 | 2 |
|
3 | | -async function writeSQL(statement) { |
4 | | - try { |
5 | | - await fs.writeFile(`sql/${process.argv[2]}.sql`, statement); |
6 | | - } catch (err) { |
7 | | - console.log(err); |
| 3 | +async function writeSQL(statement, saveFileAs = '') { |
| 4 | + try { |
| 5 | + const destinationFile = process.argv[2] || saveFileAs |
| 6 | + |
| 7 | + if (!destinationFile) { |
| 8 | + throw new Error("Missing saveFileAs parameter") |
8 | 9 | } |
| 10 | + |
| 11 | + await fs.writeFile(`sql/${process.argv[2]}.sql`, statement); |
| 12 | + } catch (err) { |
| 13 | + console.log(err); |
| 14 | + } |
9 | 15 | } |
10 | 16 |
|
11 | 17 | async function readCSV(csvFileName = '') { |
12 | | - const fileAndTableName = process.argv[2] || csvFileName |
13 | | - |
14 | 18 | try { |
| 19 | + |
| 20 | + const fileAndTableName = process.argv[2] || csvFileName |
| 21 | + |
| 22 | + if (!fileAndTableName) { |
| 23 | + throw new Error("Missing csvFileName parameter") |
| 24 | + } |
| 25 | + |
15 | 26 | const data = await fs.readFile(`csv/${fileAndTableName}.csv`, { encoding: 'utf8' }); |
16 | | - |
| 27 | + |
17 | 28 | const linesArray = data.split(/\r|\n/).filter(line => line) |
18 | 29 | const columnNames = linesArray.shift().split(",") |
19 | | - |
20 | | - let insertStart = `INSERT INTO ${fileAndTableName} (` |
21 | | - columnNames.forEach(name => insertStart += `${name}, `) |
22 | | - insertStart = insertStart.slice(0,-2) + ")\nVALUES\n" |
| 30 | + |
| 31 | + let beginSQLInsert = `INSERT INTO ${fileAndTableName} (` |
| 32 | + columnNames.forEach(name => beginSQLInsert += `${name}, `) |
| 33 | + beginSQLInsert = beginSQLInsert.slice(0, -2) + ")\nVALUES\n" |
23 | 34 |
|
24 | 35 | let values = '' |
25 | 36 | linesArray.forEach(line => { |
26 | | - const arr = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/) |
27 | | - |
28 | | - if (arr.length > columnNames.length) { |
29 | | - console.log(arr) |
30 | | - throw new Error("Too Many Values in row") |
31 | | - } |
| 37 | + // Parses each line of CSV into field values array |
| 38 | + const arr = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/) |
| 39 | + |
| 40 | + if (arr.length > columnNames.length) { |
| 41 | + console.log(arr) |
| 42 | + throw new Error("Too Many Values in row") |
| 43 | + } else if (arr.length < columnNames.length) { |
| 44 | + console.log(arr) |
| 45 | + throw new Error("Too Few Values in row") |
| 46 | + } |
32 | 47 |
|
33 | | - const timestampRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}$/ |
34 | | - // value.match(timestampRegex) || |
35 | | - let valueLine = '\t(' |
36 | | - arr.forEach(value => { |
37 | | - // NULL values |
38 | | - // Numbers, Strings accepted as numbers, and Booleans (0 or 1) |
39 | | - if (value === "NULL" || !isNaN(+value)) { |
40 | | - valueLine += `${value}, ` |
41 | | - } |
42 | | - else { |
43 | | - // If a string is wrapped in quotes, it doesn't need more |
44 | | - if (value.at(0) === '"') valueLine += `${value}, ` |
45 | | - else { |
46 | | - // This wraps strings in quotes that need them |
47 | | - // also wraps timestamps |
48 | | - valueLine += `"${value}", ` |
49 | | - } |
50 | | - } |
51 | | - }) |
52 | | - valueLine = valueLine.slice(0,-2) + "),\n" |
53 | | - values += valueLine |
| 48 | + let valueLine = '\t(' |
| 49 | + arr.forEach(value => { |
| 50 | + // Matches NULL values, Numbers, |
| 51 | + // Strings accepted as numbers, and Booleans (0 or 1) |
| 52 | + if (value === "NULL" || !isNaN(+value)) { |
| 53 | + valueLine += `${value}, ` |
| 54 | + } |
| 55 | + else { |
| 56 | + // If a string is wrapped in quotes, it doesn't need more |
| 57 | + if (value.at(0) === '"') valueLine += `${value}, ` |
| 58 | + else { |
| 59 | + // This wraps strings in quotes |
| 60 | + // also wraps timestamps |
| 61 | + valueLine += `"${value}", ` |
| 62 | + } |
| 63 | + } |
| 64 | + }) |
| 65 | + valueLine = valueLine.slice(0, -2) + "),\n" |
| 66 | + values += valueLine |
54 | 67 | }) |
55 | | - values = values.slice(0,-2) + ";" |
| 68 | + values = values.slice(0, -2) + ";" |
56 | 69 |
|
57 | | - const sqlStatement = insertStart + values |
| 70 | + const sqlStatement = beginSQLInsert + values |
58 | 71 |
|
59 | 72 | // Write File |
60 | 73 | writeSQL(sqlStatement) |
|
0 commit comments