using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using Swarmops.Basic.Types.Financial; using Swarmops.Basic.Types.Swarm; namespace Swarmops.Database { public partial class SwarmDb { #region Field reading code // original.Identity, original.PersonId, original.OrganizationId, original.CreatedDateTime, original.Open, // original.GrantedDateTime, original.GrantedByPersonId, original.Score1, original.Score2, original.Score3) private const string applicantFieldSequence = " ApplicantId, PersonId, OrganizationId, CreatedDateTime, Open, " + // 0-4 "GrantedDateTime, GrantedByPersonId, Score1, Score2, Score3, " + // 5-9 "(Score1+Score2+Score3) AS ScoreTotal " + // included for sortability only "FROM Applicants "; private static BasicApplicant ReadApplicantFromDataReader (IDataRecord reader) { int applicantId = reader.GetInt32 (0); int personId = reader.GetInt32 (1); int organizationId = reader.GetInt32(2); DateTime createdDateTime = reader.GetDateTime (3); bool open = reader.GetBoolean(4); DateTime grantedDateTime = reader.GetDateTime(5); int grantedByPersonId = reader.GetInt32(6); int score1 = reader.GetInt32(7); int score2 = reader.GetInt32(8); int score3 = reader.GetInt32(9); return new BasicApplicant(applicantId, personId, organizationId, createdDateTime, open, grantedDateTime, grantedByPersonId, score1, score2, score3); } #endregion #region Record reading - SELECT statements /// /// Gets a salary from the database. /// /// The salary database identity. /// The requested salary. /// Thrown if there is no such identity. public BasicApplicant GetApplicant (int applicantId) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "SELECT" + applicantFieldSequence + "WHERE ApplicantId=" + applicantId + ";", connection); using (DbDataReader reader = command.ExecuteReader()) { if (reader.Read()) { return ReadApplicantFromDataReader (reader); } throw new ArgumentException ("Unknown Applicant Id: " + applicantId); } } } /// /// Gets salaries from the database. /// /// /// An optional combination of a Person and/or Organization object and/or DatabaseCondition /// specifiers. /// /// A list of matching salaries. public BasicApplicant[] GetApplicants (params object[] conditions) { List result = new List(); using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ( "SELECT" + applicantFieldSequence + " " + ConstructWhereClause ("Applicants", conditions), connection); using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { result.Add (ReadApplicantFromDataReader (reader)); } return result.ToArray(); } } } public Dictionary> GetApplicantsFromPeople(int[] personIds) { Dictionary> result = new Dictionary>(); if (personIds == null || personIds.Length == 0) { return result; } // Optimization: If more than 128 personIds, build a hash table, get _all_ memberships, and return // the matching ones logic wise. We've had _queries_ that exceed 64k text in length... /* if (personIds.Length > 128) { return GetParticipationsForPeopleOptimizedForLargeSets(personIds, gracePeriod); }*/ using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); string commandString = "SELECT " + applicantFieldSequence + " WHERE " + "Open=1 AND PersonId in (" + JoinIds(personIds) + ")"; DbCommand command = GetDbCommand(commandString, connection); command.CommandTimeout = 30; // If 30s is too short, something needs optimization. using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { BasicApplicant applicant = ReadApplicantFromDataReader(reader); int personId = applicant.PersonId; if (!result.ContainsKey(personId)) { result[personId] = new List(); } result[personId].Add(applicant); // Add to this person's list of memberships } } } return result; } #endregion #region Creation and manipulation - stored procedures public int CreateApplicant (int personId, int organizationId) { DateTime now = DateTime.UtcNow; using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ("CreateApplicant", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName (command, "personId", personId); AddParameterWithName (command, "organizationId", organizationId); AddParameterWithName (command, "dateTimeNow", now); return Convert.ToInt32 (command.ExecuteScalar()); } } /* public void SetSalaryNetPaid (int salaryId, bool netPaid) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ("SetSalaryNetPaid", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName (command, "salaryId", salaryId); AddParameterWithName (command, "netPaid", netPaid); command.ExecuteNonQuery(); // "Open" is set in the stored procedure to "NOT (TaxPaid AND NetPaid)". // So if both are paid, Open is set to false. } } public void SetSalaryNetSalary (int salaryId, Int64 netSalaryCents) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ("SetSalaryNetSalaryPrecise", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName (command, "salaryId", salaryId); AddParameterWithName (command, "netSalaryCents", netSalaryCents); command.ExecuteNonQuery(); } } public void SetSalaryTaxPaid (int salaryId, bool taxPaid) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand ("SetSalaryTaxPaid", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName (command, "salaryId", salaryId); AddParameterWithName (command, "taxPaid", taxPaid); command.ExecuteNonQuery(); // "Open" is set in the stored procedure to "NOT (TaxPaid AND NetPaid)". // So if both are paid, Open is set to false. } } public void SetSalaryAttested(int salaryId, bool attested) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand("SetSalaryAttested", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName(command, "salaryId", salaryId); AddParameterWithName(command, "attested", attested); command.ExecuteNonQuery(); } } public void SetSalaryOpen(int salaryId, bool attested) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = GetDbCommand("SetSalaryOpen", connection); command.CommandType = CommandType.StoredProcedure; AddParameterWithName(command, "salaryId", salaryId); AddParameterWithName(command, "open", attested); command.ExecuteNonQuery(); } }*/ #endregion } }