Пример #1
0
        /// <summary>
        /// Generate the SQL UPDATE that updates all the foreign keys to null
        /// </summary>
        /// <returns></returns>
        protected override SqlCommandInfo GenerateDeleteString()
        {
            var update = new SqlUpdateBuilder(Factory.Dialect, Factory)
                         .SetTableName(qualifiedTableName)
                         .AddColumns(JoinColumnNames, "null");

            if (CollectionType.UseLHSPrimaryKey)
            {
                update.SetIdentityColumn(KeyColumnNames, KeyType);
            }
            else
            {
                var ownerPersister = (IOuterJoinLoadable)OwnerEntityPersister;
                update.SetJoin(ownerPersister.TableName, KeyColumnNames, KeyType, JoinColumnNames, ownerPersister.GetPropertyColumnNames(CollectionType.LHSPropertyName));
            }

            if (HasIndex)
            {
                update.AddColumns(IndexColumnNames, "null");
            }

            if (HasWhere)
            {
                update.AddWhereFragment(sqlWhereString);
            }

            if (Factory.Settings.IsCommentsEnabled)
            {
                update.SetComment("delete one-to-many " + Role);
            }

            return(update.ToSqlCommandInfo());
        }
Пример #2
0
        /// <summary>
        /// Generate the SQL UPDATE that updates a particular row's foreign
        /// key to null
        /// </summary>
        /// <returns></returns>
        protected override SqlCommandInfo GenerateDeleteRowString()
        {
            SqlUpdateBuilder update = new SqlUpdateBuilder(Factory.Dialect, Factory);

            update.SetTableName(qualifiedTableName)
            .AddColumns(KeyColumnNames, "null");

            if (HasIndex && !indexContainsFormula)
            {
                update.AddColumns(IndexColumnNames, "null");
            }

            if (Factory.Settings.IsCommentsEnabled)
            {
                update.SetComment("delete one-to-many row " + Role);
            }

            //use a combination of foreign key columns and pk columns, since
            //the ordering of removal and addition is not guaranteed when
            //a child moves from one parent to another
            update.AddWhereFragment(KeyColumnNames, KeyType, " = ")
            .AddWhereFragment(ElementColumnNames, ElementType, " = ");

            return(update.ToSqlCommandInfo());
        }
        protected virtual SqlUpdateBuilder GenerateUpdate(bool[] includeProperty)
        {
            SqlUpdateBuilder builder = new SqlUpdateBuilder(factory);

            builder.SetTableName(TableName);

            for (int i = 0; i < HydrateSpan; i++)
            {
                if (includeProperty[i])
                {
                    builder.AddColumns(propertyColumnNames[i], PropertyTypes[i]);
                }
            }

            builder.SetIdentityColumn(IdentifierColumnNames, IdentifierType);
            if (IsVersioned)
            {
                if (OptimisticLockMode == OptimisticLockMode.Version)
                {
                    builder.SetVersionColumn(new string[] { VersionColumnName }, VersionType);
                }
            }

            return(builder);
        }
        public void UpdateStringSqlTest()
        {
            Configuration   cfg     = new Configuration();
            ISessionFactory factory = cfg.BuildSessionFactory();

            ISessionFactoryImplementor factoryImpl = (ISessionFactoryImplementor)factory;
            SqlUpdateBuilder           update      = new SqlUpdateBuilder(factoryImpl.Dialect, factoryImpl);

            update.SetTableName("test_update_builder");

            update.AddColumns(new string[] { "intColumn" }, NHibernateUtil.Int32);
            update.AddColumns(new string[] { "longColumn" }, NHibernateUtil.Int64);
            update.AddColumn("literalColumn", false, (ILiteralType)NHibernateUtil.Boolean);
            update.AddColumn("stringColumn", 5.ToString());

            update.SetIdentityColumn(new string[] { "decimalColumn" }, NHibernateUtil.Decimal);
            update.SetVersionColumn(new string[] { "versionColumn" }, (IVersionType)NHibernateUtil.Int32);

            update.AddWhereFragment("a=b");
            SqlCommandInfo sqlCommand = update.ToSqlCommandInfo();

            Assert.AreEqual(CommandType.Text, sqlCommand.CommandType);
            string falseString = factoryImpl.Dialect.ToBooleanValueString(false);
            string expectedSql =
                "UPDATE test_update_builder SET intColumn = ?, longColumn = ?, literalColumn = " + falseString + ", stringColumn = 5 WHERE decimalColumn = ? AND versionColumn = ? AND a=b";

            Assert.AreEqual(expectedSql, sqlCommand.Text.ToString(), "SQL String");

            SqlType[] actualParameterTypes = sqlCommand.ParameterTypes;
            Assert.AreEqual(4, actualParameterTypes.Length, "Four parameters");

            SqlType[] expectedParameterTypes = new SqlType[]
            {
                SqlTypeFactory.Int32,
                SqlTypeFactory.Int64,
                SqlTypeFactory.Decimal,
                SqlTypeFactory.Int32
            };

            Assert.AreEqual(expectedParameterTypes[0], actualParameterTypes[0], "firstParam Type");
            Assert.AreEqual(expectedParameterTypes[1], actualParameterTypes[1], "secondParam Type");
            Assert.AreEqual(expectedParameterTypes[2], actualParameterTypes[2], "thirdParam Type");
            Assert.AreEqual(expectedParameterTypes[3], actualParameterTypes[3], "fourthParam Type");
        }
