|
From: <pe...@us...> - 2003-12-24 00:24:36
|
Update of /cvsroot/neuclear/neuclear-ledger/src/java/org/neuclear/ledger/implementations In directory sc8-pr-cvs1:/tmp/cvs-serv23141/src/java/org/neuclear/ledger/implementations Modified Files: SQLLedger.java Log Message: Created a kind of poor man's version of ofbiz.org's EntityEngine. It doesnt use xml to configure it, but code. Should mainly be used to create tables. Can also insert rows, but hasnt been thoroughly tested. At some point I will improve that part and add some kind of smart querying engine to it. Similar to EntityEngine. But I dont need that myself right now. SQLLedger now uses this to create its tables. It is not fully working yet, but will be shortly. Index: SQLLedger.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-ledger/src/java/org/neuclear/ledger/implementations/SQLLedger.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** SQLLedger.java 16 Dec 2003 21:08:59 -0000 1.5 --- SQLLedger.java 24 Dec 2003 00:24:32 -0000 1.6 *************** *** 10,13 **** --- 10,15 ---- import org.neuclear.commons.sql.SQLTools; import org.neuclear.commons.sql.SQLContext; + import org.neuclear.commons.sql.DefaultConnectionSource; + import org.neuclear.commons.sql.entities.EntityModel; import org.neuclear.commons.NeuClearException; import org.neuclear.ledger.*; *************** *** 42,49 **** */ public SQLLedger(final ConnectionSource con, final String id) throws LowlevelLedgerException, UnknownLedgerException { ! super(id, getLedgerName(con, id)); this.con = new SQLContext(con); } private static String getLedgerName(final ConnectionSource con, final String id) throws UnknownLedgerException, LowlevelLedgerException { try { --- 44,110 ---- */ public SQLLedger(final ConnectionSource con, final String id) throws LowlevelLedgerException, UnknownLedgerException { ! super(id, "sql ledger"); this.con = new SQLContext(con); + // create(this.con); + + createLedger(id); + } + public void createLedger(String name) throws LowlevelLedgerException { + try { + final PreparedStatement stmt = prepQuery("insert into ledger (id,title,created) values (?,?,now())"); + stmt.setString(1, name); + stmt.setString(2, name); + stmt.execute(); + } catch (SQLException e) { + rollbackUT(); + throw new LowlevelLedgerException(this, e); + } + + } + public static void create(ConnectionSource con) { + try { + Connection connection=con.getConnection(); + EntityModel ledgerModel=new EntityModel("ledger",true); + ledgerModel.addTitle(); + // ledgerModel.addComment(); + ledgerModel.addTimeStamp(); + EntityModel bookModel=new EntityModel("book",true); + bookModel.addTitle(); + bookModel.addTimeStamp(); + EntityModel xactModel=new EntityModel("transaction",true); + xactModel.addComment(); + xactModel.addValueTime(); + xactModel.addReference(ledgerModel); + EntityModel entryModel=new EntityModel("entry",false); + entryModel.addMoney(); + entryModel.addReference(bookModel); + entryModel.addReference(xactModel); + + EntityModel hxactModel=new EntityModel("held_transaction",true); + hxactModel.addComment(); + hxactModel.addValueTime(); + hxactModel.addTimeStamp("held_until"); + hxactModel.addReference(ledgerModel); + hxactModel.addReference(xactModel); + hxactModel.addBoolean("cancelled"); + EntityModel hentryModel=new EntityModel("held_entry",false); + hentryModel.addMoney(); + hentryModel.addReference(bookModel); + hentryModel.addReference(hxactModel); + entryModel.create(connection); + hentryModel.create(connection); + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + public static void main(String args[]){ + create(new DefaultConnectionSource()); + } private static String getLedgerName(final ConnectionSource con, final String id) throws UnknownLedgerException, LowlevelLedgerException { try { *************** *** 73,77 **** public final boolean bookExists(final String bookID) throws LowlevelLedgerException { try { ! final PreparedStatement stmt = prepQuery("select id from account where id=?"); stmt.setString(1, bookID); final ResultSet rs = stmt.executeQuery(); --- 134,138 ---- public final boolean bookExists(final String bookID) throws LowlevelLedgerException { try { ! final PreparedStatement stmt = prepQuery("select id from book where id=?"); stmt.setString(1, bookID); final ResultSet rs = stmt.executeQuery(); *************** *** 86,90 **** throw new BookExistsException(this, bookID); try { ! final PreparedStatement stmt = prepQuery("insert into account values (?,?,3)"); stmt.setString(1, bookID); stmt.setString(2, title); --- 147,151 ---- throw new BookExistsException(this, bookID); try { ! final PreparedStatement stmt = prepQuery("insert into book values (?,?,now())"); stmt.setString(1, bookID); stmt.setString(2, title); *************** *** 214,218 **** private long insertTransaction(final UnPostedTransaction transaction) throws SQLException, LowlevelLedgerException { ! final PreparedStatement tranInsert = prepQuery("insert into transaction (value_date,comment,ledgerid) values (?,?,?)"); tranInsert.setTimestamp(1, SQLTools.toTimestamp(transaction.getTransactionTime())); tranInsert.setString(2, transaction.getComment()); --- 275,279 ---- private long insertTransaction(final UnPostedTransaction transaction) throws SQLException, LowlevelLedgerException { ! final PreparedStatement tranInsert = prepQuery("insert into transaction (valuetime,comment,ledgerid) values (?,?,?)"); tranInsert.setTimestamp(1, SQLTools.toTimestamp(transaction.getTransactionTime())); tranInsert.setString(2, transaction.getComment()); *************** *** 230,234 **** private long insertHeldTransaction(final UnPostedHeldTransaction transaction) throws SQLException, LowlevelLedgerException { ! final PreparedStatement tranInsert = prepQuery("insert into held_transaction (value_date,comment,held_until,ledgerid) values (?,?,?,?)"); tranInsert.setTimestamp(3, SQLTools.toTimestamp(transaction.getExpiryTime())); --- 291,295 ---- private long insertHeldTransaction(final UnPostedHeldTransaction transaction) throws SQLException, LowlevelLedgerException { ! final PreparedStatement tranInsert = prepQuery("insert into held_transaction (valuetime,comment,held_until,ledgerid) values (?,?,?,?)"); tranInsert.setTimestamp(3, SQLTools.toTimestamp(transaction.getExpiryTime())); *************** *** 248,252 **** private void insertTransactionItem(final long xid, final TransactionItem item) throws SQLException, LowlevelLedgerException { ! final PreparedStatement itemInsert = prepQuery("insert into entry (transactionid,accountid,amount,ack) values (?,?,?,1)"); itemInsert.setLong(1, xid); itemInsert.setString(2, item.getBook().getBookID()); --- 309,313 ---- private void insertTransactionItem(final long xid, final TransactionItem item) throws SQLException, LowlevelLedgerException { ! final PreparedStatement itemInsert = prepQuery("insert into entry (transactionid,bookid,amount) values (?,?,?)"); itemInsert.setLong(1, xid); itemInsert.setString(2, item.getBook().getBookID()); *************** *** 256,260 **** private void insertHeldTransactionItem(final long xid, final TransactionItem item) throws SQLException, LowlevelLedgerException { ! final PreparedStatement itemInsert = prepQuery("insert into held_entry (held_transactionid,accountid,amount,ack) values (?,?,?,1)"); itemInsert.setLong(1, xid); itemInsert.setString(2, item.getBook().getBookID()); --- 317,321 ---- private void insertHeldTransactionItem(final long xid, final TransactionItem item) throws SQLException, LowlevelLedgerException { ! final PreparedStatement itemInsert = prepQuery("insert into held_entry (held_transactionid,bookid,amount,ack) values (?,?,?,1)"); itemInsert.setLong(1, xid); itemInsert.setString(2, item.getBook().getBookID()); *************** *** 273,277 **** try { ! PreparedStatement stmt = prepQuery("select value_date,comment from transaction where id=? and ledgerid=?"); stmt.setLong(1, id); stmt.setString(2, getId()); --- 334,338 ---- try { ! PreparedStatement stmt = prepQuery("select valuetime,comment from transaction where id=? and ledgerid=?"); stmt.setLong(1, id); stmt.setString(2, getId()); *************** *** 284,288 **** final UnPostedTransaction transaction = new UnPostedTransaction(this, comment, started); ! stmt = prepQuery("select accountid,amount from entry where transactionid=?"); stmt.setLong(1, id); rs = stmt.executeQuery(); --- 345,349 ---- final UnPostedTransaction transaction = new UnPostedTransaction(this, comment, started); ! stmt = prepQuery("select bookid,amount from entry where transactionid=?"); stmt.setLong(1, id); rs = stmt.executeQuery(); *************** *** 311,315 **** try { ! PreparedStatement stmt = prepQuery("select value_date,held_until,comment from held_transaction where id=? and ledgerid=?"); stmt.setLong(1, id); stmt.setString(2, getId()); --- 372,376 ---- try { ! PreparedStatement stmt = prepQuery("select valuetime,held_until,comment from held_transaction where id=? and ledgerid=?"); stmt.setLong(1, id); stmt.setString(2, getId()); *************** *** 323,327 **** final UnPostedHeldTransaction transaction = new UnPostedHeldTransaction(this, comment, started, ended); ! stmt = prepQuery("select accountid,amount from held_entry where held_transactionid=?"); stmt.setLong(1, id); rs = stmt.executeQuery(); --- 384,388 ---- final UnPostedHeldTransaction transaction = new UnPostedHeldTransaction(this, comment, started, ended); ! stmt = prepQuery("select bookid,amount from held_entry where held_transactionid=?"); stmt.setLong(1, id); rs = stmt.executeQuery(); *************** *** 349,353 **** public final double getBalance(final Book book, final Date time) throws LowlevelLedgerException { try { ! final PreparedStatement stmt = prepQuery("select sum(e.amount) from entry e,transaction t where e.transactionid=t.id and e.accountid=? and t.value_date<= ? and t.ledgerid=?"); stmt.setString(1, book.getBookID()); --- 410,414 ---- public final double getBalance(final Book book, final Date time) throws LowlevelLedgerException { try { ! final PreparedStatement stmt = prepQuery("select sum(e.amount) from entry e,transaction t where e.transactionid=t.id and e.bookid=? and t.valuetime<= ? and t.ledgerid=?"); stmt.setString(1, book.getBookID()); *************** *** 377,385 **** final PreparedStatement stmt = prepQuery("select sum(u.amount) from (" + "select sum( e.amount) as amount from entry e,transaction t " + ! "where e.transactionid=t.id and e.accountid=? and t.value_date<= ? and t.ledgerid=?" + "union " + "select sum( e.amount) as amount from held_entry e, held_transaction t " + "where " + ! "e.held_transactionid=t.id and e.accountid=? and t.value_date<= ? " + "and e.amount<0 and t.held_until>= ? and t.cancelled=0 and t.transactionid is null and t.ledgerid=?" + ") u " --- 438,446 ---- final PreparedStatement stmt = prepQuery("select sum(u.amount) from (" + "select sum( e.amount) as amount from entry e,transaction t " + ! "where e.transactionid=t.id and e.bookid=? and t.valuetime<= ? and t.ledgerid=?" + "union " + "select sum( e.amount) as amount from held_entry e, held_transaction t " + "where " + ! "e.held_transactionid=t.id and e.bookid=? and t.valuetime<= ? " + "and e.amount<0 and t.held_until>= ? and t.cancelled=0 and t.transactionid is null and t.ledgerid=?" + ") u " *************** *** 432,436 **** public final Book getBook(final String bookID) throws UnknownBookException, LowlevelLedgerException { try { ! final PreparedStatement stmt = prepQuery("select screenname from account where id=?"); stmt.setString(1, bookID); final ResultSet rs = stmt.executeQuery(); --- 493,497 ---- public final Book getBook(final String bookID) throws UnknownBookException, LowlevelLedgerException { try { ! final PreparedStatement stmt = prepQuery("select comment from book where id=?"); stmt.setString(1, bookID); final ResultSet rs = stmt.executeQuery(); |