|
From: <kg...@us...> - 2003-08-15 22:00:42
|
Update of /cvsroot/babeldoc/babeldoc/modules/sql/src/com/babeldoc/sql/journal
In directory sc8-pr-cvs1:/tmp/cvs-serv11104/modules/sql/src/com/babeldoc/sql/journal
Added Files:
PostgreSqlJournal.java
Log Message:
Add PostgreSQL journal implementation.
--- NEW FILE: PostgreSqlJournal.java ---
package com.babeldoc.sql.journal;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.babeldoc.core.I18n;
import com.babeldoc.core.LogService;
import com.babeldoc.core.journal.IJournalTicket;
import com.babeldoc.core.journal.JournalOperation;
import com.babeldoc.core.journal.JournalTicket;
import com.babeldoc.core.journal.JournalException;
import com.babeldoc.core.journal.query.JournalQuery;
import com.babeldoc.core.pipeline.PipelineDocument;
import com.babeldoc.core.resource.ResourceException;
import com.babeldoc.sql.util.SqlQueryManager;
/**
* PostgreSqlJournal.java
*
*
* Created: Thu May 29 15:50:20 2003
*
* @author <a href="mailto:kg...@ga...">Ken Geis</a>
*/
public class PostgreSqlJournal extends GenericSqlJournal
{
public final static String DELTA_MAKEROW =
"journalAddBlankDataRow-postgresql";
public final static String LOG_KEY_GET = "logKeyGet-postgresql";
/**
* Write a document to the rdbms. Same as in MySqlJournal, but it's
* probably not a good idea to just subclass it.
*
* @param con the connection
* @param id the log id
* @param step the log step
*/
protected void writeDelta (Connection conn, long id, long step,
PipelineDocument doc) throws SQLException
{
String insertStmt = SqlQueryManager.getSqlQuery(DELTA_MAKEROW);
PreparedStatement stmt = null;
ResultSet rs = null;
ByteArrayOutputStream baos = null;
try
{
// Write the document object to an array of bytes
baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(doc);
oos.flush();
byte[] docData = baos.toByteArray();
// Now write the row
stmt = conn.prepareStatement(insertStmt);
stmt.setLong(1, id);
stmt.setLong(2, step);
stmt.setBytes(3, docData);
stmt.execute();
writeExtraData(conn, id, step, doc);
}
catch (Exception e)
{
throw new SQLException(e.toString());
}
finally
{
try
{
if (rs != null) rs.close();
}
catch (Exception ex)
{
LogService.getInstance().logError(I18n.get("sql.403"), ex);
}
try
{
if (stmt != null) stmt.close();
}
catch (Exception ex)
{
LogService.getInstance().logError(I18n.get("sql.403"), ex);
}
try
{
if (baos != null) baos.close();
}
catch (Exception ex)
{
LogService.getInstance().logError(I18n.get("sql.403"), ex);
}
}
}
/**
* getTicketsProcessQueryString concoct the query string from the bits and
* pieces. Calls back to the getTicketsProcess...
*
* @param jQuery
* @return
*/
protected StringBuffer getTicketsProcessQueryString (JournalQuery jQuery)
{
StringBuffer query = super.getTicketsProcessQueryString(jQuery);
// Limit stuff
if (jQuery.getStartIndex() > 0)
{
query.append(" limit " + jQuery.getStartIndex() +
" offset " + jQuery.getNumResults());
// HACK ALERT - need to do this to stop the parent skipping
// on the result set.
//
// The above comment was taken from the MySQL implementation.
// I don't know how it effects PostgreSQL.
jQuery.setStartIndex(0);
}
return query;
}
/**
* Get a ticket for tracking a document. This implementation logs the
* operation to the log file and creates a directory for operations on
* the ticket.
*
* @return
* @throws JournalException
*/
public IJournalTicket newTicket () throws JournalException
{
try
{
//getLog().debug("[PostgreSqlJournal.newTicket] called");
long nextVal = getNewLogID();
IJournalTicket ticket = new JournalTicket(nextVal);
// Log the operation
log(ticket, JournalOperation.newTicket, null, null, null);
return ticket;
}
catch (Exception ex)
{
throw new JournalException("[GenericSqlJournal.newTicket]", ex);
}
}
/**
* Get a ticket for tracking a document. This implementation logs the
* operation to the log file and creates a directory for operations on
* the ticket.
*
* @param parentTicket
* @return
* @throws JournalException
*/
public IJournalTicket forkTicket (IJournalTicket parentTicket)
throws JournalException
{
try
{
long nextVal = getNewLogID();
IJournalTicket ticket = new JournalTicket(nextVal);
// Log the operation
log(ticket, JournalOperation.forkTicket, parentTicket.toString(),
null, null);
return ticket;
} catch (Exception ex) {
throw new JournalException("[GenericSqlJournal.forkTicket]", ex);
}
}
protected long getNewLogID ()
throws SQLException, ResourceException
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try
{
String SQL = SqlQueryManager.getSqlQuery(LOG_KEY_GET);
//getLog().debug("[PostgreSqlJournal.newTicket] called");
conn = (Connection) resource.checkOut();
stmt = conn.prepareStatement(SQL);
rs = stmt.executeQuery();
rs.next();
long nextVal = rs.getLong(1);
return nextVal;
}
finally
{
try
{
if (rs != null) rs.close();
}
catch (Exception ex)
{
LogService.getInstance().logError(I18n.get("sql.403"), ex);
}
try
{
if (stmt != null) stmt.close();
}
catch (Exception ex)
{
LogService.getInstance().logError(I18n.get("sql.403"), ex);
}
try
{
if (conn != null) resource.checkIn(conn);
}
catch (Exception ex)
{
LogService.getInstance().logError(I18n.get("sql.403"), ex);
}
}
}
}
|