protected override Expression VisitSimpleConstraint(SqlSimpleConstraintExpression simpleConstraintExpression)
		{
			base.VisitSimpleConstraint(simpleConstraintExpression);

			if (simpleConstraintExpression.Constraint != SqlSimpleConstraint.AutoIncrement)
			{
				return simpleConstraintExpression;
			}

			var options = simpleConstraintExpression.Value as object[];

			if (options != null && options.Length == 2 && (options[0] as long?) > 0)
			{
				if (options[1] as long? == 0)
				{
					options[1] = 1L;
				}

				this.Write("(");
				this.WriteDeliminatedListOfItems(options, this.Write);
				this.Write(")");
			}

			return simpleConstraintExpression;
		}
Ejemplo n.º 2
0
        protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            var autoIncrementColumn = createTableExpression.ColumnDefinitionExpressions.SingleOrDefault(c => c.ConstraintExpressions.OfType<SqlSimpleConstraintExpression>().Any(d => d.Constraint == SqlSimpleConstraint.AutoIncrement));

            if (autoIncrementColumn != null)
            {
                var primaryKeyConstraint = createTableExpression.TableConstraints.OfType<SqlSimpleConstraintExpression>().SingleOrDefault(c => c.Constraint == SqlSimpleConstraint.PrimaryKey);

                if (primaryKeyConstraint != null)
                {
                    if (!primaryKeyConstraint.ColumnNames.Contains(autoIncrementColumn.ColumnName))
                    {
                        var newPrimaryKeyConstraint = new SqlSimpleConstraintExpression(SqlSimpleConstraint.PrimaryKey, new [] { autoIncrementColumn.ColumnName });
                        var newUniqueConstraint = new SqlSimpleConstraintExpression(SqlSimpleConstraint.Unique, primaryKeyConstraint.ColumnNames.Concat(autoIncrementColumn.ColumnName).ToArray());

                        return createTableExpression.UpdateConstraints(new ReadOnlyList<Expression>(createTableExpression.TableConstraints.Where(c => c != primaryKeyConstraint).Concat(newPrimaryKeyConstraint).Concat(newUniqueConstraint)));
                    }
                }
                else
                {
                    var newPrimaryKeyConstraint = new SqlSimpleConstraintExpression(SqlSimpleConstraint.PrimaryKey, new [] { autoIncrementColumn.ColumnName });

                    return createTableExpression.UpdateConstraints(new ReadOnlyList<Expression>(newPrimaryKeyConstraint));
                }
            }

            return base.VisitCreateTable(createTableExpression);
        }
        protected override Expression VisitSimpleConstraint(SqlSimpleConstraintExpression simpleConstraintExpression)
        {
            if (this.currentIsPrimaryKey && simpleConstraintExpression.Constraint == SqlSimpleConstraint.AutoIncrement)
            {
                return null;
            }

            return base.VisitSimpleConstraint(simpleConstraintExpression);
        }
 protected override Expression VisitSimpleConstraint(SqlSimpleConstraintExpression expression)
 {
     this.hashCode ^= expression.Value?.GetHashCode() ?? 0;
     this.hashCode ^= expression.Constraint.GetHashCode();
     return base.VisitSimpleConstraint(expression);
 }
        protected override Expression VisitSimpleConstraint(SqlSimpleConstraintExpression expression)
        {
            SqlSimpleConstraintExpression current;
            if (!TryGetCurrent(expression, out current))
            {
                return expression;
            }

            if (!(this.result &= object.Equals(current.Value, expression.Value)))
            {
                return expression;
            }

            if (!(this.result &= current.Constraint == expression.Constraint))
            {
                return expression;
            }

            if (!(this.result &= current.NodeType == expression.NodeType))
            {
                return expression;
            }

            if (!(this.result &= current.Type == expression.Type))
            {
                return expression;
            }

            this.currentObject = current.ColumnNames;
            this.VisitObjectList(expression.ColumnNames);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current;
            return expression;
        }
		protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
		{
			var primaryKeyConstraint = createTableExpression
				.TableConstraints
				.OfType<SqlSimpleConstraintExpression>()
				.SingleOrDefault(c => c.Constraint == SqlSimpleConstraint.PrimaryKey);

			if (primaryKeyConstraint != null)
			{
				var autoIncrementColumns = createTableExpression
					.ColumnDefinitionExpressions
					.SelectMany(c => c.ConstraintExpressions.OfType<SqlSimpleConstraintExpression>().Select(d => new { Constraint = d, ColumnDefinition = c}))
					.Where(c => c.Constraint.Constraint == SqlSimpleConstraint.AutoIncrement)
					.ToList();

				if (autoIncrementColumns.Count > 1)
				{
					throw new UnsupportedDataAccessModelDefinitionException();
				}

				if (autoIncrementColumns.Count > 0)
				{
					var autoIncrementColumn = autoIncrementColumns.Single();

					var newTableConstraints = createTableExpression
						.TableConstraints
						.Where(c => c != primaryKeyConstraint);

					if (primaryKeyConstraint.ColumnNames.Length > 1
						|| autoIncrementColumn.ColumnDefinition.ConstraintExpressions.OfType<SqlSimpleConstraintExpression>().All(c => c.Constraint != SqlSimpleConstraint.PrimaryKey))
					{
						var uniqueConstraint = new SqlSimpleConstraintExpression(SqlSimpleConstraint.Unique, primaryKeyConstraint.ColumnNames);

						newTableConstraints = newTableConstraints.Concat(uniqueConstraint);
					}

					this.primaryKeyNameByTablesWithReducedPrimaryKeyName[createTableExpression.Table.Name] = autoIncrementColumn.ColumnDefinition.ColumnName;

					this.columnsToMakeNotNull.Clear();

					primaryKeyConstraint
						.ColumnNames
						.Where(c => c != autoIncrementColumn.ColumnDefinition.ColumnName)
						.ForEach(c => this.columnsToMakeNotNull.Add(c));

					createTableExpression = new SqlCreateTableExpression
					(
						createTableExpression.Table,
						createTableExpression.IfNotExist,
						this.VisitExpressionList(createTableExpression.ColumnDefinitionExpressions),
						newTableConstraints
					);

					this.columnsToMakeNotNull.Clear();

					return createTableExpression;
				}
			}

			return base.VisitCreateTable(createTableExpression);
		}
