public override DbExpression Visit(DbMethodCallExpression exp) { IMethodHandler methodHandler; if (MethodHandlers.TryGetValue(exp.Method.Name, out methodHandler)) { if (methodHandler.CanProcess(exp)) { methodHandler.Process(exp, this); return(exp); } } DbFunctionAttribute dbFunction = exp.Method.GetCustomAttribute <DbFunctionAttribute>(); if (dbFunction != null) { string schema = dbFunction.Schema; string functionName = string.IsNullOrEmpty(dbFunction.Name) ? exp.Method.Name : dbFunction.Name; if (!string.IsNullOrEmpty(schema)) { this.QuoteName(schema); this.SqlBuilder.Append("."); } this.QuoteName(functionName); this.SqlBuilder.Append("("); string c = ""; foreach (DbExpression argument in exp.Arguments) { this.SqlBuilder.Append(c); argument.Accept(this); c = ","; } this.SqlBuilder.Append(")"); return(exp); } if (exp.IsEvaluable()) { DbParameterExpression dbParameter = new DbParameterExpression(exp.Evaluate(), exp.Type); return(dbParameter.Accept(this)); } throw UtilExceptions.NotSupportedMethod(exp.Method); }
public static void AmendDbInfo(DbColumn column, DbExpression exp) { if (column.DbType == null || exp.NodeType != DbExpressionType.Parameter) { return; } DbParameterExpression expToAmend = (DbParameterExpression)exp; if (expToAmend.DbType == null) { expToAmend.DbType = column.DbType; } }
public override DbExpression Visit(DbParameterExpression exp) { object paramValue = exp.Value; Type paramType = exp.Type; if (paramType.IsEnum) { paramType = Enum.GetUnderlyingType(paramType); if (paramValue != null) paramValue = Convert.ChangeType(paramValue, paramType); } else if (paramType == UtilConstants.TypeOfBoolean) { paramType = UtilConstants.TypeOfInt32; if (paramValue != null) { paramValue = (bool)paramValue ? Boxed_1 : Boxed_0; } } if (paramValue == null) paramValue = DBNull.Value; DbParam p = this._parameters.Find(paramValue, paramType, exp.DbType); if (p != null) { this._sqlBuilder.Append(p.Name); return exp; } string paramName = GenParameterName(this._parameters.Count); p = DbParam.Create(paramName, paramValue, paramType); if (paramValue.GetType() == UtilConstants.TypeOfString) { if (exp.DbType == DbType.AnsiStringFixedLength || exp.DbType == DbType.StringFixedLength) p.Size = ((string)paramValue).Length; else if (((string)paramValue).Length <= 4000) p.Size = 4000; } if (exp.DbType != null) p.DbType = exp.DbType; this._parameters.Add(p); this._sqlBuilder.Append(paramName); return exp; }
public override DbExpression TypeTranslate(ExpressionParser parser, ConstantExpression expr) { if (expr.Value is ObjectQuery objectQuery) { if (WorkSpace.EntitySetCache.TryGetValue(objectQuery.ElementType, out EntitySet entitySet)) { DbParameterExpression p = new DbParameterExpression(parser.ParameterAliasGenerator.GetName(), new EntityType(entitySet)); return(new DbEntitySetExpression(entitySet, p)); } throw new NotSupportedException(); } else { return(new DbConstantExpression(expr.Value, null)); } }
public override DbExpression Visit(DbParameterExpression exp) { object paramValue = exp.Value; Type paramType = exp.Type; if (paramType.IsEnum()) { paramType = Enum.GetUnderlyingType(paramType); if (paramValue != null) { paramValue = Convert.ChangeType(paramValue, paramType); } } if (paramValue == null) { paramValue = DBNull.Value; } DbParam p; string paramName = GenParameterName(this._parameters.Count); p = DbParam.Create(paramName, paramValue, paramType); if (paramValue.GetType() == UtilConstants.TypeOfString) { if (exp.DbType == DbType.AnsiStringFixedLength || exp.DbType == DbType.StringFixedLength) { p.Size = ((string)paramValue).Length; } else if (((string)paramValue).Length <= 4000) { p.Size = 4000; } } if (exp.DbType != null) { p.DbType = exp.DbType; } this._parameters.Add(p); this._sqlBuilder.Append(paramName); return(exp); }
public override DbExpression Visit(DbConstantExpression exp) { if (exp.Value == null || exp.Value == DBNull.Value) { this.SqlBuilder.Append("NULL"); return(exp); } var objType = exp.Value.GetType(); if (objType == PublicConstants.TypeOfBoolean) { this.SqlBuilder.Append(((bool)exp.Value) ? "1" : "0"); return(exp); } else if (objType == PublicConstants.TypeOfString) { if (string.Empty.Equals(exp.Value)) { this.SqlBuilder.Append("'", exp.Value, "'"); } else { this.SqlBuilder.Append("N'", exp.Value, "'"); } return(exp); } else if (objType.IsEnum) { this.SqlBuilder.Append(Convert.ChangeType(exp.Value, Enum.GetUnderlyingType(objType)).ToString()); return(exp); } else if (NumericTypes.ContainsKey(exp.Value.GetType())) { this.SqlBuilder.Append(exp.Value); return(exp); } DbParameterExpression p = new DbParameterExpression(exp.Value); p.Accept(this); return(exp); }
public override DbExpression Visit(DbMethodCallExpression exp) { IMethodHandler methodHandler; if (MethodHandlers.TryGetValue(exp.Method.Name, out methodHandler)) { if (methodHandler.CanProcess(exp)) { methodHandler.Process(exp, this); return exp; } } if (exp.IsEvaluable()) { DbParameterExpression dbParameter = new DbParameterExpression(exp.Evaluate(), exp.Type); return dbParameter.Accept(this); } throw UtilExceptions.NotSupportedMethod(exp.Method); }
public override DbExpression Visit(DbConstantExpression exp) { if (exp.Value == null || exp.Value == DBNull.Value) { this._sqlBuilder.Append("NULL"); return(exp); } var objType = exp.Value.GetType(); if (objType == UtilConstants.TypeOfBoolean) { this._sqlBuilder.Append(((bool)exp.Value) ? "1" : "0"); return(exp); } else if (objType == UtilConstants.TypeOfString) { this._sqlBuilder.Append("N'", exp.Value, "'"); return(exp); } else if (objType.IsEnum()) { this._sqlBuilder.Append(((int)exp.Value).ToString()); return(exp); } else if (NumericTypes.ContainsKey(exp.Value.GetType())) { this._sqlBuilder.Append(exp.Value); return(exp); } DbParameterExpression p = new DbParameterExpression(exp.Value); p.Accept(this); return(exp); }
/// <summary> /// 尝试将 exp 转换成 DbParameterExpression。 /// </summary> /// <param name="exp"></param> /// <param name="val"></param> /// <returns></returns> public static bool TryParseToParameterExpression(this DbMemberExpression exp, out DbParameterExpression val) { val = null; if (!exp.CanEvaluate()) return false; //求值 val = exp.ParseToParameterExpression(); return true; }
public override DbExpression Visit(DbParameterExpression exp) { return(exp.Accept(this._generator)); }
public DbExpressionBinding(DbProjectExpression e, DbParameterExpression p) { this.Expression = e; this.Parameter = p; }
public override object Visit(DbParameterExpression exp) { return(exp.Value); }
public static bool AreEqual(DbParameterExpression exp1, DbParameterExpression exp2) { return(Utils.AreEqual(exp1.Value, exp2.Value)); }
public override bool Visit(DbParameterExpression exp) { return(true); }
public virtual TEntity Insert <TEntity>(TEntity entity, string table) { Utils.CheckNull(entity); TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(entity.GetType()); Dictionary <PropertyDescriptor, object> keyValueMap = CreateKeyValueMap(typeDescriptor); Dictionary <PropertyDescriptor, DbExpression> insertColumns = new Dictionary <PropertyDescriptor, DbExpression>(); foreach (PropertyDescriptor propertyDescriptor in typeDescriptor.PropertyDescriptors) { if (propertyDescriptor.IsAutoIncrement) { continue; } object val = propertyDescriptor.GetValue(entity); if (propertyDescriptor.IsPrimaryKey) { keyValueMap[propertyDescriptor] = val; } DbParameterExpression valExp = DbExpression.Parameter(val, propertyDescriptor.PropertyType, propertyDescriptor.Column.DbType); insertColumns.Add(propertyDescriptor, valExp); } PropertyDescriptor nullValueKey = keyValueMap.Where(a => a.Value == null && !a.Key.IsAutoIncrement).Select(a => a.Key).FirstOrDefault(); if (nullValueKey != null) { /* 主键为空并且主键又不是自增列 */ throw new ChloeException(string.Format("The primary key '{0}' could not be null.", nullValueKey.Property.Name)); } DbTable dbTable = table == null ? typeDescriptor.Table : new DbTable(table, typeDescriptor.Table.Schema); DbInsertExpression e = new DbInsertExpression(dbTable); foreach (var kv in insertColumns) { e.InsertColumns.Add(kv.Key.Column, kv.Value); } PropertyDescriptor autoIncrementPropertyDescriptor = typeDescriptor.AutoIncrement; if (autoIncrementPropertyDescriptor == null) { this.ExecuteSqlCommand(e); return(entity); } IDbExpressionTranslator translator = this.DatabaseProvider.CreateDbExpressionTranslator(); List <DbParam> parameters; string sql = translator.Translate(e, out parameters); sql = string.Concat(sql, ";", this.GetSelectLastInsertIdClause()); //SELECT @@IDENTITY 返回的是 decimal 类型 object retIdentity = this.Session.ExecuteScalar(sql, parameters.ToArray()); if (retIdentity == null || retIdentity == DBNull.Value) { throw new ChloeException("Unable to get the identity value."); } retIdentity = PublicHelper.ConvertObjType(retIdentity, autoIncrementPropertyDescriptor.PropertyType); autoIncrementPropertyDescriptor.SetValue(entity, retIdentity); return(entity); }
public static bool ExpressionEquals(DbParameterExpression exp1, DbParameterExpression exp2) { return exp1.Value == exp2.Value; }
public override DbExpression Visit(DbParameterExpression exp) { return(exp); }
public override DbExpression Visit(DbParameterExpression exp) { object paramValue = exp.Value; Type paramType = exp.Type; if (paramType.IsEnum()) { paramType = UtilConstants.TypeOfInt32; if (paramValue != null) { paramValue = (int)paramValue; } } else if (paramType == UtilConstants.TypeOfBoolean) { paramType = UtilConstants.TypeOfInt32; if (paramValue != null) { paramValue = (bool)paramValue ? Boxed_1 : Boxed_0; } } if (paramValue == null) { paramValue = DBNull.Value; } DbParam p; if (paramValue == DBNull.Value) { p = this._parameters.Where(a => Utils.AreEqual(a.Value, paramValue) && a.Type == paramType).FirstOrDefault(); } else { p = this._parameters.Where(a => Utils.AreEqual(a.Value, paramValue)).FirstOrDefault(); } if (p != null) { this._sqlBuilder.Append(p.Name); return(exp); } string paramName = GenParameterName(this._parameters.Count); p = DbParam.Create(paramName, paramValue, paramType); if (paramValue.GetType() == UtilConstants.TypeOfString) { if (exp.DbType == DbType.AnsiStringFixedLength || exp.DbType == DbType.StringFixedLength) { p.Size = ((string)paramValue).Length; } else if (((string)paramValue).Length <= 4000) { p.Size = 4000; } } if (exp.DbType != null) { p.DbType = exp.DbType; } this._parameters.Add(p); this._sqlBuilder.Append(paramName); return(exp); }
public override ISqlPart Visit(DbParameterExpression dbExpression) { throw new NotImplementedException(); }
public override TEntity Insert <TEntity>(TEntity entity, string table) { PublicHelper.CheckNull(entity); TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity)); Dictionary <PropertyDescriptor, object> keyValueMap = PrimaryKeyHelper.CreateKeyValueMap(typeDescriptor); Dictionary <PropertyDescriptor, DbExpression> insertColumns = new Dictionary <PropertyDescriptor, DbExpression>(); foreach (PropertyDescriptor propertyDescriptor in typeDescriptor.PropertyDescriptors) { if (propertyDescriptor.IsAutoIncrement) { continue; } if (propertyDescriptor.HasSequence()) { DbMethodCallExpression getNextValueForSequenceExp = PublicHelper.MakeNextValueForSequenceDbExpression(propertyDescriptor); insertColumns.Add(propertyDescriptor, getNextValueForSequenceExp); continue; } object val = propertyDescriptor.GetValue(entity); if (propertyDescriptor.IsPrimaryKey) { keyValueMap[propertyDescriptor] = val; } DbParameterExpression valExp = DbExpression.Parameter(val, propertyDescriptor.PropertyType, propertyDescriptor.Column.DbType); insertColumns.Add(propertyDescriptor, valExp); } PropertyDescriptor nullValueKey = keyValueMap.Where(a => a.Value == null && !a.Key.IsAutoIncrement).Select(a => a.Key).FirstOrDefault(); if (nullValueKey != null) { /* 主键为空并且主键又不是自增列 */ throw new ChloeException(string.Format("The primary key '{0}' could not be null.", nullValueKey.Property.Name)); } DbTable dbTable = table == null ? typeDescriptor.Table : new DbTable(table, typeDescriptor.Table.Schema); DbInsertExpression insertExp = new DbInsertExpression(dbTable); foreach (var kv in insertColumns) { insertExp.InsertColumns.Add(kv.Key.Column, kv.Value); } List <Action <TEntity, IDataReader> > mappers = new List <Action <TEntity, IDataReader> >(); foreach (var item in typeDescriptor.PropertyDescriptors.Where(a => a.IsAutoIncrement || a.HasSequence())) { mappers.Add(GetMapper <TEntity>(item, insertExp.Returns.Count)); insertExp.Returns.Add(item.Column); } if (mappers.Count == 0) { this.ExecuteNonQuery(insertExp); return(entity); } IDbExpressionTranslator translator = this.DatabaseProvider.CreateDbExpressionTranslator(); List <DbParam> parameters; string sql = translator.Translate(insertExp, out parameters); IDataReader dataReader = this.Session.ExecuteReader(sql, parameters.ToArray()); using (dataReader) { dataReader.Read(); foreach (var mapper in mappers) { mapper(entity, dataReader); } } return(entity); }
/// <summary> /// 尝试将 exp 转换成 DbParameterExpression。 /// </summary> /// <param name="exp"></param> /// <param name="val"></param> /// <returns></returns> public static bool TryParseToParameterExpression(this DbMemberExpression exp, out DbParameterExpression val) { val = null; if (!exp.CanEvaluate()) { return(false); } //求值 val = exp.ParseToParameterExpression(); return(true); }
/// <summary> /// 尝试将 exp 转换成 DbParameterExpression。 /// </summary> /// <param name="exp"></param> /// <param name="val"></param> /// <returns></returns> public static bool TryConvertToParameterExpression(this DbMemberExpression exp, out DbParameterExpression val) { val = null; if (!exp.IsEvaluable()) { return(false); } //求值 val = exp.ConvertToParameterExpression(); return(true); }
protected virtual async Task <TEntity> Insert <TEntity>(TEntity entity, string table, bool @async) { PublicHelper.CheckNull(entity); TypeDescriptor typeDescriptor = EntityTypeContainer.GetDescriptor(typeof(TEntity)); Dictionary <PrimitivePropertyDescriptor, object> keyValueMap = PrimaryKeyHelper.CreateKeyValueMap(typeDescriptor); Dictionary <PrimitivePropertyDescriptor, DbExpression> insertColumns = new Dictionary <PrimitivePropertyDescriptor, DbExpression>(); foreach (PrimitivePropertyDescriptor propertyDescriptor in typeDescriptor.PrimitivePropertyDescriptors) { if (propertyDescriptor.IsAutoIncrement) { continue; } object val = propertyDescriptor.GetValue(entity); if (propertyDescriptor.IsPrimaryKey) { keyValueMap[propertyDescriptor] = val; } PublicHelper.NotNullCheck(propertyDescriptor, val); DbParameterExpression valExp = DbExpression.Parameter(val, propertyDescriptor.PropertyType, propertyDescriptor.Column.DbType); insertColumns.Add(propertyDescriptor, valExp); } PrimitivePropertyDescriptor nullValueKey = keyValueMap.Where(a => a.Value == null && !a.Key.IsAutoIncrement).Select(a => a.Key).FirstOrDefault(); if (nullValueKey != null) { /* 主键为空并且主键又不是自增列 */ throw new ChloeException(string.Format("The primary key '{0}' could not be null.", nullValueKey.Property.Name)); } DbTable dbTable = PublicHelper.CreateDbTable(typeDescriptor, table); DbInsertExpression e = new DbInsertExpression(dbTable); foreach (var kv in insertColumns) { e.InsertColumns.Add(kv.Key.Column, kv.Value); } PrimitivePropertyDescriptor autoIncrementPropertyDescriptor = typeDescriptor.AutoIncrement; if (autoIncrementPropertyDescriptor == null) { await this.ExecuteNonQuery(e, @async); return(entity); } IDbExpressionTranslator translator = this.DatabaseProvider.CreateDbExpressionTranslator(); DbCommandInfo dbCommandInfo = translator.Translate(e); dbCommandInfo.CommandText = string.Concat(dbCommandInfo.CommandText, ";", this.GetSelectLastInsertIdClause()); //SELECT @@IDENTITY 返回的是 decimal 类型 object retIdentity = await this.ExecuteScalar(dbCommandInfo, @async); if (retIdentity == null || retIdentity == DBNull.Value) { throw new ChloeException("Unable to get the identity value."); } retIdentity = PublicHelper.ConvertObjectType(retIdentity, autoIncrementPropertyDescriptor.PropertyType); autoIncrementPropertyDescriptor.SetValue(entity, retIdentity); return(entity); }
public abstract T Visit(DbParameterExpression exp);