@@ -39,12 +39,9 @@ private static JdbcConnectionPool obtainConnectionPool() {
39
39
40
40
/**
41
41
* This command provides a template to execute updates (including inserts) on the database
42
- *
43
- * @param sqlData An object that contains the necessary components to run a SQL statement.
44
- * Usually contains some SQL text and some values that will be injected
45
- * into the statement at run-time.
46
42
*/
47
- <T > void executeUpdateTemplate (SqlData <T > sqlData ) {
43
+ void executeUpdateTemplate (String description , String preparedStatement , Object ... params ) {
44
+ final SqlData <Object > sqlData = new SqlData <>(description , preparedStatement , params );
48
45
try (Connection connection = dataSource .getConnection ()) {
49
46
try (PreparedStatement st = prepareStatementWithKeys (sqlData , connection )) {
50
47
executeUpdateOnPreparedStatement (sqlData , st );
@@ -68,16 +65,6 @@ public <T> long executeInsertTemplate(
68
65
}
69
66
}
70
67
71
- private <T > long executeInsertTemplate (SqlData <T > sqlData ) {
72
- try (Connection connection = dataSource .getConnection ()) {
73
- try (PreparedStatement st = prepareStatementWithKeys (sqlData , connection )) {
74
- return executeInsertOnPreparedStatement (sqlData , st );
75
- }
76
- } catch (SQLException ex ) {
77
- throw new SqlRuntimeException (ex );
78
- }
79
- }
80
-
81
68
<T > long executeInsertOnPreparedStatement (SqlData <T > sqlData , PreparedStatement st ) throws SQLException {
82
69
sqlData .applyParametersToPreparedStatement (st );
83
70
st .executeUpdate ();
@@ -115,24 +102,24 @@ private <T> PreparedStatement prepareStatementWithKeys(SqlData<T> sqlData, Conne
115
102
@ Override
116
103
public long saveNewBorrower (String borrowerName ) {
117
104
CheckUtils .checkStringNotNullOrEmpty (borrowerName );
118
- return executeInsertTemplate (new SqlData <>(
105
+ return executeInsertTemplate (
119
106
"adds a new library borrower" ,
120
- "INSERT INTO library.borrower (name) VALUES (?);" , borrowerName )) ;
107
+ "INSERT INTO library.borrower (name) VALUES (?);" , borrowerName );
121
108
}
122
109
123
110
@ Override
124
111
public long createLoan (Book book , Borrower borrower , Date borrowDate ) {
125
- return executeInsertTemplate (new SqlData <>(
112
+ return executeInsertTemplate (
126
113
"Creates a new loan of a book to a borrower" ,
127
- "INSERT INTO library.loan (book, borrower, borrow_date) VALUES (?, ?, ?);" , book .id , borrower .id , borrowDate )) ;
114
+ "INSERT INTO library.loan (book, borrower, borrow_date) VALUES (?, ?, ?);" , book .id , borrower .id , borrowDate );
128
115
}
129
116
130
117
@ Override
131
118
public long saveNewBook (String bookTitle ) {
132
119
CheckUtils .checkStringNotNullOrEmpty (bookTitle );
133
- return executeInsertTemplate (new SqlData <>(
120
+ return executeInsertTemplate (
134
121
"Creates a new book in the database" ,
135
- "INSERT INTO library.book (title) VALUES (?);" , bookTitle )) ;
122
+ "INSERT INTO library.book (title) VALUES (?);" , bookTitle );
136
123
}
137
124
138
125
@ Override
@@ -147,25 +134,25 @@ public long saveNewUser(String username) {
147
134
public void updateBorrower (long id , String borrowerName ) {
148
135
CheckUtils .checkIntParamPositive (id );
149
136
CheckUtils .checkStringNotNullOrEmpty (borrowerName );
150
- executeUpdateTemplate (new SqlData <>(
137
+ executeUpdateTemplate (
151
138
"Updates the borrower's data" ,
152
- "UPDATE library.borrower SET name = ? WHERE id = ?;" , borrowerName , id )) ;
139
+ "UPDATE library.borrower SET name = ? WHERE id = ?;" , borrowerName , id );
153
140
}
154
141
155
142
@ Override
156
143
public void deleteBook (long id ) {
157
144
CheckUtils .checkIntParamPositive (id );
158
- executeUpdateTemplate (new SqlData <>(
145
+ executeUpdateTemplate (
159
146
"Deletes a book from the database" ,
160
- "DELETE FROM library.book WHERE id = ?;" , id )) ;
147
+ "DELETE FROM library.book WHERE id = ?;" , id );
161
148
}
162
149
163
150
@ Override
164
151
public void deleteBorrower (long id ) {
165
152
CheckUtils .checkIntParamPositive (id );
166
- executeUpdateTemplate (new SqlData <>(
153
+ executeUpdateTemplate (
167
154
"Deletes a borrower from the database" ,
168
- "DELETE FROM library.borrower WHERE id = ?;" , id )) ;
155
+ "DELETE FROM library.borrower WHERE id = ?;" , id );
169
156
}
170
157
171
158
@ Override
@@ -391,9 +378,9 @@ public Optional<Boolean> areCredentialsValid(String username, String password) {
391
378
public void updateUserWithPassword (long id , String password ) {
392
379
CheckUtils .checkIntParamPositive (id );
393
380
String hashedPassword = createHashedValueFromPassword (password );
394
- executeUpdateTemplate (new SqlData <>(
381
+ executeUpdateTemplate (
395
382
"Updates the user's password field with a new hash" ,
396
- "UPDATE auth.user SET password_hash = ? WHERE id = ?;" , hashedPassword , id )) ;
383
+ "UPDATE auth.user SET password_hash = ? WHERE id = ?;" , hashedPassword , id );
397
384
}
398
385
399
386
private String createHashedValueFromPassword (String password ) {
0 commit comments