diff --git a/README.md b/README.md index c211ae9..26e735f 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,11 @@ Keep the JAR and launch script together when moving the application. - The application creates and initializes `database.db` in its working directory using `src/main/resources/sql/init.sql`. +- Repository queries live in domain-specific folders under + `src/main/resources/sql` (for example, `bill/get.sql`). Paths are registered + in the `SqlQuery` enum and loaded with `SqlLoader.load(SqlQuery.BILL_GET)`, + using UTF-8 decoding and caching. When adding a query, add its SQL file and + enum entry; tests verify that every entry has a non-empty resource. - Running `./gradlew run` uses the repository root; the packaged launch commands above use `build/libs/`. Each location has its own database. - Reports are written to `report.pdf` in the working directory, replacing diff --git a/src/main/java/openconsignment/common/SqlLoader.java b/src/main/java/openconsignment/common/SqlLoader.java new file mode 100644 index 0000000..ad531dd --- /dev/null +++ b/src/main/java/openconsignment/common/SqlLoader.java @@ -0,0 +1,39 @@ +package openconsignment.common; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.sql.SQLException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public final class SqlLoader { + + private static final Map CACHE = new ConcurrentHashMap<>(); + + private SqlLoader() {} + + /** Loads and caches the UTF-8 SQL resource registered for the query. */ + public static String load(SqlQuery query) throws SQLException { + String cachedSql = CACHE.get(query); + if (cachedSql != null) { + return cachedSql; + } + + String sql = readResource(query.resourcePath()); + String previousSql = CACHE.putIfAbsent(query, sql); + return previousSql != null ? previousSql : sql; + } + + static String readResource(String name) throws SQLException { + String path = "/sql/" + name; + try (InputStream stream = SqlLoader.class.getResourceAsStream(path)) { + if (stream == null) { + throw new SQLException("SQL resource not found: " + path); + } + return new String(stream.readAllBytes(), StandardCharsets.UTF_8); + } catch (IOException ex) { + throw new SQLException("Failed to read SQL resource: " + path, ex); + } + } +} diff --git a/src/main/java/openconsignment/common/SqlQuery.java b/src/main/java/openconsignment/common/SqlQuery.java new file mode 100644 index 0000000..409c9a2 --- /dev/null +++ b/src/main/java/openconsignment/common/SqlQuery.java @@ -0,0 +1,62 @@ +package openconsignment.common; + +/** Catalog of repository SQL resources, with paths relative to the classpath's /sql directory. */ +public enum SqlQuery { + BILL_SAVE("bill/save.sql"), + BILL_SAVE_LR("bill/save-lr.sql"), + BILL_GET("bill/get.sql"), + BILL_GET_LRS("bill/get-lrs.sql"), + BILL_DELETE("bill/delete.sql"), + BILL_DELETE_LRS("bill/delete-lrs.sql"), + BILL_GET_ALL("bill/get-all.sql"), + + BUYER_SAVE("buyer/save.sql"), + BUYER_DELETE("buyer/delete.sql"), + BUYER_RENAME("buyer/rename.sql"), + BUYER_GET_ALL("buyer/get-all.sql"), + + COLLECTION_PENDING_BILLS_FOR_BUYER("collection/pending-bills-for-buyer.sql"), + COLLECTION_PENDING_AMOUNT_FOR_BILL("collection/pending-amount-for-bill.sql"), + COLLECTION_SAVE("collection/save.sql"), + COLLECTION_SAVE_ITEM("collection/save-item.sql"), + COLLECTION_LAST_VOUCHER("collection/last-voucher.sql"), + COLLECTION_GET_ITEMS("collection/get-items.sql"), + COLLECTION_GET("collection/get.sql"), + COLLECTION_DELETE("collection/delete.sql"), + COLLECTION_DELETE_ITEMS("collection/delete-items.sql"), + COLLECTION_GET_ALL("collection/get-all.sql"), + COLLECTION_DETAILS_FOR_BUYER("collection/details-for-buyer.sql"), + + PAYMENT_SAVE("payment/save.sql"), + PAYMENT_SAVE_ITEM("payment/save-item.sql"), + PAYMENT_PENDING_BILLS_FOR_SUPPLIER("payment/pending-bills-for-supplier.sql"), + PAYMENT_PENDING_AMOUNT_FOR_BILL("payment/pending-amount-for-bill.sql"), + PAYMENT_LAST_VOUCHER("payment/last-voucher.sql"), + PAYMENT_GET_ITEMS("payment/get-items.sql"), + PAYMENT_GET("payment/get.sql"), + PAYMENT_DELETE("payment/delete.sql"), + PAYMENT_DELETE_ITEMS("payment/delete-items.sql"), + PAYMENT_GET_ALL("payment/get-all.sql"), + PAYMENT_DETAILS_FOR_SUPPLIER("payment/details-for-supplier.sql"), + + SUPPLIER_SAVE("supplier/save.sql"), + SUPPLIER_DELETE("supplier/delete.sql"), + SUPPLIER_RENAME("supplier/rename.sql"), + SUPPLIER_GET_ALL("supplier/get-all.sql"), + + TRANSPORT_SAVE("transport/save.sql"), + TRANSPORT_DELETE("transport/delete.sql"), + TRANSPORT_RENAME("transport/rename.sql"), + TRANSPORT_GET_ALL("transport/get-all.sql"), + TRANSPORT_REPORT("transport/report.sql"); + + private final String resourcePath; + + SqlQuery(String resourcePath) { + this.resourcePath = resourcePath; + } + + public String resourcePath() { + return resourcePath; + } +} diff --git a/src/main/java/openconsignment/repository/BillRepository.java b/src/main/java/openconsignment/repository/BillRepository.java index 09add82..915a8d8 100644 --- a/src/main/java/openconsignment/repository/BillRepository.java +++ b/src/main/java/openconsignment/repository/BillRepository.java @@ -9,6 +9,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.inject.Inject; +import openconsignment.common.SqlLoader; +import openconsignment.common.SqlQuery; import openconsignment.entity.BillEntity; import openconsignment.entity.LREntity; @@ -23,13 +25,7 @@ public BillRepository(Connection conn) { public void save(BillEntity billEntity) throws SQLException { try { - // language=sql - String sql = - "INSERT INTO `Bill_Entry_Table`(`Supplier Name`,`Buyer Name`,`Bill No.`,`Bill Date`,`Transport`,`LR Date`,`Bill Amount`,`Collection Due`,`Due`) " - + "VALUES (?,?,?,?,?,?,?,?,?) ON CONFLICT(`Bill No.`) DO UPDATE SET " - + "`Supplier Name`=excluded.`Supplier Name`, `Buyer Name`=excluded.`Buyer Name`, `Bill Date`=excluded.`Bill Date`, " - + "`Transport`=excluded.`Transport`, `LR Date`=excluded.`LR Date`, `Bill Amount`=excluded.`Bill Amount`, " - + "`Collection Due`=excluded.`Collection Due`, `Due`=excluded.`Due`;"; + String sql = SqlLoader.load(SqlQuery.BILL_SAVE); conn.setAutoCommit(false); @@ -49,7 +45,7 @@ public void save(BillEntity billEntity) throws SQLException { if (null != billEntity.getLrEntities()) { for (LREntity lr : billEntity.getLrEntities()) { - sql = "INSERT INTO `LR_Table`(`Bill No.`,`LR No.`,`PM`) VALUES (?,?,?)"; + sql = SqlLoader.load(SqlQuery.BILL_SAVE_LR); preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, billEntity.getBillNo()); preparedStatement.setString(2, lr.getLrNo()); @@ -67,7 +63,7 @@ public void save(BillEntity billEntity) throws SQLException { public BillEntity getBill(String billNo) throws SQLException { try { - String sql = "select * from `Bill_Entry_Table` where `Bill No.`=? collate nocase"; + String sql = SqlLoader.load(SqlQuery.BILL_GET); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, billNo); ResultSet billResultSet = preparedStatement.executeQuery(); @@ -86,7 +82,7 @@ public BillEntity getBill(String billNo) throws SQLException { .billAmount(billResultSet.getString("Bill Amount")) .build(); - sql = "select * from `LR_Table` where `Bill No.`=? collate nocase"; + sql = SqlLoader.load(SqlQuery.BILL_GET_LRS); preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, billNo); ResultSet lrPmResultSet = preparedStatement.executeQuery(); @@ -110,12 +106,12 @@ public BillEntity getBill(String billNo) throws SQLException { public void deleteBill(String billNo) throws SQLException { try { conn.setAutoCommit(false); - String sql = "DELETE FROM `Bill_Entry_Table` where `Bill No.`=? collate nocase"; + String sql = SqlLoader.load(SqlQuery.BILL_DELETE); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, billNo); preparedStatement.execute(); - String sql2 = "DELETE FROM `LR_Table` where `Bill No.`=? collate nocase"; + String sql2 = SqlLoader.load(SqlQuery.BILL_DELETE_LRS); preparedStatement = conn.prepareStatement(sql2); preparedStatement.setString(1, billNo); preparedStatement.execute(); @@ -131,7 +127,7 @@ public void deleteBill(String billNo) throws SQLException { public List getBills() throws SQLException { List res = new ArrayList<>(); try { - String sql = "SELECT * FROM `Bill_Entry_Table`;"; + String sql = SqlLoader.load(SqlQuery.BILL_GET_ALL); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { diff --git a/src/main/java/openconsignment/repository/BuyerRepository.java b/src/main/java/openconsignment/repository/BuyerRepository.java index 28ad3e1..417eccf 100644 --- a/src/main/java/openconsignment/repository/BuyerRepository.java +++ b/src/main/java/openconsignment/repository/BuyerRepository.java @@ -9,6 +9,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.inject.Inject; +import openconsignment.common.SqlLoader; +import openconsignment.common.SqlQuery; public class BuyerRepository { @@ -21,7 +23,7 @@ public BuyerRepository(Connection conn) { public void saveBuyer(String buyerName) throws SQLException { try { - String sql = "INSERT INTO `Buyer_Master_Table`(`Name`) VALUES (?);"; + String sql = SqlLoader.load(SqlQuery.BUYER_SAVE); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, buyerName); ps.execute(); @@ -33,7 +35,7 @@ public void saveBuyer(String buyerName) throws SQLException { public void deleteBuyer(String buyerName) throws SQLException { try { - String sql = "DELETE FROM `Buyer_Master_Table` WHERE name=?"; + String sql = SqlLoader.load(SqlQuery.BUYER_DELETE); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, buyerName); ps.execute(); @@ -45,7 +47,7 @@ public void deleteBuyer(String buyerName) throws SQLException { public void renameBuyer(String oldName, String newName) throws SQLException { try { - String sql = "UPDATE `Buyer_Master_Table` SET Name=? WHERE Name=?"; + String sql = SqlLoader.load(SqlQuery.BUYER_RENAME); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, newName); ps.setString(2, oldName); @@ -59,8 +61,7 @@ public void renameBuyer(String oldName, String newName) throws SQLException { public List getAllBuyers() throws SQLException { try { List buyerList = new ArrayList<>(); - ResultSet buyerResultSet = conn.prepareStatement( - "select * from Buyer_Master_Table order by name collate nocase") + ResultSet buyerResultSet = conn.prepareStatement(SqlLoader.load(SqlQuery.BUYER_GET_ALL)) .executeQuery(); while (buyerResultSet.next()) { buyerList.add(buyerResultSet.getString("name")); diff --git a/src/main/java/openconsignment/repository/CollectionRepository.java b/src/main/java/openconsignment/repository/CollectionRepository.java index 1092ff6..82a2991 100644 --- a/src/main/java/openconsignment/repository/CollectionRepository.java +++ b/src/main/java/openconsignment/repository/CollectionRepository.java @@ -9,6 +9,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.inject.Inject; +import openconsignment.common.SqlLoader; +import openconsignment.common.SqlQuery; import openconsignment.common.Utils; import openconsignment.entity.BillEntity; import openconsignment.entity.CollectionEntity; @@ -26,69 +28,7 @@ public CollectionRepository(Connection conn) { public List fetchPendingBillsForBuyer(String buyerName) throws SQLException { try { - // language=sql - String sql = - """ - with cte_collected as ( - select - `Bill No.`, - sum(`Amount Collected`) as amount_collected - from - Collection_Entry_Extended_Table - group by - `Bill No.` - ), - cte_bill as ( - select - `Bill No.`, - `Bill Date`, - `Bill Amount`, - `Supplier Name` - from - Bill_Entry_Table - WHERE - `Buyer Name` = ? - ), - cte_bill_collected as ( - select - `Bill No.`, - `Bill Date`, - `Bill Amount`, - `Supplier Name`, - coalesce(`amount_collected`, 0) as amount_collected - from - cte_bill - left join cte_collected - using(`Bill No.`) - ), - cte_pending as ( - select - `Bill No.`, - `Bill Date`, - `Bill Amount`, - `Supplier Name`, - `Bill Amount` - amount_collected as pending_amount - from - cte_bill_collected - ), - cte_final as ( - select - `Bill No.`, - `Bill Date`, - `Bill Amount`, - `Supplier Name` - from - cte_pending - where - pending_amount > 0 - order by - 1 - ) - select - * - from - cte_final; - """; + String sql = SqlLoader.load(SqlQuery.COLLECTION_PENDING_BILLS_FOR_BUYER); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, buyerName); ResultSet resultSet = preparedStatement.executeQuery(); @@ -111,54 +51,7 @@ cte_final as ( public int fetchPendingAmountForBillNo(String billNo) throws SQLException { try { - // language=sql - String sql = - """ - with cte_collected as ( - select - `Bill No.`, - sum(`Amount Collected`) as amount_collected - from - Collection_Entry_Extended_Table - group by - `Bill No.` - ), - cte_bill as ( - select - * - from - Bill_Entry_Table - WHERE - `Bill No.` = ? - ), - cte_bill_collected as ( - select - `Bill No.`, - `Bill Amount`, - coalesce(`amount_collected`, 0) as amount_collected - from - cte_bill - left join cte_collected - using(`Bill No.`) - ), - cte_pending as ( - select - `Bill No.`, - `Bill Amount` - amount_collected as pending_amount - from - cte_bill_collected - ), - cte_final as ( - select - pending_amount - from - cte_pending - ) - select - * - from - cte_final; - """; + String sql = SqlLoader.load(SqlQuery.COLLECTION_PENDING_AMOUNT_FOR_BILL); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, billNo); ResultSet resultSet = preparedStatement.executeQuery(); @@ -178,16 +71,7 @@ public void saveCollection(CollectionEntity collectionEntity) throws SQLExceptio try { conn.setAutoCommit(false); deleteCollection(collectionEntity.getVoucherNo()); - // language=sql - String sql = - """ - INSERT INTO `Collection_Entry_Table`(`Voucher No.`,`Voucher Date`,`Buyer Name`,`Total Amount`) - VALUES (?,?,?,?) - on conflict(`Voucher No.`) do update set - `Voucher Date` = excluded.`Voucher Date`, - `Buyer Name` = excluded.`Buyer Name`, - `Total Amount` = excluded.`Total Amount`; -"""; + String sql = SqlLoader.load(SqlQuery.COLLECTION_SAVE); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, collectionEntity.getVoucherNo()); preparedStatement.setString(2, collectionEntity.getVoucherDate()); @@ -195,32 +79,7 @@ on conflict(`Voucher No.`) do update set preparedStatement.setString(4, collectionEntity.getTotalAmount()); preparedStatement.execute(); - // language=sql - sql = - """ - INSERT - INTO - `Collection_Entry_Extended_Table`(`Voucher No.`, - `Supplier Name`, - `Bill No.`, - `Bill Date`, - `Bill Amount`, - `Collection Due`, - `Amount Collected`, - `Bank`, - `DD No.`, - `DD Date`) - VALUES (?, - ?, - ?, - ?, - ?, - ?, - ?, - ?, - ?, - ?); -"""; + sql = SqlLoader.load(SqlQuery.COLLECTION_SAVE_ITEM); if (null != collectionEntity.getItems()) { for (CollectionItemEntity collectionItem : collectionEntity.getItems()) { @@ -252,8 +111,7 @@ on conflict(`Voucher No.`) do update set public String getLastVoucher() throws SQLException { try { - // language=sql - String sql = "SELECT MAX(`Voucher No.`) from `COLLECTION_ENTRY_TABLE`;"; + String sql = SqlLoader.load(SqlQuery.COLLECTION_LAST_VOUCHER); PreparedStatement preparedStatement = conn.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery(); return resultSet.getString("Max(`Voucher No.`)"); @@ -266,7 +124,7 @@ public String getLastVoucher() throws SQLException { public CollectionEntity getCollection(String voucherNo) throws SQLException { try { - String sql = "select * from `Collection_Entry_Extended_Table` where `Voucher No.`=? collate nocase"; + String sql = SqlLoader.load(SqlQuery.COLLECTION_GET_ITEMS); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, voucherNo); ResultSet collectionResultSet = preparedStatement.executeQuery(); @@ -286,7 +144,7 @@ public CollectionEntity getCollection(String voucherNo) throws SQLException { collectionResultSet.getString("DD Date"))); } - sql = "select * from `Collection_Entry_Table` where `Voucher No.`=? collate nocase"; + sql = SqlLoader.load(SqlQuery.COLLECTION_GET); preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, voucherNo); collectionResultSet = preparedStatement.executeQuery(); @@ -311,13 +169,13 @@ public void deleteCollection(String voucherNo) throws SQLException { try { conn.setAutoCommit(false); - String sql = "DELETE FROM `Collection_Entry_Table` where `Voucher No.`=? collate nocase"; + String sql = SqlLoader.load(SqlQuery.COLLECTION_DELETE); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, voucherNo); preparedStatement.execute(); - sql = "DELETE FROM `Collection_Entry_Extended_Table` where `Voucher No.`=? collate nocase"; + sql = SqlLoader.load(SqlQuery.COLLECTION_DELETE_ITEMS); preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, voucherNo); @@ -333,7 +191,7 @@ public void deleteCollection(String voucherNo) throws SQLException { public List getCollections() throws SQLException { try { List res = new ArrayList<>(); - String sql = "SELECT * FROM `Collection_Entry_Table`;"; + String sql = SqlLoader.load(SqlQuery.COLLECTION_GET_ALL); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { @@ -354,14 +212,7 @@ public List getCollections() throws SQLException { public List getCollectionDetailsForBuyer(String buyerName) throws SQLException { try { - // language=sql - String sql = - """ - select * from `Bill_Entry_Table` left join - (`Collection_Entry_Extended_Table` - join `Collection_Entry_Table` using ("Voucher No.")) - using("Bill No.") where `Bill_Entry_Table`.`Buyer Name`=? - """; + String sql = SqlLoader.load(SqlQuery.COLLECTION_DETAILS_FOR_BUYER); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, buyerName); ResultSet resultSet = preparedStatement.executeQuery(); diff --git a/src/main/java/openconsignment/repository/PaymentRepository.java b/src/main/java/openconsignment/repository/PaymentRepository.java index e997777..eada8c5 100644 --- a/src/main/java/openconsignment/repository/PaymentRepository.java +++ b/src/main/java/openconsignment/repository/PaymentRepository.java @@ -9,6 +9,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.inject.Inject; +import openconsignment.common.SqlLoader; +import openconsignment.common.SqlQuery; import openconsignment.common.Utils; import openconsignment.entity.BillEntity; import openconsignment.entity.PaymentEntity; @@ -28,15 +30,7 @@ public void savePayment(PaymentEntity paymentEntity) throws SQLException { try { conn.setAutoCommit(false); deletePayment(paymentEntity.getVoucherNo()); - // language=sql - String sql = - """ - INSERT INTO `Payment_Entry_Table`(`Voucher No.`,`Voucher Date`,`Supplier Name`,`Total Amount`) VALUES (?,?,?,?) - on conflict(`Voucher No.`) do update set - `Voucher Date` = excluded.`Voucher Date`, - `Supplier Name` = excluded.`Supplier Name`, - `Total Amount` = excluded.`Total Amount`; -"""; + String sql = SqlLoader.load(SqlQuery.PAYMENT_SAVE); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, paymentEntity.getVoucherNo()); preparedStatement.setString(2, paymentEntity.getVoucherDate()); @@ -44,8 +38,7 @@ on conflict(`Voucher No.`) do update set preparedStatement.setString(4, paymentEntity.getTotalAmount()); preparedStatement.execute(); - sql = - "INSERT INTO `Payment_Entry_Extended_Table`(`Voucher No.`,`Buyer Name`,`Bill No.`,`Bill Date`,`Bill Amount`,`Due Amount`,`Amount Paid`,`Bank`,`DD No.`,`DD Date`) VALUES (?,?,?,?,?,?,?,?,?,?)"; + sql = SqlLoader.load(SqlQuery.PAYMENT_SAVE_ITEM); if (null != paymentEntity.getItems()) { for (PaymentItemEntity paymentItem : paymentEntity.getItems()) { @@ -74,69 +67,7 @@ on conflict(`Voucher No.`) do update set public List fetchPendingBillsForSupplier(String supplierName) throws SQLException { try { - // language=sql - String sql = - """ - with cte_paid as ( - select - `Bill No.`, - sum(`Amount Paid`) as amount_paid - from - Payment_Entry_Extended_Table - group by - `Bill No.` - ), - cte_bill as ( - select - `Bill No.`, - `Bill Date`, - `Bill Amount`, - `Buyer Name` - from - Bill_Entry_Table - WHERE - `Supplier Name` = ? - ), - cte_bill_paid as ( - select - `Bill No.`, - `Bill Date`, - `Bill Amount`, - `Buyer Name`, - coalesce(`amount_paid`, 0) as amount_paid - from - cte_bill - left join cte_paid - using(`Bill No.`) - ), - cte_pending as ( - select - `Bill No.`, - `Bill Date`, - `Bill Amount`, - `Buyer Name`, - `Bill Amount` - amount_paid as pending_amount - from - cte_bill_paid - ), - cte_final as ( - select - `Bill No.`, - `Bill Date`, - `Bill Amount`, - `Buyer Name` - from - cte_pending - where - pending_amount > 0 - order by - 1 - ) - select - * - from - cte_final; - """; + String sql = SqlLoader.load(SqlQuery.PAYMENT_PENDING_BILLS_FOR_SUPPLIER); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, supplierName); ResultSet resultSet = preparedStatement.executeQuery(); @@ -159,54 +90,7 @@ cte_final as ( public int fetchPendingAmountForBillNo(String billNo) throws SQLException { try { - // language=sql - String sql = - """ - with cte_paid as ( - select - `Bill No.`, - sum(`Amount Paid`) as amount_paid - from - Payment_Entry_Extended_Table - group by - `Bill No.` - ), - cte_bill as ( - select - * - from - Bill_Entry_Table - WHERE - `Bill No.` = ? - ), - cte_bill_paid as ( - select - `Bill No.`, - `Bill Amount`, - coalesce(`amount_paid`, 0) as amount_paid - from - cte_bill - left join cte_paid - using(`Bill No.`) - ), - cte_pending as ( - select - `Bill No.`, - `Bill Amount` - amount_paid as pending_amount - from - cte_bill_paid - ), - cte_final as ( - select - pending_amount - from - cte_pending - ) - select - * - from - cte_final; - """; + String sql = SqlLoader.load(SqlQuery.PAYMENT_PENDING_AMOUNT_FOR_BILL); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, billNo); ResultSet resultSet = preparedStatement.executeQuery(); @@ -224,8 +108,7 @@ cte_final as ( public String getLastVoucher() throws SQLException { try { - // language=sql - String sql = "SELECT MAX(`Voucher No.`) from `PAYMENT_ENTRY_TABLE`;"; + String sql = SqlLoader.load(SqlQuery.PAYMENT_LAST_VOUCHER); PreparedStatement preparedStatement = conn.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery(); return resultSet.getString("Max(`Voucher No.`)"); @@ -237,7 +120,7 @@ public String getLastVoucher() throws SQLException { public PaymentEntity getPayment(String voucherNo) throws SQLException { try { - String sql = "select * from `Payment_Entry_Extended_Table` where `Voucher No.`=? collate nocase"; + String sql = SqlLoader.load(SqlQuery.PAYMENT_GET_ITEMS); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, voucherNo); ResultSet rs = ps.executeQuery(); @@ -259,7 +142,7 @@ public PaymentEntity getPayment(String voucherNo) throws SQLException { list.add(paymentItem); } - sql = "select * from `Payment_Entry_Table` where `Voucher No.`=? collate nocase"; + sql = SqlLoader.load(SqlQuery.PAYMENT_GET); ps = conn.prepareStatement(sql); ps.setString(1, voucherNo); @@ -288,13 +171,13 @@ public void deletePayment(String voucherNo) throws SQLException { try { conn.setAutoCommit(false); - String sql = "DELETE FROM `Payment_Entry_Table` where `Voucher No.`=? collate nocase"; + String sql = SqlLoader.load(SqlQuery.PAYMENT_DELETE); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, voucherNo); preparedStatement.execute(); - sql = "DELETE FROM `Payment_Entry_Extended_Table` where `Voucher No.`=? collate nocase"; + sql = SqlLoader.load(SqlQuery.PAYMENT_DELETE_ITEMS); preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, voucherNo); @@ -310,7 +193,7 @@ public void deletePayment(String voucherNo) throws SQLException { public List getPayments() throws SQLException { try { List res = new ArrayList<>(); - String sql = "SELECT * FROM `Payment_Entry_Table`;"; + String sql = SqlLoader.load(SqlQuery.PAYMENT_GET_ALL); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { @@ -331,15 +214,7 @@ public List getPayments() throws SQLException { public List getPaymentDetailsForSupplier(String supplierName) throws SQLException { try { - // language=sql - String sql = - """ - select * from `Bill_Entry_Table` left join - (`Payment_Entry_Extended_Table` - join `Payment_Entry_Table` using ("Voucher No.")) - using("Bill No.") where `Bill_Entry_Table`.`Supplier Name`=? - order by `Bill_Entry_Table`.`Bill No.` - """; + String sql = SqlLoader.load(SqlQuery.PAYMENT_DETAILS_FOR_SUPPLIER); PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1, supplierName); ResultSet resultSet = preparedStatement.executeQuery(); diff --git a/src/main/java/openconsignment/repository/SupplierRepository.java b/src/main/java/openconsignment/repository/SupplierRepository.java index e2bae61..ed4831a 100644 --- a/src/main/java/openconsignment/repository/SupplierRepository.java +++ b/src/main/java/openconsignment/repository/SupplierRepository.java @@ -9,6 +9,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.inject.Inject; +import openconsignment.common.SqlLoader; +import openconsignment.common.SqlQuery; public class SupplierRepository { @@ -21,7 +23,7 @@ public SupplierRepository(Connection conn) { public void saveSupplier(String supplierName) throws SQLException { try { - String sql = "INSERT INTO `Supplier_Master_Table`(`Name`) VALUES (?);"; + String sql = SqlLoader.load(SqlQuery.SUPPLIER_SAVE); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, supplierName); ps.execute(); @@ -33,7 +35,7 @@ public void saveSupplier(String supplierName) throws SQLException { public void deleteSupplier(String supplierName) throws SQLException { try { - String sql = "DELETE FROM `Supplier_Master_Table` WHERE name=?"; + String sql = SqlLoader.load(SqlQuery.SUPPLIER_DELETE); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, supplierName); ps.execute(); @@ -45,7 +47,7 @@ public void deleteSupplier(String supplierName) throws SQLException { public void renameSupplier(String oldName, String newName) throws SQLException { try { - String sql = "UPDATE `Supplier_Master_Table` SET Name=? WHERE Name=?"; + String sql = SqlLoader.load(SqlQuery.SUPPLIER_RENAME); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, newName); ps.setString(2, oldName); @@ -59,8 +61,7 @@ public void renameSupplier(String oldName, String newName) throws SQLException { public List getAllSuppliers() throws SQLException { try { List supplierList = new ArrayList<>(); - ResultSet supplierResultSet = conn.prepareStatement( - "select * from Supplier_Master_Table order by name collate nocase") + ResultSet supplierResultSet = conn.prepareStatement(SqlLoader.load(SqlQuery.SUPPLIER_GET_ALL)) .executeQuery(); while (supplierResultSet.next()) { supplierList.add(supplierResultSet.getString("name")); diff --git a/src/main/java/openconsignment/repository/TransportRepository.java b/src/main/java/openconsignment/repository/TransportRepository.java index e0bdf3f..752b1bf 100644 --- a/src/main/java/openconsignment/repository/TransportRepository.java +++ b/src/main/java/openconsignment/repository/TransportRepository.java @@ -9,6 +9,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.inject.Inject; +import openconsignment.common.SqlLoader; +import openconsignment.common.SqlQuery; import openconsignment.model.TransportLedgerRow; public class TransportRepository { @@ -22,7 +24,7 @@ public TransportRepository(Connection conn) { public void saveTransport(String tranportName) throws SQLException { try { - String sql = "INSERT INTO `Transport_Master_Table`(`Name`) VALUES (?);"; + String sql = SqlLoader.load(SqlQuery.TRANSPORT_SAVE); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, tranportName); ps.execute(); @@ -34,7 +36,7 @@ public void saveTransport(String tranportName) throws SQLException { public void deleteTransport(String transportName) throws SQLException { try { - String sql = "DELETE FROM `Transport_Master_Table` WHERE name=?"; + String sql = SqlLoader.load(SqlQuery.TRANSPORT_DELETE); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, transportName); ps.execute(); @@ -46,7 +48,7 @@ public void deleteTransport(String transportName) throws SQLException { public void renameTransport(String oldName, String newName) throws SQLException { try { - String sql = "UPDATE `Transport_Master_Table` SET Name=? WHERE Name=?"; + String sql = SqlLoader.load(SqlQuery.TRANSPORT_RENAME); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, newName); ps.setString(2, oldName); @@ -60,8 +62,7 @@ public void renameTransport(String oldName, String newName) throws SQLException public List getAllTransports() throws SQLException { try { List transportList = new ArrayList<>(); - ResultSet transportResultSet = conn.prepareStatement( - "select * from Transport_Master_Table order by name collate nocase") + ResultSet transportResultSet = conn.prepareStatement(SqlLoader.load(SqlQuery.TRANSPORT_GET_ALL)) .executeQuery(); while (transportResultSet.next()) { transportList.add(transportResultSet.getString("name")); @@ -78,20 +79,7 @@ public List getTransportReportFor(String transportName, Stri try { List result = new ArrayList<>(); - // language=sql - String sql = - """ - select `LR No.`,`PM`,`Bill No.`,`Supplier Name`,`Buyer Name`,`LR Date` - from `LR_Table` join `Bill_Entry_Table` using (`Bill No.`) - where `Transport`=? - and - substr(`LR Date`, 7, 4)||substr(`LR Date`, 4, 2)||substr(`LR Date`, 1, 2) - between - substr(?, 7, 4)||substr(?, 4, 2)||substr(?, 1, 2) - and - substr(?, 7, 4)||substr(?, 4, 2)||substr(?, 1, 2) - order by datetime(substr(`LR Date`, 7, 4)||'-'||substr(`LR Date`, 4, 2)||'-'||substr(`LR Date`, 1, 2)) -"""; + String sql = SqlLoader.load(SqlQuery.TRANSPORT_REPORT); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, transportName); diff --git a/src/main/resources/fxml/entry/BillEntry.fxml b/src/main/resources/fxml/entry/BillEntry.fxml index 4284919..04c2a05 100644 --- a/src/main/resources/fxml/entry/BillEntry.fxml +++ b/src/main/resources/fxml/entry/BillEntry.fxml @@ -1,119 +1,129 @@ - - - - - - - - - - - - - - - - - - - - - - -