Пример #5
0
        private SqlString GenerateLockString()
        {
            ISessionFactoryImplementor factory = lockable.Factory;
            SqlUpdateBuilder           update  = new SqlUpdateBuilder(factory.Dialect, factory);

            update.SetTableName(lockable.RootTableName);
            update.SetIdentityColumn(lockable.RootTableIdentifierColumnNames, lockable.IdentifierType);
            update.SetVersionColumn(new string[] { lockable.VersionColumnName }, lockable.VersionType);
            update.AddColumns(new string[] { lockable.VersionColumnName }, null, lockable.VersionType);
            if (factory.Settings.IsCommentsEnabled)
            {
                update.SetComment(lockMode + " lock " + lockable.EntityName);
            }
            return(update.ToSqlString());
        }
Пример #6
0
        /// <summary>
        /// Generate the SQL UPDATE that updates a foreign key to a value
        /// </summary>
        /// <returns></returns>
        protected override SqlString GenerateInsertRowString( )
        {
            SqlUpdateBuilder update = new SqlUpdateBuilder(factory);

            update.SetTableName(qualifiedTableName)
            .AddColumns(KeyColumnNames, KeyType)
            .SetIdentityColumn(ElementColumnNames, ElementType);
            if (HasIndex)
            {
                update.AddColumns(IndexColumnNames, IndexType);
            }
            //identifier collections not supported for 1-to-many

            return(update.ToSqlString());
        }
Пример #7
0
        /// <summary>
        /// Generate the SQL UPDATE that updates all the foreign keys to null
        /// </summary>
        /// <returns></returns>
        protected override SqlString GenerateDeleteString( )
        {
            SqlUpdateBuilder update = new SqlUpdateBuilder(factory)
                                      .SetTableName(qualifiedTableName)
                                      .AddColumns(KeyColumnNames, "null")
                                      .SetIdentityColumn(KeyColumnNames, KeyType);

            if (HasIndex)
            {
                update.AddColumns(IndexColumnNames, "null");
            }
            if (HasWhere)
            {
                update.AddWhereFragment(sqlWhereString);
            }

            return(update.ToSqlString());
        }
Пример #8
0
        /// <summary>
        /// Generate the SQL UPDATE that updates a foreign key to a value
        /// </summary>
        /// <returns></returns>
        protected override SqlCommandInfo GenerateInsertRowString()
        {
            SqlUpdateBuilder update = new SqlUpdateBuilder(Factory.Dialect, Factory);

            update.SetTableName(qualifiedTableName)
            .AddColumns(KeyColumnNames, KeyType);
            if (HasIndex && !indexContainsFormula)
            {
                update.AddColumns(IndexColumnNames, IndexType);
            }

            //identifier collections not supported for 1-to-many
            if (Factory.Settings.IsCommentsEnabled)
            {
                update.SetComment("create one-to-many row " + Role);
            }

            update.SetIdentityColumn(ElementColumnNames, ElementType);

            return(update.ToSqlCommandInfo());
        }
Пример #9
0
        /// <summary>
        /// Generate the SQL UPDATE that updates all the foreign keys to null
        /// </summary>
        /// <returns></returns>
        protected override SqlCommandInfo GenerateDeleteString()
        {
            SqlUpdateBuilder update = new SqlUpdateBuilder(Factory.Dialect, Factory)
                                      .SetTableName(qualifiedTableName)
                                      .AddColumns(KeyColumnNames, "null")
                                      .SetIdentityColumn(KeyColumnNames, KeyType);

            if (HasIndex)
            {
                update.AddColumns(IndexColumnNames, "null");
            }

            if (HasWhere)
            {
                update.AddWhereFragment(sqlWhereString);
            }

            if (Factory.Settings.IsCommentsEnabled)
            {
                update.SetComment("delete one-to-many " + Role);
            }

            return(update.ToSqlCommandInfo());
        }
Пример #10
0
        /// <summary>
        /// Generate the SQL UPDATE that updates a particular row's foreign
        /// key to null
        /// </summary>
        /// <returns></returns>
        protected override SqlString GenerateDeleteRowString( )
        {
            SqlUpdateBuilder update = new SqlUpdateBuilder(factory);

            update.SetTableName(qualifiedTableName)
            .AddColumns(KeyColumnNames, "null");

            if (HasIndex)
            {
                update.AddColumns(IndexColumnNames, "null");
            }

            if (hasIdentifier)
            {
                update.AddWhereFragment(rowSelectColumnNames, rowSelectType, " = ");
            }
            else
            {
                update.AddWhereFragment(KeyColumnNames, KeyType, " = ");
                update.AddWhereFragment(rowSelectColumnNames, rowSelectType, " = ");
            }

            return(update.ToSqlString());
        }