protected override int DoUpdateRows(object id, PersistentCollection collection, ISessionImplementor session)
 {
     try
     {
         IDbCommand  st      = null;
         ICollection entries = collection.Entries();
         try
         {
             int i     = 0;
             int count = 0;
             foreach (object entry in entries)
             {
                 if (collection.NeedsUpdating(entry, i, ElementType))
                 {
                     if (st == null)
                     {
                         st = session.Batcher.PrepareBatchCommand(SqlUpdateRowString);
                     }
                     if (!hasIdentifier)
                     {
                         WriteKey(st, id, true, session);
                     }
                     collection.WriteTo(st, this, entry, i, true);
                     session.Batcher.AddToBatch(1);
                     count++;
                 }
                 i++;
             }
             return(count);
         }
         catch (Exception e)
         {
             //TODO: change to SqlException
             // NB This calls cmd.Dispose
             session.Batcher.AbortBatch(e);
             throw;
         }
     }
     catch (HibernateException)
     {
         // Do not call Convert on HibernateExceptions
         throw;
     }
     catch (Exception sqle)
     {
         throw Convert(sqle, "could not update collection rows: " + MessageHelper.InfoString(this, id));
     }
 }
		public void InsertRows( PersistentCollection collection, object id, ISessionImplementor session )
		{
			if( !isInverse )
			{
				if( log.IsDebugEnabled )
				{
					log.Debug( "Inserting rows of collection: " + role + "#" + id );
				}

				collection.PreInsert( this );
				int i = 0;
				int count = 0;

				try
				{
					// insert all the new entries
					ICollection entries = collection.Entries();
					try
					{
						// Moved the IDbCommand outside the loop, because ADO.NET doesn't do batch commands,
						// so it's more efficient. But it
						foreach( object entry in entries )
						{
							if( collection.NeedsInserting( entry, i, elementType ) )
							{
								IDbCommand st = null;
								if( st == null )
								{
									st = session.Batcher.PrepareBatchCommand( SqlInsertRowString );
								}
								WriteKey( st, id, false, session );
								collection.WriteTo( st, this, entry, i, false );
								session.Batcher.AddToBatch( 1 );
								collection.AfterRowInsert( this, entry, i );
								count++;
							}
							i++;
						}

						if( log.IsDebugEnabled )
						{
							log.Debug( string.Format( "done inserting rows: {0} inserted", count ) );
						}
					}
					catch( Exception e )
					{
						session.Batcher.AbortBatch( e );
						throw;
					}
				}
				catch( HibernateException )
				{
					// Do not call Convert on HibernateExceptions
					throw;
				}
				catch( Exception sqle )
				{
					throw Convert( sqle, "could not insert collection rows: " + MessageHelper.InfoString( this, id ) );
				}

			}
		}
		public void Recreate( PersistentCollection collection, object id, ISessionImplementor session )
		{
			if( !isInverse )
			{
				if( log.IsDebugEnabled )
				{
					log.Debug( "Inserting collection: " + MessageHelper.InfoString( this, id ) );
				}

				try
				{
					// create all the new entries
					ICollection entries = collection.Entries();
					if( entries.Count > 0 )
					{
						int i = 0;
						int count = 0;
						try
						{
							collection.PreInsert( this );

							foreach( object entry in entries )
							{
								if( collection.EntryExists( entry, i ) )
								{
									IDbCommand st = session.Batcher.PrepareBatchCommand( SqlInsertRowString );
									WriteKey( st, id, false, session );
									collection.WriteTo( st, this, entry, i, false );
									session.Batcher.AddToBatch( 1 );
									collection.AfterRowInsert( this, entry, i );
									count++;
								}
								i++;
							}
						} 
							//TODO: change to SqlException
						catch( Exception e )
						{
							session.Batcher.AbortBatch( e );
							throw;
						}
						if( log.IsDebugEnabled )
						{
							log.Debug( string.Format( "done inserting collection: {0} rows inserted", count ) );
						}
					}
					else
					{
						if( log.IsDebugEnabled )
						{
							log.Debug( "collection was empty" );
						}
					}
				}
				catch( HibernateException )
				{
					// Do not call Convert on HibernateExceptions
					throw;
				}
				catch( Exception sqle )
				{
					throw Convert( sqle, "could not insert collection: " + MessageHelper.InfoString( this, id ) );
				}
			}
		}
