예제 #1
0
        protected override Expression VisitConstant(ConstantExpression c)
        {
            if (!c.Value.GetType().IsPrimitive&& !(c.Value is string))
            {
                var tableType = c.Value.GetType().GetGenericArguments().FirstOrDefault();

                var isVariable = c.Type.GetCustomAttribute <CompilerGeneratedAttribute>() != null;

                if (isVariable)
                {
                    return(base.VisitConstant(c));
                }

                SqlStatementBuilder.TableType = c.Value.GetType();
                SqlStatementBuilder.TableName = OrmUtilities.GetTableName(tableType);
                SqlStatementBuilder.Columns   = OrmUtilities.GetColumns(tableType);
            }
            else
            {
                switch (c.Value)
                {
                case string s:
                    SqlStatementBuilder.AddStringToCondition(s);
                    break;

                case int i:
                    SqlStatementBuilder.AddIntToCondition(i);
                    break;
                }
            }
            return(base.VisitConstant(c));
        }
예제 #2
0
        public void GetColumns_Type_ColumnsNamedCorrectly()
        {
            var columns = OrmUtilities.GetColumns(typeof(PersonWithTableAndColumnAttribute));

            columns.ContainsKey("Id").ShouldBe(true);
            columns.ContainsKey("nomen").ShouldBe(true);
        }
예제 #3
0
        public void GetPrimaryKeyAttribute_PrimaryKey_ReturnsCorrectProperty()
        {
            var property = OrmUtilities.GetPrimaryKeyProperty(typeof(PersonWithPrimaryKey));

            property.ShouldNotBeNull();
            property.Name.ShouldBe("MyPrimaryKey");
            property.PropertyType.ShouldBe(typeof(int));
        }
예제 #4
0
        /// <inheritdoc />
        public ChangeTrackerEntry GetEntry(int id, Type type)
        {
            var idProperty = OrmUtilities.GetPrimaryKeyProperty(type);

            return(_entries.Values.
                   FirstOrDefault(entry => entry.Item.GetType() == type &&
                                  idProperty.GetValue(entry.Item).Equals(id)));
        }
예제 #5
0
        public void GetColumns_Type_ValuesAndTypeCorrect()
        {
            var columns = OrmUtilities.GetColumns(typeof(PersonWithTableAndColumnAttribute));

            columns["Id"].Item1.ShouldBe(typeof(int));
            columns["Id"].Item2.ShouldBeNull();
            columns["nomen"].Item1.ShouldBe(typeof(string));
            columns["nomen"].Item2.ShouldBeNull();
        }
예제 #6
0
        public void GetColumns_Object_ColumnsNamedCorrectly()
        {
            var person = new PersonWithTableAndColumnAttribute();

            var columns = OrmUtilities.GetColumns(person);

            columns.ContainsKey("Id").ShouldBe(true);
            columns.ContainsKey("nomen").ShouldBe(true);
        }
예제 #7
0
        private void SubmitDeletedEntries()
        {
            var deletedObjects = ChangeTracker.DeletedObjects;

            foreach (var deletedObject in deletedObjects)
            {
                _sqlBuilder.TableName = OrmUtilities.GetTableName(deletedObject.GetType());
                _sqlBuilder.Columns   = OrmUtilities.GetColumns(deletedObject);

                _dbDriver.RunDeleteStatement(_sqlBuilder.DeleteStatement);
            }
        }
예제 #8
0
        public void GetColumns_Object_ValuesAndTypeCorrect()
        {
            var person = new PersonWithTableAndColumnAttribute
            {
                Id   = 1,
                Name = "Stefan"
            };

            var columns = OrmUtilities.GetColumns(person);

            columns["Id"].Item1.ShouldBe(typeof(int));
            columns["Id"].Item2.Equals(1).ShouldBe(true);
            columns["nomen"].Item1.ShouldBe(typeof(string));
            columns["nomen"].Item2.Equals("Stefan").ShouldBe(true);
        }
예제 #9
0
        private void SubmitModifiedEntries()
        {
            var modifiedObjects = ChangeTracker.ModifiedObjects;

            foreach (var modifiedObject in modifiedObjects)
            {
                _sqlBuilder.TableName = OrmUtilities.GetTableName(modifiedObject.GetType());
                _sqlBuilder.Columns   = OrmUtilities.GetColumns(modifiedObject);
                _sqlBuilder.IdName    = OrmUtilities.GetPrimaryKeyProperty(modifiedObject.GetType()).Name;

                _dbDriver.RunUpdateStatement(_sqlBuilder.UpdateStatement);

                ChangeTracker.GetEntry(modifiedObject).State = ChangeTrackerEntry.States.Unmodified;
                ChangeTracker.GetEntry(modifiedObject).UpdateOriginals(modifiedObject);
            }
        }
예제 #10
0
        private void SubmitInsertedEntries()
        {
            var insertedObjects = ChangeTracker.InsertedObjects;

            foreach (var objectToInsert in insertedObjects)
            {
                _sqlBuilder.TableName = OrmUtilities.GetTableName(objectToInsert.GetType());
                _sqlBuilder.Columns   = OrmUtilities.GetColumns(objectToInsert);
                _sqlBuilder.IdName    = OrmUtilities.GetPrimaryKeyProperty(objectToInsert.GetType()).Name;

                var newId = _dbDriver.RunInsertStatement(_sqlBuilder.InsertStatement);
                SetId(objectToInsert, newId);

                ChangeTracker.GetEntry(objectToInsert).UpdateOriginals(objectToInsert);
                ChangeTracker.GetEntry(objectToInsert).State = ChangeTrackerEntry.States.Unmodified;
            }
        }
예제 #11
0
        private static void SetId(object objectToSet, int id)
        {
            var idProperty = OrmUtilities.GetPrimaryKeyProperty(objectToSet.GetType());

            idProperty.SetValue(objectToSet, id);
        }
예제 #12
0
        private static int GetId(object obj)
        {
            var idProperty = OrmUtilities.GetPrimaryKeyProperty(obj.GetType());

            return((int)idProperty.GetValue(obj));
        }
예제 #13
0
        public void GetTableName_TableAttribute_AttributeNameProperty()
        {
            var tableName = OrmUtilities.GetTableName(typeof(PersonWithTableAndColumnAttribute));

            tableName.ShouldBe("people");
        }
예제 #14
0
        public void GetTableName_NoTableAttribute_ClassName()
        {
            var tableName = OrmUtilities.GetTableName(typeof(PersonWithId));

            tableName.ShouldBe("PersonWithId");
        }
예제 #15
0
 public void GetPrimaryKeyAttribute_MultiplePrimaryKeys_ThrowsException()
 {
     Should.Throw <Exception>(
         () => OrmUtilities.GetPrimaryKeyProperty(typeof(PersonWithMultiplePrimaryKeys))
         );
 }
예제 #16
0
 public void GetPrimaryKeyAttribute_PrimaryKeyAsString_ThrowsException()
 {
     Should.Throw <Exception>(
         () => OrmUtilities.GetPrimaryKeyProperty(typeof(PersonWithPrimaryKeyAsString))
         );
 }