public Func <IDataReader, RequestContext, object> GetParser(IDataReaderWrapper dataReader, RequestContext requestContext, Type targetType) { string key = $"{requestContext.StatementKey}_{GetColumnKey(dataReader)}_{targetType.FullName}"; if (!_cachedDeserializer.ContainsKey(key)) { lock (this) { if (!_cachedDeserializer.ContainsKey(key)) { Func <IDataReader, RequestContext, object> deser = null; if (targetType.IsValueType || targetType == TypeUtils.StringType) { deser = CreateValueTypeParserImpl(dataReader, requestContext, targetType); } else { deser = CreateParserImpl(dataReader, requestContext, targetType); } _cachedDeserializer.Add(key, deser); } } } return(_cachedDeserializer[key]); }
private bool Query(string sql, Dictionary <string, object> parms, out IDataWrapper reader) { try { DB2Command cmd = new DB2Command(sql, conn, trans) { CommandTimeout = (int)Timeout, CommandType = CommandType.Text }; if (parms != null) { foreach (string key in parms.Keys) { cmd.Parameters.Add(key, parms[key]); } } reader = new IDataReaderWrapper(cmd.ExecuteReader(CommandBehavior.SingleResult)); return(true); } catch (Exception ex) { LastError = ex.Message; Logger.WriteLogExcept(LogTitle, ex); Logger.WriteLog(LogTitle, sql); reader = null; return(false); } }
public RowMapperResultSetExtractor(IRowMapper rowMapper, int rowsExpected, IDataReaderWrapper dataReaderWrapper) { //TODO use datareaderwrapper if (rowMapper == null) { throw new ArgumentNullException("rowMapper"); } this.rowMapper = rowMapper; this.rowsExpected = rowsExpected; }
public static async Task <DataSet> ToDataSetAsync(IDataReaderWrapper dataReader) { DataSet dataSet = new DataSet(); do { DataTable dataTable = await ToDataTableAsync(dataReader); dataSet.Tables.Add(dataTable); }while (await dataReader.NextResultAsync()); return(dataSet); }
public async static Task <DbSet> ToDbSetAsync(IDataReaderWrapper dataReader) { DbSet dbSet = new DbSet(); do { var dbTable = await ToDbTableAsync(dataReader); dbSet.Tables.Add(dbTable); }while (await dataReader.NextResultAsync()); return(dbSet); }
public object ToSingle(RequestContext context, IDataReaderWrapper dataReader, Type targetType, bool isDispose = true) { try { dataReader.Read(); var rowParser = dataReader.GetRowParser(targetType); return(rowParser(dataReader)); } finally { Dispose(dataReader, isDispose); } }
public async Task <object> ToSingleAsync(RequestContext context, IDataReaderWrapper dataReader, Type targetType = null, bool isDispose = true) { try { await dataReader.ReadAsync(); var deser = _dataRowParserFactory.GetParser(dataReader, context, targetType); return(deser(dataReader, context)); } finally { Dispose(dataReader, isDispose); } }
public async Task <object> ToSingleAsync(RequestContext context, IDataReaderWrapper dataReader, Type targetType, bool isDispose = true) { try { await dataReader.ReadAsync(); var rowParser = dataReader.GetRowParser(targetType); return(rowParser(dataReader)); } finally { Dispose(dataReader, isDispose); } }
public async static Task <DbTable> ToDbTableAsync(IDataReaderWrapper dataReader) { DbTable dbTable = new DbTable(); InitDbTableColumns(dataReader, dbTable); while (await dataReader.ReadAsync()) { var dbRow = dbTable.AddRow(); for (int i = 0; i < dataReader.FieldCount; i++) { var colName = dataReader.GetName(i); dbRow[colName] = dataReader[colName]; } } return(dbTable); }
public async Task <IMultipleResult> FillMultipleAsync(RequestContext context, IMultipleResult multipleResult) { return(await ExecuteWrapAsync(async (dbSession) => { IDataReaderWrapper dataReader = null; try { dataReader = CommandExecuter.ExecuteReader(dbSession, context); return await multipleResult.InitDataAsync(context, dataReader, _dataReaderDeserializer); } finally { DisposeReader(dataReader); } }, context, DataSourceChoice.Read)); }
public IMultipleResult FillMultiple(RequestContext context, IMultipleResult multipleResult) { return(ExecuteWrap((dbSession) => { IDataReaderWrapper dataReader = null; try { dataReader = CommandExecuter.ExecuteReader(dbSession, context); return multipleResult.InitData(context, dataReader, _dataReaderDeserializer); } finally { DisposeReader(dataReader); } }, context, DataSourceChoice.Read)); }
public object ToSingle(RequestContext context, IDataReaderWrapper dataReader, Type targetType, bool isDispose = true) { try { if (dataReader.Read()) { var deser = _dataRowParserFactory.GetParser(dataReader, context, targetType); return(deser(dataReader, context)); } return(null); } finally { Dispose(dataReader, isDispose); } }
public static async Task <DataTable> ToDataTableAsync(IDataReaderWrapper dataReader) { DataTable dataTable = new DataTable(); InitDataTableColumns(dataReader, dataTable); while (await dataReader.ReadAsync()) { DataRow dataRow = dataTable.NewRow(); for (int i = 0; i < dataReader.FieldCount; i++) { dataRow[i] = dataReader[i]; } dataTable.Rows.Add(dataRow); } return(dataTable); }
public Task <T> GetNestedAsync <T>(RequestContext context) { return(ExecuteWrapAsync <T>(async(dbSession) => { IDataReaderWrapper dataReader = null; try { dataReader = await CommandExecuter.ExecuteReaderAsync(dbSession, context); return _nestedObjectConverter.ToNested <T>(context, dataReader, _dataReaderDeserializer); } finally { DisposeReader(dataReader); } }, context, DataSourceChoice.Read)); }
public async Task <DbSet> GetDbSetAsync(RequestContext context) { return(await ExecuteWrapAsync(async (dbSession) => { IDataReaderWrapper dataReader = null; try { dataReader = await CommandExecuter.ExecuteReaderAsync(dbSession, context); return await DataReaderConvert.ToDbSetAsync(dataReader); } finally { DisposeReader(dataReader); } }, context, DataSourceChoice.Read)); }
public async Task <T> ToSingleAsync <T>(RequestContext context, IDataReaderWrapper dataReader, bool isDispose = true) { try { if (await dataReader.ReadAsync()) { var targetType = typeof(T); var rowParser = dataReader.GetRowParser(targetType); return((T)rowParser(dataReader)); } return(default(T)); } finally { Dispose(dataReader, isDispose); } }
public async Task <T> ToSingleAsync <T>(RequestContext context, IDataReaderWrapper dataReader, bool isDispose = true) { try { var targetType = typeof(T); if (await dataReader.ReadAsync()) { var deser = _dataRowParserFactory.GetParser(dataReader, context, targetType); object target = deser(dataReader, context); return((T)target); } return(default(T)); } finally { Dispose(dataReader, isDispose); } }
public IEnumerable <object> ToEnumerable(RequestContext context, IDataReaderWrapper dataReader, Type targetType, bool isDispose = true) { try { IList <object> list = new List <object>(); if (dataReader.Read()) { var rowParser = dataReader.GetRowParser(targetType); do { var target = rowParser(dataReader); list.Add(target); } while (dataReader.Read()); } return(list); } finally { Dispose(dataReader, isDispose); } }
public IEnumerable <T> ToEnumerable <T>(RequestContext context, IDataReaderWrapper dataReader, bool isDispose = true) { try { IList <T> list = new List <T>(); var targetType = typeof(T); if (dataReader.Read()) { var deser = _dataRowParserFactory.GetParser(dataReader, context, targetType); do { T target = (T)deser(dataReader, context); list.Add(target); } while (dataReader.Read()); } return(list); } finally { Dispose(dataReader, isDispose); } }
public IMultipleResult InitData(RequestContext requestContext, IDataReaderWrapper dataReaderWrapper, IDataReaderDeserializer dataReaderDeserializer) { foreach (var resultMapKV in _resultDataMap) { var resultMap = resultMapKV.Value; switch (resultMap.Type) { case ResultMap.ResultTypeType.Single: { resultMap.Result = dataReaderDeserializer.ToSingle(requestContext, dataReaderWrapper, resultMap.ResultType, false); break; } case ResultMap.ResultTypeType.Enumerable: { resultMap.Result = dataReaderDeserializer.ToEnumerable(requestContext, dataReaderWrapper, resultMap.ResultType, false); break; } } dataReaderWrapper.NextResult(); } return(this); }
public T ToNested <T>(RequestContext context, IDataReaderWrapper dataReaderWrapper, IDataReaderDeserializer dataReaderDeserializer) { var convert = _nestedObjectConvertFactory.CreateNestedObjectConvert(context, typeof(T)); return((T)convert(context, dataReaderWrapper, dataReaderDeserializer)); }
private Func<IDataReader, RequestContext, object> CreateParserImpl(IDataReaderWrapper dataReader, RequestContext context, Type targetType) { var statement = context.Statement; var resultMap = statement?.ResultMap; resultMap = statement?.MultipleResultMap?.Results.FirstOrDefault(m => m.Index == dataReader.ResultIndex)?.Map ?? resultMap; var constructorMap = resultMap?.Constructor; var columns = Enumerable.Range(0, dataReader.FieldCount) .Select(i => new { Index = i, Name = dataReader.GetName(i) }) .ToDictionary((col) => col.Name); var dynamicMethod = new DynamicMethod("Deserialize" + Guid.NewGuid().ToString("N"), targetType, new[] { TypeUtils.DataReaderType, TypeUtils.RequestContextType }, targetType, true); var iLGenerator = dynamicMethod.GetILGenerator(); iLGenerator.DeclareLocal(targetType); ConstructorInfo targetCtor = null; if (constructorMap == null) { targetCtor = targetType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); } else { var ctorArgTypes = constructorMap.Args.Select(arg => arg.ArgType).ToArray(); targetCtor = targetType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, ctorArgTypes, null); //load arg value foreach (var arg in constructorMap.Args) { var col = columns[arg.Column]; EmitLoadColVal(iLGenerator, dataReader, col.Index, arg.TypeHandler, arg.ArgType, null); } } if (targetCtor == null) throw new SmartSqlException($"No parameterless constructor defined for the target type: \"{targetType.FullName}\""); iLGenerator.Emit(OpCodes.Newobj, targetCtor); // [target] iLGenerator.Emit(OpCodes.Stloc_0); foreach (var col in columns) { var colName = col.Key; var colIndex = col.Value.Index; var result = resultMap?.Properties?.FirstOrDefault(r => r.Column == colName); string propertyName = result != null ? result.Name : colName; var property = targetType.GetProperty(propertyName); if (property == null) { continue; } if (!property.CanWrite) { continue; } Type propertyType = property.PropertyType; Label isDbNullLabel = iLGenerator.DefineLabel(); EmitLoadColVal(iLGenerator, dataReader, colIndex, result?.TypeHandler, propertyType, isDbNullLabel); iLGenerator.Emit(OpCodes.Call, property.SetMethod);// stack is empty iLGenerator.MarkLabel(isDbNullLabel); } iLGenerator.Emit(OpCodes.Ldloc_0);// stack is [rval] iLGenerator.Emit(OpCodes.Ret); var funcType = System.Linq.Expressions.Expression.GetFuncType(TypeUtils.DataReaderType, TypeUtils.RequestContextType, targetType); return (Func<IDataReader, RequestContext, object>)dynamicMethod.CreateDelegate(funcType); }
public RowMapperResultSetExtractor(IRowMapper <T> rowMapper, int rowsExpected, IDataReaderWrapper dataReaderWrapper) { //TODO use datareaderwrapper AssertUtils.ArgumentNotNull(rowMapper, "rowMapper"); this.rowMapper = rowMapper; this.rowsExpected = rowsExpected; }
public RowMapperResultSetExtractor(RowMapperDelegate <T> rowMapperDelegate, int rowsExpected, IDataReaderWrapper dataReaderWrapper) { //TODO use datareaderwrapper AssertUtils.ArgumentNotNull(rowMapperDelegate, "rowMapperDelegate"); this.rowMapperDelegate = rowMapperDelegate; this.rowsExpected = rowsExpected; }
public async Task <IEnumerable <T> > ToEnumerableAsync <T>(RequestContext context, IDataReaderWrapper dataReader, bool isDispose = true) { try { IList <T> list = new List <T>(); if (await dataReader.ReadAsync()) { var targetType = typeof(T); var rowParser = dataReader.GetRowParser(targetType); do { var item = (T)rowParser(dataReader); list.Add(item); } while (await dataReader.ReadAsync()); } return(list); } finally { Dispose(dataReader, isDispose); } }
public async Task <IEnumerable <object> > ToEnumerableAsync(RequestContext context, IDataReaderWrapper dataReader, Type targetType, bool isDispose = true) { try { IList <object> list = new List <object>(); if (await dataReader.ReadAsync()) { var deser = _dataRowParserFactory.GetParser(dataReader, context, targetType); do { var target = deser(dataReader, context); list.Add(target); } while (await dataReader.ReadAsync()); } return(list); } finally { Dispose(dataReader, isDispose); } }
public RowMapperResultSetExtractor(RowMapperDelegate rowMapperDelegate, int rowsExpected, IDataReaderWrapper dataReaderWrapper) { //TODO use datareaderwrapper this.rowMapperDelegate = rowMapperDelegate ?? throw new ArgumentNullException(nameof(rowMapperDelegate)); this.rowsExpected = rowsExpected; }