Example #1
0
File: Query.cs Project: kujotx/Oak
        public static object InsertInto(this object o, string table, ConnectionProfile connectionProfile = null)
        {
            if (connectionProfile == null) connectionProfile = new ConnectionProfile();

            DynamicRepository dynamicModel = new DynamicRepository(connectionProfile, table, "Id");

            return dynamicModel.Insert(o);
        }
Example #2
0
        public DynamicModels Execute(dynamic options,
            DynamicRepository repository,
            string associationName,
            string selectClause,
            IEnumerable<dynamic> models,
            string parentMemberName)
        {
            if (ShouldDiscardCache(options)) Cache = null;

            if (Cache != null) return Cache;

            var many = repository.Query(selectClause).ToList();

            foreach (var item in many)
            {
                var model = models.First(s => s.Id == item.GetMember(parentMemberName));

                item.SetMember(model.GetType().Name, model);
            }

            foreach (var model in models)
            {
                var assocation = model.AssociationNamed(associationName);

                var relatedTo = many.Where(s => s.GetMember(model.GetType().Name).Equals(model)).Select(s => s);

                assocation.EagerLoadMany.Cache = new DynamicModels(relatedTo);

                assocation.AddNewAssociationMethod(assocation.EagerLoadMany.Cache, model);
            }

            return new DynamicModels(many);
        }
        void saving_dynamic_params()
        {
            before = () =>
            {
                seed = new Seed();

                seed.PurgeDb();

                seed.CreateTable("Blogs", new dynamic[]
                {
                    new { Id = "int", Identity = true, PrimaryKey = true },
                    new { Title = "nvarchar(255)" }
                }).ExecuteNonQuery();

                nameValueCollection.Add("Title", "Some Title");
            };

            it["persists saveable values to the database"] = () =>
            {
                var blogs = new DynamicRepository("Blogs");

                var blogId = blogs.Insert(asDynamic);

                var blog = blogs.Single(blogId);

                (blog.Title as string).should_be("Some Title");
            };
        }
Example #4
0
 public virtual void AddRepository(DynamicModels collection, DynamicRepository repository)
 {
     collection.SetMember("Repository", repository);
 }
Example #5
0
        public BelongsTo(DynamicRepository repository)
            : this(repository, null)
        {

        }
Example #6
0
 public HasOneThrough(DynamicRepository repository, DynamicRepository through)
     : this(repository, through, null)
 {
 }
Example #7
0
        public HasOne(DynamicRepository repository)
            : this(repository, null)
        {

        }
Example #8
0
        public HasMany(DynamicRepository repository, string methodName)
        {
            this.Repository = repository;

            this.MethodName = methodName ?? repository.TableName;
        }
Example #9
0
        public HasMany(DynamicRepository repository, string named)
        {
            this.repository = repository;

            this.named = named ?? repository.GetType().Name;
        }
Example #10
0
 public BelongsTo(DynamicRepository repository, string named)
 {
     this.Repository = repository;
     Named = named ?? Singular(repository);
 }
Example #11
0
 public HasOneThrough(DynamicRepository repository, DynamicRepository through, string named)
 {
     this.Repository = repository;
     this.through = through;
     Named = named ?? Singular(Repository);
 }
Example #12
0
        public HasOne(DynamicRepository repository, string named)
        {
            this.Repository = repository;

            Named = named ?? Singular(Repository);
        }
Example #13
0
        public static DynamicRepository RepositoryFor(string tableName)
        {
            var repo = new DynamicRepository(tableName);

            AssociationByConventions.ApplyProjection(repo);

            return repo;
        }
Example #14
0
        public HasManyThrough(DynamicRepository repository, DynamicRepository through, string methodName)
        {
            this.Repository = repository;

            this.through = through;

            this.throughTable = through.TableName;

            this.MethodName = methodName ?? repository.TableName;
        }
