public static T Upsert <T>(this IDbConnection conn, T t) where T : class { var map = MappingRepo <T> .GetMap(); var property = map.Identifiers[0]; if (map.Identifiers.Count != 1 || property.Insert) { var command = CrudOperator <T> .Upsert; if (command == null) { throw new UpsertNotSupportedOnAutoGeneratedIdTypesException(typeof(T)); } AttachParamsAndRun(conn, t, command); return(t); } var id = property.ValueAccessor(t); if (property.Type.GetTypeInfo().IsValueType&& Activator.CreateInstance(property.Type).Equals(id)) // default value { conn.Insert(t); return(t); } conn.Update(t); return(t); }
public static T Insert <T>(this IDbConnection connection, T t) where T : class { var commandGenerator = CrudOperator <T> .Insert; var parameters = commandGenerator .GetColumns() .Select(x => x.ValueAccessor(t)) .ToArray(); using (var command = connection.CreateCommand()) { command.CommandText = commandGenerator.GetSql(); command.AttachPositionalParameters(parameters); var map = MappingRepo <T> .GetMap(); if (map.HasAutoGeneratedId()) { var id = command.ExecuteScalar(); map.Identifiers[0].SetValue(t, id); return(t); } command.ExecuteNonQuery(); } return(t); }
public void Sanity() { var customTypeObject = new CustomTypeObject(); var property = MappingRepo <CustomTypeObject> .GetMap().Properties[0]; property.SetValue(customTypeObject, "5,3"); CollectionAssert.AreEqual(customTypeObject.SomeIntArray, new[] { 5, 3 }); Assert.AreEqual(property.ValueAccessor(customTypeObject), "5,3"); }
public static void Main(string[] args) { var r = new[] { 1, 2 }.Skip(-5).ToArray(); Console.WriteLine(r.Length); var map = MappingRepo <LocationDto> .GetMap(); MeasureTime(TextBuildQuery); }
public string GetSql(Action <IRestrictable <T> > restriction, List <object> parameters) { var map = MappingRepo <T> .GetMap(); var restrictable = new Restrictable <T>(); restriction(restrictable); var where = restrictable.BuildRestrictionsIncludeWhere(parameters, map.Dialect); return("DELETE FROM " + map.TableName + where + ";"); }
public IdRestriction(object id) { var identifiers = MappingRepo <T> .GetMap().Identifiers; if (identifiers.Count > 1) { throw new Exception("Id restriction can be used only when the object Id is composed of a single field"); } _property = identifiers[0]; _id = id; }
public void Sanity2() { MappingRepoDictionary.InitializeAssemblies(GetType().Assembly); MappingRepoDictionary.InitializeAssemblies(GetType().Assembly); var customTypeObject = new CustomTypeObject(); var property = MappingRepo <CustomTypeObject> .GetMap().Properties[0]; property.SetValue(customTypeObject, "5,3"); CollectionAssert.AreEqual(customTypeObject.SomeIntArray, new[] { 5, 3 }); Assert.AreEqual(property.ValueAccessor(customTypeObject), "5,3"); }
public void Equal() { var map = MappingRepo <SimpleObject> .GetMap(); var property = ExpressionProcessor <SimpleObject> .GetPropertyFromCache(x => x.Long); var res = new OperatorRestriction <long>(property, 5L, "="); var parameters = new List <object>(); var sql = res.Apply(parameters, map.Dialect); Assert.AreEqual("`LongColumnName` = ?p0", sql); Assert.AreEqual(5, parameters[0]); }
public void Regex() { var map = MappingRepo <SimpleObject> .GetMap(); var property = ExpressionProcessor <SimpleObject> .GetPropertyFromCache(x => x.Long); var res = new RegexRestriction <string>(property, "[a-zA-Z]+"); var parameters = new List <object>(); var sql = res.Apply(parameters, map.Dialect); Assert.AreEqual("`LongColumnName` REGEXP ?p0", sql); Assert.AreEqual("[a-zA-Z]+", parameters[0]); }
public void In() { var map = MappingRepo <SimpleObject> .GetMap(); var property = ExpressionProcessor <SimpleObject> .GetPropertyFromCache(x => x.Long); var values = new[] { 1L, 2, 3 }; var res = new InRestriction <long>(property, values); var parameters = new List <object>(); var sql = res.Apply(parameters, map.Dialect); Assert.AreEqual("`LongColumnName` IN (?p0,?p1,?p2)", sql); CollectionAssert.AreEquivalent(values, parameters); }
static CrudOperator() { var map = MappingRepo <T> .GetMap(); GetById = new CachedSqlCommandGenerator(new GetByIdGenerator <T>()); Update = new CachedSqlCommandGenerator(new UpdateGenerator <T>()); Insert = new CachedSqlCommandGenerator(new InsertGenerator <T>()); Delete = new CachedSqlCommandGenerator(new DeleteGenerator <T>()); if (!map.HasAutoGeneratedId()) { Upsert = new CachedSqlCommandGenerator(new UpsertGenerator <T>()); } }
public string GetSql(Action <IRestrictable <T> > restriction, List <object> parameters) { var map = MappingRepo <T> .GetMap(); var baseSql = "SELECT COUNT(*) FROM " + map.TableName; if (restriction == null) { return(baseSql + ";"); } var restrictable = new Restrictable <T>(); restriction(restrictable); var where = restrictable.BuildRestrictionsIncludeWhere(parameters, map.Dialect); return(baseSql + where + ";"); }
public UpsertGenerator() { _map = MappingRepo <T> .GetMap(); }
public DeleteGenerator() { _map = MappingRepo <T> .GetMap(); }
public GetByIdGenerator() { _map = MappingRepo <T> .GetMap(); }
public BulkUpsert() { _map = MappingRepo <T> .GetMap(); }
public AtomicIncrementGenerator() { _map = MappingRepo <T> .GetMap(); }
private static IList <Property> GetProperties(IEnumerable <Expression <Func <T, object> > > properties) { MappingRepo <T> .GetMap(); return(properties.Select(ExpressionProcessor <T> .GetPropertyFromCache).ToArray()); }
public UpdateByQuery() { _map = MappingRepo <T> .GetMap(); }
public UpdateGenerator() { _map = MappingRepo <T> .GetMap(); _properties = _map.Properties; }
public QueryBuilder() { _map = MappingRepo <T> .GetMap(); }
public UpdateGenerator(IList <Property> properties) { _map = MappingRepo <T> .GetMap(); _properties = properties; }