Form() public static method

public static Form ( PlSqlParser context ) : SqlTableConstraint
context PlSqlParser
return Deveel.Data.Sql.Statements.SqlTableConstraint
コード例 #1
0
            public static IAlterTableAction Build(PlSqlParser.AlterTableActionContext context)
            {
                if (context.addColumnAction() != null)
                {
                    var column = TableColumn.Form(context.addColumnAction().tableColumn(), new List <ColumnConstraint>());
                    return(new AddColumnAction(column));
                }
                if (context.addConstraintAction() != null)
                {
                    var constraint = TableConstraint.Form(context.addConstraintAction().tableConstraint());
                    return(new AddConstraintAction(constraint));
                }

                if (context.dropColumnAction() != null)
                {
                    var columnName = context.dropColumnAction().id().GetText();
                    return(new DropColumnAction(columnName));
                }

                if (context.dropConstraintAction() != null)
                {
                    var constraintName = context.dropConstraintAction().id().GetText();
                    return(new DropConstraintAction(constraintName));
                }

                if (context.dropPrimaryKeyAction() != null)
                {
                    return(new DropPrimaryKeyAction());
                }

                if (context.dropDefaultAction() != null)
                {
                    var columnName = Name.Simple(context.dropDefaultAction().columnName());
                    return(new DropDefaultAction(columnName));
                }

                if (context.setDefaultAction() != null)
                {
                    var columnName   = Name.Simple(context.setDefaultAction().columnName());
                    var defaultValue = Expression.Build(context.setDefaultAction().expressionWrapper());
                    return(new SetDefaultAction(columnName, defaultValue));
                }

                throw new ParseCanceledException("The ALTER TABLE action is not supported");
            }
コード例 #2
0
        public static SqlStatement Build(PlSqlParser.CreateTableStatementContext context)
        {
            var tableName           = Name.Object(context.objectName());
            var columnOrConstraints = context.columnOrConstraintList().columnOrConstraint();

            bool ifNotExists = context.IF() != null && context.NOT() != null && context.EXISTS() != null;
            bool temporary   = context.TEMPORARY() != null;

            var tableColumns      = new List <SqlTableColumn>();
            var tableConstraints  = new List <SqlTableConstraint>();
            var columnConstraints = new List <ColumnConstraint>();

            foreach (var columnOrConstraint in columnOrConstraints)
            {
                if (columnOrConstraint.tableColumn() != null)
                {
                    tableColumns.Add(TableColumn.Form(columnOrConstraint.tableColumn(), columnConstraints));
                }
                else if (columnOrConstraint.tableConstraint() != null)
                {
                    tableConstraints.Add(TableConstraint.Form(columnOrConstraint.tableConstraint()));
                }
            }

            if (columnConstraints.Count > 0)
            {
                var constraintGroups = columnConstraints.GroupBy(x => x.Type);

                foreach (var constraintGroup in constraintGroups)
                {
                    var columnNames = constraintGroup.Select(x => x.ColumnName).ToArray();
                    var index       = tableConstraints.FindIndex(x => String.IsNullOrEmpty(x.ConstraintName) &&
                                                                 x.ConstraintType == constraintGroup.Key);
                    if (index != -1)
                    {
                        var unnamedConstraint = tableConstraints[index];
                        var columns           = new List <string>(unnamedConstraint.Columns);
                        foreach (var columnName in columnNames)
                        {
                            if (!columns.Contains(columnName))
                            {
                                columns.Add(columnName);
                            }
                        }

                        // TODO: set the new columns
                    }
                    else
                    {
                        tableConstraints.Add(new SqlTableConstraint(constraintGroup.Key, columnNames));
                    }
                }
            }

            var block = new SequenceOfStatements();

            block.Statements.Add(new CreateTableStatement(tableName, tableColumns)
            {
                IfNotExists = ifNotExists,
                Temporary   = temporary
            });

            foreach (var constraint in tableConstraints)
            {
                block.Statements.Add(new AlterTableStatement(tableName, new AddConstraintAction(constraint)));
            }

            return(block);
        }