From: Michael D. <mik...@us...> - 2004-04-06 01:06:03
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Id In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22179 Modified Files: IdentifierGeneratorFactory.cs Added Files: IncrementGenerator.cs Log Message: Added IncrementGenerator for JIRA NH-23 --- NEW FILE: IncrementGenerator.cs --- using System; using System.Collections; using System.Data; using System.Runtime.CompilerServices; using NHibernate.Dialect; using NHibernate.Engine; namespace NHibernate.Id { /// <summary> /// An <see cref="IIdentifierGenerator"/> that returns an Int64, constructed /// by counting from the maximum primary key value at startup. /// </summary> /// <remarks> /// <para> /// Not safe for use in a cluster! /// </para> /// <para> /// Mapping parameters supported (but not usually needed) are: <c>table</c>, <c>column</c> /// </para> /// </remarks> /// public class IncrementGenerator : IIdentifierGenerator, IConfigurable { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(IncrementGenerator)); private static readonly string Schema = "schema"; private static readonly string Table = "target_table"; private static readonly string PK = "target_column"; private long next; private string sql; public IncrementGenerator() { } #region IIdentifierGenerator Members [MethodImpl(MethodImplOptions.Synchronized)] public object Generate(ISessionImplementor session, object obj) { if(sql!=null) { GetNext( session.Connection ); } return next++; } #endregion #region IConfigurable Members public void Configure(IType type, System.Collections.IDictionary parms, Dialect.Dialect d) { string table = parms["table"]; if(table==null) table = parms[IncrementGenerator.Table); string column = parms["column"]; if(column==null) parms[IncrementGenerator.PK]; string schema = parms[IncrementGenerator.Schema]; sql = "select max(" + column + ") from " + ( schema==null ? table : schema + ":" + table ); } #endregion private void GetNext(IDbConnection conn) { log.Debug("fetching initial value: " + sql); IDbCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = sql; cmd.Prepare(); IDataReader rs = null; try { rs = cmd.ExecuteReader(); if(rs.Read()) { next = rs[0] + 1; } else { next = 1; } sql = null; log.Debug("first free id: " + next); } finally { if(rs!=null) rs.Close(); cmd.Dispose(); } } } } Index: IdentifierGeneratorFactory.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Id/IdentifierGeneratorFactory.cs,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** IdentifierGeneratorFactory.cs 28 Mar 2004 06:06:40 -0000 1.7 --- IdentifierGeneratorFactory.cs 6 Apr 2004 00:53:20 -0000 1.8 *************** *** 54,59 **** idgenerators.Add("seqhilo", typeof(SequenceHiLoGenerator)); idgenerators.Add("vm", typeof(CounterGenerator)); ! //TODO: no Increment Generator ! //idgenerators.Add("increment", typeof(IncrementGenerator)); idgenerators.Add("foreign", typeof(ForeignGenerator)); idgenerators.Add("guid", typeof(GuidGenerator)); --- 54,58 ---- idgenerators.Add("seqhilo", typeof(SequenceHiLoGenerator)); idgenerators.Add("vm", typeof(CounterGenerator)); ! idgenerators.Add("increment", typeof(IncrementGenerator)); idgenerators.Add("foreign", typeof(ForeignGenerator)); idgenerators.Add("guid", typeof(GuidGenerator)); |