public Book findByIsbn(String isbn) { String sql = "select * from book where isbn = @isbn"; SqlParameter param = makeParameter("@isbn", isbn); SqlDataReader reader = null; Book book = null; try { reader = accessor.select(sql, param); if (reader.Read()) { book = new Book(); book.Id = (Int64?) read(reader, "id"); book.BookType = (BookType)Enum.ToObject(typeof(BookType), (Int32) read(reader, "bookType")); book.Title = (String) read(reader, "title"); book.Isbn = (String)read(reader, "isbn"); book.Publisher = (String) read(reader, "publisher"); book.Price = (Int32?) read(reader, "price"); book.BuyDate = (DateTime) read(reader, "buydate"); book.Status = (BookStatus)Enum.ToObject(typeof(BookStatus), (Int32) read(reader, "status")); // TODO BookEvalをたどって取得する? book.Eval = (String) read(reader, "eval"); } } finally { closeReader(reader); } return book; }
/// <summary> /// 書籍情報を登録する。 /// </summary> /// <param name="?"></param> public void regist(BookInfo bookInfo) { if (bookInfo == null) { throw new NullReferenceException("BookInfo is null"); } // TODO 宣言的トランザクションに対応する。 DataBaseAccessor accessor = new DataBaseAccessor(); accessor.open(); BookEval eval = null; // Evalのマスタ存在チェック if (bookInfo.EvalType != null) { BookEvalDao evalDao = new BookEvalDao(accessor); eval = evalDao.findById(bookInfo.EvalType); if (eval == null) { // TODO Exceptionを確認 throw new ApplicationRuntimeException("評価マスタ上に存在しない評価です。"); } } // Bookの存在有無チェック BookDao bookDao = new BookDao(accessor); Book existsBook = bookDao.findByIsbn(bookInfo.Isbn); if (existsBook != null) { throw new ApplicationRuntimeException("この本は既に登録済みです。入力内容をご確認ください"); } // BookEntityの作成 Book registBook = new Book(); registBook.BookType = bookInfo.BookType; registBook.Title = bookInfo.Title; registBook.Isbn = bookInfo.Isbn; registBook.Publisher = bookInfo.Publisher; registBook.Price = bookInfo.Price; registBook.BuyDate = bookInfo.BuyDate; registBook.Status = bookInfo.Status; registBook.BookEval = eval; registBook.Eval = bookInfo.Eval; bookDao.insert(registBook); }
public Book insert(Book book) { String sql = "insert into book (bookType, title, isbn, publisher, price, buydate, status, bookEval_id, eval) values (@bookType, @title, @isbn, @publisher, @price, @buydate, @status, @bookEval_id, @eval)"; SqlParameter bookTypeParam = makeParameter("@bookType", book.BookType); SqlParameter titleParam = makeParameter("@title", book.Title); SqlParameter isbnParam = makeParameter("@isbn", book.Isbn); SqlParameter publichserParam = makeParameter("@publisher", book.Publisher); SqlParameter priceParam = makeParameter("@price", book.Price); SqlParameter buyDateParam = makeParameter("@buydate", book.BuyDate); SqlParameter statusParam = makeParameter("@status", book.Status); SqlParameter evalTypeParam = null; if (book.BookEval != null) { evalTypeParam = makeParameter("@bookEval_id", book.BookEval.Id); } else { evalTypeParam = makeParameter("@bookEval_id", null); } SqlParameter evalParam = makeParameter("@eval", book.Eval); SqlParameter[] sqlParameters = new SqlParameter[] { bookTypeParam, titleParam, isbnParam, publichserParam, priceParam, buyDateParam, statusParam, evalTypeParam, evalParam }; if (accessor.executeNonQuery(sql, sqlParameters) == 0) { throw new ApplicationException("DBInsertに失敗しました"); } // IDの取得 // TODO 改善 Book insertedBook = findByIsbn(book.Isbn); book.Id = insertedBook.Id; return book; }