Exemplo n.º 1
0
        /// <summary>
        ///Inserts two foreign key refernces into a mapping table
        /// </summary>
        public virtual void InsertManyToMany <T>(IDwarf obj1, IDwarf obj2, string alternateTableName = null)
        {
            if (!obj1.IsSaved)
            {
                obj1.Save();
            }

            if (!obj2.IsSaved)
            {
                obj2.Save();
            }

            var type1 = obj1.GetType();
            var type2 = obj2.GetType();

            var tableName = ManyToManyAttribute.GetManyToManyTableName(type1, type2, alternateTableName);

            DbContextHelper <T> .ClearCacheForType(type1);

            DbContextHelper <T> .ClearCacheForType(type2);

            var preReq = string.Format("IF NOT EXISTS (SELECT * FROM [{0}] WHERE {1}Id = {2} AND {3}Id = {4}) \r\n",
                                       tableName, type1.Name, ValueToSqlString(obj1.Id), type2.Name, ValueToSqlString(obj2.Id));

            var query = new QueryBuilder <T>()
                        .InsertInto(tableName)
                        .Values(type1.Name + "Id", obj1.Id)
                        .Values(type2.Name + "Id", obj2.Id)
                        .ToQuery();

            ExecuteNonQuery <T>(preReq + query);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Builds and executes insert statement
        /// </summary>
        public virtual void Insert <T, TY>(Dwarf <TY> dwarf, Guid?customId = null) where TY : Dwarf <TY>, new()
        {
            var type = DbContextHelper <T> .ClearCacheForType(DwarfHelper.DeProxyfy(dwarf));

            dwarf.Id = customId ?? Guid.NewGuid();

            ExecuteNonQuery <T, TY>(new QueryBuilder <T>().InsertInto(type).Values(dwarf));

            dwarf.IsSaved = true;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Builds and executes an update statement
        /// </summary>
        public virtual void Update <T>(Dwarf <T> dwarf, IEnumerable <ExpressionProperty> properties) where T : Dwarf <T>, new()
        {
            var type = DbContextHelper <T> .ClearCacheForType(DwarfHelper.DeProxyfy(dwarf));

            var command = new QueryBuilder <T>()
                          .Update(type)
                          .Set(dwarf, properties)
                          .Where(dwarf, Cfg.PKProperties[type]);

            ExecuteNonQuery <T>(command);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Builds and executes a delete statement
        /// </summary>
        public virtual void Delete <T>(Dwarf <T> dwarf) where T : Dwarf <T>, new()
        {
            var type = DbContextHelper <T> .ClearCacheForType(DwarfHelper.DeProxyfy(dwarf));

            var command = new QueryBuilder <T>()
                          .DeleteFrom <T>()
                          .Where(dwarf, Cfg.PKProperties[type]);

            ExecuteNonQuery <T>(command);

            dwarf.IsSaved = false;
        }
Exemplo n.º 5
0
        /// <summary>
        ///Delete two foreign key refernces from the mapping table
        /// </summary>
        public virtual void DeleteManyToMany <T>(IDwarf obj1, IDwarf obj2, string alternateTableName = null)
        {
            var type1 = obj1.GetType();
            var type2 = obj2.GetType();

            var tableName = ManyToManyAttribute.GetManyToManyTableName(type1, type2, alternateTableName);

            DbContextHelper <T> .ClearCacheForType(type1);

            DbContextHelper <T> .ClearCacheForType(type2);

            var query = new QueryBuilder()
                        .DeleteFrom("dbo.[" + tableName + "]")
                        .Where("[" + type1.Name + "Id] = " + ValueToSqlString(obj1.Id))
                        .Where("[" + type2.Name + "Id] = " + ValueToSqlString(obj2.Id));

            ExecuteNonQuery <T>(query);
        }