Esempio n. 1
0
        public SqlTableConstraint BuildConstraint()
        {
            if (String.Equals(ConstraintTypeNames.Check, ConstraintType, StringComparison.OrdinalIgnoreCase))
            {
                var exp = ExpressionBuilder.Build(CheckExpression);
                return(new SqlTableConstraint(ConstraintName, Tables.ConstraintType.Check, Columns.ToArray())
                {
                    CheckExpression = exp
                });
            }
            if (String.Equals(ConstraintTypeNames.PrimaryKey, ConstraintType, StringComparison.OrdinalIgnoreCase))
            {
                return(SqlTableConstraint.PrimaryKey(ConstraintName, Columns.ToArray()));
            }
            if (String.Equals(ConstraintTypeNames.UniqueKey, ConstraintType, StringComparison.OrdinalIgnoreCase))
            {
                return(SqlTableConstraint.UniqueKey(ConstraintName, Columns.ToArray()));
            }
            if (String.Equals(ConstraintTypeNames.ForeignKey, ConstraintType, StringComparison.OrdinalIgnoreCase))
            {
                var fTable   = ReferencedTableName.Name;
                var fColumns = ReferencedColumns;
                var onDelete = ForeignKeyAction.NoAction;
                var onUpdate = ForeignKeyAction.NoAction;

                if (!String.IsNullOrEmpty(OnDeleteAction))
                {
                    onDelete = SqlCodeObjectBuilder.GetForeignKeyAction(OnDeleteAction);
                }
                if (!String.IsNullOrEmpty(OnUpdateAction))
                {
                    onUpdate = SqlCodeObjectBuilder.GetForeignKeyAction(OnUpdateAction);
                }

                var fkey = SqlTableConstraint.ForeignKey(ConstraintName, Columns.ToArray(), fTable,
                                                         fColumns.ToArray(), onDelete, onUpdate);

                return(fkey);
            }

            throw new NotSupportedException();
        }
 public static void AddUniqueKey(this IQuery query, ObjectName tableName, string constraintName, params string[] columnNames)
 {
     query.AddConstraint(tableName, SqlTableConstraint.UniqueKey(constraintName, columnNames));
 }
Esempio n. 3
0
        public SqlTableColumn BuildColumn(ITypeResolver typeResolver, string tableName, IList <SqlTableConstraint> constraints)
        {
            var dataType = DataTypeBuilder.Build(typeResolver, DataType);

            var columnInfo = new SqlTableColumn(ColumnName, dataType);

            if (Default != null)
            {
                columnInfo.DefaultExpression = ExpressionBuilder.Build(Default);
            }

            if (IsIdentity)
            {
                columnInfo.DefaultExpression = SqlExpression.FunctionCall("UNIQUEKEY",
                                                                          new[] { SqlExpression.Constant(tableName) });
                columnInfo.IsIdentity = true;
            }

            foreach (var constraint in Constraints)
            {
                if (String.Equals(ConstraintTypeNames.Check, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase))
                {
                    var exp = ExpressionBuilder.Build(constraint.CheckExpression);
                    constraints.Add(SqlTableConstraint.Check(null, exp));
                }
                else if (String.Equals(ConstraintTypeNames.ForeignKey, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase))
                {
                    var fTable   = constraint.ReferencedTable.Name;
                    var fColumn  = constraint.ReferencedColumn.Text;
                    var onDelete = ForeignKeyAction.NoAction;
                    var onUpdate = ForeignKeyAction.NoAction;

                    if (!String.IsNullOrEmpty(constraint.OnDeleteAction))
                    {
                        onDelete = SqlCodeObjectBuilder.GetForeignKeyAction(constraint.OnDeleteAction);
                    }
                    if (!String.IsNullOrEmpty(constraint.OnUpdateAction))
                    {
                        onUpdate = SqlCodeObjectBuilder.GetForeignKeyAction(constraint.OnUpdateAction);
                    }

                    constraints.Add(SqlTableConstraint.ForeignKey(null, new[] { ColumnName }, fTable, new[] { fColumn }, onDelete, onUpdate));
                }
                else if (String.Equals(ConstraintTypeNames.PrimaryKey, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase))
                {
                    constraints.Add(SqlTableConstraint.PrimaryKey(null, new[] { ColumnName }));
                }
                else if (String.Equals(ConstraintTypeNames.UniqueKey, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase))
                {
                    constraints.Add(SqlTableConstraint.UniqueKey(null, new[] { ColumnName }));
                }
                else if (String.Equals(ConstraintTypeNames.NotNull, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase))
                {
                    columnInfo.IsNotNull = true;
                }
                else if (String.Equals(ConstraintTypeNames.Null, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase))
                {
                    columnInfo.IsNotNull = false;
                }
            }

            return(columnInfo);
        }