Skip to content

Commit c46e6ee

Browse files
committed
refactor: sql statements organized
Signed-off-by: Rajdeep Roy Chowdhury <rrajdeeproychowdhury@gmail.com>
1 parent 67da751 commit c46e6ee

56 files changed

Lines changed: 619 additions & 482 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ Keep the JAR and launch script together when moving the application.
9595

9696
- The application creates and initializes `database.db` in its working
9797
directory using `src/main/resources/sql/init.sql`.
98+
- Repository queries live in domain-specific folders under
99+
`src/main/resources/sql` (for example, `bill/get.sql`). Paths are registered
100+
in the `SqlQuery` enum and loaded with `SqlLoader.load(SqlQuery.BILL_GET)`,
101+
using UTF-8 decoding and caching. When adding a query, add its SQL file and
102+
enum entry; tests verify that every entry has a non-empty resource.
98103
- Running `./gradlew run` uses the repository root; the packaged launch
99104
commands above use `build/libs/`. Each location has its own database.
100105
- Reports are written to `report.pdf` in the working directory, replacing
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package openconsignment.common;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.nio.charset.StandardCharsets;
6+
import java.sql.SQLException;
7+
import java.util.Map;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
10+
public final class SqlLoader {
11+
12+
private static final Map<SqlQuery, String> CACHE = new ConcurrentHashMap<>();
13+
14+
private SqlLoader() {}
15+
16+
/** Loads and caches the UTF-8 SQL resource registered for the query. */
17+
public static String load(SqlQuery query) throws SQLException {
18+
String cachedSql = CACHE.get(query);
19+
if (cachedSql != null) {
20+
return cachedSql;
21+
}
22+
23+
String sql = readResource(query.resourcePath());
24+
String previousSql = CACHE.putIfAbsent(query, sql);
25+
return previousSql != null ? previousSql : sql;
26+
}
27+
28+
static String readResource(String name) throws SQLException {
29+
String path = "/sql/" + name;
30+
try (InputStream stream = SqlLoader.class.getResourceAsStream(path)) {
31+
if (stream == null) {
32+
throw new SQLException("SQL resource not found: " + path);
33+
}
34+
return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
35+
} catch (IOException ex) {
36+
throw new SQLException("Failed to read SQL resource: " + path, ex);
37+
}
38+
}
39+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package openconsignment.common;
2+
3+
/** Catalog of repository SQL resources, with paths relative to the classpath's /sql directory. */
4+
public enum SqlQuery {
5+
BILL_SAVE("bill/save.sql"),
6+
BILL_SAVE_LR("bill/save-lr.sql"),
7+
BILL_GET("bill/get.sql"),
8+
BILL_GET_LRS("bill/get-lrs.sql"),
9+
BILL_DELETE("bill/delete.sql"),
10+
BILL_DELETE_LRS("bill/delete-lrs.sql"),
11+
BILL_GET_ALL("bill/get-all.sql"),
12+
13+
BUYER_SAVE("buyer/save.sql"),
14+
BUYER_DELETE("buyer/delete.sql"),
15+
BUYER_RENAME("buyer/rename.sql"),
16+
BUYER_GET_ALL("buyer/get-all.sql"),
17+
18+
COLLECTION_PENDING_BILLS_FOR_BUYER("collection/pending-bills-for-buyer.sql"),
19+
COLLECTION_PENDING_AMOUNT_FOR_BILL("collection/pending-amount-for-bill.sql"),
20+
COLLECTION_SAVE("collection/save.sql"),
21+
COLLECTION_SAVE_ITEM("collection/save-item.sql"),
22+
COLLECTION_LAST_VOUCHER("collection/last-voucher.sql"),
23+
COLLECTION_GET_ITEMS("collection/get-items.sql"),
24+
COLLECTION_GET("collection/get.sql"),
25+
COLLECTION_DELETE("collection/delete.sql"),
26+
COLLECTION_DELETE_ITEMS("collection/delete-items.sql"),
27+
COLLECTION_GET_ALL("collection/get-all.sql"),
28+
COLLECTION_DETAILS_FOR_BUYER("collection/details-for-buyer.sql"),
29+
30+
PAYMENT_SAVE("payment/save.sql"),
31+
PAYMENT_SAVE_ITEM("payment/save-item.sql"),
32+
PAYMENT_PENDING_BILLS_FOR_SUPPLIER("payment/pending-bills-for-supplier.sql"),
33+
PAYMENT_PENDING_AMOUNT_FOR_BILL("payment/pending-amount-for-bill.sql"),
34+
PAYMENT_LAST_VOUCHER("payment/last-voucher.sql"),
35+
PAYMENT_GET_ITEMS("payment/get-items.sql"),
36+
PAYMENT_GET("payment/get.sql"),
37+
PAYMENT_DELETE("payment/delete.sql"),
38+
PAYMENT_DELETE_ITEMS("payment/delete-items.sql"),
39+
PAYMENT_GET_ALL("payment/get-all.sql"),
40+
PAYMENT_DETAILS_FOR_SUPPLIER("payment/details-for-supplier.sql"),
41+
42+
SUPPLIER_SAVE("supplier/save.sql"),
43+
SUPPLIER_DELETE("supplier/delete.sql"),
44+
SUPPLIER_RENAME("supplier/rename.sql"),
45+
SUPPLIER_GET_ALL("supplier/get-all.sql"),
46+
47+
TRANSPORT_SAVE("transport/save.sql"),
48+
TRANSPORT_DELETE("transport/delete.sql"),
49+
TRANSPORT_RENAME("transport/rename.sql"),
50+
TRANSPORT_GET_ALL("transport/get-all.sql"),
51+
TRANSPORT_REPORT("transport/report.sql");
52+
53+
private final String resourcePath;
54+
55+
SqlQuery(String resourcePath) {
56+
this.resourcePath = resourcePath;
57+
}
58+
59+
public String resourcePath() {
60+
return resourcePath;
61+
}
62+
}

‎src/main/java/openconsignment/repository/BillRepository.java‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.logging.Level;
1010
import java.util.logging.Logger;
1111
import javax.inject.Inject;
12+
import openconsignment.common.SqlLoader;
13+
import openconsignment.common.SqlQuery;
1214
import openconsignment.entity.BillEntity;
1315
import openconsignment.entity.LREntity;
1416

@@ -23,13 +25,7 @@ public BillRepository(Connection conn) {
2325

2426
public void save(BillEntity billEntity) throws SQLException {
2527
try {
26-
// language=sql
27-
String sql =
28-
"INSERT INTO `Bill_Entry_Table`(`Supplier Name`,`Buyer Name`,`Bill No.`,`Bill Date`,`Transport`,`LR Date`,`Bill Amount`,`Collection Due`,`Due`) "
29-
+ "VALUES (?,?,?,?,?,?,?,?,?) ON CONFLICT(`Bill No.`) DO UPDATE SET "
30-
+ "`Supplier Name`=excluded.`Supplier Name`, `Buyer Name`=excluded.`Buyer Name`, `Bill Date`=excluded.`Bill Date`, "
31-
+ "`Transport`=excluded.`Transport`, `LR Date`=excluded.`LR Date`, `Bill Amount`=excluded.`Bill Amount`, "
32-
+ "`Collection Due`=excluded.`Collection Due`, `Due`=excluded.`Due`;";
28+
String sql = SqlLoader.load(SqlQuery.BILL_SAVE);
3329

3430
conn.setAutoCommit(false);
3531

@@ -49,7 +45,7 @@ public void save(BillEntity billEntity) throws SQLException {
4945

5046
if (null != billEntity.getLrEntities()) {
5147
for (LREntity lr : billEntity.getLrEntities()) {
52-
sql = "INSERT INTO `LR_Table`(`Bill No.`,`LR No.`,`PM`) VALUES (?,?,?)";
48+
sql = SqlLoader.load(SqlQuery.BILL_SAVE_LR);
5349
preparedStatement = conn.prepareStatement(sql);
5450
preparedStatement.setString(1, billEntity.getBillNo());
5551
preparedStatement.setString(2, lr.getLrNo());
@@ -67,7 +63,7 @@ public void save(BillEntity billEntity) throws SQLException {
6763

6864
public BillEntity getBill(String billNo) throws SQLException {
6965
try {
70-
String sql = "select * from `Bill_Entry_Table` where `Bill No.`=? collate nocase";
66+
String sql = SqlLoader.load(SqlQuery.BILL_GET);
7167
PreparedStatement preparedStatement = conn.prepareStatement(sql);
7268
preparedStatement.setString(1, billNo);
7369
ResultSet billResultSet = preparedStatement.executeQuery();
@@ -86,7 +82,7 @@ public BillEntity getBill(String billNo) throws SQLException {
8682
.billAmount(billResultSet.getString("Bill Amount"))
8783
.build();
8884

89-
sql = "select * from `LR_Table` where `Bill No.`=? collate nocase";
85+
sql = SqlLoader.load(SqlQuery.BILL_GET_LRS);
9086
preparedStatement = conn.prepareStatement(sql);
9187
preparedStatement.setString(1, billNo);
9288
ResultSet lrPmResultSet = preparedStatement.executeQuery();
@@ -110,12 +106,12 @@ public BillEntity getBill(String billNo) throws SQLException {
110106
public void deleteBill(String billNo) throws SQLException {
111107
try {
112108
conn.setAutoCommit(false);
113-
String sql = "DELETE FROM `Bill_Entry_Table` where `Bill No.`=? collate nocase";
109+
String sql = SqlLoader.load(SqlQuery.BILL_DELETE);
114110
PreparedStatement preparedStatement = conn.prepareStatement(sql);
115111
preparedStatement.setString(1, billNo);
116112
preparedStatement.execute();
117113

118-
String sql2 = "DELETE FROM `LR_Table` where `Bill No.`=? collate nocase";
114+
String sql2 = SqlLoader.load(SqlQuery.BILL_DELETE_LRS);
119115
preparedStatement = conn.prepareStatement(sql2);
120116
preparedStatement.setString(1, billNo);
121117
preparedStatement.execute();
@@ -131,7 +127,7 @@ public void deleteBill(String billNo) throws SQLException {
131127
public List<BillEntity> getBills() throws SQLException {
132128
List<BillEntity> res = new ArrayList<>();
133129
try {
134-
String sql = "SELECT * FROM `Bill_Entry_Table`;";
130+
String sql = SqlLoader.load(SqlQuery.BILL_GET_ALL);
135131
PreparedStatement ps = conn.prepareStatement(sql);
136132
ResultSet rs = ps.executeQuery();
137133
while (rs.next()) {

‎src/main/java/openconsignment/repository/BuyerRepository.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.logging.Level;
1010
import java.util.logging.Logger;
1111
import javax.inject.Inject;
12+
import openconsignment.common.SqlLoader;
13+
import openconsignment.common.SqlQuery;
1214

1315
public class BuyerRepository {
1416

@@ -21,7 +23,7 @@ public BuyerRepository(Connection conn) {
2123

2224
public void saveBuyer(String buyerName) throws SQLException {
2325
try {
24-
String sql = "INSERT INTO `Buyer_Master_Table`(`Name`) VALUES (?);";
26+
String sql = SqlLoader.load(SqlQuery.BUYER_SAVE);
2527
PreparedStatement ps = conn.prepareStatement(sql);
2628
ps.setString(1, buyerName);
2729
ps.execute();
@@ -33,7 +35,7 @@ public void saveBuyer(String buyerName) throws SQLException {
3335

3436
public void deleteBuyer(String buyerName) throws SQLException {
3537
try {
36-
String sql = "DELETE FROM `Buyer_Master_Table` WHERE name=?";
38+
String sql = SqlLoader.load(SqlQuery.BUYER_DELETE);
3739
PreparedStatement ps = conn.prepareStatement(sql);
3840
ps.setString(1, buyerName);
3941
ps.execute();
@@ -45,7 +47,7 @@ public void deleteBuyer(String buyerName) throws SQLException {
4547

4648
public void renameBuyer(String oldName, String newName) throws SQLException {
4749
try {
48-
String sql = "UPDATE `Buyer_Master_Table` SET Name=? WHERE Name=?";
50+
String sql = SqlLoader.load(SqlQuery.BUYER_RENAME);
4951
PreparedStatement ps = conn.prepareStatement(sql);
5052
ps.setString(1, newName);
5153
ps.setString(2, oldName);
@@ -59,8 +61,7 @@ public void renameBuyer(String oldName, String newName) throws SQLException {
5961
public List<String> getAllBuyers() throws SQLException {
6062
try {
6163
List<String> buyerList = new ArrayList<>();
62-
ResultSet buyerResultSet = conn.prepareStatement(
63-
"select * from Buyer_Master_Table order by name collate nocase")
64+
ResultSet buyerResultSet = conn.prepareStatement(SqlLoader.load(SqlQuery.BUYER_GET_ALL))
6465
.executeQuery();
6566
while (buyerResultSet.next()) {
6667
buyerList.add(buyerResultSet.getString("name"));

0 commit comments

Comments
 (0)