Commits

Inside Zhou committed e2f8db2

在SQLHelper中增加对数据库事务隔离设置的支持。

  • Participants
  • Parent commits f1741f8

Comments (0)

Files changed (4)

Json/SimpleDateTimeConverter.cs

 namespace Inside.Util.Json {
 	public class SimpleDateTimeConverter : JsonConverter {
 		internal static bool CanConvert(string str) {
-			return !string.IsNullOrEmpty(str) && str.StartsWith(@"datetime(", StringComparison.Ordinal) && str.EndsWith(@")", StringComparison.Ordinal);
+			var dt = DateTime.Now;
+			return !string.IsNullOrEmpty(str) && (DateTime.TryParse(str, out dt)) || (str.StartsWith(@"datetime(", StringComparison.Ordinal) && str.EndsWith(@")", StringComparison.Ordinal));
 		}
 
 		internal static DateTime StrToObj(string str) {
+			var dt = DateTime.Now;
+			if(DateTime.TryParse(str, out dt)) {
+				return DateTime.Parse(str);
+			}
+
 			if(!CanConvert(str)) {
 				throw new ArgumentException(string.Format("非法的datetime字符串表示 : {0}", str));
 			}
 		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
 			if(value is DateTime) {
 				DateTime dt = (DateTime)value;
-				writer.WriteValue(string.Format(@"datetime({0})", dt.ToString("yyyy-MM-dd HH:mm:ss.fff")));
+				if(serializer.TypeNameHandling == TypeNameHandling.None) {
+					writer.WriteValue(dt.ToString("yyyy-MM-dd HH:mm:ss.fff"));
+				}
+				else {
+					writer.WriteValue(string.Format(@"datetime({0})", dt.ToString("yyyy-MM-dd HH:mm:ss.fff")));
+				}
 			}
 			else {
 				throw new ArgumentException(string.Format("不是合法的DateTime对象 : {0} : {1}", value.GetType(), value));
 		IsNotNull,
 		GreaterThan,
 		GreaterOrEqual,
-		LessThan,
-		LessOrEqual,
+		LesserThan,
+		LesserOrEqual,
 		Like,
 		RightLike,
 		LeftLike,
 				case ConditionType.IsNull:
 					sb.AppendFormat("({0} IS NULL)", name);
 					return sb.ToString();
-				case ConditionType.LessOrEqual:
+				case ConditionType.LesserOrEqual:
 					sb.AppendFormat("({0} <= :{1})", name, conditionIndexer);
 					break;
-				case ConditionType.LessThan:
+				case ConditionType.LesserThan:
 					sb.AppendFormat("({0} < :{1})", name, conditionIndexer);
 					break;
 				case ConditionType.LeftLike:
 			}
 		}
 
+		public static System.Data.IsolationLevel ScopeTransactionIsolationToDbTransactionIsolation(System.Transactions.IsolationLevel isolation) {
+			switch(isolation) {
+				case System.Transactions.IsolationLevel.Chaos:
+					return System.Data.IsolationLevel.Chaos;
+				case System.Transactions.IsolationLevel.ReadCommitted:
+					return System.Data.IsolationLevel.ReadCommitted;
+				case System.Transactions.IsolationLevel.ReadUncommitted:
+					return System.Data.IsolationLevel.ReadUncommitted;
+				case System.Transactions.IsolationLevel.RepeatableRead:
+					return System.Data.IsolationLevel.RepeatableRead;
+				case System.Transactions.IsolationLevel.Serializable:
+					return System.Data.IsolationLevel.Serializable;
+				case System.Transactions.IsolationLevel.Snapshot:
+					return System.Data.IsolationLevel.Snapshot;
+				case System.Transactions.IsolationLevel.Unspecified:
+				default:
+					return System.Data.IsolationLevel.Unspecified;
+			}
+		}
+
+		public static System.Transactions.IsolationLevel DbTransactionIsolationToScopeTransactionIsolation(System.Data.IsolationLevel isolation) {
+			switch(isolation) {
+				case System.Data.IsolationLevel.Chaos:
+					return System.Transactions.IsolationLevel.Chaos;
+				case System.Data.IsolationLevel.ReadCommitted:
+					return System.Transactions.IsolationLevel.ReadCommitted;
+				case System.Data.IsolationLevel.ReadUncommitted:
+					return System.Transactions.IsolationLevel.ReadUncommitted;
+				case System.Data.IsolationLevel.RepeatableRead:
+					return System.Transactions.IsolationLevel.RepeatableRead;
+				case System.Data.IsolationLevel.Serializable:
+					return System.Transactions.IsolationLevel.Serializable;
+				case System.Data.IsolationLevel.Snapshot:
+					return System.Transactions.IsolationLevel.Snapshot;
+				case System.Data.IsolationLevel.Unspecified:
+				default:
+					return System.Transactions.IsolationLevel.Unspecified;
+			}
+		}
+
 		#endregion
 
 		/// <summary>
 		/// <remark>
 		/// 调用者必须负责释放此事务帮助对象。
 		/// </remark>
-		public TransactionHelper BeginTrans() {
+		public TransactionHelper BeginTrans(System.Data.IsolationLevel isolation = System.Data.IsolationLevel.ReadCommitted) {
 			TransactionHelper th = null;
 
 			if(null == Transaction.Current) {
 				if(this.UseDistributedTransaction) {
-					th = new TransactionHelper(new TransactionScope());
+					th = new TransactionHelper(new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = DbTransactionIsolationToScopeTransactionIsolation(isolation) }));
 					th.Disposing += (s, e) => {
 						this.IsInTransaction = false;
 					};
 					switch(this.Conn.State) {
 						case ConnectionState.Open:
 							if(!this.IsInTransaction) {
-								trans = this.Conn.BeginTransaction();
+								trans = this.Conn.BeginTransaction(isolation);
 							}
 							break;
 						case ConnectionState.Broken:
 						case ConnectionState.Connecting:
 						case ConnectionState.Executing:
 						case ConnectionState.Fetching:
-							throw new SqlPlanException(3, "数据库连接的状态不正常。".Fmt(this.Conn.State.ToString()));
+							throw new SqlPlanException(3, "数据库连接的状态不正常 : {0}".Fmt(this.Conn.State.ToString()));
+						case ConnectionState.Closed:
 						default:
 							this.Conn.Open();
-							trans = this.Conn.BeginTransaction();
+							trans = this.Conn.BeginTransaction(isolation);
 							break;
 					}
 
 				}
 			}
 			else {
-				th = new TransactionHelper(new TransactionScope());
+				th = new TransactionHelper(new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = DbTransactionIsolationToScopeTransactionIsolation(isolation) }));
 			}
 
 			this.IsInTransaction = true;