/// <summary>
        /// Fetch data to fill the form fields, from the BookInfo object based on the key
        /// </summary>
        /// <param name="key">Name of the BookData field from which to get the data</param>
        /// <returns>Value from the BookData field specified by the given key</returns>
        private object BookInfoGet(string key)
        {
            if (_bookData == null) //Singleton, so we don't have to reopen the DB with saved info, after every form field loads and its load event handler calls BookInfoGet
            {
                try
                {
                    _bookData = BookKeeper.GetData(_bookFile);
                }
                catch (Exception)
                {
                    MainWindow.Info(UiLang.Get("BookInfoNotAvailable"), 1);
                    IsEnabled = false;
                    Close();
                    _bookData = new BookData();
                    return(null);
                }
            }

            return(typeof(BookData).GetField(key) != null ? typeof(BookData).GetField(key).GetValue(_bookData) : null);
        }
Exemple #2
0
        /// <summary>
        /// Save dictionary with book info back into a file
        /// </summary>
        /// <param name="bookData">The dictionary object containing the book info</param>
        /// <exception cref="FileNotFoundException">Supplied book file was not found</exception>
        /// <exception cref="RowNotInTableException">Database record for the supplied book file doesn't exists and it was not possible to regenerate it</exception>
        public static void SaveData(BookData bookData)
        {
            bookData.Path = GetRelativeBookFilePath(bookData.Path);
            const string sql        = "SELECT EXISTS( SELECT * FROM books WHERE Path = @Path LIMIT 1)";
            var          parameters = new[] { new SQLiteParameter("Path", bookData.Path) };
            var          exists     = Db.QueryExists(sql, parameters);

            if (!exists)
            {
                GenerateData(bookData.Path);

                exists = Db.QueryExists(sql, parameters);

                if (!exists)
                {
                    throw new RowNotInTableException("Row for the specified book file was not found in the database, and the attempted regeneration failed.");
                }
            }

            Db.NonQuery(
                "UPDATE books SET Title = @Title, Author = @Author, Contributor = @Contributor, Publisher = @Publisher, Language = @Language, Published = @Published, Description = @Description, Series = @Series, Coverage = @Coverage, Size = @Size, Favorite = @Favorite, Sync = @Sync, Cover = @Cover WHERE Path = @Path",
                typeof(BookData).GetFields().Select(property => new SQLiteParameter(property.Name, property.GetValue(bookData))).ToArray());
        }
Exemple #3
0
        /// <summary>
        /// Casts book row from a sql query to a BookData object
        /// </summary>
        /// <param name="query"></param>
        /// <returns>Filled in BookData object</returns>
        public static BookData CastSqlBookRowToBookData(SQLiteDataReader query)
        {
            var bookData = new BookData
            {
                Title       = (string)query["Title"],
                Author      = (string)query["Author"],
                Contributor = (string)query["Contributor"],
                Publisher   = (string)query["Publisher"],
                Language    = (string)query["Language"],
                Published   = (DateTime?)(query["Published"].ToString() != String.Empty ? query["Published"] : null),
                Description = (string)query["Description"],
                Series      = (string)query["Series"],
                Coverage    = (string)query["Coverage"],
                Created     = (DateTime)query["Created"],
                Size        = Convert.ToUInt64(query["Size"]),
                Favorite    = (bool)query["Favorite"],
                Sync        = (bool)query["Sync"],
                Cover       = (byte[])(query["Cover"].ToString() != String.Empty ? query["Cover"] : null),
                Path        = GetAbsoluteBookFilePath((string)query["Path"])
            };

            return(bookData);
        }