public static VersionValue GetUnsavedVersionValue( String versionUnsavedValue, IGetter versionGetter, IVersionType versionType, ConstructorInfo constructor) { if (versionUnsavedValue == null) { if (constructor != null) { object defaultValue = versionGetter.Get(Instantiate(constructor)); if (defaultValue != null && defaultValue.GetType().IsValueType) { return(new VersionValue(defaultValue)); } else { // if the version of a newly instantiated object is not the same // as the version seed value, use that as the unsaved-value return (versionType.IsEqual(versionType.Seed(null), defaultValue) ? VersionValue.VersionUndefined : new VersionValue(defaultValue)); } } else { return(VersionValue.VersionUndefined); } } else if ("undefined" == versionUnsavedValue) { return(VersionValue.VersionUndefined); } else if ("null" == versionUnsavedValue) { return(VersionValue.VersionSaveNull); } else if ("negative" == versionUnsavedValue) { return(VersionValue.VersionNegative); } else { // NHibernate-specific try { return(new VersionValue(versionType.FromStringValue(versionUnsavedValue))); } catch (InvalidCastException ice) { throw new MappingException("Bad version type: " + versionType.Name, ice); } catch (Exception e) { throw new MappingException("Could not parse version unsaved-value: " + versionUnsavedValue, e); } } }
/// <summary> /// Create an initial version number /// </summary> /// <param name="versionType">The <see cref="IVersionType"/> of the versioned property.</param> /// <param name="session">The current <see cref="ISession" />.</param> /// <returns>A seed value to initialize the versioned property with.</returns> public static object Seed(IVersionType versionType, ISessionImplementor session) { object seed = versionType.Seed(session); if (log.IsDebugEnabled) { log.Debug("Seeding: " + seed); } return seed; }
/// <summary> /// Create an initial version number /// </summary> /// <param name="versionType">The <see cref="IVersionType"/> of the versioned property.</param> /// <param name="session">The current <see cref="ISession" />.</param> /// <returns>A seed value to initialize the versioned property with.</returns> public static object Seed(IVersionType versionType, ISessionImplementor session) { object seed = versionType.Seed(session); if (log.IsDebugEnabled) { log.Debug("Seeding: " + seed); } return(seed); }
void PostProcessInsert(IASTNode insert) { var insertStatement = (InsertStatement)insert; insertStatement.Validate(); SelectClause selectClause = insertStatement.SelectClause; var persister = insertStatement.IntoClause.Queryable; if (!insertStatement.IntoClause.IsExplicitIdInsertion) { // We need to generate ids as part of this bulk insert. // // Note that this is only supported for sequence-style generators and // post-insert-style generators; basically, only in-db generators IIdentifierGenerator generator = persister.IdentifierGenerator; if (!SupportsIdGenWithBulkInsertion(generator)) { throw new QueryException("can only generate ids as part of bulk insert with either sequence or post-insert style generators"); } IASTNode idSelectExprNode = null; var seqGenerator = generator as SequenceGenerator; if (seqGenerator != null) { string seqName = seqGenerator.GeneratorKey(); string nextval = SessionFactoryHelper.Factory.Dialect.GetSelectSequenceNextValString(seqName); idSelectExprNode = ASTFactory.CreateNode(SQL_TOKEN, nextval); } else { //Don't need this, because we should never ever be selecting no columns in an insert ... select... //and because it causes a bug on DB2 /*String idInsertString = sessionFactoryHelper.getFactory().getDialect().getIdentityInsertString(); * if ( idInsertString != null ) { * idSelectExprNode = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, idInsertString ); * }*/ } if (idSelectExprNode != null) { selectClause.InsertChild(0, idSelectExprNode); insertStatement.IntoClause.PrependIdColumnSpec(); } } bool includeVersionProperty = persister.IsVersioned && !insertStatement.IntoClause.IsExplicitVersionInsertion && persister.VersionPropertyInsertable; if (includeVersionProperty) { // We need to seed the version value as part of this bulk insert IVersionType versionType = persister.VersionType; IASTNode versionValueNode; if (SessionFactoryHelper.Factory.Dialect.SupportsParametersInInsertSelect) { versionValueNode = ASTFactory.CreateNode(PARAM, "?"); IParameterSpecification paramSpec = new VersionTypeSeedParameterSpecification(versionType); ((ParameterNode)versionValueNode).HqlParameterSpecification = paramSpec; _parameters.Insert(0, paramSpec); } else { if (IsIntegral(versionType)) { try { object seedValue = versionType.Seed(null); versionValueNode = ASTFactory.CreateNode(SQL_TOKEN, seedValue.ToString()); } catch (Exception t) { throw new QueryException("could not determine seed value for version on bulk insert [" + versionType + "]", t); } } else if (IsDatabaseGeneratedTimestamp(versionType)) { string functionName = SessionFactoryHelper.Factory.Dialect.CurrentTimestampSQLFunctionName; versionValueNode = ASTFactory.CreateNode(SQL_TOKEN, functionName); } else { throw new QueryException("cannot handle version type [" + versionType + "] on bulk inserts with dialects not supporting parameters in insert-select statements"); } } selectClause.InsertChild(0, versionValueNode); insertStatement.IntoClause.PrependVersionColumnSpec(); } if (insertStatement.IntoClause.IsDiscriminated) { string sqlValue = insertStatement.IntoClause.Queryable.DiscriminatorSQLValue; IASTNode discrimValue = ASTFactory.CreateNode(SQL_TOKEN, sqlValue); insertStatement.SelectClause.AddChild(discrimValue); } }
public void Bind(DbCommand command, IList <Parameter> sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session) { int position = sqlQueryParametersList.GetEffectiveParameterLocations(IdBackTrack).Single(); // version parameter can't appear more than once type.NullSafeSet(command, type.Seed(session), position, session); }
public static VersionValue GetUnsavedVersionValue( String versionUnsavedValue, IGetter versionGetter, IVersionType versionType, ConstructorInfo constructor) { if (versionUnsavedValue == null) { if (constructor != null) { object defaultValue = versionGetter.Get(Instantiate(constructor)); if (defaultValue != null && defaultValue.GetType().IsValueType) return new VersionValue(defaultValue); else { // if the version of a newly instantiated object is not the same // as the version seed value, use that as the unsaved-value return versionType.IsEqual(versionType.Seed(null), defaultValue) ? VersionValue.VersionUndefined : new VersionValue(defaultValue); } } else { return VersionValue.VersionUndefined; } } else if ("undefined" == versionUnsavedValue) { return VersionValue.VersionUndefined; } else if ("null" == versionUnsavedValue) { return VersionValue.VersionSaveNull; } else if ("negative" == versionUnsavedValue) { return VersionValue.VersionNegative; } else { // NHibernate-specific try { return new VersionValue(versionType.FromStringValue(versionUnsavedValue)); } catch (InvalidCastException ice) { throw new MappingException("Bad version type: " + versionType.Name, ice); } catch (Exception e) { throw new MappingException("Could not parse version unsaved-value: " + versionUnsavedValue, e); } } }
public int Bind(IDbCommand statement, QueryParameters qp, ISessionImplementor session, int position) { type.NullSafeSet(statement, type.Seed(session), position, session); return(1); }