示例#1
0
        //This method would extract all the ids contain in one sql statement. These ids might be table name, column name or just some id useless. Then, we would check how many table name and column name are contained in the sql statement "p1". Finally, we would connect each table and column in the id list with this method "m".
        public void updateConnection(sqlStmtParser p1, desMethod m, string opt)
        {
            //This means all the component id in one sql statement. This id might be table name, column name or just some id useless.
            List<string> idList = p1.getAllIds();
            if (idList== null) return;
            List<dbTable> tableIds = new List<dbTable>();
            List<dbColumn> columnIds = new List<dbColumn>();

            //We check how many table name the id list contains and how many columns id it contians.
            foreach (var id in idList)
            {
                if (id != null)
                {
                    if (tablesInfo.Find(x => x.name.Equals(id, StringComparison.OrdinalIgnoreCase)) != null)
                    {
                        var tempTable = tablesInfo.Find(x => x.name.Equals(id, StringComparison.OrdinalIgnoreCase));
                        tableIds.Add(tempTable);
                        foreach (var col in tempTable.columns)
                        {
                            if (idList.Find(x => x.Equals(col.name, StringComparison.OrdinalIgnoreCase)) != null) columnIds.Add(col);
                        }
                    }
                }
            }

            //We connect each table id in the id list with this method "m".
            for (int i = 0; i < tablesInfo.Count; i++)
            {
                if (tableIds.Find(x => x == tablesInfo[i]) != null)
                {
                    tablesInfo[i].insertMethod(m, opt, "direct");
                    foreach (var mm in m.followmethods)
                    {
                        var tempMeDes = extractor.getMethodInfo(mm);
                        tablesInfo[i].insertMethod(tempMeDes, opt, "follow");
                    }
                    foreach (var mm in m.finalmethods)
                    {
                        var tempMeDes = extractor.getMethodInfo(mm);
                        tablesInfo[i].insertMethod(tempMeDes, opt, "final");
                    }
                }
            }
            if (columnIds.Count == 0) return;

            //we connect all the column id in the id list with the method m.
            for (int i=0; i < tablesInfo.Count; i++)
            {
                for (int j=0; j<tablesInfo[i].columns.Count; j++)
                {
                    if (columnIds.Find(x => x == tablesInfo[i].columns[j]) != null)
                    {
                        tablesInfo[i].columns[j].insertMethod(m, opt, "direct");
                        foreach (var mm in m.followmethods)
                        {
                            var tempMeDes = extractor.getMethodInfo(mm);
                            tablesInfo[i].columns[j].insertMethod(tempMeDes, opt, "follow");
                        }
                        foreach (var mm in m.finalmethods)
                        {
                            var tempMeDes = extractor.getMethodInfo(mm);
                            tablesInfo[i].columns[j].insertMethod(tempMeDes, opt, "final");
                        }
                    }
                }
            }
        }
示例#2
0
        //Checking each method to figure out all SQL local invocations. The analysis contains: 1. finding all the variables that SQL invocation like variable with type "String" (The details about handle these variables are achieved in method "statIsDeclaration"); 2. finding all the string usage directly like "Select * from tableName (This is achieved by statIsOthers.)
        public void GoThroughMethods(IEnumerable<MethodDefinition> methods)
        {
            string methodNameForTest = "";
            int previoussql = 0;
            foreach (MethodDefinition m in methods)
            {
                //if (CompareTest(m.Name) == false) continue;
                List<string[]> variables = new List<string[]>();
                variables.Clear();
                var stats = m.GetDescendants<Statement>();

                foreach (var stat in stats) //checking each statement in the method
                {
                    if (stat.Content == null) continue;
                    if (stat is DeclarationStatement)
                    {
                        statIsDeclaration(m, stat, variables);
                    }
                    statIsOthers(m, stat, variables);
                }
                if (variables.Count > 0) //means this method might contain sql string
                {
                    foreach (var list in variables)
                    {
                        if (list[1] != "")
                        {
                            sqlStmtParser p1 = new sqlStmtParser(list[1]);
                            if (p1.isStmt == true)
                            {
                                Console.WriteLine(list[0] + " --> " + list[1]);
                                Console.WriteLine("Yes it is! TYPE: " + p1.stmtType);
                                sqlCount++;
                                var methodDes = getMethodInfo(m);
                                methodDes.sqlStmts.Add(p1);
                            }
                        }
                    }
                    if (sqlCount > previoussql) //check is there any new sql in this method
                    {
                        allDirectMethods.Add(getMethodInfo(m));
                        Console.WriteLine(m.GetFullName());
                        methodNameForTest += m.GetFullName() + "\r\n";
                        Console.WriteLine("============================");
                        previoussql = sqlCount;
                    }
                }
            }
        }