public override object FormatValue(object value) { if (value == null) { return("NULL"); } else { string N = this.Context.GetN(); var type = UtilMethods.GetUnderType(value.GetType()); if (type == UtilConstants.DateType) { var date = value.ObjToDate(); if (date < Convert.ToDateTime("1900-1-1")) { date = Convert.ToDateTime("1900-1-1"); } return("to_date('" + date.ToString("yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') "); } else if (type.IsEnum()) { return(Convert.ToInt64(value)); } else if (type.IsIn(UtilConstants.IntType, UtilConstants.LongType, UtilConstants.ShortType)) { return(value); } else if (type == UtilConstants.GuidType) { return("'" + value.ToString() + "'"); } else if (type == UtilConstants.ByteArrayType) { string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", ""); return(bytesString); } else if (type == UtilConstants.BoolType) { return(value.ObjToBool() ? "1" : "0"); } else if (type == UtilConstants.DateTimeOffsetType) { var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value); return("to_date('" + date.ToString("yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') "); } else if (type == UtilConstants.StringType || type == UtilConstants.ObjType) { return(N + "'" + value.ToString().ToSqlFilter() + "'"); } else { return(N + "'" + value.ToString() + "'"); } } }
public override object FormatValue(object value) { var n = "N"; if (this.Context.CurrentConnectionConfig.MoreSettings != null && this.Context.CurrentConnectionConfig.MoreSettings.DisableNvarchar) { n = ""; } if (value == null) { return("NULL"); } else { var type = UtilMethods.GetUnderType(value.GetType()); if (type == UtilConstants.DateType) { var date = value.ObjToDate(); if (date < Convert.ToDateTime("1900-1-1")) { date = Convert.ToDateTime("1900-1-1"); } return("'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'"); } else if (type == UtilConstants.ByteArrayType) { string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", ""); return(bytesString); } else if (type.IsEnum()) { if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true) { return(value.ToSqlValue()); } else { return(Convert.ToInt64(value)); } } else if (type == UtilConstants.BoolType) { return(value.ObjToBool() ? "1" : "0"); } else if (type == UtilConstants.StringType || type == UtilConstants.ObjType) { return(n + "'" + GetString(value).ToSqlFilter() + "'"); } else { return(n + "'" + GetString(value) + "'"); } } }
private static object GetValue(PropertyInfo p, T item) { var result = p.GetValue(item, null); if (result != null && UtilMethods.GetUnderType(p.PropertyType) == UtilConstants.BoolType) { if (result.ObjToBool() == false) { result = null; } } return(result); }
private MemberExpression ResolveValue(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool?isLeft, bool isSetTempData, bool isSingle) { expression = expression.Expression as MemberExpression; baseParameter.ChildExpression = expression; if (UtilMethods.GetUnderType(expression.Type) == UtilConstants.BoolType && parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression)) { ResolveBoolLogic(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle); } else { ResolveWhereLogic(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle); } return(expression); }
private static void CreateDateProperty(Type type, JsonProperty item) { var property = type.GetProperties().Where(it => it.Name == item.PropertyName).First(); var itemType = UtilMethods.GetUnderType(property); if (property.GetCustomAttributes(true).Any(it => it is SugarColumn)) { var sugarAttribute = (SugarColumn)property.GetCustomAttributes(true).First(it => it is SugarColumn); item.Converter = new IsoDateTimeConverter() { DateTimeFormat = sugarAttribute.SerializeDateTimeFormat }; } }
public object FormatValue(int i, string name, object value) { if (value == null) { return("NULL"); } else { var type = UtilMethods.GetUnderType(value.GetType()); if (type == UtilConstants.DateType) { var date = value.ObjToDate(); if (date < Convert.ToDateTime("1900-1-1")) { date = Convert.ToDateTime("1900-1-1"); } return("'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'"); } else if (type.IsEnum()) { if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true) { return(value.ToSqlValue()); } else { return(Convert.ToInt64(value)); } } else if (type == UtilConstants.ByteArrayType) { var parameterName = this.Builder.SqlParameterKeyWord + name + i; this.Parameters.Add(new SugarParameter(parameterName, value)); return(parameterName); } else if (type == UtilConstants.BoolType) { return(value.ObjToBool() ? "1" : "0"); } else if (type == UtilConstants.StringType || type == UtilConstants.ObjType) { return("'" + value.ToString().ToSqlFilter() + "'"); } else { return("'" + value.ToString() + "'"); } } }
protected virtual bool IsNoSamgeType(EntityColumnInfo ec, DbColumnInfo dc) { if (!string.IsNullOrEmpty(ec.DataType)) { return(ec.DataType != dc.DataType); } var propertyType = UtilMethods.GetUnderType(ec.PropertyInfo); var properyTypeName = string.Empty; if (propertyType.IsEnum()) { properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(ec.Length > 9 ? UtilConstants.LongType.Name : UtilConstants.IntType.Name); } else { var name = GetType(propertyType.Name); properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(name); } var dataType = dc.DataType; if (properyTypeName == "boolean" && dataType == "bool") { return(false); } if (properyTypeName?.ToLower() == "varchar" && dataType?.ToLower() == "string") { return(false); } if (properyTypeName?.ToLower() == "number" && dataType?.ToLower() == "decimal") { return(false); } if (properyTypeName?.ToLower() == "int" && dataType?.ToLower() == "decimal" && dc.Length == 22 && dc.Scale == 0 && this.Context.CurrentConnectionConfig.DbType == DbType.Oracle) { return(false); } if (properyTypeName?.ToLower() == "date" && dataType?.ToLower() == "datetime") { return(false); } if (properyTypeName == null || dataType == null) { return(properyTypeName != dataType); } else { return(properyTypeName.ToLower() != dataType.ToLower()); } }
private void SetUpdateItemByEntity(int i, T item, List <DbColumnInfo> updateItem) { foreach (var column in EntityInfo.Columns) { if (column.IsIgnore) { continue; } var columnInfo = new DbColumnInfo() { Value = column.PropertyInfo.GetValue(item, null), DbColumnName = GetDbColumnName(column.PropertyName), PropertyName = column.PropertyName, PropertyType = UtilMethods.GetUnderType(column.PropertyInfo), TableId = i }; if (columnInfo.PropertyType.IsEnum() && columnInfo.Value != null) { if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true) { columnInfo.PropertyType = UtilConstants.StringType; columnInfo.Value = columnInfo.Value.ToString(); } else { columnInfo.Value = Convert.ToInt64(columnInfo.Value); } } if (column.IsJson) { columnInfo.IsJson = true; if (columnInfo.Value != null) { columnInfo.Value = this.Context.Utilities.SerializeObject(columnInfo.Value); } } if (column.IsArray) { columnInfo.IsArray = true; } var tranColumn = EntityInfo.Columns.FirstOrDefault(it => it.IsTranscoding && it.DbColumnName.Equals(column.DbColumnName, StringComparison.CurrentCultureIgnoreCase)); if (tranColumn != null && columnInfo.Value.HasValue()) { columnInfo.Value = UtilMethods.EncodeBase64(columnInfo.Value.ToString()); } updateItem.Add(columnInfo); } this.UpdateBuilder.DbColumnInfoList.AddRange(updateItem); }
private void SetInsertItemByEntity(int i, T item, List <DbColumnInfo> insertItem) { if (item == null) { return; } foreach (var column in EntityInfo.Columns) { if (column.IsIgnore || column.IsOnlyIgnoreInsert) { continue; } var columnInfo = new DbColumnInfo() { Value = column.PropertyInfo.GetValue(item, null), DbColumnName = GetDbColumnName(column.PropertyName), PropertyName = column.PropertyName, PropertyType = UtilMethods.GetUnderType(column.PropertyInfo), TableId = i }; if (column.IsJson) { columnInfo.IsJson = true; } if (column.IsArray) { columnInfo.IsArray = true; } if (columnInfo.PropertyType.IsEnum()) { columnInfo.Value = Convert.ToInt64(columnInfo.Value); } if (column.IsJson && columnInfo.Value != null) { if (columnInfo.Value != null) { columnInfo.Value = this.Context.Utilities.SerializeObject(columnInfo.Value); } } var tranColumn = EntityInfo.Columns.FirstOrDefault(it => it.IsTranscoding && it.DbColumnName.Equals(column.DbColumnName, StringComparison.CurrentCultureIgnoreCase)); if (tranColumn != null && columnInfo.Value.HasValue()) { columnInfo.Value = UtilMethods.EncodeBase64(columnInfo.Value.ToString()); } insertItem.Add(columnInfo); } }
private void SetCopyData(out DataTable dt, out SqlBulkCopy bulkCopy) { dt = this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString); foreach (var rowInfos in DbColumnInfoList) { var dr = dt.NewRow(); foreach (DataColumn item in dt.Columns) { var rows = rowInfos.ToList(); var value = rows.FirstOrDefault(it => it.DbColumnName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase) || it.PropertyName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase) ); if (value != null) { if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType) { if (value.Value != null && value.Value.ToString() == DateTime.MinValue.ToString()) { value.Value = Convert.ToDateTime("1753/01/01"); } } dr[item.ColumnName] = value.Value; } } dt.Rows.Add(dr); } bulkCopy = null; if (this.Context.Ado.Transaction != null) { var sqlTran = this.Context.Ado.Transaction as SqlTransaction; bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection, SqlBulkCopyOptions.CheckConstraints, sqlTran); } else { bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection); } //获取目标表的名称 bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString; //写入DataReader对象 if (this.Context.Ado.Connection.State == ConnectionState.Closed) { this.Context.Ado.Connection.Open(); } }
public virtual object FormatValue(object value) { if (value == null) { return("NULL"); } else { var type = UtilMethods.GetUnderType(value.GetType()); if (type == UtilConstants.DateType) { var date = value.ObjToDate(); if (date < Convert.ToDateTime("1900-1-1")) { date = Convert.ToDateTime("1900-1-1"); } return("'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'"); } else if (type == UtilConstants.ByteArrayType) { string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", ""); return(bytesString); } else if (type.IsEnum()) { return(Convert.ToInt64(value)); } else if (type == UtilConstants.BoolType) { return(value.ObjToBool() ? "1" : "0"); } else if (type == UtilConstants.StringType || type == UtilConstants.ObjType) { return("N'" + value.ToString().ToSqlFilter() + "'"); } else if (type == UtilConstants.FloatType) { return("N'" + Convert.ToDouble(value).ToString() + "'"); } else { return("N'" + value.ToString() + "'"); } } }
public override object FormatValue(object value) { if (value == null) { return("NULL"); } else { var type = UtilMethods.GetUnderType(value.GetType()); if (type == UtilConstants.StringType && value.ToString().Contains("{SugarSeq:=}")) { return(value.ToString().Replace("{SugarSeq:=}", "")); } if (type == UtilConstants.DateType) { var date = value.ObjToDate(); if (date < Convert.ToDateTime("1900-1-1")) { date = Convert.ToDateTime("1900-1-1"); } return("to_date('" + date.ToString("yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') "); } else if (type.IsEnum()) { return(Convert.ToInt64(value)); } else if (type == UtilConstants.ByteArrayType) { string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", ""); return(bytesString); } else if (type == UtilConstants.BoolType) { return(value.ObjToBool() ? "1" : "0"); } else if (type == UtilConstants.StringType || type == UtilConstants.ObjType) { return("N'" + value.ToString().ToSqlFilter() + "'"); } else { return("N'" + value.ToString() + "'"); } } }
private void SetInsertItemByDic(int i, T item, List <DbColumnInfo> insertItem) { foreach (var column in item as Dictionary <string, object> ) { var columnInfo = new DbColumnInfo() { Value = column.Value, DbColumnName = column.Key, PropertyName = column.Key, PropertyType = column.Value == null?DBNull.Value.GetType() : UtilMethods.GetUnderType(column.Value.GetType()), TableId = i }; if (columnInfo.PropertyType.IsEnum()) { columnInfo.Value = Convert.ToInt64(columnInfo.Value); } insertItem.Add(columnInfo); } }
private void SetUpdateItemByEntity(int i, T item, List <DbColumnInfo> updateItem) { foreach (var column in EntityInfo.Columns) { var columnInfo = new DbColumnInfo() { Value = column.PropertyInfo.GetValue(item, null), DbColumnName = GetDbColumnName(column.PropertyName), PropertyName = column.PropertyName, PropertyType = UtilMethods.GetUnderType(column.PropertyInfo), TableId = i }; if (columnInfo.PropertyType.IsEnum()) { columnInfo.Value = Convert.ToInt64(columnInfo.Value); } updateItem.Add(columnInfo); } this.UpdateBuilder.DbColumnInfoList.AddRange(updateItem); }
protected virtual DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item) { var propertyType = UtilMethods.GetUnderType(item.PropertyInfo); var result = new DbColumnInfo() { TableId = entityInfo.Columns.IndexOf(item), DbColumnName = item.DbColumnName.HasValue() ? item.DbColumnName : item.PropertyName, IsPrimarykey = item.IsPrimarykey, IsIdentity = item.IsIdentity, TableName = tableName, IsNullable = item.IsNullable, DefaultValue = item.DefaultValue, ColumnDescription = item.ColumnDescription, Length = item.Length, DecimalDigits = item.DecimalDigits }; GetDbType(item, propertyType, result); return(result); }
private void SetUpdateItemByDic(int i, T item, List <DbColumnInfo> updateItem) { foreach (var column in item as Dictionary <string, object> ) { var columnInfo = new DbColumnInfo() { Value = column.Value, DbColumnName = column.Key, PropertyName = column.Key, PropertyType = UtilMethods.GetUnderType(column.Value.GetType()), TableId = i }; if (columnInfo.PropertyType.IsEnum()) { columnInfo.Value = Convert.ToInt64(columnInfo.Value); } updateItem.Add(columnInfo); } this.UpdateBuilder.DbColumnInfoList.AddRange(updateItem); }
protected virtual bool IsSamgeType(EntityColumnInfo ec, DbColumnInfo dc) { if (!string.IsNullOrEmpty(ec.DataType)) { return(ec.DataType != dc.DataType); } var propertyType = UtilMethods.GetUnderType(ec.PropertyInfo); var properyTypeName = string.Empty; if (propertyType.IsEnum()) { properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(ec.Length > 9 ? UtilConstants.LongType.Name : UtilConstants.IntType.Name); } else { properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(propertyType.Name); } var dataType = dc.DataType; return(properyTypeName != dataType); }
protected List <T> GetValueTypeList <T>(Type type, IDataReader dataReader) { List <T> reval = new List <T>(); using (IDataReader re = dataReader) { while (re.Read()) { var value = re.GetValue(0); if (value == DBNull.Value) { reval.Add(default(T)); } else { reval.Add((T)Convert.ChangeType(re.GetValue(0), UtilMethods.GetUnderType(type))); } } } return(reval); }
private static void GetValueTypeList <T>(Type type, IDataReader dataReader, List <T> result) { var value = dataReader.GetValue(0); if (type == UtilConstants.GuidType) { value = Guid.Parse(value.ToString()); } if (value == DBNull.Value) { result.Add(default(T)); } else if (type.IsEnum) { result.Add((T)Enum.Parse(type, value.ObjToString())); } else { result.Add((T)Convert.ChangeType(value, UtilMethods.GetUnderType(type))); } }
private void Select(MemberInitExpression expression, ExpressionParameter parameter, bool isSingle) { foreach (MemberBinding binding in expression.Bindings) { if (binding.BindingType != MemberBindingType.Assignment) { throw new NotSupportedException(); } MemberAssignment memberAssignment = (MemberAssignment)binding; var memberName = memberAssignment.Member.Name; var item = memberAssignment.Expression; if (IsNullable(item) && item is UnaryExpression) { var memtype = ExpressionTool.GetMemberInfoType(memberAssignment.Member); if (IsNullable(memtype) && UtilMethods.GetUnderType(memtype) == UtilMethods.GetUnderType(item.Type)) { item = (item as UnaryExpression).Operand; } } ResolveNewExpressions(parameter, item, memberName); } }
public string GetValue(object entityValue) { if (entityValue == null) { return(null); } var type = UtilMethods.GetUnderType(entityValue.GetType()); if (UtilConstants.NumericalTypes.Contains(type)) { return(entityValue.ToString()); } else if (type == UtilConstants.DateType) { return(this.DbMehtods.ToDate(new MethodCallExpressionModel() { Args = new System.Collections.Generic.List <MethodCallExpressionArgs>() { new MethodCallExpressionArgs() { MemberName = $"'{entityValue}'" } } })); } else { return(this.DbMehtods.ToString(new MethodCallExpressionModel() { Args = new System.Collections.Generic.List <MethodCallExpressionArgs>() { new MethodCallExpressionArgs() { MemberName = $"'{entityValue}'" } } })); } }
protected List <T> GetValueTypeList <T>(Type type, IDataReader dataReader) { List <T> reval = new List <T>(); while (dataReader.Read()) { var value = dataReader.GetValue(0); if (type == UtilConstants.GuidType) { value = Guid.Parse(value.ToString()); } if (value == DBNull.Value) { reval.Add(default(T)); } else { reval.Add((T)Convert.ChangeType(value, UtilMethods.GetUnderType(type))); } } return(reval); }
protected override DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item) { var result = new DbColumnInfo() { DataType = this.Context.Ado.DbBind.GetDbTypeName(UtilMethods.GetUnderType(item.PropertyInfo).Name), TableId = entityInfo.Columns.IndexOf(item), DbColumnName = item.DbColumnName.IsValuable() ? item.DbColumnName : item.PropertyName, IsPrimarykey = item.IsPrimarykey, IsIdentity = item.IsIdentity, TableName = tableName, IsNullable = item.IsNullable, DefaultValue = item.DefaultValue, ColumnDescription = item.ColumnDescription, Length = item.Length }; if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0) { result.Length = 1; } return(result); }
private void SetInsertItemByEntity(int i, T item, List <DbColumnInfo> insertItem) { foreach (var column in EntityInfo.Columns) { if (column.IsIgnore || column.IsOnlyIgnoreInsert) { continue; } var columnInfo = new DbColumnInfo() { Value = column.PropertyInfo.GetValue(item, null), DbColumnName = GetDbColumnName(column.PropertyName), PropertyName = column.PropertyName, PropertyType = UtilMethods.GetUnderType(column.PropertyInfo), TableId = i }; if (columnInfo.PropertyType.IsEnum()) { columnInfo.Value = Convert.ToInt64(columnInfo.Value); } insertItem.Add(columnInfo); } }
internal void Init() { this.UpdateBuilder.TableName = EntityInfo.EntityName; if (IsMappingTable) { var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == EntityInfo.EntityName); if (mappingInfo != null) { this.UpdateBuilder.TableName = mappingInfo.DbTableName; } } Check.Exception(UpdateObjs == null || UpdateObjs.Count() == 0, "UpdateObjs is null"); int i = 0; foreach (var item in UpdateObjs) { List <DbColumnInfo> updateItem = new List <DbColumnInfo>(); foreach (var column in EntityInfo.Columns) { var columnInfo = new DbColumnInfo() { Value = column.PropertyInfo.GetValue(item, null), DbColumnName = GetDbColumnName(column.PropertyName), PropertyName = column.PropertyName, PropertyType = UtilMethods.GetUnderType(column.PropertyInfo), TableId = i }; if (columnInfo.PropertyType.IsEnum()) { columnInfo.Value = Convert.ToInt64(columnInfo.Value); } updateItem.Add(columnInfo); } this.UpdateBuilder.DbColumnInfoList.AddRange(updateItem); ++i; } }
private void ResolveHasValue(ExpressionParameter parameter, MemberExpression expression) { parameter.CommonTempData = CommonTempDataType.Result; this.Expression = expression.Expression; this.Start(); var methodParamter = new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData, MemberValue = null }; if (expression.Expression?.Type != null) { methodParamter.Type = UtilMethods.GetUnderType(expression.Expression?.Type); } var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel() { Args = new List <MethodCallExpressionArgs>() { methodParamter } }); if (parameter.BaseExpression != null && ExpressionTool.IsLogicOperator(parameter.BaseExpression) && parameter.IsLeft == true) { if (base.Context.Result.Contains(ExpressionConst.FormatSymbol)) { base.Context.Result.Replace(ExpressionConst.FormatSymbol, ""); } this.Context.Result.Append(result + " " + ExpressionTool.GetOperator(parameter.BaseExpression.NodeType) + " "); } else { this.Context.Result.Append(result); } parameter.CommonTempData = null; }
public override void NoExistLogic(EntityInfo entityInfo) { var tableName = GetTableName(entityInfo); //Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1"); List <DbColumnInfo> columns = new List <DbColumnInfo>(); if (entityInfo.Columns.HasValue()) { foreach (var item in entityInfo.Columns.OrderBy(it => it.IsPrimarykey ? 0 : 1)) { if (item.IsIgnore) { continue; } if (item.PropertyInfo != null && UtilMethods.GetUnderType(item.PropertyInfo.PropertyType) == UtilConstants.GuidType && item.Length == 0 && item.DataType == null) { item.Length = Guid.NewGuid().ToString().Length; } DbColumnInfo dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item); columns.Add(dbColumnInfo); } } this.Context.DbMaintenance.CreateTable(tableName, columns, true); }
private void Update(MemberInitExpression expression, ExpressionParameter parameter) { int i = 0; foreach (MemberBinding binding in expression.Bindings) { ++i; if (binding.BindingType != MemberBindingType.Assignment) { throw new NotSupportedException(); } MemberAssignment memberAssignment = (MemberAssignment)binding; var type = expression.Type; var memberName = this.Context.GetDbColumnName(type.Name, memberAssignment.Member.Name); var item = memberAssignment.Expression; //Column IsJson Handler if (memberAssignment.Member.CustomAttributes != null) { var customAttribute = memberAssignment.Member.GetCustomAttribute <SugarColumn>(); if (customAttribute?.IsJson ?? false) { var paramterValue = ExpressionTool.DynamicInvoke(item); var parameterName = AppendParameter(new SerializeService().SerializeObject(paramterValue)); this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); continue; } } if ((item is MemberExpression) && ((MemberExpression)item).Expression == null) { var paramterValue = ExpressionTool.DynamicInvoke(item); string parameterName = AppendParameter(paramterValue); this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); } else if (IsNotMember(item)) { if (base.Context.Result.IsLockCurrentParameter == false) { base.Context.Result.CurrentParameter = parameter; base.Context.Result.IsLockCurrentParameter = true; parameter.IsAppendTempDate(); base.Expression = item; base.Expression = (item as UnaryExpression).Operand; base.Start(); parameter.IsAppendResult(); var result = this.Context.DbMehtods.IIF(new MethodCallExpressionModel() { Args = new List <MethodCallExpressionArgs>() { new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData.ObjToString() + "=1" }, new MethodCallExpressionArgs() { IsMember = true, MemberName = AppendParameter(0) }, new MethodCallExpressionArgs() { IsMember = true, MemberName = AppendParameter(1) } } }); parameter.Context.Result.Append(base.Context.GetEqString(memberName, result)); base.Context.Result.CurrentParameter = null; } } else if (IsNotParameter(item)) { try { parameter.Context.Result.Append(base.Context.GetEqString(memberName, AppendParameter(ExpressionTool.DynamicInvoke(item).ObjToBool()))); } catch { throw new NotSupportedException(item.ToString()); } } else if (IsMethod(item)) { if (item is UnaryExpression) { item = (item as UnaryExpression).Operand; } var callMethod = item as MethodCallExpression; if (MethodTimeMapping.Any(it => it.Key == callMethod.Method.Name) || MethodMapping.Any(it => it.Key == callMethod.Method.Name) || IsExtMethod(callMethod.Method.Name) || IsSubMethod(callMethod) || callMethod.Method.DeclaringType.FullName.StartsWith(UtilConstants.AssemblyName + UtilConstants.Dot)) { MethodCall(parameter, memberName, item); } else { var paramterValue = ExpressionTool.DynamicInvoke(item); string parameterName = AppendParameter(paramterValue); this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); } } else if (IsConst(item) && IsConvert(item) && UtilMethods.IsNullable(item.Type) && UtilMethods.GetUnderType(item.Type) == UtilConstants.BoolType) { item = (item as UnaryExpression).Operand; parameter.Context.Result.Append(base.Context.GetEqString(memberName, GetNewExpressionValue(item))); } else if (IsConst(item)) { base.Expression = item; base.Start(); string parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex; parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); var addItem = new SugarParameter(parameterName, parameter.CommonTempData); var dataType = UtilMethods.GetUnderType(item.Type); if (addItem.Value == null && dataType == UtilConstants.DateType) { addItem.DbType = System.Data.DbType.Date; } this.Context.Parameters.Add(addItem); this.Context.ParameterIndex++; } else if (item is MemberExpression) { if (base.Context.Result.IsLockCurrentParameter == false) { base.Context.Result.CurrentParameter = parameter; base.Context.Result.IsLockCurrentParameter = true; parameter.IsAppendTempDate(); base.Expression = item; base.Start(); parameter.IsAppendResult(); parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString())); base.Context.Result.CurrentParameter = null; } } else if (item is BinaryExpression) { var result = GetNewExpressionValue(item); if (result.HasValue()) { result = result.Replace(",", UtilConstants.ReplaceCommaKey); } this.Context.Result.Append(base.Context.GetEqString(memberName, result)); } else if (item is MemberInitExpression) { try { var value = ExpressionTool.DynamicInvoke(item); var parameterName = AppendParameter(value); parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); } catch (Exception ex) { throw new NotSupportedException("Not Supported " + item.ToString() + " " + ex.Message); } } else if (item is NewExpression) { try { var value = ExpressionTool.DynamicInvoke(item); var parameterName = AppendParameter(value); parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName)); } catch (Exception ex) { throw new NotSupportedException("Not Supported " + item.ToString() + " " + ex.Message); } } else if (item is ConditionalExpression) { var result = GetNewExpressionValue(item); this.Context.Result.Append(base.Context.GetEqString(memberName, result)); } } }
private void BindMethod(ILGenerator generator, EntityColumnInfo columnInfo, int ordinal) { IDbBind bind = Context.Ado.DbBind; bool isNullableType = false; MethodInfo method = null; Type bindPropertyType = UtilMethods.GetUnderType(columnInfo.PropertyInfo, ref isNullableType); string dbTypeName = UtilMethods.GetParenthesesValue(DataRecord.GetDataTypeName(ordinal)); if (dbTypeName.IsNullOrEmpty()) { dbTypeName = bindPropertyType.Name; } string propertyName = columnInfo.PropertyName; string validPropertyName = bind.GetPropertyTypeName(dbTypeName); validPropertyName = validPropertyName == "byte[]" ? "byteArray" : validPropertyName; CSharpDataType validPropertyType = (CSharpDataType)Enum.Parse(typeof(CSharpDataType), validPropertyName); #region Sqlite Logic if (this.Context.CurrentConnectionConfig.DbType == DbType.Sqlite) { if (bindPropertyType.IsEnum()) { method = isNullableType ? getConvertEnum_Null.MakeGenericMethod(bindPropertyType) : getEnum.MakeGenericMethod(bindPropertyType); } else if (bindPropertyType == UtilConstants.IntType) { method = isNullableType ? getConvertInt32 : getInt32; } else if (bindPropertyType == UtilConstants.ByteType) { method = isNullableType ? getConvertByte : getByte; } else if (bindPropertyType == UtilConstants.StringType && dbTypeName?.ToLower() == "timestamp") { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } else if (bindPropertyType == UtilConstants.StringType) { method = getString; } else { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } if (method.IsVirtual) { generator.Emit(OpCodes.Callvirt, method); } else { generator.Emit(OpCodes.Call, method); } return; } ; #endregion #region Common Database Logic string bindProperyTypeName = bindPropertyType.Name.ToLower(); bool isEnum = bindPropertyType.IsEnum(); if (isEnum) { validPropertyType = CSharpDataType.@enum; } switch (validPropertyType) { case CSharpDataType.@int: CheckType(bind.IntThrow, bindProperyTypeName, validPropertyName, propertyName); if (bindProperyTypeName.IsContainsIn("int", "int32")) { method = isNullableType ? getConvertInt32 : getInt32; } if (bindProperyTypeName.IsContainsIn("int64")) { method = isNullableType ? getConvertInt64 : getInt64; } if (bindProperyTypeName.IsContainsIn("byte")) { method = isNullableType ? getConvertByte : getByte; } if (bindProperyTypeName.IsContainsIn("int16")) { method = isNullableType ? getConvertInt16 : getInt16; } break; case CSharpDataType.@bool: if (bindProperyTypeName == "bool" || bindProperyTypeName == "boolean") { method = isNullableType ? getConvertBoolean : getBoolean; } break; case CSharpDataType.@string: if (this.Context.CurrentConnectionConfig.DbType != DbType.Oracle) { CheckType(bind.StringThrow, bindProperyTypeName, validPropertyName, propertyName); } method = getString; if (bindProperyTypeName == "guid") { method = isNullableType ? getConvertStringGuid : getStringGuid; } break; case CSharpDataType.DateTime: CheckType(bind.DateThrow, bindProperyTypeName, validPropertyName, propertyName); if (bindProperyTypeName == "datetime") { method = isNullableType ? getConvertDateTime : getDateTime; } if (bindProperyTypeName == "datetime" && dbTypeName.ToLower() == "time") { method = isNullableType ? getConvertTime : getTime; } if (bindProperyTypeName == "datetimeoffset") { method = isNullableType ? getConvertdatetimeoffset : getdatetimeoffset; } break; case CSharpDataType.@decimal: CheckType(bind.DecimalThrow, bindProperyTypeName, validPropertyName, propertyName); if (bindProperyTypeName == "decimal") { method = isNullableType ? getConvertDecimal : getDecimal; } break; case CSharpDataType.@float: case CSharpDataType.@double: CheckType(bind.DoubleThrow, bindProperyTypeName, validPropertyName, propertyName); if (bindProperyTypeName.IsIn("double", "single") && dbTypeName != "real") { method = isNullableType ? getConvertDouble : getDouble; } else { method = isNullableType ? getConvertFloat : getFloat; } if (dbTypeName.Equals("float", StringComparison.CurrentCultureIgnoreCase) && isNullableType && bindProperyTypeName.Equals("single", StringComparison.CurrentCultureIgnoreCase)) { method = getConvertDoubleToFloat; } if (bindPropertyType == UtilConstants.DecType) { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } if (bindPropertyType == UtilConstants.IntType) { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } break; case CSharpDataType.Guid: CheckType(bind.GuidThrow, bindProperyTypeName, validPropertyName, propertyName); if (bindProperyTypeName == "guid") { method = isNullableType ? getConvertGuid : getGuid; } break; case CSharpDataType.@byte: if (bindProperyTypeName == "byte") { method = isNullableType ? getConvertByte : getByte; } break; case CSharpDataType.@enum: method = isNullableType ? getConvertEnum_Null.MakeGenericMethod(bindPropertyType) : getEnum.MakeGenericMethod(bindPropertyType); break; case CSharpDataType.@short: CheckType(bind.ShortThrow, bindProperyTypeName, validPropertyName, propertyName); if (bindProperyTypeName == "int16" || bindProperyTypeName == "short") { method = isNullableType ? getConvertInt16 : getInt16; } break; case CSharpDataType.@long: if (bindProperyTypeName == "int64" || bindProperyTypeName == "long") { method = isNullableType ? getConvertInt64 : getInt64; } break; case CSharpDataType.DateTimeOffset: method = isNullableType ? getConvertdatetimeoffset : getdatetimeoffset; if (bindProperyTypeName == "datetime") { method = isNullableType ? getConvertdatetimeoffsetDate : getdatetimeoffsetDate; } break; case CSharpDataType.Single: break; default: method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); break; } if (method == null && bindPropertyType == UtilConstants.StringType) { method = getConvertString; } if (bindPropertyType == UtilConstants.ObjType) { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } if (method == null) { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } if (method.IsVirtual) { generator.Emit(OpCodes.Callvirt, method); } else { generator.Emit(OpCodes.Call, method); } #endregion }
public virtual void ExistLogic(EntityInfo entityInfo) { if (entityInfo.Columns.HasValue()) { Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1"); var tableName = GetTableName(entityInfo); var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName); ConvertColumns(dbColumns); var entityColumns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList(); var dropColumns = dbColumns .Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase))) .Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.DbColumnName, StringComparison.CurrentCultureIgnoreCase))) .ToList(); var addColumns = entityColumns .Where(ec => ec.OldDbColumnName.IsNullOrEmpty() || !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase))) .Where(ec => !dbColumns.Any(dc => ec.DbColumnName.Equals(dc.DbColumnName, StringComparison.CurrentCultureIgnoreCase))).ToList(); var alterColumns = entityColumns .Where(ec => !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase))) .Where(ec => dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName) && ((ec.Length != dc.Length && !UtilMethods.GetUnderType(ec.PropertyInfo).IsEnum() && UtilMethods.GetUnderType(ec.PropertyInfo).IsIn(UtilConstants.StringType)) || ec.IsNullable != dc.IsNullable || IsSamgeType(ec, dc)))).ToList(); var renameColumns = entityColumns .Where(it => !string.IsNullOrEmpty(it.OldDbColumnName)) .Where(entityColumn => dbColumns.Any(dbColumn => entityColumn.OldDbColumnName.Equals(dbColumn.DbColumnName, StringComparison.CurrentCultureIgnoreCase))) .ToList(); var isChange = false; foreach (var item in addColumns) { this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item)); isChange = true; } foreach (var item in dropColumns) { this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName); isChange = true; } foreach (var item in alterColumns) { this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item)); isChange = true; } foreach (var item in renameColumns) { this.Context.DbMaintenance.RenameColumn(tableName, item.OldDbColumnName, item.DbColumnName); isChange = true; } foreach (var item in entityColumns) { var dbColumn = dbColumns.FirstOrDefault(dc => dc.DbColumnName.Equals(item.DbColumnName, StringComparison.CurrentCultureIgnoreCase)); if (dbColumn == null) { continue; } var pkDiff = item.IsPrimarykey != dbColumn.IsPrimarykey; var idEntityDiff = item.IsIdentity != dbColumn.IsIdentity; if (dbColumn != null && pkDiff && !idEntityDiff) { var isAdd = item.IsPrimarykey; if (isAdd) { this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName); } else { this.Context.DbMaintenance.DropConstraint(tableName, string.Format("PK_{0}_{1}", tableName, item.DbColumnName)); } } else if (pkDiff || idEntityDiff) { ChangeKey(entityInfo, tableName, item); } } if (isChange && IsBackupTable) { this.Context.DbMaintenance.BackupTable(tableName, tableName + DateTime.Now.ToString("yyyyMMddHHmmss"), MaxBackupDataRows); } } }