_parse_rohs.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const fs = require('fs');
  2. const content = fs.readFileSync('d:/Workspaces/slaj/docs/slaj-rohs_tbl-all_without-data_hg.sql', 'utf8');
  3. // Split by CREATE TABLE
  4. const blocks = content.split(/\n\s*CREATE TABLE /);
  5. const tables = {};
  6. let blockIdx = 0;
  7. for (const block of blocks) {
  8. if (blockIdx === 0) {
  9. blockIdx++;
  10. // First block is before first CREATE TABLE - but check if it starts with CREATE TABLE
  11. const hm = block.match(/^\s*CREATE TABLE\s*"SLAJ_ROHS"\."(\w+)"/);
  12. if (!hm) continue;
  13. // process it anyway
  14. }
  15. const headerMatch = block.match(/^\s*"SLAJ_ROHS"\."(\w+)"/);
  16. if (!headerMatch) continue;
  17. const name = headerMatch[1];
  18. // Find the matching ); for this block by counting parentheses
  19. let depth = 0;
  20. let startIdx = block.indexOf('(');
  21. if (startIdx === -1) continue;
  22. let colsText = '';
  23. for (let i = startIdx + 1; i < block.length && depth >= 0; i++) {
  24. if (block[i] === '(') depth++;
  25. else if (block[i] === ')') {
  26. if (depth === 0) {
  27. colsText = block.substring(startIdx + 1, i);
  28. break;
  29. }
  30. depth--;
  31. }
  32. }
  33. const cols = [];
  34. // Split by lines and parse
  35. const lines = colsText.split('\n');
  36. for (let i = 0; i < lines.length; i++) {
  37. let line = lines[i].trim();
  38. // Accumulate continuation lines
  39. while (i + 1 < lines.length) {
  40. const next = lines[i+1].trim();
  41. if (next.startsWith('"') || /^(CONSTRAINT|PRIMARY KEY|UNIQUE|CHECK|EXCLUDE|USING|FOREIGN)/i.test(next) || next === '' || next.startsWith('--')) {
  42. break;
  43. }
  44. i++;
  45. line += ' ' + next;
  46. if (next.endsWith(',')) break;
  47. }
  48. line = line.replace(/,\s*$/, '').trim();
  49. if (!line || /^(CONSTRAINT|PRIMARY KEY|UNIQUE|CHECK|USING|EXCLUDE|FOREIGN)/i.test(line) || line.startsWith('--')) {
  50. continue;
  51. }
  52. const cm = line.match(/^"(\w+)"\s+(.+)$/);
  53. if (cm) {
  54. const colName = cm[1];
  55. let colType = cm[2].trim();
  56. // Remove trailing NOT NULL, DEFAULT, etc.
  57. colType = colType.replace(/\s+(?:NOT\s+NULL|DEFAULT\s+[^,]+|NULL|COLLATE\s+\w+|PRIMARY\s+KEY|REFERENCES\s+[^,]+)(?=\s|$)/gi, ' ').trim();
  58. cols.push({name: colName.toUpperCase(), type: colType.toUpperCase().replace(/\s+/g, ' ')});
  59. }
  60. }
  61. tables[name] = cols;
  62. blockIdx++;
  63. }
  64. // Save
  65. fs.writeFileSync('d:/Workspaces/slaj/_rohs_tables.json', JSON.stringify(tables, null, 2));
  66. console.log('Total tables:', Object.keys(tables).length);
  67. // Categorize - skip MLOG, MV, backups, etc.
  68. const skipSet = new Set();
  69. for (const t of Object.keys(tables)) {
  70. if (t.startsWith('MLOG$_')) skipSet.add(t);
  71. else if (t.startsWith('MV_')) skipSet.add(t);
  72. else if (t.startsWith('T_VWMS_')) skipSet.add(t);
  73. else if (/_BAK\d*$/.test(t)) skipSet.add(t);
  74. else if (/_MW$/.test(t) || /_MW_/.test(t)) skipSet.add(t);
  75. else if (/_0731$/.test(t)) skipSet.add(t);
  76. else if (/_1125$/.test(t)) skipSet.add(t);
  77. else if (/_BAK25$/.test(t)) skipSet.add(t);
  78. else if (/_0326$/.test(t)) skipSet.add(t);
  79. else if (/_20250407$/.test(t)) skipSet.add(t);
  80. else if (/_20250113$/.test(t)) skipSet.add(t);
  81. else if (/_20250110$/.test(t)) skipSet.add(t);
  82. else if (/BAK2025/.test(t)) skipSet.add(t);
  83. }
  84. const real = Object.keys(tables).filter(t => !skipSet.has(t));
  85. const skip = [...skipSet].sort();
  86. console.log('Real tables:', real.length);
  87. console.log('Skipped (backup/log/MV):', skip.length);
  88. console.log('');
  89. console.log('=== REAL TABLES ===');
  90. real.sort().forEach(t => console.log(t + ' (' + tables[t].length + ' cols)'));
  91. console.log('');
  92. console.log('=== SKIPPED TABLES ===');
  93. skip.forEach(t => console.log(t));