public void TestTableComment()
        {
            TGSqlParser sqlParser = new TGSqlParser(EDbVendor.dbvsnowflake);

            sqlParser.sqltext = @"CREATE TABLE ""TestTable""
                                    (
                                    ""Col1"" int NOT NULL COMMENT 'Test comment.',
                                    ""Col2"" int NOT NULL COMMENT 'Test comment 2.'
                                    )
                                    COMMENT = 'Table comment'; ";
            Assert.IsTrue(sqlParser.parse() == 0);
            Assert.IsTrue(sqlParser.sqlstatements[0].sqlstatementtype == ESqlStatementType.sstcreatetable);
            TCreateTableSqlStatement sql = (TCreateTableSqlStatement)sqlParser.sqlstatements[0];

            Assert.IsTrue(sql.TableOptions.Count == 1);
            TCreateTableOption tableOption = sql.TableOptions[0];

            Assert.IsTrue(tableOption.CreateTableOptionType == ECreateTableOption.etoComment);
            Assert.IsTrue(tableOption.CommentToken.ToString().Equals("'Table comment'", StringComparison.CurrentCultureIgnoreCase));
            Assert.IsTrue(sql.TableComment.ToString().Equals("'Table comment'", StringComparison.CurrentCultureIgnoreCase));
            TColumnDefinition cd = sql.ColumnList.getColumn(0);

            Assert.IsTrue(cd.Comment.ToString().Equals("'Test comment.'", StringComparison.CurrentCultureIgnoreCase));
        }
Пример #2
0
        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);
                }
            }
        }