Ejemplo n.º 7
0
        protected override Expression VisitSimpleConstraint(SqlSimpleConstraintExpression simpleConstraintExpression)
        {
            switch (simpleConstraintExpression.Constraint)
            {
            case SqlSimpleConstraint.DefaultValue:
                if (simpleConstraintExpression.Value != null)
                {
                    this.Write("DEFAULT");
                    this.Write(" ");
                    this.Write(simpleConstraintExpression.Value);
                }
                break;
            case SqlSimpleConstraint.NotNull:
                this.Write("NOT NULL");
                break;
            case SqlSimpleConstraint.AutoIncrement:
            {
                var s = this.sqlDialect.GetSyntaxSymbolString(SqlSyntaxSymbol.AutoIncrement);

                if (!string.IsNullOrEmpty(s))
                {
                    this.Write(s);
                }

                var autoIncrementParams = simpleConstraintExpression.Value as object[];

                if (autoIncrementParams != null)
                {
                    this.Write("(");
                    this.WriteDeliminatedListOfItems(autoIncrementParams, this.WriteQuotedStringOrObject);
                    this.Write(")");
                }

                break;
            }
            case SqlSimpleConstraint.PrimaryKey:
                this.Write("PRIMARY KEY");
                if (simpleConstraintExpression.ColumnNames != null)
                {
                    this.Write("(");
                    this.WriteDeliminatedListOfItems(simpleConstraintExpression.ColumnNames, this.WriteQuotedIdentifier);
                    this.Write(")");
                }
                break;
            case SqlSimpleConstraint.Unique:
                this.Write("UNIQUE");
                if (simpleConstraintExpression.ColumnNames != null)
                {
                    this.Write("(");
                    this.WriteDeliminatedListOfItems(simpleConstraintExpression.ColumnNames, this.WriteQuotedIdentifier);
                    this.Write(")");
                }
                break;
            }

            return simpleConstraintExpression;
        }
Ejemplo n.º 8
0
		protected virtual Expression VisitSimpleConstraint(SqlSimpleConstraintExpression simpleConstraintExpression)
		{
			return simpleConstraintExpression;
		}
 protected override Expression VisitSimpleConstraint(SqlSimpleConstraintExpression expression)
 {
     this.hashCode ^= expression.Value?.GetHashCode() ?? 0;
     this.hashCode ^= expression.Constraint.GetHashCode();
     return(base.VisitSimpleConstraint(expression));
 }
        private Expression BuildCreateTableExpression(TypeDescriptor typeDescriptor)
        {
            var columnExpressions = new List<SqlColumnDefinitionExpression>();

            currentTableConstraints = new List<Expression>();

            var columnInfos = QueryBinder.GetColumnInfos
            (
                this.model.TypeDescriptorProvider,
                typeDescriptor.PersistedAndRelatedObjectProperties,
                (c, d) => c.IsPrimaryKey && !c.PropertyType.IsDataAccessObjectType(),
                (c, d) => c.IsPrimaryKey
            );

            foreach (var columnInfo in columnInfos)
            {
                columnExpressions.Add(this.BuildColumnDefinition(columnInfo));
            }

            columnInfos = QueryBinder.GetColumnInfos
            (
                this.model.TypeDescriptorProvider,
                typeDescriptor.PersistedProperties.Where(c => !c.PropertyType.IsDataAccessObjectType()),
                (c, d) => d == 0 ? !c.IsPrimaryKey : c.IsPrimaryKey,
                (c, d) => d == 0 ? !c.IsPrimaryKey : c.IsPrimaryKey
            );

            foreach (var columnInfo in columnInfos)
            {
                columnExpressions.Add(this.BuildColumnDefinition(columnInfo));
            }

            foreach (var property in typeDescriptor.PersistedProperties
                .Where(c => c.PropertyType.IsDataAccessObjectType()))
            {
                columnInfos = QueryBinder.GetColumnInfos
                (
                    this.model.TypeDescriptorProvider,
                    new [] { property },
                    (c, d) => d == 0 || c.IsPrimaryKey,
                    (c, d) => c.IsPrimaryKey
                );

                columnExpressions.AddRange(this.BuildForeignKeyColumnDefinitions(property, columnInfos));
            }

            columnExpressions.AddRange(BuildRelatedColumnDefinitions(typeDescriptor));

            var tableName = typeDescriptor.PersistedName;

            var primaryKeys = QueryBinder.GetPrimaryKeyColumnInfos(this.model.TypeDescriptorProvider, typeDescriptor);

            if (primaryKeys.Length > 0)
            {
                var columnNames = primaryKeys.Select(c => c.ColumnName).ToArray();

                var compositePrimaryKeyConstraint = new SqlSimpleConstraintExpression(SqlSimpleConstraint.PrimaryKey, columnNames);

                this.currentTableConstraints.Add(compositePrimaryKeyConstraint);
            }

            return new SqlCreateTableExpression(new SqlTableExpression(tableName), false, columnExpressions, this.currentTableConstraints);
        }
Ejemplo n.º 11
0
 protected virtual Expression VisitSimpleConstraint(SqlSimpleConstraintExpression simpleConstraintExpression)
 {
     return(simpleConstraintExpression);
 }