public void Initialize() { dao = null; dao = new HuntDAO(); conn = null; conn = SQLiteConnectionFactory.GetPrimaryDBConnection(); }
private string BuildQuery() { log.Debug("Attempting to build query from form"); string query = "SELECT * FROM hunts"; QueryBuilder queryBuilder = new QueryBuilder(); HuntDAO huntDAO = new HuntDAO(); SQLiteConnection conn = SQLiteConnectionFactory.GetPrimaryDBConnection(); List<string> whereClauses = new List<string>(); if (checkBox_huntId.Checked == true) { whereClauses.Add(Constants.column_huntId + " = " + numericUpDown_hunt_id.Value); } else { if (checkBox_startDate.Checked == true) { long start = SQLiteHelper.ToMillisFromDateTime(datePicker_start.Value.Date); whereClauses.Add(Constants.column_huntDate + " " + comboBox_startDateRelational.SelectedItem.ToString() + " " + start); } if (checkBox_endDate.Checked == true) { long end = SQLiteHelper.ToMillisFromDateTime(datePicker_end.Value.Date); whereClauses.Add(Constants.column_huntDate + " " + comboBox_endDateRelational.SelectedItem.ToString() + " " + end); } if (checkBox_searchProvider.Checked == true) { whereClauses.Add(Constants.column_habitatId + " IN (" + "SELECT habitat_id FROM habitats WHERE provider_id = " + comboBox_provider.SelectedValue + ")"); } if (checkBox_searchHabitat.Checked == true) { whereClauses.Add(Constants.column_habitatId + " = " + comboBox_habitat.SelectedValue); } if (checkBox_searchGroup.Checked == true) { whereClauses.Add(Constants.column_groupId + " = " + comboBox_group.SelectedValue); } if (checkBox_searchGuide.Checked == true) { whereClauses.Add(Constants.column_huntId + " IN (" + "SELECT hunt_id FROM hunts_guides WHERE guide_id = " + comboBox_guide.SelectedValue + ")"); } } query = queryBuilder.buildHuntQuery("SELECT * FROM hunts", whereClauses, "", "ORDER BY hunt_date"); log.Debug("Built query: " + query); return query; }
public HuntEditor(int huntId) : this() { log.Debug("Loading Editor for huntId: " + huntId); SQLiteConnection conn = SQLiteConnectionFactory.GetPrimaryDBConnection(); HuntDAO dao = new HuntDAO(); HabitatDAO habitatDAO = new HabitatDAO(); Hunt hunt = dao.ReadById(huntId, conn); AddGuidesToList(hunt); isUpdate = true; this.label_huntId.Text = Convert.ToString(hunt.HuntId); this.comboBox_habitat.SelectedValue = hunt.Habitat.HabitatId; this.comboBox_provider.SelectedValue = hunt.Habitat.ProviderId; this.comboBox_group.SelectedValue = hunt.Group.GroupId; this.datePicker_huntDate.Value = hunt.HuntDate; this.numericUpDown_pheasant.Value = hunt.Harvest.Pheasant; this.numericUpDown_grouse.Value = hunt.Harvest.Grouse; this.numericUpDown_partridge.Value = hunt.Harvest.Partridge; this.numericUpDown_birdsSeen.Value = hunt.Harvest.BirdsSeen; this.numericUpDown_birdsMissed.Value = hunt.Harvest.BirdsMissed; this.numericUpDown_numberOfGuns.Value = hunt.NumberOfGuns; this.textBox_crop.Text = hunt.Harvest.Crop; this.textBox_comments.Text = hunt.Comments; checkBox_allowDelete.Enabled = true; string cropHarvested = hunt.Harvest.CropHarvested; if (cropHarvested == "Y") { radioButton_harvestYes.Checked = true; } else if (cropHarvested == "N") { radioButton_harvestNo.Checked = true; } }
public void SaveDummyHunts(int num) { //backup current db //File.Copy(Configuration.Instance.PrimaryDatabaseName, Configuration.Instance.PrimaryDatabaseName + ".tmp"); //Thread.Sleep(2000); HuntDAO huntDao = new HuntDAO(); using (SQLiteConnection conn = SQLiteConnectionFactory.GetPrimaryDBConnection()) { //delete all from all tables SQLiteHelper.ExecuteDML(conn, "DELETE FROM hunts"); SQLiteHelper.ExecuteDML(conn, "DELETE FROM providers"); SQLiteHelper.ExecuteDML(conn, "DELETE FROM habitats"); SQLiteHelper.ExecuteDML(conn, "DELETE FROM guides"); SQLiteHelper.ExecuteDML(conn, "DELETE FROM groups"); SQLiteHelper.ExecuteDML(conn, "DELETE FROM hunts_guides"); Provider[] providers = new Provider[70]; Habitat[] habitats = new Habitat[210]; Group[] groups = new Group[250]; Guide[] guides = new Guide[25]; for (int i = 0; i < providers.Length; i++) { providers[i] = InsertTestProvider(conn, i); } for (int i = 0; i < habitats.Length; i++) { habitats[i] = InsertTestHabitat(conn, providers[rand.Next(0, providers.Length)], i); } for (int i = 0; i < groups.Length; i++) { groups[i] = InsertTestGroup(conn, i); } for (int i = 0; i < guides.Length; i++) { guides[i] = InsertTestGuide(conn, i); } for (int i = 0; i < num; i++) { List<Guide> guidesL = new List<Guide>(); guidesL.Add(guides[rand.Next(0,guides.Length)]); if (rand.Next(0, 20) == 10) { guidesL.Add(guides[rand.Next(0, guides.Length)]); } Hunt hunt = getHunt( habitats[rand.Next(0,habitats.Length)], groups[rand.Next(0,groups.Length)], guidesL, conn); huntDao.Create(hunt, conn); } } }
private void button_save_Click(object sender, EventArgs e) { log.Debug("Save button Clicked"); SQLiteConnection conn = SQLiteConnectionFactory.GetPrimaryDBConnection(); HuntDAO dao = new HuntDAO(); DataRow selectedDataRow; Hunt hunt = new Hunt(); Habitat habitat = new Habitat(); Group group = new Group(); Harvest harvest = new Harvest(); selectedDataRow = ((DataRowView)comboBox_habitat.SelectedItem).Row; int habitatId = Convert.ToInt32(selectedDataRow["habitat_id"]); habitat.HabitatId = habitatId; selectedDataRow = ((DataRowView)comboBox_group.SelectedItem).Row; int groupId = Convert.ToInt32(selectedDataRow["group_id"]); group.GroupId = groupId; DateTime huntDate = datePicker_huntDate.Value.Date; hunt.NumberOfGuns = (int)numericUpDown_numberOfGuns.Value; harvest.Pheasant = (int)numericUpDown_pheasant.Value; harvest.Grouse = (int)numericUpDown_grouse.Value; harvest.Partridge = (int)numericUpDown_partridge.Value; harvest.BirdsSeen = (int)numericUpDown_birdsSeen.Value; harvest.BirdsMissed = (int)numericUpDown_birdsMissed.Value; harvest.Crop = textBox_crop.Text; harvest.CropHarvested = ""; if (radioButton_harvestYes.Checked == true) { harvest.CropHarvested = "Y"; } else if (radioButton_harvestNo.Checked == true) { harvest.CropHarvested = "N"; } string comments = textBox_comments.Text; hunt.Habitat = habitat; hunt.Group = group; hunt.Guides = guides.ToList(); hunt.HuntDate = huntDate; hunt.Harvest = harvest; hunt.Comments = comments; try { label_message.ForeColor = Color.Blue; if (isUpdate) { hunt.HuntId = Convert.ToInt32(label_huntId.Text); log.Debug("Attempting to update hunt " + hunt.HuntId); dao.Update(hunt, conn); label_message.Text = "Updated Hunt: " + hunt.HuntId; } else { log.Debug("Attempting to save new hunt"); dao.Create(hunt, conn); label_message.Text = "Saved new Hunt"; this.Close(); } } catch (Exception ex) { log.Error("Error saving/updating hunt", ex); label_message.ForeColor = Color.Red; label_message.Text = ex.Message; } }
private void button_delete_Click(object sender, EventArgs e) { log.Debug("Delete button clicked"); HuntDAO dao = new HuntDAO(); SQLiteConnection conn = SQLiteConnectionFactory.GetPrimaryDBConnection(); label_message.ForeColor = Color.Blue; if (label_huntId.Text.Length <= 0) { label_message.Text = "No Hunt Selected"; return; } int huntId = Convert.ToInt32(label_huntId.Text); Hunt hunt = new Hunt(); hunt.HuntId = huntId; try { log.Debug("Attempting to delete hunt " + huntId); dao.Delete(hunt, conn); label_message.Text = "Hunt " + huntId + " Deleted"; log.Debug("Hunt " + huntId + " Deleted"); button_save.Enabled = false; button_delete.Enabled = false; checkBox_allowDelete.Enabled = false; } catch (Exception ex) { log.Error("Error deleting hunt " + hunt.HuntId + ": " + ex.Message); label_message.ForeColor = Color.Red; label_message.Text = "Failed to delete"; } }
private List<Hunt> PerformQuery(string query) { log.Debug("Attempting to perform query"); lastQuery = query; HuntDAO huntDAO = new HuntDAO(); SQLiteConnection conn = SQLiteConnectionFactory.GetPrimaryDBConnection(); return huntDAO.Read(query, conn); }