@vasoyaprince14/sql-analyzer
Version:
🚀 Enhanced SQL database analyzer with AI-powered insights, comprehensive security analysis, RLS policy auditing, and beautiful HTML reports
175 lines (172 loc) • 5.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SQLOptimizer = void 0;
const pg_1 = require("pg");
const index_suggestor_1 = require("./index-suggestor");
const schema_analyzer_1 = require("./schema-analyzer");
const database_health_auditor_1 = require("./database-health-auditor");
const health_report_generator_1 = require("./health-report-generator");
/**
* Legacy SQL Optimizer class for backward compatibility
* For new projects, use EnhancedSQLAnalyzer instead
*
* @deprecated Use EnhancedSQLAnalyzer for new implementations
*/
class SQLOptimizer {
constructor(config) {
this.config = {
maxExecutionTime: 30000,
benchmarkIterations: 5,
enableColors: true,
logLevel: 'info',
...config
};
this.client = new pg_1.Client({
connectionString: this.config.databaseUrl,
});
// Initialize remaining components
this.indexSuggestor = new index_suggestor_1.IndexSuggestor(this.client);
this.schemaAnalyzer = new schema_analyzer_1.SchemaAnalyzer(this.client);
this.healthAuditor = new database_health_auditor_1.DatabaseHealthAuditor(this.client);
this.healthReporter = new health_report_generator_1.HealthReportGenerator();
}
async connect() {
await this.client.connect();
}
async disconnect() {
await this.client.end();
}
/**
* Simplified query analysis
* @deprecated Use EnhancedSQLAnalyzer.analyze() instead
*/
async analyzeQuery(sql) {
try {
// Basic EXPLAIN ANALYZE
const explainResult = await this.client.query(`EXPLAIN ANALYZE ${sql}`);
// Create a minimal AnalysisResult for index suggestions
const tempResult = {
query: sql,
performance: {
executionTime: 0,
rowsReturned: 0,
bufferUsage: undefined,
cacheHitRatio: 0
},
issues: [],
suggestions: [],
timestamp: new Date(),
duration: 0
};
// Get index suggestions
const indexSuggestions = await this.indexSuggestor.suggestIndexes(tempResult);
return {
query: sql,
performance: {
executionTime: 0,
rowsReturned: 0,
bufferUsage: undefined,
cacheHitRatio: 0,
planningTime: 0,
estimatedCost: 0
},
issues: [],
suggestions: indexSuggestions,
executionPlan: explainResult.rows.map(row => row['QUERY PLAN']).join('\n'),
timestamp: new Date(),
duration: 0
};
}
catch (error) {
throw new Error(`Query analysis failed: ${error.message}`);
}
}
/**
* Analyze database schema
* @deprecated Use EnhancedSQLAnalyzer for comprehensive analysis
*/
async analyzeSchema() {
return await this.schemaAnalyzer.analyze();
}
/**
* Generate index suggestions
* @deprecated Use EnhancedSQLAnalyzer for comprehensive analysis
*/
async suggestIndexes(sql) {
if (sql) {
const tempResult = {
query: sql,
performance: {
executionTime: 0,
rowsReturned: 0,
bufferUsage: undefined,
cacheHitRatio: 0
},
issues: [],
suggestions: [],
timestamp: new Date(),
duration: 0
};
return await this.indexSuggestor.suggestIndexes(tempResult);
}
else {
// Return empty array for now - comprehensive analysis should use EnhancedSQLAnalyzer
return [];
}
}
/**
* Perform health audit
* @deprecated Use EnhancedSQLAnalyzer for comprehensive analysis
*/
async performHealthAudit() {
const healthReport = await this.healthAuditor.performHealthAudit();
return healthReport;
}
/**
* Generate report
* @deprecated Use EnhancedSQLAnalyzer for comprehensive reports
*/
async generateReport(result, options = {}) {
const format = options.format || 'cli';
if (typeof result === 'object' && result.databaseInfo) {
// Health report
switch (format) {
case 'json':
return this.healthReporter.generateJSONReport(result);
case 'html':
return this.healthReporter.generateHTMLReport(result);
default:
return this.healthReporter.generateCLIReport(result);
}
}
else {
// Analysis result - simplified report
const report = `
SQL Analysis Report (Legacy)
============================
SQL: ${result.query}
Execution Time: ${result.performance?.executionTime}ms
Planning Time: ${result.performance?.planningTime}ms
Suggestions: ${result.suggestions?.length} found
Issues: ${result.issues?.length} found
Note: This is a simplified legacy report.
For comprehensive analysis, use EnhancedSQLAnalyzer instead.
`;
return report;
}
}
/**
* Get configuration
*/
getConfig() {
return this.config;
}
/**
* Update configuration
*/
updateConfig(updates) {
this.config = { ...this.config, ...updates };
}
}
exports.SQLOptimizer = SQLOptimizer;
//# sourceMappingURL=optimizer.js.map