bwscheddebrock_trassenplaner/search_all_netz.js

52 lines
1.4 KiB
JavaScript

const { Pool } = require('pg');
require('dotenv').config();
const pool = new Pool({
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
async function searchAllTables() {
try {
const tablesRes = await pool.query(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'bw_scheddebrock'
`);
for (const tableRow of tablesRes.rows) {
const tableName = tableRow.table_name;
const columnsRes = await pool.query(`
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'bw_scheddebrock' AND table_name = $1
`, [tableName]);
for (const colRow of columnsRes.rows) {
const colName = colRow.column_name;
try {
const searchRes = await pool.query(`
SELECT COUNT(*) FROM bw_scheddebrock."${tableName}"
WHERE CAST("${colName}" AS TEXT) ILIKE '%netz%'
`);
const count = parseInt(searchRes.rows[0].count);
if (count > 0) {
console.log(`Table: ${tableName}, Column: ${colName}, Count: ${count}`);
}
} catch (e) {
// Skip columns that can't be cast to text or other errors
}
}
}
} catch (err) {
console.error("Error:", err);
} finally {
await pool.end();
}
}
searchAllTables();