コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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");
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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 + ";");
        }
コード例 #6
0
        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;
        }
コード例 #7
0
        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");
        }
コード例 #8
0
        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]);
        }
コード例 #9
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]);
        }
コード例 #10
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);
        }
コード例 #11
0
        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>());
            }
        }
コード例 #12
0
        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 + ";");
        }
コード例 #13
0
 public UpsertGenerator()
 {
     _map = MappingRepo <T> .GetMap();
 }
コード例 #14
0
 public DeleteGenerator()
 {
     _map = MappingRepo <T> .GetMap();
 }
コード例 #15
0
 public GetByIdGenerator()
 {
     _map = MappingRepo <T> .GetMap();
 }
コード例 #16
0
 public BulkUpsert()
 {
     _map = MappingRepo <T> .GetMap();
 }
コード例 #17
0
 public AtomicIncrementGenerator()
 {
     _map = MappingRepo <T> .GetMap();
 }
コード例 #18
0
        private static IList <Property> GetProperties(IEnumerable <Expression <Func <T, object> > > properties)
        {
            MappingRepo <T> .GetMap();

            return(properties.Select(ExpressionProcessor <T> .GetPropertyFromCache).ToArray());
        }
コード例 #19
0
 public UpdateByQuery()
 {
     _map = MappingRepo <T> .GetMap();
 }
コード例 #20
0
        public UpdateGenerator()
        {
            _map = MappingRepo <T> .GetMap();

            _properties = _map.Properties;
        }
コード例 #21
0
 public QueryBuilder()
 {
     _map = MappingRepo <T> .GetMap();
 }
コード例 #22
0
        public UpdateGenerator(IList <Property> properties)
        {
            _map = MappingRepo <T> .GetMap();

            _properties = properties;
        }