Example #15
0
        public HasManyThrough(DynamicRepository repository, DynamicRepository through, string named)
        {
            this.repository = repository;

            this.through = through;

            this.named = named ?? repository.GetType().Name;
        }
Example #16
0
        public HasMany(DynamicRepository repository)
            : this(repository, null)
        {

        }
Example #17
0
 public HasOne(DynamicRepository repository)
 {
     this.repository = repository;
 }
Example #18
0
        public HasManyAndBelongsTo(DynamicRepository repository, DynamicRepository reference)
        {
            Repository = repository;

            this.reference = reference;

            var sorted = new[] { repository.TableName, reference.TableName }.OrderBy(s => s);

            throughTable = sorted.First() + sorted.Last();

            MethodName = repository.TableName;
        }
Example #19
0
 public HasOneThrough(DynamicRepository repository, DynamicRepository through)
 {
     this.repository = repository;
     this.through = through;
 }
Example #20
0
        public HasOne(DynamicRepository repository, string methodName)
        {
            this.Repository = repository;

            MethodName = methodName ?? Singular(Repository);
        }
Example #21
0
 public BelongsTo(DynamicRepository repository)
 {
     this.repository = repository;
     name = Singular(repository);
 }
Example #22
0
 public HasOneThrough(DynamicRepository repository, DynamicRepository through, string methodName)
 {
     this.Repository = repository;
     this.through = through;
     MethodName = methodName ?? Singular(Repository);
 }
Example #23
0
 public Uniqueness(string property, DynamicRepository usingRepository)
     : base(property)
 {
     Repository = usingRepository;
 }
Example #24
0
        public BelongsTo(DynamicRepository repository, string methodName)
        {
            this.Repository = repository;

            MethodName = methodName ?? Singular(repository);
        }
Example #25
0
        public static DynamicModels Execute(IEnumerable<dynamic> models,
            DynamicRepository repository,
            string associationName,
            string sql,
            Func<dynamic, dynamic, bool> findClause)
        {
            var belongsResult = new List<dynamic>(repository.Query(sql));

            foreach (var item in belongsResult)
            {
                var relatedModels = models.Where(s => findClause(item, s));

                foreach (var relateModel in relatedModels)
                {
                    var association = relateModel.AssociationNamed(associationName);

                    association.Model = item;

                    var propName = relateModel.GetType().Name;

                    if (item.RespondsTo(propName))
                    {
                        ConvertToList(propName, item);

                        item.GetMember(propName).Add(relateModel);
                    }
                    else
                    {
                        item.SetMember(propName, relateModel);
                    }
                }
            }

            return new DynamicModels(belongsResult);
        }
Example #26
0
        public static DynamicRepository RepositoryFor(string tableName, string connectionString)
        {
            var repo = new DynamicRepository(new ConnectionProfile { ConnectionString = connectionString }, tableName);

            AssociationByConventions.ApplyProjection(repo, connectionString);

            return repo;
        }
        void mass_assignment()
        {
            before = () =>
            {
                seed = new Seed();

                seed.PurgeDb();

                seed.CreateTable("Users", new dynamic[]
                {
                    new { Id = "int", Identity = true, PrimaryKey = true },
                    new { Name = "nvarchar(255)" },
                    new { IsAdmin = "bit", Default = false }
                }).ExecuteNonQuery();

                nameValueCollection.Add("Name", "John");

                nameValueCollection.Add("IsAdmin", "true");
            };

            it["allows the ability to exclude fields"] = () =>
            {
                var users = new DynamicRepository("Users");

                var userId = users.Insert(asDynamic.Exclude("IsAdmin"));

                var user = users.Single(userId);

                (user.Name as string).should_be("John");

                ((bool)user.IsAdmin).should_be(false);
            };

            it["allows the ability to select fields"] = () =>
            {
                var users = new DynamicRepository("Users");

                var userId = users.Insert(asDynamic.Select("Name"));

                var user = users.Single(userId);

                (user.Name as string).should_be("John");

                ((bool)user.IsAdmin).should_be(false);
            };
        }