public static string BuildInsertQuery(this OrmMap map, string argValues) { string insertTemplate = "INSERT INTO {0}({1}) VALUES(" + argValues + ")"; string argCols = MapArgColumnList(map); return(String.Format(insertTemplate, map.TableName, argCols)); }
public static string BuildSelectWhereQuery(this OrmMap map, string whereSection) { string selectTemplate = "SELECT {0} FROM {1} WHERE " + whereSection; string argCols = MapArgColumnList(map); return(String.Format(selectTemplate, argCols, map.TableName)); }
public static string BuildSelectAllQuery(this OrmMap map) { string selectTemplate = "SELECT {0} FROM {1}"; string argCols = MapArgColumnList(map); return(String.Format(selectTemplate, argCols, map.TableName)); }
public static OrmMap FromType(Type type) { TableAttribute attr = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault(); if (attr == null) { throw new Exception("this type have no TableAttribute"); } OrmMap map = new OrmMap(); map.TableName = attr.TableName; foreach (PropertyInfo p in type.GetProperties()) { ColumnAttribute ca = (ColumnAttribute)p.GetCustomAttributes(typeof(ColumnAttribute), true).SingleOrDefault(); if (ca != null) { map[ca.ColumnName] = p; if (ca is IdAttribute) { map.ID = ca.ColumnName; } } } return(map); }
public static string MapArgColumnList(this OrmMap map) { StringBuilder b = new StringBuilder(); foreach (string col in map.Columns) { b.Append(col); b.Append(','); } b.Remove(b.Length - 1, 1); return(b.ToString()); }
public void RegisterType(Type type) { OrmMap map = null; if (!mappingPool.ContainsKey(type)) { map = OrmMap.FromType(type); mappingPool[type] = map; } else { map = mappingPool[type]; } }
public ICollection <T> SelectAll <T>() where T : class, new() { OrmMap map = mappingPool[typeof(T)]; string selectQuery = map.BuildSelectAllQuery(); using (DbConnection connection = GetOpenConnection()) { DbCommand command = connection.CreateCommand(); command.CommandText = map.BuildSelectAllQuery(); DbDataReader reader = command.ExecuteReader(); DbReaderAdapter adapter = new DbReaderAdapter(reader, map); return(adapter.GetMultipleResult <T>()); } }
public int Delete <T>(object id) where T : class, new() { OrmMap map = mappingPool[typeof(T)]; string whereStatement = String.Format("{0}=@id", map.ID); string deleteQuery = map.BuildDeleteQuery(whereStatement); using (DbConnection connection = GetOpenConnection()) { DbCommand command = connection.CreateCommand(); command.CommandText = deleteQuery; DbParameter param = command.CreateParameter(); param.DbType = map.GetDbType(map.ID); param.ParameterName = "@id"; param.Value = id; command.Parameters.Add(param); return(command.ExecuteNonQuery()); } }
public int Insert(object o) { Type type = o.GetType(); OrmMap map = mappingPool[type]; StringBuilder argListBuilder = new StringBuilder(); List <KeyValuePair <string, string> > colNameToNamedParam = new List <KeyValuePair <string, string> >(); foreach (string col in map.Columns) { string namedParam = '@' + col; colNameToNamedParam.Add(new KeyValuePair <string, string>(col, namedParam)); argListBuilder.Append(namedParam); argListBuilder.Append(','); } argListBuilder.Remove(argListBuilder.Length - 1, 1); string insertQuery = map.BuildInsertQuery(argListBuilder.ToString()); using (DbConnection connection = GetOpenConnection()) { DbCommand command = connection.CreateCommand(); command.CommandText = insertQuery; foreach (var pair in colNameToNamedParam) { DbParameter param = command.CreateParameter(); param.DbType = map.GetDbType(pair.Key); param.ParameterName = pair.Value; param.Value = map[pair.Key].GetValue(o) ?? DBNull.Value; command.Parameters.Add(param); } try { return(command.ExecuteNonQuery()); } catch (Exception e) { return(0); } } }
public T SelectById <T>(object id) where T : class, new() { OrmMap map = mappingPool[typeof(T)]; string whereStatement = String.Format("{0}=@id", map.ID); string selectQuery = map.BuildSelectWhereQuery(whereStatement); using (DbConnection connection = GetOpenConnection()) { DbCommand command = connection.CreateCommand(); command.CommandText = selectQuery; DbParameter param = command.CreateParameter(); param.DbType = map.GetDbType(map.ID); param.ParameterName = "@id"; param.Value = id; command.Parameters.Add(param); DbDataReader reader = command.ExecuteReader(); DbReaderAdapter adapter = new DbReaderAdapter(reader, map); return(adapter.GetSingleResult <T>()); } }
public static OrmMap FromType(Type type) { TableAttribute attr = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault(); if (attr == null) { throw new Exception("this type have no TableAttribute"); } OrmMap map = new OrmMap(); map.TableName = attr.TableName; foreach (PropertyInfo p in type.GetProperties()) { ColumnAttribute ca = (ColumnAttribute)p.GetCustomAttributes(typeof(ColumnAttribute), true).SingleOrDefault(); if (ca != null) { map[ca.ColumnName] = p; if (ca is IdAttribute) { map.ID = ca.ColumnName; } } } return map; }
public static string BuildDeleteQuery(this OrmMap map, string whereSection) { string deleteTemplate = "DELETE FROM {0} WHERE " + whereSection; return(String.Format(deleteTemplate, map.TableName)); }
public DbReaderAdapter(DbDataReader reader, OrmMap map) { this.reader = reader; this.map = map; }