/// <summary> /// 将时间点有关的SQL条件填充到ConnectiveSqlClauseCollection /// </summary> /// <param name="builder"></param> /// <param name="timePoint"></param> /// <param name="fieldPrefix">字段前缀。如果是表空间或Schema,不要忘记后面的'.'</param> public void FillTimePointInBuilder(ConnectiveSqlClauseCollection builder, DateTime timePoint, string fieldPrefix) { //输出结果应该是 //VersionStartTime <= getdate() AND (VersionEndTime > getdate() OR VersionEndTime IS NULL) builder.NullCheck("builder"); string timeString = TSqlBuilder.Instance.DBCurrentTimeFunction; if (timePoint == DateTime.MinValue && this._UseSimulatedTime && TimePointContext.Current.UseCurrentTime == false) { timePoint = TimePointContext.Current.SimulatedTime; } if (timePoint != DateTime.MinValue) { timeString = TSqlBuilder.Instance.FormatDateTime(timePoint); } WhereSqlClauseBuilder sBuilder = new WhereSqlClauseBuilder(); string vsFieldName = JoinFieldName(fieldPrefix, this.VersionStartTimeFieldName); sBuilder.AppendItem(vsFieldName, timeString, "<=", true); WhereSqlClauseBuilder eBuilder = new WhereSqlClauseBuilder(LogicOperatorDefine.Or); string veFieldName = JoinFieldName(fieldPrefix, this.VersionEndTimeFieldName); eBuilder.AppendItem(veFieldName, timeString, ">", true).AppendItem(veFieldName, "NULL", "IS", true); builder.LogicOperator = LogicOperatorDefine.And; builder.Add(sBuilder).Add(eBuilder); }
protected virtual WhereSqlClauseBuilder PrepareWhereSqlBuilder(T obj, ORMappingItemCollection mapping) { WhereSqlClauseBuilder primaryKeyBuilder = ORMapping.GetWhereSqlClauseBuilderByPrimaryKey(obj); string vsFieldName = GetPropertyFieldName("VersionStartTime", mapping); if (primaryKeyBuilder.Exists(item => ((SqlClauseBuilderItemIUW)item).DataField == vsFieldName) == false) { primaryKeyBuilder.AppendItem(vsFieldName, obj.VersionStartTime); } return(primaryKeyBuilder); }
private string GetJoinedExistedCountSql(IConnectiveSqlClause ownerKeyBuilder, IConnectiveSqlClause existedKeys, ORMappingItemCollection mapping) { HashSet <string> fields = MergeFields(ownerKeyBuilder.GetFields(), existedKeys.GetFields()); WhereSqlClauseBuilder builder = new WhereSqlClauseBuilder(); foreach (string field in fields) { builder.AppendItem("A." + field, "B." + field, SqlClauseBuilderBase.EqualTo, true); } string innerSql = this.GetInnerExistedObjsSql(ownerKeyBuilder, existedKeys, mapping); string sql = string.Format("SELECT COUNT(1) FROM ({0}) B WHERE {1}", innerSql, builder.ToSqlString(TSqlBuilder.Instance)); return(sql); }
/// <summary> /// 得到一个已经存在的记录的临时表 /// </summary> /// <param name="ownerKeyBuilder"></param> /// <param name="existedKeys"></param> /// <param name="mapping"></param> /// <returns></returns> private string GetInnerExistedObjsSql(IConnectiveSqlClause ownerKeyBuilder, IConnectiveSqlClause existedKeys, ORMappingItemCollection mapping) { ConnectiveSqlClauseCollection connective = new ConnectiveSqlClauseCollection(LogicOperatorDefine.And); connective.Add(ownerKeyBuilder); string endTimeFieldName = GetPropertyFieldName("VersionEndTime", mapping); WhereSqlClauseBuilder veBuilder = new WhereSqlClauseBuilder(); veBuilder.AppendItem(endTimeFieldName, DBTimePointActionContext.MaxVersionEndTime); connective.Add(veBuilder); connective.Add(existedKeys); return(string.Format("SELECT * FROM {0} WHERE {1}", this.GetTableName(default(T), mapping), connective.ToSqlString(TSqlBuilder.Instance))); }