Ejemplo n.º 1
0
        /// <summary>
        /// Inserts the given entity in the database.
        /// Overrides the ID property of the given entity.
        /// </summary>
        /// <param name="entity">Entity to insert.</param>
        public void Add(SrsEntry entity)
        {
            DaoConnection connection = null;

            try
            {
                connection = DaoConnection.Open(DaoConnectionEnum.SrsDatabase);

                // Create a parameter list and two string builders that will
                // be used to put the SQL request together.
                List <DaoParameter> parameters    = new List <DaoParameter>();
                StringBuilder       sqlQueryStart = new StringBuilder(
                    "INSERT INTO " + SqlHelper.Table_SrsEntry + "(");
                StringBuilder sqlQueryEnd = new StringBuilder(
                    "VALUES(");

                // CreationDate
                if (entity.CreationDate != null)
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_CreationDate + ",");
                    sqlQueryEnd.Append("@CreationDate,");
                    parameters.Add(new DaoParameter(
                                       "@CreationDate", entity.CreationDate.Value.ToUniversalTime().Ticks));
                }

                // NextAnswerDate
                if (entity.NextAnswerDate != null)
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_NextAnswerDate + ",");
                    sqlQueryEnd.Append("@NextAnswerDate,");
                    parameters.Add(new DaoParameter(
                                       "@NextAnswerDate", entity.NextAnswerDate.Value.ToUniversalTime().Ticks));
                }

                // Meanings
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_Meanings + ",");
                sqlQueryEnd.Append("@Meanings,");
                parameters.Add(new DaoParameter("@Meanings",
                                                MultiValueFieldHelper.Trim(entity.Meanings ?? string.Empty)));

                // Readings
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_Readings + ",");
                sqlQueryEnd.Append("@Readings,");
                parameters.Add(new DaoParameter("@Readings",
                                                MultiValueFieldHelper.Trim(entity.Readings ?? string.Empty)));

                // CurrentGrade
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_CurrentGrade + ",");
                sqlQueryEnd.Append("@CurrentGrade,");
                parameters.Add(new DaoParameter("@CurrentGrade", entity.CurrentGrade));

                // FailureCount
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_FailureCount + ",");
                sqlQueryEnd.Append("@FailureCount,");
                parameters.Add(new DaoParameter("@FailureCount", entity.FailureCount));

                // SuccessCount
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_SuccessCount + ",");
                sqlQueryEnd.Append("@SuccessCount,");
                parameters.Add(new DaoParameter("@SuccessCount", entity.SuccessCount));

                // SuspensionDate
                if (entity.SuspensionDate.HasValue)
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_SuspensionDate + ",");
                    sqlQueryEnd.Append("@SuspensionDate,");
                    parameters.Add(new DaoParameter("@SuspensionDate", entity.SuspensionDate.Value.ToUniversalTime().Ticks));
                }

                // AssociatedVocab
                if (!string.IsNullOrWhiteSpace(entity.AssociatedVocab))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_AssociatedVocab + ",");
                    sqlQueryEnd.Append("@AssociatedVocab,");
                    parameters.Add(new DaoParameter(
                                       "@AssociatedVocab", entity.AssociatedVocab));
                }

                // AssociatedKanji
                if (!string.IsNullOrWhiteSpace(entity.AssociatedKanji))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_AssociatedKanji + ",");
                    sqlQueryEnd.Append("@AssociatedKanji,");
                    parameters.Add(new DaoParameter(
                                       "@AssociatedKanji", entity.AssociatedKanji));
                }

                // MeaningNote
                if (!string.IsNullOrWhiteSpace(entity.MeaningNote))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_MeaningNote + ",");
                    sqlQueryEnd.Append("@MeaningNote,");
                    parameters.Add(new DaoParameter(
                                       "@MeaningNote", entity.MeaningNote));
                }

                // ReadingNote
                if (!string.IsNullOrWhiteSpace(entity.ReadingNote))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_ReadingNote + ",");
                    sqlQueryEnd.Append("@ReadingNote,");
                    parameters.Add(new DaoParameter(
                                       "@ReadingNote", entity.ReadingNote));
                }

                // Tags
                if (!string.IsNullOrWhiteSpace(entity.Tags))
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_Tags + ",");
                    sqlQueryEnd.Append("@Tags,");
                    parameters.Add(new DaoParameter(
                                       "@Tags", MultiValueFieldHelper.Trim(entity.Tags)));
                }

                // LastUpdateDate
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_LastUpdateDate + ",");
                sqlQueryEnd.Append("@LastUpdateDate,");
                parameters.Add(new DaoParameter(
                                   "@LastUpdateDate", DateTime.UtcNow.Ticks));

                // ServerId
                if (entity.ServerId.HasValue)
                {
                    sqlQueryStart.Append(SqlHelper.Field_SrsEntry_ServerId + ",");
                    sqlQueryEnd.Append("@ServerId,");
                    parameters.Add(new DaoParameter(
                                       "@ServerId", entity.ServerId));
                }

                // IsDeleted (because why not?)
                sqlQueryStart.Append(SqlHelper.Field_SrsEntry_IsDeleted + ",");
                sqlQueryEnd.Append("@IsDeleted,");
                parameters.Add(new DaoParameter("@IsDeleted", entity.IsDeleted));

                // We are done with the string builders.

                // Bring the query pieces together.
                string finalQuery =
                    sqlQueryStart.ToString().TrimEnd(new char[] { ',' }) + ") "
                    + sqlQueryEnd.ToString().TrimEnd(new char[] { ',' }) + ")";

                // Execute the query.
                if (connection.ExecuteNonQuery(finalQuery, parameters.ToArray()) == 1)
                {
                    // If the row was inserted, put the insert ID in the entity.
                    entity.ID = connection.GetLastInsertId();
                }
            }
            finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
        }