|
| 1 | +package com.baeldung.cockroachdb.repository; |
| 2 | + |
| 3 | +import com.baeldung.cockroachdb.domain.Article; |
| 4 | + |
| 5 | +import java.sql.*; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.UUID; |
| 9 | + |
| 10 | +/** |
| 11 | + * Repository for the articles table related operations |
| 12 | + */ |
| 13 | +public class ArticleRepository { |
| 14 | + |
| 15 | + private static final String TABLE_NAME = "articles"; |
| 16 | + private Connection connection; |
| 17 | + |
| 18 | + public ArticleRepository(Connection connection) { |
| 19 | + this.connection = connection; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Creates the articles table. |
| 24 | + */ |
| 25 | + public void createTable() throws SQLException { |
| 26 | + StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(TABLE_NAME) |
| 27 | + .append("(id uuid PRIMARY KEY, ") |
| 28 | + .append("title string,") |
| 29 | + .append("author string)"); |
| 30 | + |
| 31 | + final String query = sb.toString(); |
| 32 | + Statement stmt = connection.createStatement(); |
| 33 | + stmt.execute(query); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Alter the articles table adding a column |
| 38 | + * |
| 39 | + * @param columnName Column name of the additional column |
| 40 | + * @param columnType Column type of the additional column |
| 41 | + * @throws SQLException |
| 42 | + */ |
| 43 | + public void alterTable(String columnName, String columnType) throws SQLException { |
| 44 | + StringBuilder sb = new StringBuilder("ALTER TABLE ").append(TABLE_NAME) |
| 45 | + .append(" ADD ") |
| 46 | + .append(columnName) |
| 47 | + .append(" ") |
| 48 | + .append(columnType); |
| 49 | + |
| 50 | + final String query = sb.toString(); |
| 51 | + Statement stmt = connection.createStatement(); |
| 52 | + stmt.execute(query); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Insert a new article in the articles table |
| 57 | + * |
| 58 | + * @param article New article to insert |
| 59 | + * @throws SQLException |
| 60 | + */ |
| 61 | + public void insertArticle(Article article) throws SQLException { |
| 62 | + StringBuilder sb = new StringBuilder("INSERT INTO ").append(TABLE_NAME) |
| 63 | + .append("(id, title, author) ") |
| 64 | + .append("VALUES (?,?,?)"); |
| 65 | + |
| 66 | + final String query = sb.toString(); |
| 67 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 68 | + preparedStatement.setString(1, article.getId().toString()); |
| 69 | + preparedStatement.setString(2, article.getTitle()); |
| 70 | + preparedStatement.setString(3, article.getAuthor()); |
| 71 | + preparedStatement.execute(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Select article by Title |
| 76 | + * |
| 77 | + * @param title title of the article to retrieve |
| 78 | + * @return article with the given title |
| 79 | + * @throws SQLException |
| 80 | + */ |
| 81 | + public Article selectByTitle(String title) throws SQLException { |
| 82 | + StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME) |
| 83 | + .append(" WHERE title = ?"); |
| 84 | + |
| 85 | + final String query = sb.toString(); |
| 86 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 87 | + preparedStatement.setString(1, title); |
| 88 | + |
| 89 | + try (ResultSet rs = preparedStatement.executeQuery()) { |
| 90 | + |
| 91 | + List<Article> articles = new ArrayList<>(); |
| 92 | + |
| 93 | + while (rs.next()) { |
| 94 | + Article article = new Article( |
| 95 | + UUID.fromString(rs.getString("id")), |
| 96 | + rs.getString("title"), |
| 97 | + rs.getString("author") |
| 98 | + ); |
| 99 | + articles.add(article); |
| 100 | + } |
| 101 | + return articles.get(0); |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Select all the articles |
| 108 | + * |
| 109 | + * @return list of all articles |
| 110 | + * @throws SQLException |
| 111 | + */ |
| 112 | + public List<Article> selectAll() throws SQLException { |
| 113 | + StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME); |
| 114 | + |
| 115 | + final String query = sb.toString(); |
| 116 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 117 | + try (ResultSet rs = preparedStatement.executeQuery()) { |
| 118 | + |
| 119 | + List<Article> articles = new ArrayList<>(); |
| 120 | + |
| 121 | + while (rs.next()) { |
| 122 | + Article article = new Article( |
| 123 | + UUID.fromString(rs.getString("id")), |
| 124 | + rs.getString("title"), |
| 125 | + rs.getString("author") |
| 126 | + ); |
| 127 | + articles.add(article); |
| 128 | + } |
| 129 | + return articles; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Delete article by title |
| 135 | + * |
| 136 | + * @param title title of the article to delete |
| 137 | + * @throws SQLException |
| 138 | + */ |
| 139 | + public void deleteArticleByTitle(String title) throws SQLException { |
| 140 | + StringBuilder sb = new StringBuilder("DELETE FROM ").append(TABLE_NAME) |
| 141 | + .append(" WHERE title = ?"); |
| 142 | + |
| 143 | + final String query = sb.toString(); |
| 144 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 145 | + preparedStatement.setString(1, title); |
| 146 | + preparedStatement.execute(); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Delete all rows in the table |
| 151 | + * |
| 152 | + * @throws SQLException |
| 153 | + */ |
| 154 | + public void truncateTable() throws SQLException { |
| 155 | + StringBuilder sb = new StringBuilder("TRUNCATE TABLE ").append(TABLE_NAME); |
| 156 | + |
| 157 | + final String query = sb.toString(); |
| 158 | + Statement stmt = connection.createStatement(); |
| 159 | + stmt.execute(query); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Delete table |
| 164 | + */ |
| 165 | + public void deleteTable() throws SQLException { |
| 166 | + StringBuilder sb = new StringBuilder("DROP TABLE IF EXISTS ").append(TABLE_NAME); |
| 167 | + |
| 168 | + final String query = sb.toString(); |
| 169 | + Statement stmt = connection.createStatement(); |
| 170 | + stmt.execute(query); |
| 171 | + } |
| 172 | +} |
0 commit comments