/// <summary> /// Inserts the CompanyProject if it doesnt already exist /// </summary> /// <param name="companyId"></param> /// <param name="projectUuid"></param> public void InsertCompanyProject(string companyId, string projectUuid) { CompanyProject cp = new CompanyProject(); cp.CompanyId = companyId; cp.ProjectUuid = projectUuid; lock (DbContext.locker) { //System.Diagnostics.Debug.WriteLine("Deserialize: query: " + query); var rowsAffected = Db.Query<CompanyProject>("Select * FROM CompanyProject WHERE CompanyProject.CompanyId = ?" + " AND CompanyProject.ProjectUuid = ?", companyId, projectUuid).Count; System.Diagnostics.Debug.WriteLine("Deserialize: CompanyProject rowsAffected: " + rowsAffected); if (rowsAffected == 0) { // The item does not exists in the database so safe to insert Db.Insert(cp); } } }
/// <summary> /// Inserts the project and its respective children (only Company and CompanyProject) /// into the database. /// </summary> /// <param name="project"></param> /// <returns>Returns true if the project was inserted, returns false if a project with the same /// uuid (primary key) already exists in the table.</returns> public bool InsertProject(Project project) { System.Diagnostics.Debug.WriteLine("DbProject InsertProject(Project project): initiated"); if (CheckIfProjectExist(project.uuid)) { return false; } //Project did not exist, safe to insert. DbCompany dbCompany = new DbCompany(); foreach (Company c in project.companies) { dbCompany.InsertCompany(c); } lock (DbContext.locker) { Db.Insert(project); // Db.InsertOrReplaceWithChildren(project, recursive: true); } // This could perhaps be done in the above foreach loop, // but because of lack of concurrency control in SQLite its done in its own loop. foreach (Company c in project.companies) { CompanyProject cp = new CompanyProject(); cp.ProjectUuid = project.uuid; cp.CompanyId = c.id; lock (DbContext.locker) { Db.Insert(cp); // Db.InsertOrReplaceWithChildren(project, recursive: true); } } // Project was successfully inserted return true; }
private async void TestInsertProject(object sender, EventArgs e) { DbContext DbContext = DbContext.GetDbContext; SQLiteConnection Db = DbContext.Db; System.Diagnostics.Debug.WriteLine("Before insert Project.Count: " + Db.Query<Project>("Select * from Project").Count()); ProjectsController jc = new ProjectsController(); string testUuid = "colemak"; Project project = new Project() { uuid = testUuid, }; string companyId = "Ikea"; Company comp = new Company() { id = companyId }; string courseId = "sverige"; Course course = new Course() { id = courseId }; string sgId = "dykking"; StudyGroup sg = new StudyGroup() { id = sgId }; StudyGroupProject sgj = new StudyGroupProject() { StudyGroupId = sgId, ProjectUuid = testUuid }; CourseProject lj = new CourseProject() { CourseId = courseId, ProjectUuid = testUuid }; CompanyProject cp = new CompanyProject() { CompanyId = companyId, ProjectUuid = testUuid }; string jtId = "10aarErfaringEcma6"; JobType jt = new JobType() { id = jtId }; JobTypeProject jtp = new JobTypeProject() { ProjectUuid = testUuid, JobTypeId = jtId }; // try catch on tables that will not be affected by a job delete try { Db.Insert(comp); } catch { } Db.Insert(project); try { Db.Insert(course); } catch { } try { Db.Insert(sg); } catch { } Db.Insert(sgj); Db.Insert(lj); Db.Insert(cp); try { Db.Insert(jt); } catch { } Db.Insert(jtp); System.Diagnostics.Debug.WriteLine("After insert: Project.Count: " + Db.Query<Project>("Select * from Project").Count()); }