public static bool ExecuteHasRows(this MySqlConnection dbConnection, string commandString, string parameterName1, object parameterValue1, string parameterName2, object parameterValue2) { MySqlCommand command = new MySqlCommand(commandString, dbConnection); command.Parameters.AddWithValue(parameterName1, parameterValue1); command.Parameters.AddWithValue(parameterName2, parameterValue2); return command.ExecuteHasRows(); }
/// <summary> /// Get the status and perhaps response regarding as signature request. /// </summary> /// <param name="certificateId">Id of the certificate.</param> /// <param name="signatureResponse">Signed signature response.</param> /// <returns>Status of the signature response.</returns> public SignatureResponseStatus GetSignatureResponseStatus(Guid certificateId, out Signed<SignatureResponse> signatureResponse) { MySqlCommand selectResponseCommand = new MySqlCommand("SELECT Value FROM signatureresponse WHERE Id = @Id", DbConnection); selectResponseCommand.Parameters.AddWithValue("@Id", certificateId.ToByteArray()); MySqlDataReader selectResponseReader = selectResponseCommand.ExecuteReader(); if (selectResponseReader.Read()) { byte[] signatureResponseData = selectResponseReader.GetBlob(0); selectResponseReader.Close(); signatureResponse = Serializable.FromBinary<Signed<SignatureResponse>>(signatureResponseData); return signatureResponse.Value.Status; } else { selectResponseReader.Close(); MySqlCommand selectRequestCommand = new MySqlCommand("SELECT count(*) FROM signaturerequest WHERE Id = @Id", DbConnection); selectRequestCommand.Parameters.AddWithValue("@Id", certificateId.ToByteArray()); if (selectRequestCommand.ExecuteHasRows()) { signatureResponse = null; return SignatureResponseStatus.Pending; } else { signatureResponse = null; return SignatureResponseStatus.Unknown; } } }
public static bool ExecuteHasRows(this MySqlConnection dbConnection, string commandString) { MySqlCommand command = new MySqlCommand(commandString, dbConnection); return command.ExecuteHasRows(); }
/// <summary> /// Add an authority. /// </summary> /// <param name="certificate">Authority to be added.</param> /// <returns>Index of the authority.</returns> public int AddAuthority( IRpcConnection connection, Certificate certificate) { if (certificate == null) throw new ArgumentNullException("certificate"); if (certificate.Validate(this.certificateStorage) != CertificateValidationResult.Valid) throw new PiSecurityException(ExceptionCode.InvalidCertificate, "Authority certificate not valid."); if (!(certificate is AuthorityCertificate)) throw new PiSecurityException(ExceptionCode.NoAuthorizedAuthority, "No an authority certificate."); MySqlTransaction transaction = DbConnection.BeginTransaction(); MySqlCommand countCommand = new MySqlCommand("SELECT count(*) FROM authority WHERE VotingId = @VotingId", DbConnection, transaction); countCommand.Add("@VotingId", this.parameters.VotingId.ToByteArray()); if ((long)countCommand.ExecuteScalar() >= this.parameters.AuthorityCount) throw new PiArgumentException(ExceptionCode.AlreadyEnoughAuthorities, "Already enough authorities."); MySqlCommand addedCommand = new MySqlCommand("SELECT count(*) FROM authority WHERE VotingId = @VotingId AND AuthorityId = @AuthorityId", DbConnection, transaction); addedCommand.Add("@VotingId", this.parameters.VotingId.ToByteArray()); addedCommand.Add("@AuthorityId", certificate.Id.ToByteArray()); if (addedCommand.ExecuteHasRows()) throw new PiArgumentException(ExceptionCode.AuthorityAlreadyInVoting, "Already an authority of the voting."); MySqlCommand indexCommand = new MySqlCommand("SELECT max(AuthorityIndex) + 1 FROM authority WHERE VotingId = @VotingId", DbConnection, transaction); indexCommand.Add("@VotingId", this.parameters.VotingId.ToByteArray()); object authorityIndexNull = indexCommand.ExecuteScalar(); int authorityIndex = authorityIndexNull == DBNull.Value ? 1 : Convert.ToInt32((long)authorityIndexNull); MySqlCommand insertCommand = new MySqlCommand("INSERT INTO authority (VotingId, AuthorityIndex, AuthorityId, Certificate) VALUES (@VotingId, @AuthorityIndex, @AuthorityId, @Certificate)", DbConnection, transaction); insertCommand.Parameters.AddWithValue("@VotingId", this.parameters.VotingId.ToByteArray()); insertCommand.Parameters.AddWithValue("@AuthorityIndex", authorityIndex); insertCommand.Parameters.AddWithValue("@AuthorityId", certificate.Id.ToByteArray()); insertCommand.Parameters.AddWithValue("@Certificate", certificate.ToBinary()); insertCommand.ExecuteNonQuery(); Logger.Log(LogLevel.Info, "Connection {0}: Authority id {1} added to voting id {2}", connection.Id, certificate.Id.ToString(), Id.ToString()); transaction.Commit(); return authorityIndex; }