示例#4
0
        protected override int DoUpdateRows(object id, PersistentCollection collection, ISessionImplementor session)
        {
            // we finish all the "removes" first to take care of possible unique
            // constraints and so that we can take better advantage of batching

            IDbCommand  st;
            ICollection entries;
            int         i;
            int         count;

            try
            {
                // update removed rows fks to null
                count = 0;
                try
                {
                    st      = null;
                    i       = 0;
                    entries = collection.Entries();
                    foreach (object entry in entries)
                    {
                        if (collection.NeedsUpdating(entry, i, ElementType))                            // will still be issued when it used to be null
                        {
                            if (st == null)
                            {
                                st = session.Batcher.PrepareBatchCommand(SqlDeleteRowString);
                            }
                            WriteKey(st, id, false, session);
                            WriteIndex(st, collection.GetIndex(entry, i), false, session);
                            session.Batcher.AddToBatch(-1);
                            count++;
                        }
                        i++;
                    }
                }
                catch (Exception e)
                {
                    session.Batcher.AbortBatch(e);
                    throw;
                }

                // now update all changed or added rows fks
                count = 0;
                try
                {
                    st      = null;
                    i       = 0;
                    entries = collection.Entries();
                    foreach (object entry in entries)
                    {
                        if (collection.NeedsUpdating(entry, i, ElementType))                            // will still be issued when it used to be null
                        {
                            if (st == null)
                            {
                                st = session.Batcher.PrepareBatchCommand(SqlInsertRowString);
                            }
                            WriteKey(st, id, false, session);
                            collection.WriteTo(st, this, entry, i, false);
                            session.Batcher.AddToBatch(1);
                            count++;
                        }
                        i++;
                    }
                }
                catch (Exception e)
                {
                    //TODO: change to SqlException
                    session.Batcher.AbortBatch(e);
                    throw;
                }
                return(count);
            }
            catch (HibernateException)
            {
                // Do not call Convert on HibernateExceptions
                throw;
            }
            catch (Exception sqle)
            {
                throw Convert(sqle, "could not update collection rows: " + MessageHelper.InfoString(this, id));
            }
        }
		protected override int DoUpdateRows( object id, PersistentCollection collection, ISessionImplementor session )
		{
			// we finish all the "removes" first to take care of possible unique 
			// constraints and so that we can take better advantage of batching

			IDbCommand st;
			ICollection entries;
			int i;
			int count;

			try
			{
				// update removed rows fks to null
				count = 0;
				try
				{
					st = null;
					i = 0;
					entries = collection.Entries();
					foreach( object entry in entries )
					{
						if( collection.NeedsUpdating( entry, i, ElementType ) ) // will still be issued when it used to be null
						{
							if( st == null )
							{
								st = session.Batcher.PrepareBatchCommand( SqlDeleteRowString );
							}
							WriteKey( st, id, false, session );
							WriteIndex( st, collection.GetIndex( entry, i ), false, session );
							session.Batcher.AddToBatch( -1 );
							count++;
						}
						i++;
					}
				}
				catch( Exception e )
				{
					session.Batcher.AbortBatch( e );
					throw;
				}

				// now update all changed or added rows fks
				count = 0;
				try
				{
					st = null;
					i = 0;
					entries = collection.Entries();
					foreach( object entry in entries )
					{
						if( collection.NeedsUpdating( entry, i, ElementType ) ) // will still be issued when it used to be null
						{
							if( st == null )
							{
								st = session.Batcher.PrepareBatchCommand( SqlInsertRowString );
							}
							WriteKey( st, id, false, session );
							collection.WriteTo( st, this, entry, i, false );
							session.Batcher.AddToBatch( 1 );
							count++;
						}
						i++;
					}
				}
				catch( Exception e )
				{
					//TODO: change to SqlException
					session.Batcher.AbortBatch( e );
					throw;
				}
				return count;
			}
			catch( HibernateException )
			{
				// Do not call Convert on HibernateExceptions
				throw;
			}
			catch( Exception sqle )
			{
				throw Convert( sqle, "could not update collection rows: " + MessageHelper.InfoString( this, id ) );
			}
		}
		protected override int DoUpdateRows( object id, PersistentCollection collection, ISessionImplementor session )
		{
			try
			{
				IDbCommand st = null;
				ICollection entries = collection.Entries();
				try
				{
					int i = 0;
					int count = 0;
					foreach( object entry in entries )
					{
						if( collection.NeedsUpdating( entry, i, ElementType ) )
						{
							if( st == null )
							{
								st = session.Batcher.PrepareBatchCommand( SqlUpdateRowString );
							}
							if( !hasIdentifier )
							{
								WriteKey( st, id, true, session );
							}
							collection.WriteTo( st, this, entry, i, true );
							session.Batcher.AddToBatch( 1 );
							count++;
						}
						i++;
					}
					return count;
				}
				catch( Exception e )
				{
					//TODO: change to SqlException
					// NB This calls cmd.Dispose
					session.Batcher.AbortBatch( e );
					throw;
				}
			}
			catch( HibernateException )
			{
				// Do not call Convert on HibernateExceptions
				throw;
			}
			catch( Exception sqle )
			{
				throw Convert( sqle, "could not update collection rows: " + MessageHelper.InfoString( this, id ) );
			}
		}
