private void tableTokensInStmt(List <columnInfo> columnInfos, List <tableInfo> tableInfos, TCustomSqlStatement stmt) { for (int i = 0; i < stmt.tables.size(); i++) { if (stmt.tables.getTable(i).BaseTable) { if ((stmt.dbvendor == EDbVendor.dbvmssql) && ((stmt.tables.getTable(i).FullName.Equals("deleted", StringComparison.OrdinalIgnoreCase)) || (stmt.tables.getTable(i).FullName.Equals("inserted", StringComparison.OrdinalIgnoreCase)))) { continue; } if (stmt.tables.getTable(i).EffectType == ETableEffectType.tetSelectInto) { continue; } tableInfo tableInfo = new tableInfo(); tableInfo.fullName = stmt.tables.getTable(i).FullName; tableInfos.Add(tableInfo); for (int j = 0; j < stmt.tables.getTable(i).LinkedColumns.size(); j++) { columnInfo columnInfo = new columnInfo(); columnInfo.table = tableInfo; columnInfo.column = stmt.tables.getTable(i).LinkedColumns.getObjectName(j); columnInfos.Add(columnInfo); } } } if (stmt is TSelectSqlStatement && ((TSelectSqlStatement)stmt).IntoClause != null) { TExpressionList tables = ((TSelectSqlStatement)stmt).IntoClause.ExprList; for (int j = 0; j < tables.size(); j++) { tableInfo tableInfo = new tableInfo(); tableInfo.fullName = tables.getExpression(j).ToString(); tableInfo.stmt = stmt; tableInfos.Add(tableInfo); } } for (int i = 0; i < stmt.Statements.size(); i++) { tableTokensInStmt(columnInfos, tableInfos, stmt.Statements.get(i)); } }
private void analyzeSqlStatement(procedureInfo procedureInfo, TCustomSqlStatement stmt) { if (stmt is TMssqlBlock) { TMssqlBlock block = (TMssqlBlock)stmt; if (block.BodyStatements != null) { for (int i = 0; i < block.BodyStatements.size(); i++) { analyzeSqlStatement(procedureInfo, block.BodyStatements.get(i)); } } } else if (stmt is TMssqlIfElse) { TMssqlIfElse ifElse = (TMssqlIfElse)stmt; if (ifElse.Stmt != null) { analyzeSqlStatement(procedureInfo, ifElse.Stmt); } if (ifElse.Condition != null) { } if (ifElse.ElseStmt != null) { analyzeSqlStatement(procedureInfo, ifElse.ElseStmt); } } else if (stmt is TMssqlDeclare) { TMssqlDeclare declareStmt = (TMssqlDeclare)stmt; if (declareStmt.Subquery != null && declareStmt.Subquery.ToString().Trim().Length > 0) { analyzeSqlStatement(procedureInfo, declareStmt.Subquery); } } else if (stmt is TMssqlExecute && ((TMssqlExecute)stmt).ModuleName != null) { TMssqlExecute executeStmt = (TMssqlExecute)stmt; operateInfo operateInfo = new operateInfo(); operateInfo.objectType = objectType.SP; operateInfo.objectUsed = executeStmt.ModuleName.ToString().Trim(); operateInfo.usageType = usageType.Exec; procedureInfo.operates.Add(operateInfo); } else if (stmt is TCreateTableSqlStatement) { TCreateTableSqlStatement createStmt = (TCreateTableSqlStatement)stmt; TColumnDefinitionList columns = createStmt.ColumnList; operateInfo operateInfo = new operateInfo(); operateInfo.objectType = objectType.Table; operateInfo.objectUsed = createStmt.TargetTable.ToString().Trim(); operateInfo.usageType = usageType.Create; for (int i = 0; i < columns.size(); i++) { TColumnDefinition column = columns.getColumn(i); operateInfo.columns.Add(column.ColumnName.ToString()); } procedureInfo.operates.Add(operateInfo); } else if (stmt is TInsertSqlStatement) { TInsertSqlStatement insertStmt = (TInsertSqlStatement)stmt; TObjectNameList columns = insertStmt.ColumnList; operateInfo operateInfo = new operateInfo(); operateInfo.objectType = objectType.Table; operateInfo.objectUsed = insertStmt.TargetTable.ToString().Trim(); operateInfo.usageType = usageType.Insert; if (columns != null) { for (int i = 0; i < columns.size(); i++) { TObjectName column = columns.getObjectName(i); operateInfo.columns.Add(column.ToString()); } } procedureInfo.operates.Add(operateInfo); // if (insertStmt.ExecStmt != null) // { // analyzeSqlStatement(procedureInfo, insertStmt.ExecStmt); // } } else if (stmt is TUpdateSqlStatement) { TUpdateSqlStatement updateStmt = (TUpdateSqlStatement)stmt; TResultColumnList columns = updateStmt.ResultColumnList; operateInfo operateInfo = new operateInfo(); operateInfo.objectType = objectType.Table; operateInfo.objectUsed = updateStmt.TargetTable.ToString().Trim(); operateInfo.usageType = usageType.Update; for (int i = 0; i < columns.size(); i++) { TResultColumn column = columns.getResultColumn(i); operateInfo.columns.Add(column.Expr.LeftOperand.ToString()); } procedureInfo.operates.Add(operateInfo); } else if (stmt is TDeleteSqlStatement) { TDeleteSqlStatement deleteStmt = (TDeleteSqlStatement)stmt; operateInfo operateInfo = new operateInfo(); operateInfo.objectType = objectType.Table; operateInfo.objectUsed = deleteStmt.TargetTable.ToString().Trim(); operateInfo.usageType = usageType.Delete; procedureInfo.operates.Add(operateInfo); } else if (stmt is TMssqlDropTable) { TMssqlDropTable dropStmt = (TMssqlDropTable)stmt; operateInfo operateInfo = new operateInfo(); operateInfo.objectType = objectType.Table; operateInfo.objectUsed = dropStmt.TargetTable.ToString().Trim(); operateInfo.usageType = usageType.Drop; procedureInfo.operates.Add(operateInfo); } else if (stmt is TDropTableSqlStatement) { TDropTableSqlStatement dropStmt = (TDropTableSqlStatement)stmt; operateInfo operateInfo = new operateInfo(); operateInfo.objectType = objectType.Table; operateInfo.objectUsed = dropStmt.TableName.ToString().Trim(); operateInfo.usageType = usageType.Drop; procedureInfo.operates.Add(operateInfo); } else if (stmt is TSelectSqlStatement) { TSelectSqlStatement selectStmt = (TSelectSqlStatement)stmt; List <columnInfo> columnInfos = new List <columnInfo>(); List <tableInfo> tableInfos = new List <tableInfo>(); tableTokensInStmt(columnInfos, tableInfos, selectStmt); LinkedHashMap <tableInfo, List <columnInfo> > columnMap = new LinkedHashMap <tableInfo, List <columnInfo> >(); for (int i = 0; i < columnInfos.Count; i++) { columnInfo column = columnInfos[i]; tableInfo table = column.table; if (columnMap.ContainsKey(table)) { List <columnInfo> columns = (List <columnInfo>)columnMap[table]; bool flag = false; foreach (columnInfo temp in columns) { if (temp.ToString().Equals(column.ToString(), StringComparison.CurrentCultureIgnoreCase)) { flag = true; break; } } if (!flag) { columns.Add(column); } } else { List <columnInfo> columns = new List <columnInfo>(); columnMap[table] = columns; columns.Add(column); } } for (int i = 0; i < tableInfos.Count; i++) { operateInfo operateInfo = new operateInfo(); operateInfo.objectType = objectType.Table; operateInfo.objectUsed = tableInfos[i].ToString(); if (tableInfos[i].stmt is TSelectSqlStatement && ((TSelectSqlStatement)tableInfos[i].stmt).IntoClause != null) { operateInfo.usageType = usageType.Insert; } else { operateInfo.usageType = usageType.Read; } if (columnMap.ContainsKey(tableInfos[i])) { foreach (columnInfo column in (List <columnInfo>)columnMap[tableInfos[i]]) { operateInfo.columns.Add(column.ToString()); operateInfo.objectUsed = column.table.ToString(); } } procedureInfo.operates.Add(operateInfo); } } }