public static DBObjectMapping <T> Get() { if (_Mapping == null) { _Mapping = new DBObjectMapping <T>(); } return(_Mapping); }
private DBTableMapping(IResolverProvider resolverProvider, DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans) { var mapping = DBObjectMapping <T> .Get(); Sql sql = resolverProvider.SqlResolver.ColumnMetaDataFromTable(TableName, Schema); List <SchemaMetadata> metadatas = currentConnection.Query <SchemaMetadata>(sql, currentTrans); ColumnMappingList = mapping.ColumnMappingList .Select(i => new DBColumnMappingInfo <T>(i, metadatas.Single(j => string.Compare(j.COLUMN_NAME, i.ColumnName, true) == 0))); }
public static List <T> ToList <T>(this IDataReader reader) where T : new() { List <T> res = new List <T>(); var mapping = DBObjectMapping <T> .Get().ColumnMappingList; int[] ordinalAry = new int[mapping.Count]; var setterAry = new Action <T, IDataRecord, int> [mapping.Count]; T obj; if (reader.Read()) { obj = new T(); string cName = null; try { for (int i = 0; i < mapping.Count; i++) { cName = mapping[i].ColumnName; ordinalAry[i] = reader.GetOrdinal(cName); setterAry[i] = mapping[i].GetPropSetterForRecord(reader.GetFieldType(ordinalAry[i])); reader.SetProperty(obj, mapping[i], setterAry[i], ordinalAry[i]); } res.Add(obj); } catch (IndexOutOfRangeException ex) { throw new IndexOutOfRangeException("Unable to find specified column[" + cName + "] in DataReader", ex); } } while (reader.Read()) { obj = new T(); for (int i = 0; i < mapping.Count; i++) { reader.SetProperty(obj, mapping[i], setterAry[i], ordinalAry[i]); } res.Add(obj); } return(res); }
public static List <T> GetFirstColumn <T>(this DBConnectionWrapper conn, Sql sql, DBTransactionWrapper trans = null) { IDbCommand command = conn.CreateCommand(sql, trans); var mapping = DBObjectMapping <SingleColumn <T> > .Get().ColumnMappingList.Single(); List <T> ret = new List <T>(); try { using (IDataReader reader = command.ExecuteReader()) { SingleColumn <T> obj = new SingleColumn <T>(); Action <SingleColumn <T>, IDataRecord, int> setter = null; if (reader.Read()) { setter = mapping.GetPropSetterForRecord(reader.GetFieldType(0)); reader.SetProperty(obj, mapping, setter, 0); ret.Add(obj.Val); } while (reader.Read()) { reader.SetProperty(obj, mapping, setter, 0); ret.Add(obj.Val); } } } catch (DbException ex) { //throw; throw new SqlCmdException("Error occurred when running SQL!", sql, ex); } return(ret); }