示例#7
0
        public void InsertRows(PersistentCollection collection, object id, ISessionImplementor session)
        {
            if (!isInverse)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Inserting rows of collection: " + role + "#" + id);
                }

                collection.PreInsert(this);
                int i     = 0;
                int count = 0;

                try
                {
                    // insert all the new entries
                    ICollection entries = collection.Entries();
                    try
                    {
                        // Moved the IDbCommand outside the loop, because ADO.NET doesn't do batch commands,
                        // so it's more efficient. But it
                        foreach (object entry in entries)
                        {
                            if (collection.NeedsInserting(entry, i, elementType))
                            {
                                IDbCommand st = null;
                                if (st == null)
                                {
                                    st = session.Batcher.PrepareBatchCommand(SqlInsertRowString);
                                }
                                WriteKey(st, id, false, session);
                                collection.WriteTo(st, this, entry, i, false);
                                session.Batcher.AddToBatch(1);
                                collection.AfterRowInsert(this, entry, i);
                                count++;
                            }
                            i++;
                        }

                        if (log.IsDebugEnabled)
                        {
                            log.Debug(string.Format("done inserting rows: {0} inserted", count));
                        }
                    }
                    catch (Exception e)
                    {
                        session.Batcher.AbortBatch(e);
                        throw;
                    }
                }
                catch (HibernateException)
                {
                    // Do not call Convert on HibernateExceptions
                    throw;
                }
                catch (Exception sqle)
                {
                    throw Convert(sqle, "could not insert collection rows: " + MessageHelper.InfoString(this, id));
                }
            }
        }
示例#8
0
        public void Recreate(PersistentCollection collection, object id, ISessionImplementor session)
        {
            if (!isInverse)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Inserting collection: " + MessageHelper.InfoString(this, id));
                }

                try
                {
                    // create all the new entries
                    ICollection entries = collection.Entries();
                    if (entries.Count > 0)
                    {
                        int i     = 0;
                        int count = 0;
                        try
                        {
                            collection.PreInsert(this);

                            foreach (object entry in entries)
                            {
                                if (collection.EntryExists(entry, i))
                                {
                                    IDbCommand st = session.Batcher.PrepareBatchCommand(SqlInsertRowString);
                                    WriteKey(st, id, false, session);
                                    collection.WriteTo(st, this, entry, i, false);
                                    session.Batcher.AddToBatch(1);
                                    collection.AfterRowInsert(this, entry, i);
                                    count++;
                                }
                                i++;
                            }
                        }
                        //TODO: change to SqlException
                        catch (Exception e)
                        {
                            session.Batcher.AbortBatch(e);
                            throw;
                        }
                        if (log.IsDebugEnabled)
                        {
                            log.Debug(string.Format("done inserting collection: {0} rows inserted", count));
                        }
                    }
                    else
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("collection was empty");
                        }
                    }
                }
                catch (HibernateException)
                {
                    // Do not call Convert on HibernateExceptions
                    throw;
                }
                catch (Exception sqle)
                {
                    throw Convert(sqle, "could not insert collection: " + MessageHelper.InfoString(this, id));
                }
            }
        }