public virtual void Insert <T>(T entity, string destinationTable = null) where T : IEntity { var info = MetaDataStore.GetTableInfoFor <T>(); var columns = info.Columns.ToList().Where(c => c.DatabaseGenerated == false); var ht = GetParametersAsHashtableFor <T>(entity, columns); // Execute var sql = string.Format( "INSERT INTO {0} ({1}) SELECT {2}", destinationTable ?? info.TableName[CrudOperation.CREATE], string.Join(",", columns.Select(c => c.Name)), string.Join(",", columns.Select(c => string.Format("@{0}", c.Name))) ); Execute(sql, ht); }
/// <summary> /// Selects all the entities in a set. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public virtual IEnumerable <T> Select <T>(T where = null, bool buffered = true) where T : class, IEntity { var info = MetaDataStore.GetTableInfoFor <T>(); Dictionary <string, object> whereItems = new Dictionary <string, object>(); if (where != null) { var columns = MetaDataStore.GetTableInfoFor <T>().Columns; foreach (var column in columns) { var value = column.PropertyInfo.GetValue(where); var dflt = column.PropertyInfo.PropertyType.Default(); if (value != null && !value.Equals(dflt)) { whereItems.Add(column.Name, column.PropertyInfo.GetValue(where)); } } } if (whereItems.Keys.Count == 0) { var sql = string.Format( "SELECT * FROM {0}", info.TableName[CrudOperation.READ] ); return(QueryEntity <T>(sql, null, CommandType.Text, buffered)); } else { var sql = string.Format( "SELECT * FROM {0} WHERE {1}", info.TableName[CrudOperation.READ], string.Join(" AND ", whereItems.Keys.Select(v => string.Format("{0} = @{1}", v, v))) ); return(QueryEntity <T>(sql, whereItems, CommandType.Text, buffered)); } }
public static void BuildMetaDataFor(Assembly assembly) { MetaDataStore.BuildMetaDataFor(assembly); }
// static constructor (get entity information) static Session() { MetaDataStore.BuildMetaDataFor(typeof(Session).Assembly); }