diff --git a/AmalgamationTool/DynamORM.Amalgamation.cs b/AmalgamationTool/DynamORM.Amalgamation.cs index dce3fc5..af91981 100644 --- a/AmalgamationTool/DynamORM.Amalgamation.cs +++ b/AmalgamationTool/DynamORM.Amalgamation.cs @@ -1840,9 +1840,18 @@ namespace DynamORM /// Enumerable containing instances of objects to insert. /// Number of inserted rows. public virtual int Insert(IEnumerable e) where T : class + { + return Insert(typeof(T), e); + } + + /// Bulk insert objects into database. + /// Type of objects to insert. + /// Enumerable containing instances of objects to insert. + /// Number of inserted rows. + public virtual int Insert(Type t, IEnumerable e) { int affected = 0; - DynamicTypeMap mapper = DynamicMapperCache.GetMapper(typeof(T)); + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(t); if (mapper != null) { @@ -1871,9 +1880,9 @@ namespace DynamORM } } else - PrepareBatchInsert(mapper, cmd, parameters); + PrepareBatchInsert(t, mapper, cmd, parameters); - foreach (T o in e) + foreach (var o in e) { foreach (KeyValuePair m in parameters) m.Key.Value = m.Value.Get(o); @@ -1938,9 +1947,18 @@ namespace DynamORM /// Enumerable containing instances of objects to update. /// Number of updated rows. public virtual int Update(IEnumerable e) where T : class + { + return Update(typeof(T), e); + } + + /// Bulk update objects in database. + /// Type of objects to update. + /// Enumerable containing instances of objects to update. + /// Number of updated rows. + public virtual int Update(Type t, IEnumerable e) { int affected = 0; - DynamicTypeMap mapper = DynamicMapperCache.GetMapper(typeof(T)); + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(t); if (mapper != null) { @@ -1969,9 +1987,9 @@ namespace DynamORM } } else - PrepareBatchUpdate(mapper, cmd, parameters); + PrepareBatchUpdate(t, mapper, cmd, parameters); - foreach (T o in e) + foreach (var o in e) { foreach (KeyValuePair m in parameters) m.Key.Value = m.Value.Get(o); @@ -2004,9 +2022,18 @@ namespace DynamORM /// Enumerable containing instances of objects to update or insert. /// Number of updated or inserted rows. public virtual int UpdateOrInsert(IEnumerable e) where T : class + { + return UpdateOrInsert(typeof(T), e); + } + + /// Bulk update or insert objects into database. + /// Type of objects to update or insert. + /// Enumerable containing instances of objects to update or insert. + /// Number of updated or inserted rows. + public virtual int UpdateOrInsert(Type t, IEnumerable e) { int affected = 0; - DynamicTypeMap mapper = DynamicMapperCache.GetMapper(typeof(T)); + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(t); if (mapper != null) { @@ -2038,7 +2065,7 @@ namespace DynamORM } } else - PrepareBatchUpdate(mapper, cmdUp, parametersUp); + PrepareBatchUpdate(t, mapper, cmdUp, parametersUp); #endregion Update @@ -2063,11 +2090,11 @@ namespace DynamORM } } else - PrepareBatchInsert(mapper, cmdIn, parametersIn); + PrepareBatchInsert(t, mapper, cmdIn, parametersIn); #endregion Insert - foreach (T o in e) + foreach (var o in e) { foreach (KeyValuePair m in parametersUp) m.Key.Value = m.Value.Get(o); @@ -2141,9 +2168,18 @@ namespace DynamORM /// Enumerable containing instances of objects to delete. /// Number of deleted rows. public virtual int Delete(IEnumerable e) where T : class + { + return Delete(typeof(T), e); + } + + /// Bulk delete objects in database. + /// Type of objects to delete. + /// Enumerable containing instances of objects to delete. + /// Number of deleted rows. + public virtual int Delete(Type t, IEnumerable e) { int affected = 0; - DynamicTypeMap mapper = DynamicMapperCache.GetMapper(typeof(T)); + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(t); if (mapper != null) { @@ -2172,9 +2208,9 @@ namespace DynamORM } } else - PrepareBatchDelete(mapper, cmd, parameters); + PrepareBatchDelete(t, mapper, cmd, parameters); - foreach (T o in e) + foreach (var o in e) { foreach (KeyValuePair m in parameters) m.Key.Value = m.Value.Get(o); @@ -2203,13 +2239,18 @@ namespace DynamORM } private void PrepareBatchInsert(DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) + { + PrepareBatchInsert(typeof(T), mapper, cmd, parameters); + } + + private void PrepareBatchInsert(Type t, DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) { DynamicPropertyInvoker currentprop = null; Dictionary temp = new Dictionary(); - Dictionary schema = this.GetSchema(); + Dictionary schema = this.GetSchema(t); int ord = 0; - IDynamicInsertQueryBuilder ib = Insert() + IDynamicInsertQueryBuilder ib = Insert(t) .SetVirtualMode(true) .CreateTemporaryParameterAction(p => temp[p.Name] = currentprop) .CreateParameterAction((p, cp) => @@ -2250,13 +2291,18 @@ namespace DynamORM } private void PrepareBatchUpdate(DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) + { + PrepareBatchUpdate(typeof(T), mapper, cmd, parameters); + } + + private void PrepareBatchUpdate(Type t, DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) { DynamicPropertyInvoker currentprop = null; Dictionary temp = new Dictionary(); - Dictionary schema = this.GetSchema(); + Dictionary schema = this.GetSchema(t); int ord = 0; - IDynamicUpdateQueryBuilder ib = Update() + IDynamicUpdateQueryBuilder ib = Update(t) .SetVirtualMode(true) .CreateTemporaryParameterAction(p => temp[p.Name] = currentprop) .CreateParameterAction((p, cp) => @@ -2323,13 +2369,18 @@ namespace DynamORM } private void PrepareBatchDelete(DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) + { + PrepareBatchDelete(typeof(T), mapper, cmd, parameters); + } + + private void PrepareBatchDelete(Type t, DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) { DynamicPropertyInvoker currentprop = null; Dictionary temp = new Dictionary(); - Dictionary schema = this.GetSchema(); + Dictionary schema = this.GetSchema(t); int ord = 0; - IDynamicDeleteQueryBuilder ib = Delete() + IDynamicDeleteQueryBuilder ib = Delete(t) .SetVirtualMode(true) .CreateTemporaryParameterAction(p => temp[p.Name] = currentprop) .CreateParameterAction((p, cp) => @@ -4791,7 +4842,7 @@ namespace DynamORM { Type generic = type.IsGenericType ? type.GetGenericTypeDefinition() : null; - if (generic != null && generic.Equals(typeof(Nullable<>)) && type.IsClass) + if (generic != null && generic.Equals(typeof(Nullable<>)) && (type.IsClass || type.IsValueType || type.IsEnum)) return true; return false; @@ -13322,6 +13373,9 @@ namespace DynamORM Column = attr; + if (attr != null && attr.AllowNull && Type.IsNullableType()) + attr.AllowNull = false; + if (property.CanRead) Get = CreateGetter(property); diff --git a/DynamORM/DynamicDatabase.cs b/DynamORM/DynamicDatabase.cs index faf8ff8..7d5bade 100644 --- a/DynamORM/DynamicDatabase.cs +++ b/DynamORM/DynamicDatabase.cs @@ -27,6 +27,7 @@ */ using System; +using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.Common; @@ -379,9 +380,18 @@ namespace DynamORM /// Enumerable containing instances of objects to insert. /// Number of inserted rows. public virtual int Insert(IEnumerable e) where T : class + { + return Insert(typeof(T), e); + } + + /// Bulk insert objects into database. + /// Type of objects to insert. + /// Enumerable containing instances of objects to insert. + /// Number of inserted rows. + public virtual int Insert(Type t, IEnumerable e) { int affected = 0; - DynamicTypeMap mapper = DynamicMapperCache.GetMapper(typeof(T)); + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(t); if (mapper != null) { @@ -410,9 +420,9 @@ namespace DynamORM } } else - PrepareBatchInsert(mapper, cmd, parameters); + PrepareBatchInsert(t, mapper, cmd, parameters); - foreach (T o in e) + foreach (var o in e) { foreach (KeyValuePair m in parameters) m.Key.Value = m.Value.Get(o); @@ -477,9 +487,18 @@ namespace DynamORM /// Enumerable containing instances of objects to update. /// Number of updated rows. public virtual int Update(IEnumerable e) where T : class + { + return Update(typeof(T), e); + } + + /// Bulk update objects in database. + /// Type of objects to update. + /// Enumerable containing instances of objects to update. + /// Number of updated rows. + public virtual int Update(Type t, IEnumerable e) { int affected = 0; - DynamicTypeMap mapper = DynamicMapperCache.GetMapper(typeof(T)); + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(t); if (mapper != null) { @@ -508,9 +527,9 @@ namespace DynamORM } } else - PrepareBatchUpdate(mapper, cmd, parameters); + PrepareBatchUpdate(t, mapper, cmd, parameters); - foreach (T o in e) + foreach (var o in e) { foreach (KeyValuePair m in parameters) m.Key.Value = m.Value.Get(o); @@ -543,9 +562,18 @@ namespace DynamORM /// Enumerable containing instances of objects to update or insert. /// Number of updated or inserted rows. public virtual int UpdateOrInsert(IEnumerable e) where T : class + { + return UpdateOrInsert(typeof(T), e); + } + + /// Bulk update or insert objects into database. + /// Type of objects to update or insert. + /// Enumerable containing instances of objects to update or insert. + /// Number of updated or inserted rows. + public virtual int UpdateOrInsert(Type t, IEnumerable e) { int affected = 0; - DynamicTypeMap mapper = DynamicMapperCache.GetMapper(typeof(T)); + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(t); if (mapper != null) { @@ -577,7 +605,7 @@ namespace DynamORM } } else - PrepareBatchUpdate(mapper, cmdUp, parametersUp); + PrepareBatchUpdate(t, mapper, cmdUp, parametersUp); #endregion Update @@ -602,11 +630,11 @@ namespace DynamORM } } else - PrepareBatchInsert(mapper, cmdIn, parametersIn); + PrepareBatchInsert(t, mapper, cmdIn, parametersIn); #endregion Insert - foreach (T o in e) + foreach (var o in e) { foreach (KeyValuePair m in parametersUp) m.Key.Value = m.Value.Get(o); @@ -680,9 +708,18 @@ namespace DynamORM /// Enumerable containing instances of objects to delete. /// Number of deleted rows. public virtual int Delete(IEnumerable e) where T : class + { + return Delete(typeof(T), e); + } + + /// Bulk delete objects in database. + /// Type of objects to delete. + /// Enumerable containing instances of objects to delete. + /// Number of deleted rows. + public virtual int Delete(Type t, IEnumerable e) { int affected = 0; - DynamicTypeMap mapper = DynamicMapperCache.GetMapper(typeof(T)); + DynamicTypeMap mapper = DynamicMapperCache.GetMapper(t); if (mapper != null) { @@ -711,9 +748,9 @@ namespace DynamORM } } else - PrepareBatchDelete(mapper, cmd, parameters); + PrepareBatchDelete(t, mapper, cmd, parameters); - foreach (T o in e) + foreach (var o in e) { foreach (KeyValuePair m in parameters) m.Key.Value = m.Value.Get(o); @@ -742,13 +779,18 @@ namespace DynamORM } private void PrepareBatchInsert(DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) + { + PrepareBatchInsert(typeof(T), mapper, cmd, parameters); + } + + private void PrepareBatchInsert(Type t, DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) { DynamicPropertyInvoker currentprop = null; Dictionary temp = new Dictionary(); - Dictionary schema = this.GetSchema(); + Dictionary schema = this.GetSchema(t); int ord = 0; - IDynamicInsertQueryBuilder ib = Insert() + IDynamicInsertQueryBuilder ib = Insert(t) .SetVirtualMode(true) .CreateTemporaryParameterAction(p => temp[p.Name] = currentprop) .CreateParameterAction((p, cp) => @@ -789,13 +831,18 @@ namespace DynamORM } private void PrepareBatchUpdate(DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) + { + PrepareBatchUpdate(typeof(T), mapper, cmd, parameters); + } + + private void PrepareBatchUpdate(Type t, DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) { DynamicPropertyInvoker currentprop = null; Dictionary temp = new Dictionary(); - Dictionary schema = this.GetSchema(); + Dictionary schema = this.GetSchema(t); int ord = 0; - IDynamicUpdateQueryBuilder ib = Update() + IDynamicUpdateQueryBuilder ib = Update(t) .SetVirtualMode(true) .CreateTemporaryParameterAction(p => temp[p.Name] = currentprop) .CreateParameterAction((p, cp) => @@ -862,13 +909,18 @@ namespace DynamORM } private void PrepareBatchDelete(DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) + { + PrepareBatchDelete(typeof(T), mapper, cmd, parameters); + } + + private void PrepareBatchDelete(Type t, DynamicTypeMap mapper, IDbCommand cmd, Dictionary parameters) { DynamicPropertyInvoker currentprop = null; Dictionary temp = new Dictionary(); - Dictionary schema = this.GetSchema(); + Dictionary schema = this.GetSchema(t); int ord = 0; - IDynamicDeleteQueryBuilder ib = Delete() + IDynamicDeleteQueryBuilder ib = Delete(t) .SetVirtualMode(true) .CreateTemporaryParameterAction(p => temp[p.Name] = currentprop) .CreateParameterAction((p, cp) => diff --git a/DynamORM/DynamicExtensions.cs b/DynamORM/DynamicExtensions.cs index 38f4637..edd9bb5 100644 --- a/DynamORM/DynamicExtensions.cs +++ b/DynamORM/DynamicExtensions.cs @@ -1322,7 +1322,7 @@ namespace DynamORM { Type generic = type.IsGenericType ? type.GetGenericTypeDefinition() : null; - if (generic != null && generic.Equals(typeof(Nullable<>)) && type.IsClass) + if (generic != null && generic.Equals(typeof(Nullable<>)) && (type.IsClass || type.IsValueType || type.IsEnum)) return true; return false; diff --git a/DynamORM/Mapper/DynamicPropertyInvoker.cs b/DynamORM/Mapper/DynamicPropertyInvoker.cs index 6e84334..e33b749 100644 --- a/DynamORM/Mapper/DynamicPropertyInvoker.cs +++ b/DynamORM/Mapper/DynamicPropertyInvoker.cs @@ -111,6 +111,9 @@ namespace DynamORM.Mapper Column = attr; + if (attr != null && attr.AllowNull && Type.IsNullableType()) + attr.AllowNull = false; + if (property.CanRead) Get = CreateGetter(property);